diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index e65fa9b76..4b3c11413 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -75,20 +75,29 @@ export default { }, Mutation: { CreatePost: async (_parent, params, context, _resolveInfo) => { - const { categoryIds } = params + const { categoryIds, pinned } = params + delete params.pinned delete params.categoryIds params = await fileUpload(params, { file: 'imageUpload', url: 'image' }) params.id = params.id || uuid() let post - const createPostCypher = `CREATE (post:Post {params}) + let createPostCypher = `CREATE (post:Post {params}) SET post.createdAt = toString(datetime()) SET post.updatedAt = toString(datetime()) WITH post MATCH (author:User {id: $userId}) MERGE (post)<-[:WROTE]-(author) - WITH post - UNWIND $categoryIds AS categoryId + WITH post, author` + + if (pinned) { + createPostCypher += ` + MERGE (post)<-[:PINNED]-(author) + WITH post + ` + } + + createPostCypher += `UNWIND $categoryIds AS categoryId MATCH (category:Category {id: categoryId}) MERGE (post)-[:CATEGORIZED]->(category) RETURN post` @@ -98,7 +107,9 @@ export default { const session = context.driver.session() try { const transactionRes = await session.run(createPostCypher, createPostVariables) - const posts = transactionRes.records.map(record => record.get('post').properties) + const posts = transactionRes.records.map(record => { + return record.get('post').properties + }) post = posts[0] } catch (e) { if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') @@ -225,6 +236,7 @@ export default { hasOne: { author: '<-[:WROTE]-(related:User)', disabledBy: '<-[:DISABLED]-(related:User)', + pinnedBy: '<-[:PINNED]-(related:User)', }, count: { commentsCount: diff --git a/backend/src/schema/resolvers/posts.spec.js b/backend/src/schema/resolvers/posts.spec.js index eaf2b6f99..919454b64 100644 --- a/backend/src/schema/resolvers/posts.spec.js +++ b/backend/src/schema/resolvers/posts.spec.js @@ -39,7 +39,11 @@ const createPostMutation = gql` slug disabled deleted - pinned + pinnedBy { + id + name + role + } language author { name @@ -422,9 +426,14 @@ describe('CreatePost', () => { author: { name: 'Admin', }, - pinned: true, + pinnedBy: { + id: 'current-user', + name: 'Admin', + role: 'admin', + }, }, }, + errors: undefined, } await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject( diff --git a/backend/src/schema/types/type/Post.gql b/backend/src/schema/types/type/Post.gql index 69305a66d..85e73975f 100644 --- a/backend/src/schema/types/type/Post.gql +++ b/backend/src/schema/types/type/Post.gql @@ -16,7 +16,7 @@ type Post { createdAt: String updatedAt: String language: String - pinned: Boolean + pinnedBy: User @relation(name:"PINNED", direction: "IN") relatedContributions: [Post]! @cypher( statement: """ @@ -41,7 +41,7 @@ type Post { @cypher( statement: "MATCH (this)<-[:SHOUTED]-(r:User) WHERE NOT r.deleted = true AND NOT r.disabled = true RETURN COUNT(DISTINCT r)" ) - + # Has the currently logged in user shouted that post? shoutedByCurrentUser: Boolean! @cypher(