//graphql-neighbouring-rights/src/cypher/ownership-rules/update-remove-carveout-rules-for-territory.cypher OPTIONAL MATCH(vendor:Vendor) WHERE vendor.id = toInteger($input.vendorId) CALL apoc.util.validate(vendor IS NULL, 'Invalid vendor id', []) MATCH(p:NrProduct:NrOwnership)-[:INCLUDES]->(t:NrTrack)-[IS_PLACEMENT_OF]->(sr:NrSoundRecording:NrOwnership)-[r:HAS_OWNERSHIP_RULE_CARVEOUT]-(s:NrOwnershipService) WHERE p.upc = $input.upc AND r.vendorId = toInteger($input.vendorId) AND s.territory = $input.territory AND COALESCE(p.manuallyOverwritten, false) = false AND COALESCE(t.manuallyOverwritten, false) = false WITH collect(r) AS existingCarveoutRules, date() as ruleDate UNWIND existingCarveoutRules as existingCarveoutRule MATCH ()-[ruleToUpdate:HAS_OWNERSHIP_RULE_CARVEOUT]->(s) WHERE ruleToUpdate.id = existingCarveoutRule.id SET ruleToUpdate.endDate = ruleDate, ruleToUpdate.lastModifiedAt = datetime(), ruleToUpdate.lastModifiedBy = "graphql-neighbouring-rights/updateOwnershipNrCarveoutRules/" + $input.identityId RETURN collect({id: ruleToUpdate.id, type: type(ruleToUpdate)}) AS rules;