First test driven approach to implement notification of report filing user

This commit is contained in:
Wolfgang Huß 2019-12-13 13:54:51 +01:00
parent 692edee313
commit f3728af97b
5 changed files with 185 additions and 48 deletions

View File

@ -45,10 +45,7 @@ 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 (
CASE
WHEN notification.createdAt IS NULL
THEN notification END ).createdAt = toString(datetime())
SET ( CASE WHEN notification.createdAt IS NULL THEN notification END ).createdAt = toString(datetime())
SET notification.updatedAt = toString(datetime())
`
break
@ -62,10 +59,7 @@ 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 (
CASE
WHEN notification.createdAt IS NULL
THEN notification END ).createdAt = toString(datetime())
SET ( CASE WHEN notification.createdAt IS NULL THEN notification END ).createdAt = toString(datetime())
SET notification.updatedAt = toString(datetime())
`
break
@ -79,10 +73,7 @@ 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 (
CASE
WHEN notification.createdAt IS NULL
THEN notification END ).createdAt = toString(datetime())
SET ( CASE WHEN notification.createdAt IS NULL THEN notification END ).createdAt = toString(datetime())
SET notification.updatedAt = toString(datetime())
`
break
@ -154,11 +145,63 @@ const handleCreateComment = async (resolve, root, args, context, resolveInfo) =>
return comment
}
const notifyReportFiler = async (resolve, root, args, context, resolveInfo) => {
// const comment = await handleContentDataOfComment(resolve, root, args, context, resolveInfo)
const report = await resolve(root, args, context, resolveInfo)
if (report) {
console.log('If report !!!')
console.log('report: ', report)
console.log('args: ', args)
const { resourceId/* Wolle , reasonCategory, reasonDescription */ } = args
const { driver, user } = context
const { id: reportId } = report
console.log('resourceId: ', resourceId)
console.log('user.id: ', user.id)
console.log('reportId: ', reportId)
// Wolle await notifyUsers(report.resource.__typename, resourceId, [user.id], 'filed_report_on_resource', context)
const session = driver.session()
const notificationWriteTxResultPromise = session.writeTransaction(async transaction => {
const notificationTransactionResponse = await transaction.run(
`
MATCH (resource {id: $resourceId})<-[:BELONGS_TO]-(:Report {id: $reportId})<-[:FILED]-(submitter:User {id: $submitterId})
WHERE resource: User OR resource: Post OR resource: Comment
MERGE (resource)-[notification:NOTIFIED {reason: $reason, reportId: $reportId}]->(submitter)
ON CREATE SET notification.createdAt = toString(datetime()), notification.updatedAt = notification.createdAt
ON MERGE SET notification.updatedAt = toString(datetime())
SET notification.read = FALSE
`,
{
reportId,
resourceId,
submitterId: user.id,
reason: 'filed_report_on_resource',
// Wolle reasonCategory,
// Wolle reasonDescription,
},
)
console.log('notificationTransactionResponse: ', notificationTransactionResponse)
log(notificationTransactionResponse)
// Wolle return notificationTransactionResponse.records.map(transformReturnType)
})
try {
await notificationWriteTxResultPromise
// Wolle if (!createdRelationshipWithNestedAttributes) return null
// Wolle return createdRelationshipWithNestedAttributes
} finally {
session.close()
}
}
return report
}
export default {
Mutation: {
CreatePost: handleContentDataOfPost,
UpdatePost: handleContentDataOfPost,
CreateComment: handleCreateComment,
UpdateComment: handleContentDataOfComment,
fileReport: notifyReportFiler,
},
}

View File

@ -38,6 +38,17 @@ const createCommentMutation = gql`
}
}
`
const fileReportMutation = gql `
mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) {
fileReport(
resourceId: $resourceId
reasonCategory: $reasonCategory
reasonDescription: $reasonDescription
) {
id
}
}
`
beforeAll(() => {
const createServerResult = createServer({
@ -96,46 +107,45 @@ describe('notifications', () => {
}
`
let title
let postContent
let postAuthor
const createPostAction = async () => {
authenticatedUser = await postAuthor.toJson()
await mutate({
mutation: createPostMutation,
variables: {
id: 'p47',
title,
postContent,
categoryIds,
},
})
authenticatedUser = await notifiedUser.toJson()
}
let commentContent
let commentAuthor
const createCommentOnPostAction = async () => {
await createPostAction()
authenticatedUser = await commentAuthor.toJson()
await mutate({
mutation: createCommentMutation,
variables: {
id: 'c47',
postId: 'p47',
commentContent,
},
})
authenticatedUser = await notifiedUser.toJson()
}
describe('authenticated', () => {
beforeEach(async () => {
authenticatedUser = await notifiedUser.toJson()
})
describe('given another user', () => {
let title
let postContent
let postAuthor
const createPostAction = async () => {
authenticatedUser = await postAuthor.toJson()
await mutate({
mutation: createPostMutation,
variables: {
id: 'p47',
title,
postContent,
categoryIds,
},
})
authenticatedUser = await notifiedUser.toJson()
}
let commentContent
let commentAuthor
const createCommentOnPostAction = async () => {
await createPostAction()
authenticatedUser = await commentAuthor.toJson()
await mutate({
mutation: createCommentMutation,
variables: {
id: 'c47',
postId: 'p47',
commentContent,
},
})
authenticatedUser = await notifiedUser.toJson()
}
describe('comments on my post', () => {
beforeEach(async () => {
title = 'My post'
@ -545,5 +555,88 @@ describe('notifications', () => {
})
})
})
describe('given me filing a report of a', () => {
let resourceId
let reasonCategory
let reasonDescription
let reportFiler
const fileReportAction = async () => {
authenticatedUser = await reportFiler.toJson()
await mutate({
mutation: fileReportMutation,
variables: {
resourceId,
reasonCategory,
reasonDescription,
},
})
authenticatedUser = await notifiedUser.toJson()
}
describe('user', () => {
})
describe('post', () => {
})
describe('comment', () => {
beforeEach(async () => {
title = 'My post'
postContent = 'My post content.'
postAuthor = await neode.create('User', {
id: 'postAuthor',
name: 'Mrs Post',
slug: 'mrs-post',
email: 'post-author@example.org',
password: '1234',
})
commentContent = 'Commenters comment.'
commentAuthor = await neode.create('User', {
id: 'commentAuthor',
name: 'Mrs Comment',
slug: 'mrs-comment',
email: 'commentauthor@example.org',
password: '1234',
})
})
it.only('sends me a notification', async () => {
await createCommentOnPostAction()
resourceId = 'c47'
reasonCategory = 'discrimination_etc'
reasonDescription = 'I am free to be gay !!!'
reportFiler = notifiedUser
await fileReportAction()
const expected = expect.objectContaining({
data: {
notifications: [
{
read: false,
createdAt: expect.any(String),
reason: 'filed_report_on_resource',
from: {
__typename: 'Comment',
id: 'c47',
content: commentContent,
},
},
],
},
})
const { query } = createTestClient(server)
await expect(
query({
query: notificationQuery,
variables: {
read: false,
},
}),
).resolves.toEqual(expected)
})
})
})
})
})

View File

@ -18,9 +18,10 @@ enum NotificationOrdering {
}
enum NotificationReason {
commented_on_post
filed_report_on_resource
mentioned_in_post
mentioned_in_comment
commented_on_post
}
type Query {

@ -1 +1 @@
Subproject commit 7ef83405006b016fe45b476ed6e34ec189d7d283
Subproject commit 808b3c5a9523505cb80b20b50348d29ba9932845