diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 5b4a5d75a..397f642af 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -41,8 +41,11 @@ const reviewMutation = gql` createdAt updatedAt closed - reviewedByModerator { - id + reviewed { + createdAt + moderator { + id + } } } } @@ -236,14 +239,19 @@ describe('moderate resources', () => { }) }) - it('returns .reviewedByModerator', async () => { + it('returns .reviewed', async () => { await expect( mutate({ mutation: reviewMutation, variables: disableVariables }), ).resolves.toMatchObject({ data: { review: { resource: { __typename: 'Comment', id: 'comment-id' }, - report: { id: expect.any(String), reviewedByModerator: { id: 'moderator-id' } }, + report: { + id: expect.any(String), + reviewed: expect.arrayContaining([ + { createdAt: expect.any(String), moderator: { id: 'moderator-id' } }, + ]), + }, }, }, }) @@ -309,14 +317,19 @@ describe('moderate resources', () => { }) }) - it('returns .reviewedByModerator', async () => { + it('returns .reviewed', async () => { await expect( mutate({ mutation: reviewMutation, variables: disableVariables }), ).resolves.toMatchObject({ data: { review: { resource: { __typename: 'Post', id: 'post-id' }, - report: { id: expect.any(String), reviewedByModerator: { id: 'moderator-id' } }, + report: { + id: expect.any(String), + reviewed: expect.arrayContaining([ + { createdAt: expect.any(String), moderator: { id: 'moderator-id' } }, + ]), + }, }, }, }) @@ -376,14 +389,19 @@ describe('moderate resources', () => { }) }) - it('returns .reviewedByModerator', async () => { + it('returns .reviewed', async () => { await expect( mutate({ mutation: reviewMutation, variables: disableVariables }), ).resolves.toMatchObject({ data: { review: { resource: { __typename: 'User', id: 'user-id' }, - report: { id: expect.any(String), reviewedByModerator: { id: 'moderator-id' } }, + report: { + id: expect.any(String), + reviewed: expect.arrayContaining([ + { createdAt: expect.any(String), moderator: { id: 'moderator-id' } }, + ]), + }, }, }, }) @@ -509,14 +527,19 @@ describe('moderate resources', () => { }) }) - it('returns .reviewedByModerator', async () => { + it('returns .reviewed', async () => { await expect( mutate({ mutation: reviewMutation, variables: enableVariables }), ).resolves.toMatchObject({ data: { review: { resource: { __typename: 'Comment', id: 'comment-id' }, - report: { id: expect.any(String), reviewedByModerator: { id: 'moderator-id' } }, + report: { + id: expect.any(String), + reviewed: expect.arrayContaining([ + { createdAt: expect.any(String), moderator: { id: 'moderator-id' } }, + ]), + }, }, }, }) @@ -569,14 +592,19 @@ describe('moderate resources', () => { }) }) - it('returns .reviewedByModerator', async () => { + it('returns .reviewed', async () => { await expect( mutate({ mutation: reviewMutation, variables: enableVariables }), ).resolves.toMatchObject({ data: { review: { resource: { __typename: 'Post', id: 'post-id' }, - report: { id: expect.any(String), reviewedByModerator: { id: 'moderator-id' } }, + report: { + id: expect.any(String), + reviewed: expect.arrayContaining([ + { createdAt: expect.any(String), moderator: { id: 'moderator-id' } }, + ]), + }, }, }, }) @@ -628,14 +656,19 @@ describe('moderate resources', () => { }) }) - it('returns .reviewedByModerator', async () => { + it('returns .reviewed', async () => { await expect( mutate({ mutation: reviewMutation, variables: enableVariables }), ).resolves.toMatchObject({ data: { review: { resource: { __typename: 'User', id: 'user-id' }, - report: { id: expect.any(String), reviewedByModerator: { id: 'moderator-id' } }, + report: { + id: expect.any(String), + reviewed: expect.arrayContaining([ + { createdAt: expect.any(String), moderator: { id: 'moderator-id' } }, + ]), + }, }, }, }) diff --git a/backend/src/schema/resolvers/reports.js b/backend/src/schema/resolvers/reports.js index 06c0a151e..c3ac3caac 100644 --- a/backend/src/schema/resolvers/reports.js +++ b/backend/src/schema/resolvers/reports.js @@ -86,11 +86,11 @@ export default { }, }, Report: { - reportsFiled: async (parent, _params, context, _resolveInfo) => { - if (typeof parent.reportsFiled !== 'undefined') return parent.reportsFiled + filed: async (parent, _params, context, _resolveInfo) => { + if (typeof parent.filed !== 'undefined') return parent.filed const session = context.driver.session() const { id } = parent - let reportsFiled + let filed const readTxPromise = session.readTransaction(async tx => { const allReportsTransactionResponse = await tx.run( ` @@ -107,7 +107,7 @@ export default { try { const txResult = await readTxPromise if (!txResult[0]) return null - reportsFiled = txResult.map(reportedRecord => { + filed = txResult.map(reportedRecord => { const { submitter, filed } = reportedRecord const relationshipWithNestedAttributes = { ...filed, @@ -118,35 +118,42 @@ export default { } finally { session.close() } - return reportsFiled + return filed }, - reviewedByModerator: async (parent, _params, context, _resolveInfo) => { - if (typeof parent.reviewedByModerator !== 'undefined') return parent.reviewedByModerator + reviewed: async (parent, _params, context, _resolveInfo) => { + if (typeof parent.reviewed !== 'undefined') return parent.reviewed const session = context.driver.session() const { id } = parent - let reviewedByModerator + let reviewed const readTxPromise = session.readTransaction(async tx => { const allReportsTransactionResponse = await tx.run( ` MATCH (resource)<-[:BELONGS_TO]-(report:Report {id: $id})<-[review:REVIEWED]-(moderator:User) - RETURN moderator + RETURN moderator, review ORDER BY report.updatedAt DESC, review.updatedAt DESC - LIMIT 1 `, { id }, ) - return allReportsTransactionResponse.records.map( - record => record.get('moderator').properties, - ) + return allReportsTransactionResponse.records.map(record => ({ + review: record.get('review').properties, + moderator: record.get('moderator').properties, + })) }) try { const txResult = await readTxPromise if (!txResult[0]) return null - reviewedByModerator = txResult[0] + reviewed = txResult.map(reportedRecord => { + const { review, moderator } = reportedRecord + const relationshipWithNestedAttributes = { + ...review, + moderator, + } + return relationshipWithNestedAttributes + }) } finally { session.close() } - return reviewedByModerator + return reviewed }, }, } diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 648f63dce..648f55688 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -35,7 +35,7 @@ describe('file a report on a resource', () => { content } } - reportsFiled { + filed { submitter { id } @@ -201,7 +201,7 @@ describe('file a report on a resource', () => { ).resolves.toMatchObject({ data: { fileReport: { - reportsFiled: [ + filed: [ { submitter: { id: 'current-user-id', @@ -243,7 +243,7 @@ describe('file a report on a resource', () => { ).resolves.toMatchObject({ data: { fileReport: { - reportsFiled: [ + filed: [ { reasonCategory: 'criminal_behavior_violation_german_law', }, @@ -288,7 +288,7 @@ describe('file a report on a resource', () => { ).resolves.toMatchObject({ data: { fileReport: { - reportsFiled: [ + filed: [ { reasonDescription: 'My reason!', }, @@ -312,7 +312,7 @@ describe('file a report on a resource', () => { ).resolves.toMatchObject({ data: { fileReport: { - reportsFiled: [ + filed: [ { reasonDescription: 'My reason !', }, @@ -487,7 +487,7 @@ describe('file a report on a resource', () => { id } } - reportsFiled { + filed { submitter { id } @@ -614,7 +614,7 @@ describe('file a report on a resource', () => { __typename: 'User', id: 'abusive-user-1', }, - reportsFiled: expect.arrayContaining([ + filed: expect.arrayContaining([ expect.objectContaining({ submitter: expect.objectContaining({ id: 'current-user-id', @@ -635,7 +635,7 @@ describe('file a report on a resource', () => { __typename: 'Post', id: 'abusive-post-1', }, - reportsFiled: expect.arrayContaining([ + filed: expect.arrayContaining([ expect.objectContaining({ submitter: expect.objectContaining({ id: 'current-user-id', @@ -656,7 +656,7 @@ describe('file a report on a resource', () => { __typename: 'Comment', id: 'abusive-comment-1', }, - reportsFiled: expect.arrayContaining([ + filed: expect.arrayContaining([ expect.objectContaining({ submitter: expect.objectContaining({ id: 'current-user-id', diff --git a/backend/src/schema/types/type/REVIEWED.gql b/backend/src/schema/types/type/REVIEWED.gql index d106e1f4c..aea005abe 100644 --- a/backend/src/schema/types/type/REVIEWED.gql +++ b/backend/src/schema/types/type/REVIEWED.gql @@ -6,7 +6,6 @@ type REVIEWED { report: Report # @cypher(statement: "MATCH (report:Report)<-[this:REVIEWED]-(:User) RETURN report") moderator: User - type: String resource: ReviewedResource } union ReviewedResource = User | Post | Comment diff --git a/backend/src/schema/types/type/Report.gql b/backend/src/schema/types/type/Report.gql index aa94415a4..49e5bdae3 100644 --- a/backend/src/schema/types/type/Report.gql +++ b/backend/src/schema/types/type/Report.gql @@ -5,10 +5,9 @@ type Report { rule: ReportRule! disable: Boolean! closed: Boolean! - reportsFiled: [FILED] + filed: [FILED] + reviewed: [REVIEWED] resource: ReportedResource - type: String - reviewedByModerator: User } union ReportedResource = User | Post | Comment