fix: remove previous email address on change

This will allow you to change back to your previous email address: The
backend won't complain because of a user who owns that email address
already.
This commit is contained in:
roschaefer 2019-10-02 01:55:14 +02:00
parent e6f8bbac9b
commit 1e31a0c620
2 changed files with 22 additions and 7 deletions

View File

@ -57,13 +57,13 @@ export default {
const writeTxResultPromise = session.writeTransaction(async txc => {
const result = await txc.run(
`
MATCH (user:User {id: $userId})-[previous:PRIMARY_EMAIL]->(:EmailAddress)
MATCH (user:User {id: $userId})-[:PRIMARY_EMAIL]->(previous:EmailAddress)
MATCH (user)<-[:BELONGS_TO]-(email:UnverifiedEmailAddress {email: $email, nonce: $nonce})
MERGE (user)-[:PRIMARY_EMAIL]->(email)
SET email:EmailAddress
SET email.verifiedAt = toString(datetime())
REMOVE email:UnverifiedEmailAddress
DELETE previous
DETACH DELETE previous
RETURN email
`,
{ userId, email, nonce },

View File

@ -63,7 +63,7 @@ describe('AddEmailAddress', () => {
describe('authenticated', () => {
beforeEach(async () => {
user = await factory.create('User', { email: 'user@example.org' })
user = await factory.create('User', { id: '567', email: 'user@example.org' })
authenticatedUser = await user.toJson()
})
@ -169,7 +169,7 @@ describe('VerifyEmailAddress', () => {
describe('authenticated', () => {
beforeEach(async () => {
user = await factory.create('User', { email: 'user@example.org' })
user = await factory.create('User', { id: '567', email: 'user@example.org' })
authenticatedUser = await user.toJson()
})
@ -238,8 +238,7 @@ describe('VerifyEmailAddress', () => {
it('connects the new `EmailAddress` as PRIMARY', async () => {
await mutate({ mutation, variables })
const result = await neode.cypher(`
MATCH(u:User)-[:PRIMARY_EMAIL]->(e:EmailAddress {email: "to-be-verified@example.org"})
MATCH(u:User)<-[:BELONGS_TO]-(:EmailAddress {email: "user@example.org"})
MATCH(u:User {id: "567"})-[:PRIMARY_EMAIL]->(e:EmailAddress {email: "to-be-verified@example.org"})
RETURN e
`)
const email = neode.hydrateFirst(result, 'e', neode.model('EmailAddress'))
@ -250,7 +249,23 @@ describe('VerifyEmailAddress', () => {
it('removes previous PRIMARY relationship', async () => {
const cypherStatement = `
MATCH(u:User)-[:PRIMARY_EMAIL]->(e:EmailAddress {email: "user@example.org"})
MATCH(u:User {id: "567"})-[:PRIMARY_EMAIL]->(e:EmailAddress {email: "user@example.org"})
RETURN e
`
let result = await neode.cypher(cypherStatement)
let email = neode.hydrateFirst(result, 'e', neode.model('EmailAddress'))
await expect(email.toJson()).resolves.toMatchObject({
email: 'user@example.org',
})
await mutate({ mutation, variables })
result = await neode.cypher(cypherStatement)
email = neode.hydrateFirst(result, 'e', neode.model('EmailAddress'))
await expect(email).toBe(false)
})
it('removes previous `EmailAddress` node', async () => {
const cypherStatement = `
MATCH(u:User {id: "567"})<-[:BELONGS_TO]-(e:EmailAddress {email: "user@example.org"})
RETURN e
`
let result = await neode.cypher(cypherStatement)