From 5d63dda6ee8f1a57fe01b63201332d1026e5524b Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Fri, 29 Nov 2019 18:43:50 +0100 Subject: [PATCH] Refactor validateReport/Review - Don't throw error if a report already exists since we use MERGE and that does not create a new resource if it exists. That is tested at the neo4j(database) level. - We also have a test to make in reports.spec.js that no duplicate is created to ensure we didn't make some error on our part. - Fix some faulty logic in validateReview --- .../validation/validationMiddleware.js | 30 ++++--------------- .../validation/validationMiddleware.spec.js | 2 -- 2 files changed, 5 insertions(+), 27 deletions(-) diff --git a/backend/src/middleware/validation/validationMiddleware.js b/backend/src/middleware/validation/validationMiddleware.js index ec761f49a..bbde03df6 100644 --- a/backend/src/middleware/validation/validationMiddleware.js +++ b/backend/src/middleware/validation/validationMiddleware.js @@ -59,35 +59,15 @@ const validateUpdatePost = async (resolve, root, args, context, info) => { const validateReport = async (resolve, root, args, context, info) => { const { resourceId } = args - const { user, driver } = context + const { user } = context if (resourceId === user.id) throw new Error('You cannot report yourself!') - const session = driver.session() - const reportQueryRes = await session.run( - ` - MATCH (:User {id: $submitterId})-[:FILED]->(:Report {closed: false})-[:BELONGS_TO]->(resource {id: $resourceId}) - WHERE resource:User OR resource:Post OR resource:Comment - RETURN labels(resource)[0] AS label - `, - { - resourceId, - submitterId: user.id, - }, - ) - session.close() - const [existingReportedResource] = reportQueryRes.records.map(record => { - return { - label: record.get('label'), - } - }) - - if (existingReportedResource) throw new Error(`${existingReportedResource.label}`) return resolve(root, args, context, info) } const validateReview = async (resolve, root, args, context, info) => { const { resourceId } = args const { user, driver } = context - if (resourceId === user.id) throw new Error('You can not review yourself!') + if (resourceId === user.id) throw new Error('You cannot review yourself!') const session = driver.session() const reportQueryRes = await session.run( ` @@ -106,7 +86,7 @@ const validateReview = async (resolve, root, args, context, info) => { const [existingReportedResource] = reportQueryRes.records.map(record => { return { label: record.get('label'), - author: record.get('author'), + author: record.get('author').properties, filed: record.get('filed'), } }) @@ -114,13 +94,13 @@ const validateReview = async (resolve, root, args, context, info) => { if (!existingReportedResource) throw new Error(`Resource not found!`) if (!existingReportedResource.filed) throw new Error( - `Before you can start the reviewing process, please report the ${existingReportedResource.label}!`, + `Before starting the review process, please report the ${existingReportedResource.label}!`, ) const authorId = existingReportedResource.label !== 'User' && existingReportedResource.author ? existingReportedResource.author.id : null - if (authorId && resourceId === authorId) + if (authorId && authorId === user.id) throw new Error(`You cannot review your own ${existingReportedResource.label}!`) return resolve(root, args, context, info) } diff --git a/backend/src/middleware/validation/validationMiddleware.spec.js b/backend/src/middleware/validation/validationMiddleware.spec.js index b0c1dbd5a..9448d50cf 100644 --- a/backend/src/middleware/validation/validationMiddleware.spec.js +++ b/backend/src/middleware/validation/validationMiddleware.spec.js @@ -48,8 +48,6 @@ beforeAll(() => { mutate = createTestClient(server).mutate }) -// const { query } = createTestClient(server) - beforeEach(async () => { users = await Promise.all([ factory.create('User', {