Refactor notificationsMiddleware/locations

- start refactoring
- locations does not have any automated tests, which makes it more
difficult to refactor and have confidence that functionality will not be
broken
- notificationsMiddleware in progress
This commit is contained in:
mattwr18 2019-12-05 20:42:45 +01:00
parent de64f1dd4a
commit 1827889582
5 changed files with 39 additions and 75 deletions

View File

@ -23,14 +23,12 @@ export default async (driver, authorizationHeader) => {
`,
{ id },
)
return updateUserLastActiveTransactionResponse.records.map(
record => record.get('user'),
)
return updateUserLastActiveTransactionResponse.records.map(record => record.get('user'))
})
try {
const [currentUser] = await writeTxResultPromise
if (!currentUser) return null
return {
if (!currentUser) return null
return {
token,
...currentUser,
}

View File

@ -3,7 +3,7 @@ import extractHashtags from '../hashtags/extractHashtags'
const updateHashtagsOfPost = async (postId, hashtags, context) => {
if (!hashtags.length) return
const session = context.driver.session()
try {
await session.writeTransaction(txc => {
return txc.run(

View File

@ -38,7 +38,7 @@ const createLocation = async (session, mapboxData) => {
lng: mapboxData.center && mapboxData.center.length ? mapboxData.center[1] : null,
}
let query =
let mutation =
'MERGE (l:Location {id: $id}) ' +
'SET l.name = $nameEN, ' +
'l.nameEN = $nameEN, ' +
@ -53,12 +53,17 @@ const createLocation = async (session, mapboxData) => {
'l.type = $type'
if (data.lat && data.lng) {
query += ', l.lat = $lat, l.lng = $lng'
mutation += ', l.lat = $lat, l.lng = $lng'
}
query += ' RETURN l.id'
mutation += ' RETURN l.id'
await session.run(query, data)
session.close()
try {
await session.writeTransaction(transaction => {
return transaction.run(mutation, data)
})
} finally {
session.close()
}
}
const createOrUpdateLocations = async (userId, locationName, driver) => {

View File

@ -1,23 +1,22 @@
import extractMentionedUsers from './mentions/extractMentionedUsers'
const postAuthorOfComment = async (comment, { context }) => {
const cypherFindUser = `
MATCH (user: User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId })
RETURN user { .id }
`
const postAuthorOfComment = async (commentId, { context }) => {
const session = context.driver.session()
let result
let postAuthorId
try {
result = await session.run(cypherFindUser, {
commentId: comment.id,
postAuthorId = await session.readTransaction(transaction => {
return transaction.run(
`
MATCH (author:User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId })
RETURN author { .id } as authorId
`,
{ commentId },
)
})
return postAuthorId.records.map(record => record.get('authorId'))
} finally {
session.close()
}
const [postAuthor] = await result.records.map(record => {
return record.get('user')
})
return postAuthor
}
const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
@ -90,10 +89,8 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
}
const session = context.driver.session()
try {
await session.run(cypher, {
id,
idsOfUsers,
reason,
await session.writeTransaction(transaction => {
return transaction.run(cypher, { id, idsOfUsers, reason })
})
} finally {
session.close()
@ -102,56 +99,24 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => {
const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => {
const idsOfUsers = extractMentionedUsers(args.content)
const post = await resolve(root, args, context, resolveInfo)
if (post) {
await notifyUsers('Post', post.id, idsOfUsers, 'mentioned_in_post', context)
}
return post
if (post) return notifyUsers('Post', post.id, idsOfUsers, 'mentioned_in_post', context)
}
const handleContentDataOfComment = async (resolve, root, args, context, resolveInfo) => {
let idsOfUsers = extractMentionedUsers(args.content)
const comment = await resolve(root, args, context, resolveInfo)
if (comment) {
const postAuthor = await postAuthorOfComment(comment, { context })
idsOfUsers = idsOfUsers.filter(id => id !== postAuthor.id)
await notifyUsers('Comment', comment.id, idsOfUsers, 'mentioned_in_comment', context)
}
return comment
const { content, id: commentId } = args
let idsOfUsers = extractMentionedUsers(content)
const [postAuthor] = await postAuthorOfComment(commentId, { context })
idsOfUsers = idsOfUsers.filter(id => id !== postAuthor.id)
if (idsOfUsers && idsOfUsers.length)
await notifyUsers('Comment', commentId, idsOfUsers, 'mentioned_in_comment', context)
if (context.user.id !== postAuthor.id)
await notifyUsers('Comment', commentId, [postAuthor.id], 'commented_on_post', context)
}
const handleCreateComment = async (resolve, root, args, context, resolveInfo) => {
const comment = await handleContentDataOfComment(resolve, root, args, context, resolveInfo)
if (comment) {
const cypherFindUser = `
MATCH (user: User)-[:WROTE]->(:Post)<-[:COMMENTS]-(:Comment { id: $commentId })
RETURN user { .id }
`
const session = context.driver.session()
let result
try {
result = await session.run(cypherFindUser, {
commentId: comment.id,
})
} finally {
session.close()
}
const [postAuthor] = await result.records.map(record => {
return record.get('user')
})
if (context.user.id !== postAuthor.id) {
await notifyUsers('Comment', comment.id, [postAuthor.id], 'commented_on_post', context)
}
}
return comment
const comment = await resolve(root, args, context, resolveInfo)
if (comment) return handleContentDataOfComment(resolve, root, args, context, resolveInfo)
}
export default {

View File

@ -4,11 +4,7 @@ import { createTestClient } from 'apollo-server-testing'
import { neode, getDriver } from '../../bootstrap/neo4j'
import createServer from '../../server'
let server
let query
let mutate
let notifiedUser
let authenticatedUser
let server, query, mutate, notifiedUser, authenticatedUser
const factory = Factory()
const driver = getDriver()
const instance = neode()