Fix, refactor, and partly implement generation of a location relation in Neo4j for users and groups

This commit is contained in:
Wolfgang Huß 2022-09-16 21:06:53 +02:00
parent d32c137cff
commit 1195be11ac
3 changed files with 14 additions and 13 deletions

View File

@ -137,7 +137,7 @@ export default {
})
try {
const group = await writeTxResultPromise
await createOrUpdateLocations(params.id, params.locationName, session)
await createOrUpdateLocations(params.id, 'Group', params.locationName, session)
return group
} catch (error) {
if (error.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')
@ -210,7 +210,7 @@ export default {
})
try {
const group = await writeTxResultPromise
await createOrUpdateLocations(params.id, params.locationName, session)
await createOrUpdateLocations(params.id, 'Group', params.locationName, session)
return group
} catch (error) {
if (error.code === 'Neo.ClientError.Schema.ConstraintValidationFailed')

View File

@ -169,7 +169,7 @@ export default {
})
try {
const user = await writeTxResultPromise
await createOrUpdateLocations(params.id, params.locationName, session)
await createOrUpdateLocations(params.id, 'User', params.locationName, session)
return user
} catch (error) {
throw new UserInputError(error.message)

View File

@ -62,7 +62,7 @@ const createLocation = async (session, mapboxData) => {
})
}
const createOrUpdateLocations = async (userId, locationName, session) => {
const createOrUpdateLocations = async (nodeId, nodeLabel, locationName, session) => {
if (isEmpty(locationName)) {
return
}
@ -121,18 +121,19 @@ const createOrUpdateLocations = async (userId, locationName, session) => {
parent = ctx
})
}
// delete all current locations from user and add new location
// delete all current locations from node and add new location
await session.writeTransaction((transaction) => {
return transaction.run(
`
MATCH (user:User {id: $userId})-[relationship:IS_IN]->(location:Location)
DETACH DELETE relationship
WITH user
MATCH (location:Location {id: $locationId})
MERGE (user)-[:IS_IN]->(location)
RETURN location.id, user.id
`,
{ userId: userId, locationId: data.id },
MATCH (node:${nodeLabel} {id: $nodeId})
OPTIONAL MATCH (node)-[relationship:IS_IN]->(:Location)
DELETE relationship
WITH node
MATCH (location:Location {id: $locationId})
MERGE (node)-[:IS_IN]->(location)
RETURN location.id, node.id
`,
{ nodeId, locationId: data.id },
)
})
}