mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
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
This commit is contained in:
parent
c0d9fe5257
commit
5d63dda6ee
@ -59,28 +59,8 @@ const validateUpdatePost = async (resolve, root, args, context, info) => {
|
|||||||
|
|
||||||
const validateReport = async (resolve, root, args, context, info) => {
|
const validateReport = async (resolve, root, args, context, info) => {
|
||||||
const { resourceId } = args
|
const { resourceId } = args
|
||||||
const { user, driver } = context
|
const { user } = context
|
||||||
if (resourceId === user.id) throw new Error('You cannot report yourself!')
|
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)
|
return resolve(root, args, context, info)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +86,7 @@ const validateReview = async (resolve, root, args, context, info) => {
|
|||||||
const [existingReportedResource] = reportQueryRes.records.map(record => {
|
const [existingReportedResource] = reportQueryRes.records.map(record => {
|
||||||
return {
|
return {
|
||||||
label: record.get('label'),
|
label: record.get('label'),
|
||||||
author: record.get('author'),
|
author: record.get('author').properties,
|
||||||
filed: record.get('filed'),
|
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) throw new Error(`Resource not found!`)
|
||||||
if (!existingReportedResource.filed)
|
if (!existingReportedResource.filed)
|
||||||
throw new Error(
|
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 =
|
const authorId =
|
||||||
existingReportedResource.label !== 'User' && existingReportedResource.author
|
existingReportedResource.label !== 'User' && existingReportedResource.author
|
||||||
? existingReportedResource.author.id
|
? existingReportedResource.author.id
|
||||||
: null
|
: null
|
||||||
if (authorId && resourceId === authorId)
|
if (authorId && authorId === user.id)
|
||||||
throw new Error(`You cannot review your own ${existingReportedResource.label}!`)
|
throw new Error(`You cannot review your own ${existingReportedResource.label}!`)
|
||||||
return resolve(root, args, context, info)
|
return resolve(root, args, context, info)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,8 +48,6 @@ beforeAll(() => {
|
|||||||
mutate = createTestClient(server).mutate
|
mutate = createTestClient(server).mutate
|
||||||
})
|
})
|
||||||
|
|
||||||
// const { query } = createTestClient(server)
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
users = await Promise.all([
|
users = await Promise.all([
|
||||||
factory.create('User', {
|
factory.create('User', {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user