mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
`BELONGS_TO` means a user owns an email address. `PRIMARY_EMAIL` means a user authenticates with that email. So right now, you get a proper error message if you try to change your email back to your old email address (because you own it already). I will make sure to delete the old email so this will be no problem anymore. But maybe in the future we might have multiple email addresses per user and then it makes a big difference to use `PRIMARY_EMAIL` or `BELONGS_TO`.
27 lines
865 B
JavaScript
27 lines
865 B
JavaScript
import { UserInputError } from 'apollo-server'
|
|
export default async function alreadyExistingMail(_parent, args, context) {
|
|
let { email } = args
|
|
email = email.toLowerCase()
|
|
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 })
|
|
} 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
|
|
}
|