Refactor report mutation/type to reduce redundancy

We don't need to save the type of the reported resource. We can derive
it.
This commit is contained in:
Robert Schäfer 2019-03-07 20:13:39 +01:00
parent 4596041186
commit 95e9c14cc6
5 changed files with 47 additions and 75 deletions

View File

@ -2,50 +2,31 @@ import uuid from 'uuid/v4'
export default {
Mutation: {
report: async (parent, { resource, description }, { driver, req, user }, resolveInfo) => {
const contextId = uuid()
report: async (parent, { id, description }, { driver, req, user }, resolveInfo) => {
const reportId = uuid()
const session = driver.session()
const data = {
id: contextId,
type: resource.type,
const reportData = {
id: reportId,
createdAt: (new Date()).toISOString(),
description: resource.description
description: description
}
await session.run(
'CREATE (r:Report $report) ' +
'RETURN r.id, r.type, r.description', {
report: data
}
)
let contentType
switch (resource.type) {
case 'post':
case 'contribution':
contentType = 'Post'
break
case 'comment':
contentType = 'Comment'
break
case 'user':
contentType = 'User'
break
await session.run(`
MATCH (author:User {id: $userId})
MATCH (resource {id: $resourceId})
CREATE (report:Report $reportData)
MERGE (resource)<-[:REPORTED]-(report)
MERGE (report)<-[:REPORTED]-(author)
RETURN report
`, {
resourceId: id,
userId: user.id,
reportData
}
await session.run(
`MATCH (author:User {id: $userId}), (context:${contentType} {id: $resourceId}), (report:Report {id: $contextId}) ` +
'MERGE (report)<-[:REPORTED]-(author) ' +
'MERGE (context)<-[:REPORTED]-(report) ' +
'RETURN context', {
resourceId: resource.id,
userId: user.id,
contextId: contextId
}
)
session.close()
// TODO: output Report compatible object
return data
return reportData
}
}
}

View File

@ -22,45 +22,38 @@ describe('report', () => {
await factory.cleanDatabase()
})
const mutation = `
mutation {
report(
id: "u2",
description: "I don't like this user"
) { description }
}
`
let headers
beforeEach(async () => {
headers = {}
})
let client
const action = () => {
client = new GraphQLClient(host, { headers })
return client.request(mutation)
}
describe('unauthenticated', () => {
let client
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(
client.request(`mutation {
report(
description: "I don't like this user",
resource: {
id: "u2",
type: user
}
) { id, createdAt }
}`)
).rejects.toThrow('Not Authorised')
await expect(action()).rejects.toThrow('Not Authorised')
})
describe('authenticated', () => {
let headers
let response
beforeEach(async () => {
headers = await login({ email: 'test@example.org', password: '1234' })
client = new GraphQLClient(host, { headers })
response = await client.request(`mutation {
report(
description: "I don't like this user",
resource: {
id: "u2",
type: user
}
) { id, createdAt }
}`,
{ headers }
)
})
it('creates a report', () => {
let { id, createdAt } = response.report
expect(response).toEqual({
report: { id, createdAt }
it('creates a report', async () => {
await expect(action()).resolves.toEqual({
report: { description: 'I don\'t like this user' }
})
})
})

View File

@ -162,6 +162,7 @@ type Report {
id: ID!
reporter: User @relation(name: "REPORTED", direction: "IN")
description: String
type: String! @cypher(statement: "MATCH (resource)<-[:REPORTED]-(this) RETURN labels(resource)[0]")
createdAt: String
comment: Comment @relation(name: "REPORTED", direction: "OUT")
contribution: Post @relation(name: "REPORTED", direction: "OUT")

View File

@ -3,17 +3,14 @@ import faker from 'faker'
export default function create (params) {
const {
description = faker.lorem.sentence(),
resource: { id: resourceId, type }
id
} = params
return `
mutation {
report(
description: "${description}",
resource: {
id: "${resourceId}",
type: ${type}
}
id: "${id}",
) {
id,
createdAt

View File

@ -173,9 +173,9 @@ import Factory from './factories'
])
await Promise.all([
asTick.create('Report', { description: 'I don\'t like this comment', resource: { id: 'c1', type: 'comment' } }),
asTrick.create('Report', { description: 'I don\'t like this post', resource: { id: 'p1', type: 'contribution' } }),
asTrack.create('Report', { description: 'I don\'t like this user', resource: { id: 'u1', type: 'user' } })
asTick.create('Report', { description: 'I don\'t like this comment', id: 'c1' }),
asTrick.create('Report', { description: 'I don\'t like this post', id: 'p1' }),
asTrack.create('Report', { description: 'I don\'t like this user', id: 'u1' })
])
await Promise.all([