From 1195be11ac166dfe0c4ffae1e85f94264dec1067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Fri, 16 Sep 2022 21:06:53 +0200 Subject: [PATCH] Fix, refactor, and partly implement generation of a location relation in Neo4j for users and groups --- backend/src/schema/resolvers/groups.js | 4 ++-- backend/src/schema/resolvers/users.js | 2 +- .../src/schema/resolvers/users/location.js | 21 ++++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/backend/src/schema/resolvers/groups.js b/backend/src/schema/resolvers/groups.js index 2f7ed4588..a0787a9cb 100644 --- a/backend/src/schema/resolvers/groups.js +++ b/backend/src/schema/resolvers/groups.js @@ -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') diff --git a/backend/src/schema/resolvers/users.js b/backend/src/schema/resolvers/users.js index 12f00ffb6..bbb407229 100644 --- a/backend/src/schema/resolvers/users.js +++ b/backend/src/schema/resolvers/users.js @@ -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) diff --git a/backend/src/schema/resolvers/users/location.js b/backend/src/schema/resolvers/users/location.js index affd3267e..b6ae798f4 100644 --- a/backend/src/schema/resolvers/users/location.js +++ b/backend/src/schema/resolvers/users/location.js @@ -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 }, ) }) }