mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
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:
parent
4596041186
commit
95e9c14cc6
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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' }
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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([
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user