Return pinnedAt date from pinPost resolver/clean up

- it's good to return the pinnedAt date for ordering
- move test to a better describe block
- remove unneeded outdated variables from graphql/PostQuery UpdatePost
- fix indentation in Post.gql
- fix pinnedAt to return pinned.createdAt, not post.createdAt

Co-authored-by: Mike Aono <aonomike@gmail.com>
This commit is contained in:
mattwr18 2019-10-18 15:10:26 +02:00
parent 411bbabcd5
commit be0c8044e8
6 changed files with 186 additions and 171 deletions

View File

@ -86,6 +86,7 @@ export default function Resolver(type, options = {}) {
}
return resolvers
}
const result = {
...undefinedToNullResolver(undefinedToNull),
...booleanResolver(boolean),

View File

@ -226,7 +226,7 @@ export default {
return emoted
},
pinPost: async (_parent, params, context, _resolveInfo) => {
let pinnedPost
let pinnedPostWithNestedAttributes
const { driver, user } = context
const session = driver.session()
const { id: userId } = user
@ -248,19 +248,27 @@ export default {
`
MATCH (user:User {id: $userId}) WHERE user.role = 'admin'
MATCH (post:Post {id: $params.id})
MERGE (user)-[:PINNED {createdAt: toString(datetime())}]->(post)
RETURN post
MERGE (user)-[pinned:PINNED {createdAt: toString(datetime())}]->(post)
RETURN post, pinned.createdAt as pinnedAt
`,
{ userId, params },
)
return pinPostTransactionResponse.records.map(record => record.get('post').properties)
return pinPostTransactionResponse.records.map(record => ({
pinnedPost: record.get('post').properties,
pinnedAt: record.get('pinnedAt'),
}))
})
try {
;[pinnedPost] = await writeTxResultPromise
const [transactionResult] = await writeTxResultPromise
const { pinnedPost, pinnedAt } = transactionResult
pinnedPostWithNestedAttributes = {
...pinnedPost,
pinnedAt
}
} finally {
session.close()
}
return pinnedPost
return pinnedPostWithNestedAttributes
},
unpinPost: async (_parent, params, context, _resolveInfo) => {
let unpinnedPost

View File

@ -581,6 +581,7 @@ describe('UpdatePost', () => {
}
createdAt
updatedAt
pinnedAt
}
}
`
@ -607,7 +608,7 @@ describe('UpdatePost', () => {
})
})
describe('moderator cannot pin posts', () => {
describe('moderators cannot pin posts', () => {
let moderator
beforeEach(async () => {
moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() })
@ -664,6 +665,22 @@ describe('UpdatePost', () => {
expected,
)
})
it('sets createdAt date for PINNED', async () => {
variables = { ...variables, id: 'created-and-pinned-by-same-admin' }
const expected = {
data: {
pinPost: {
id: 'created-and-pinned-by-same-admin',
pinnedAt: expect.any(String)
},
},
errors: undefined,
}
await expect(mutate({ mutation: pinPostMutation, variables })).resolves.toMatchObject(
expected,
)
})
})
describe('post created by another admin', () => {
@ -742,20 +759,13 @@ describe('UpdatePost', () => {
variables = { ...variables, id: 'only-pinned-post' }
await mutate({ mutation: pinPostMutation, variables })
pinnedPost = await neode.cypher(
`MATCH ()-[relationship:PINNED]->(post:Post) RETURN post, relationship`,
`MATCH ()-[pinned:PINNED]->(post:Post) RETURN post, pinned`,
)
})
it('leaves only one pinned post at a time', async () => {
expect(pinnedPost.records).toHaveLength(1)
})
it('sets createdAt date for PINNED', () => {
const [pinnedPostCreatedAt] = pinnedPost.records.map(record => {
return record.get('relationship').properties.createdAt
})
expect(pinnedPostCreatedAt).toEqual(expect.any(String))
})
})
describe('PostOrdering', () => {
@ -787,7 +797,7 @@ describe('UpdatePost', () => {
name: 'Admin',
updatedAt: new Date().toISOString(),
})
await admin.relateTo(pinnedPost, 'pinned', { createdAt: newDate.toISOString() })
await admin.relateTo(pinnedPost, 'pinned')
})
it('pinned post appear first even when created before other posts', async () => {
@ -870,7 +880,7 @@ describe('UpdatePost', () => {
})
})
describe('moderator cannot unpin posts', () => {
describe('moderators cannot unpin posts', () => {
let moderator
beforeEach(async () => {
moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() })

View File

@ -17,7 +17,7 @@ type Post {
updatedAt: String
language: String
pinnedAt: String @cypher(
statement: "MATCH (this)<-[pinned:PINNED]-(:User) WHERE NOT this.deleted = true AND NOT this.disabled = true RETURN this.createdAt"
statement: "MATCH (this)<-[pinned:PINNED]-(:User) WHERE NOT this.deleted = true AND NOT this.disabled = true RETURN pinned.createdAt"
)
pinnedBy: User @relation(name:"PINNED", direction: "IN")
relatedContributions: [Post]!

View File

@ -34,8 +34,6 @@ export default () => {
$imageUpload: Upload
$categoryIds: [ID]
$image: String
$pinned: Boolean
$unpinned: Boolean
) {
UpdatePost(
id: $id
@ -45,8 +43,6 @@ export default () => {
imageUpload: $imageUpload
categoryIds: $categoryIds
image: $image
pinned: $pinned
unpinned: $unpinned
) {
id
title