Return reviews with reports, rename variables

- Instead of just returning the moderator who reviewed, we return the
review as well
This commit is contained in:
mattwr18 2019-11-28 16:10:33 +01:00
parent 21be2c08ba
commit 89daa3356c
5 changed files with 80 additions and 42 deletions

View File

@ -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' } },
]),
},
},
},
})

View File

@ -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
},
},
}

View File

@ -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',

View File

@ -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

View File

@ -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