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 return resolvers
} }
const result = { const result = {
...undefinedToNullResolver(undefinedToNull), ...undefinedToNullResolver(undefinedToNull),
...booleanResolver(boolean), ...booleanResolver(boolean),

View File

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

View File

@ -581,6 +581,7 @@ describe('UpdatePost', () => {
} }
createdAt createdAt
updatedAt updatedAt
pinnedAt
} }
} }
` `
@ -607,7 +608,7 @@ describe('UpdatePost', () => {
}) })
}) })
describe('moderator cannot pin posts', () => { describe('moderators cannot pin posts', () => {
let moderator let moderator
beforeEach(async () => { beforeEach(async () => {
moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() }) moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() })
@ -664,6 +665,22 @@ describe('UpdatePost', () => {
expected, 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', () => { describe('post created by another admin', () => {
@ -742,20 +759,13 @@ describe('UpdatePost', () => {
variables = { ...variables, id: 'only-pinned-post' } variables = { ...variables, id: 'only-pinned-post' }
await mutate({ mutation: pinPostMutation, variables }) await mutate({ mutation: pinPostMutation, variables })
pinnedPost = await neode.cypher( 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 () => { it('leaves only one pinned post at a time', async () => {
expect(pinnedPost.records).toHaveLength(1) 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', () => { describe('PostOrdering', () => {
@ -787,7 +797,7 @@ describe('UpdatePost', () => {
name: 'Admin', name: 'Admin',
updatedAt: new Date().toISOString(), 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 () => { 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 let moderator
beforeEach(async () => { beforeEach(async () => {
moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() }) moderator = await user.update({ role: 'moderator', updatedAt: new Date().toISOString() })

View File

@ -17,7 +17,7 @@ type Post {
updatedAt: String updatedAt: String
language: String language: String
pinnedAt: String @cypher( 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") pinnedBy: User @relation(name:"PINNED", direction: "IN")
relatedContributions: [Post]! relatedContributions: [Post]!

View File

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