diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 64386800d..b55f798f1 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -16,7 +16,6 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { } const session = context.driver.session() - const createdAt = new Date().toISOString() let cypher switch (reason) { case 'mentioned_in_post': { @@ -27,7 +26,8 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { AND NOT (user)<-[:BLOCKED]-(author) MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.createdAt = $createdAt + SET notification.updatedAt = toString(datetime()) + SET notification.createdAt = toString(datetime()) ` break } @@ -40,7 +40,8 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { AND NOT (user)<-[:BLOCKED]-(postAuthor) MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.createdAt = $createdAt + SET notification.updatedAt = toString(datetime()) + SET notification.createdAt = toString(datetime()) ` break } @@ -53,7 +54,8 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { AND NOT (author)<-[:BLOCKED]-(user) MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.createdAt = $createdAt + SET notification.updatedAt = toString(datetime()) + SET notification.createdAt = toString(datetime()) ` break } @@ -63,7 +65,6 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { id, idsOfUsers, reason, - createdAt, }) session.close() } diff --git a/backend/src/schema/resolvers/comments.js b/backend/src/schema/resolvers/comments.js index 1f6803e09..7310b7af8 100644 --- a/backend/src/schema/resolvers/comments.js +++ b/backend/src/schema/resolvers/comments.js @@ -1,4 +1,4 @@ -import { neo4jgraphql } from 'neo4j-graphql-js' +import uuid from 'uuid/v4' import Resolver from './helpers/Resolver' export default { @@ -10,42 +10,34 @@ export default { // because we use relationships for this. So, we are deleting it from params // before comment creation. delete params.postId + params.id = params.id || uuid() + const session = context.driver.session() - const commentWithoutRelationships = await neo4jgraphql( - object, - params, - context, - resolveInfo, - false, - ) - - const transactionRes = await session.run( - ` - MATCH (post:Post {id: $postId}), (comment:Comment {id: $commentId}), (author:User {id: $userId}) + const createCommentCypher = ` + MATCH (post:Post {id: $postId}) + MATCH (author:User {id: $userId}) + WITH post, author + CREATE (comment:Comment {params}) + SET comment.createdAt = toString(datetime()) + SET comment.updatedAt = toString(datetime()) MERGE (post)<-[:COMMENTS]-(comment)<-[:WROTE]-(author) - RETURN comment, author`, - { - userId: context.user.id, - postId, - commentId: commentWithoutRelationships.id, - }, - ) + RETURN comment, author + ` + const transactionRes = await session.run(createCommentCypher, { + userId: context.user.id, + postId, + params, + }) + session.close() - const [commentWithAuthor] = transactionRes.records.map(record => { + const [response] = transactionRes.records.map(record => { return { - comment: record.get('comment'), - author: record.get('author'), + ...record.get('comment').properties, + author: record.get('author').properties, } }) - const { comment, author } = commentWithAuthor - - const commentReturnedWithAuthor = { - ...comment.properties, - author: author.properties, - } - session.close() - return commentReturnedWithAuthor + return response }, DeleteComment: async (object, args, context, resolveInfo) => { const session = context.driver.session() diff --git a/backend/src/schema/resolvers/posts.js b/backend/src/schema/resolvers/posts.js index 86dc78d62..2be17d3ec 100644 --- a/backend/src/schema/resolvers/posts.js +++ b/backend/src/schema/resolvers/posts.js @@ -82,6 +82,8 @@ export default { let post const 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) @@ -96,9 +98,7 @@ export default { const session = context.driver.session() try { const transactionRes = await session.run(createPostCypher, createPostVariables) - const posts = transactionRes.records.map(record => { - return record.get('post').properties - }) + const posts = transactionRes.records.map(record => record.get('post').properties) post = posts[0] } catch (e) { if (e.code === 'Neo.ClientError.Schema.ConstraintValidationFailed') @@ -118,6 +118,7 @@ export default { let updatePostCypher = `MATCH (post:Post {id: $params.id}) SET post = $params + SET post.updatedAt = toString(datetime()) ` if (categoryIds && categoryIds.length) { @@ -129,10 +130,11 @@ export default { await session.run(cypherDeletePreviousRelations, { params }) - updatePostCypher += `WITH post - UNWIND $categoryIds AS categoryId - MATCH (category:Category {id: categoryId}) - MERGE (post)-[:CATEGORIZED]->(category) + updatePostCypher += ` + WITH post + UNWIND $categoryIds AS categoryId + MATCH (category:Category {id: categoryId}) + MERGE (post)-[:CATEGORIZED]->(category) ` }