fix(backend): error when there is an abandoned email (#8315)

* fix error when there is an abandoned email

We check beforehand if an email has an user attached, but we never
delete an email without user. This will violate a unique constraint
blocking that particular email from ever being used again.

* fix tests
This commit is contained in:
Ulf Gebhardt 2025-04-10 20:17:51 +02:00 committed by GitHub
parent abd13a6cff
commit 711061b0c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 2 deletions

View File

@ -294,9 +294,9 @@ describe('VerifyEmailAddress', () => {
await expect(email).toBe(false)
})
describe('Edge case: In the meantime someone created an `EmailAddress` node with the given email', () => {
describe('Edge case: In the meantime someone created an `EmailAddress` node with the given email belonging to a user', () => {
beforeEach(async () => {
await Factory.build('emailAddress', { email: 'to-be-verified@example.org' })
await Factory.build('user', { id: '568' }, { email: 'to-be-verified@example.org' })
})
it('throws UserInputError because of unique constraints', async () => {
@ -306,6 +306,24 @@ describe('VerifyEmailAddress', () => {
})
})
})
describe('Edge case: We have an abandoned `EmailAddress` node with the given email', () => {
beforeEach(async () => {
await Factory.build('emailAddress', { email: 'to-be-verified@example.org' })
})
it('connects the new `EmailAddress` as PRIMARY', async () => {
await mutate({ mutation, variables })
const result = await neode.cypher(`
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'))
await expect(email.toJson()).resolves.toMatchObject({
email: 'to-be-verified@example.org',
})
})
})
})
})
})

View File

@ -87,6 +87,8 @@ export default {
`
MATCH (user:User {id: $userId})-[:PRIMARY_EMAIL]->(previous:EmailAddress)
MATCH (user)<-[:BELONGS_TO]-(email:UnverifiedEmailAddress {email: $email, nonce: $nonce})
OPTIONAL MATCH (abandonedEmail:EmailAddress{email: $email}) WHERE NOT EXISTS ((abandonedEmail)<-[]-())
DELETE abandonedEmail
MERGE (user)-[:PRIMARY_EMAIL]->(email)
SET email:EmailAddress
SET email.verifiedAt = toString(datetime())