From d39e702e70671ea8271d136227e12fc07b535d7a Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Wed, 11 Dec 2019 18:19:40 +0100 Subject: [PATCH] Update exisitingEmailAddress --- .../resolvers/helpers/existingEmailAddress.js | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/src/schema/resolvers/helpers/existingEmailAddress.js b/backend/src/schema/resolvers/helpers/existingEmailAddress.js index ee1a6af82..960b2066f 100644 --- a/backend/src/schema/resolvers/helpers/existingEmailAddress.js +++ b/backend/src/schema/resolvers/helpers/existingEmailAddress.js @@ -1,25 +1,29 @@ import { UserInputError } from 'apollo-server' export default async function alreadyExistingMail({ args, context }) { - const cypher = ` - MATCH (email:EmailAddress {email: $email}) - OPTIONAL MATCH (email)-[:BELONGS_TO]-(user) - RETURN email, user - ` - let transactionRes const session = context.driver.session() try { - transactionRes = await session.run(cypher, { email: args.email }) + const existingEmailAddressTxPromise = session.writeTransaction(async transaction => { + const existingEmailAddressTransactionResponse = await transaction.run( + ` + MATCH (email:EmailAddress {email: $email}) + OPTIONAL MATCH (email)-[:BELONGS_TO]-(user) + RETURN email, user + `, + { email: args.email }, + ) + return existingEmailAddressTransactionResponse.records.map(record => { + return { + alreadyExistingEmail: record.get('email').properties, + user: record.get('user') && record.get('user').properties, + } + }) + }) + const [emailBelongsToUser] = await existingEmailAddressTxPromise + const { alreadyExistingEmail, user } = emailBelongsToUser || {} + if (user) throw new UserInputError('A user account with this email already exists.') + return alreadyExistingEmail } finally { session.close() } - const [result] = transactionRes.records.map(record => { - return { - alreadyExistingEmail: record.get('email').properties, - user: record.get('user') && record.get('user').properties, - } - }) - const { alreadyExistingEmail, user } = result || {} - if (user) throw new UserInputError('A user account with this email already exists.') - return alreadyExistingEmail }