Get decide mutation to work

- Tests are not fully working yet!
This commit is contained in:
Wolfgang Huß 2019-10-30 19:01:54 +01:00
parent 54be7782ba
commit 21e82d2ea5
5 changed files with 60 additions and 45 deletions

View File

@ -89,7 +89,7 @@ const validateReport = async (resolve, root, args, context, info) => {
// const session = driver.session()
// const reportQueryRes = await session.run(
// `
// MATCH (:User {id:$submitterId})-[:REPORTED]->(resource {id:$resourceId})
// MATCH (:User {id:$submitterId})-[:REPORTED]->(resource {id:$resourceId})
// RETURN labels(resource)[0] as label
// `,
// {

View File

@ -37,9 +37,6 @@ export default {
SET decision.disabled = false, decision.updatedAt = toString(datetime())
RETURN resource {.id}
`
// Wolle
// DELETE decision
const session = driver.session()
const res = await session.run(cypher, { resourceId })
session.close()
@ -51,7 +48,8 @@ export default {
},
decide: async (object, params, context, _resolveInfo) => {
let createdRelationshipWithNestedAttributes = null
const { resourceId, disabled, closed } = params
const { resourceId } = params
let { disabled, closed } = params
const { user: moderator, driver } = context
const session = driver.session()
@ -62,7 +60,8 @@ export default {
MATCH (moderator:User)-[decision:DECIDED {closed: false}]->(resource {id: $resourceId})
WHERE resource:User OR resource:Comment OR resource:Post
RETURN decision, moderator {.id} AS decisionModerator
`, { resourceId },
`,
{ resourceId },
)
return decisionRelationshipTransactionResponse.records.map(record => ({
decision: record.get('decision'),
@ -71,31 +70,32 @@ export default {
})
try {
const cypherHeader = ''
let cypherHeader = ''
// is there an open decision?
const [existingDecisionTxResult] = await existingDecisionWriteTxResultPromise
if (!existingDecisionTxResult) {
// no open decision, then create one
if (!disabled) disabled = false // default for creation
if (!disabled) closed = false // default for creation
if (disabled === undefined) disabled = false // default for creation
if (closed === undefined) closed = false // default for creation
cypherHeader = `
MATCH (moderator:User {id: $moderatorId})
MATCH (resource {id: $resourceId})
WHERE resource:User OR resource:Comment OR resource:Post
CREATE (resource)<-[decision:DECIDED]-(moderator)
SET decision.last = true
WITH decision, resource, moderator
OPTIONAL MATCH (:User)-[lastDecision:DECIDED {last: true}]->(resource)
SET (
CASE
WHEN lastDecision IS NOT NULL
THEN lastDecision END).last = false
`
`
} else {
// an open decision …
if (!disabled) disabled = existingDecisionTxResult.decision.properties.disabled // default set to existing
if (!disabled) closed = existingDecisionTxResult.decision.properties.closed // default set to existing
if (disabled === undefined)
disabled = existingDecisionTxResult.decision.properties.disabled // default set to existing
if (closed === undefined) closed = existingDecisionTxResult.decision.properties.closed // default set to existing
// current moderator is not the same as old
if (moderator.id !== existingDecisionTxResult.decisionModerator.id) {
// an open decision from different moderator, then change relation and properties
@ -108,37 +108,37 @@ export default {
WHERE resource:User OR resource:Comment OR resource:Post
CREATE (resource)<-[decision:DECIDED]-(moderator)
SET decision = oldDecision
`
`
} else {
// an open decision from same moderator, then change properties
cypherHeader = `
MATCH (moderator:User)-[decision:DECIDED {closed: false}]->(resource {id: $resourceId})
WHERE resource:User OR resource:Comment OR resource:Post
`
`
}
}
const cypher =
cypherHeader +
`SET (
CASE
WHEN decision.createdAt IS NOT NULL
THEN decision END).updatedAt = toString(datetime())
SET (
CASE
WHEN decision.createdAt IS NULL
THEN decision END).createdAt = toString(datetime())
SET decision.disabled = $disabled, decision.closed = $closed
SET resource.disabled = $disabled
RETURN decision, resource, moderator, labels(resource)[0] AS type
`
const newDecisionWriteTxResultPromise = session.writeTransaction(async txc => {
const decisionRelationshipTransactionResponse = await txc.run(
cypherHeader + `
SET (
CASE
WHEN decision.createdAt IS NOT NULL
THEN decision END).updatedAt = toString(datetime())
SET (
CASE
WHEN decision.createdAt IS NULL
THEN decision END).createdAt = toString(datetime())
SET decision.disabled = $disabled, decision.closed = $closed
SET resource.disabled = $disabled
RETURN decision, resource, moderator, labels(resource)[0] AS type
`, {
resourceId,
moderatorId: moderator.id,
disabled,
closed,
},
)
const decisionRelationshipTransactionResponse = await txc.run(cypher, {
resourceId,
moderatorId: moderator.id,
disabled,
closed,
})
return decisionRelationshipTransactionResponse.records.map(record => ({
decision: record.get('decision'),
resource: record.get('resource'),

View File

@ -12,7 +12,14 @@ let query, mutate, authenticatedUser, variables, moderator, nonModerator
const disableMutation = gql`
mutation($id: ID!) {
disable(id: $id)
decide(resourceId: $id, disabled: true, closed: false) {
post {
id
}
comment {
id
}
}
}
`
const enableMutation = gql`
@ -123,7 +130,7 @@ describe('moderate resources', () => {
it('returns null', async () => {
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: null },
data: { decide: null },
})
})
})
@ -139,7 +146,7 @@ describe('moderate resources', () => {
it('returns disabled resource id', async () => {
variables = { id: 'comment-id' }
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
data: { decide: { comment: { id: 'comment-id' } } },
errors: undefined,
})
})
@ -152,7 +159,7 @@ describe('moderate resources', () => {
}
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
data: { decide: { comment: { id: 'comment-id' } } },
})
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
})
@ -164,7 +171,7 @@ describe('moderate resources', () => {
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
data: { decide: { comment: { id: 'comment-id' } } },
})
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
})
@ -181,7 +188,7 @@ describe('moderate resources', () => {
it('returns disabled resource id', async () => {
variables = { id: 'sample-post-id' }
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: 'sample-post-id' },
data: { decide: { post: { id: 'sample-post-id' } } },
})
})
@ -189,12 +196,14 @@ describe('moderate resources', () => {
variables = { id: 'sample-post-id' }
const before = { data: { Post: [{ id: 'sample-post-id', decidedByModerator: null }] } }
const expected = {
data: { Post: [{ id: 'sample-post-id', decidedByModerator: { id: 'moderator-id' } }] },
data: {
Post: [{ id: 'sample-post-id', decidedByModerator: { id: 'moderator-id' } }],
},
}
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: 'sample-post-id' },
data: { decide: { post: { id: 'sample-post-id' } } },
})
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
})
@ -206,7 +215,7 @@ describe('moderate resources', () => {
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
data: { disable: 'sample-post-id' },
data: { decide: { post: { id: 'sample-post-id' } } },
})
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
})

View File

@ -12,6 +12,8 @@ type REPORTED {
user: User
post: Post
comment: Comment
# decided: DECIDED
}
# this list equals the strings of an array in file "webapp/constants/modals.js"

View File

@ -91,7 +91,11 @@
<div v-if="content.resource.decidedByModerator">
{{ $t('moderation.reports.disabledBy') }}
<br />
<hc-user :user="content.resource.decidedByModerator" :showAvatar="false" :trunc="30" />
<hc-user
:user="content.resource.decidedByModerator"
:showAvatar="false"
:trunc="30"
/>
</div>
<span v-else class="no-decision">{{ $t('moderation.reports.noDecision') }}</span>
</td>