diff --git a/src/resolvers/reports.js b/src/resolvers/reports.js index c471d7b7a..79bab6646 100644 --- a/src/resolvers/reports.js +++ b/src/resolvers/reports.js @@ -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 } } } diff --git a/src/resolvers/reports.spec.js b/src/resolvers/reports.spec.js index 253cdadcc..07cfc92eb 100644 --- a/src/resolvers/reports.spec.js +++ b/src/resolvers/reports.spec.js @@ -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' } }) }) }) diff --git a/src/schema.graphql b/src/schema.graphql index 74d042933..65e11fe19 100644 --- a/src/schema.graphql +++ b/src/schema.graphql @@ -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") diff --git a/src/seed/factories/reports.js b/src/seed/factories/reports.js index 4dcd479f1..ccf5299bd 100644 --- a/src/seed/factories/reports.js +++ b/src/seed/factories/reports.js @@ -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 diff --git a/src/seed/seed-db.js b/src/seed/seed-db.js index 5f5b764a4..b4ecd6d7f 100644 --- a/src/seed/seed-db.js +++ b/src/seed/seed-db.js @@ -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([