Skip to content

Commit

Permalink
fix: support edge case changeEmailDomain no users to update (#10725)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <[email protected]>
  • Loading branch information
mattkrick authored Jan 29, 2025
1 parent 976904e commit 8c0c0db
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions packages/server/graphql/private/mutations/changeEmailDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,46 @@ const changeEmailDomain: MutationResolvers['changeEmailDomain'] = async (
// RESOLUTION
const pg = getKysely()

const [updatedUserRes] = await Promise.all([
pg
.with('TeamMembersUpdate', (qc) =>
qc
.updateTable('TeamMember')
.set({
email: sql`CONCAT(LEFT(email, POSITION('@' in email)), ${normalizedNewDomain}::VARCHAR)`
})
.where('userId', 'in', userIdsToUpdate)
)
.with('OrganizationApprovedDomainUpdate', (qc) =>
qc
.updateTable('OrganizationApprovedDomain')
.set({
domain: sql`REPLACE("domain", ${normalizedOldDomain}, ${normalizedNewDomain})`
})
.where('domain', 'like', normalizedOldDomain)
)
.with('OrganizationUpdate', (qc) =>
qc
.updateTable('Organization')
.set({activeDomain: normalizedNewDomain})
.where('activeDomain', '=', normalizedOldDomain)
)
.with('SAMLUpdate', (qc) =>
qc
.updateTable('SAMLDomain')
.set({domain: normalizedNewDomain})
.where('domain', '=', normalizedOldDomain)
)
.updateTable('User')
.set({
email: sql`CONCAT(LEFT(email, POSITION('@' in email)), ${normalizedNewDomain}::VARCHAR)`
})
.where('id', 'in', userIdsToUpdate)
.returning('id')
.execute()
])
await pg
.with('OrganizationApprovedDomainUpdate', (qc) =>
qc
.updateTable('OrganizationApprovedDomain')
.set({
domain: sql`REPLACE("domain", ${normalizedOldDomain}, ${normalizedNewDomain})`
})
.where('domain', 'like', normalizedOldDomain)
)
.with('OrganizationUpdate', (qc) =>
qc
.updateTable('Organization')
.set({activeDomain: normalizedNewDomain})
.where('activeDomain', '=', normalizedOldDomain)
)
.updateTable('SAMLDomain')
.set({domain: normalizedNewDomain})
.where('domain', '=', normalizedOldDomain)
.execute()

if (userIdsToUpdate.length === 0) {
return {usersUpdatedIds: [], usersNotUpdatedIds}
}

const updatedUserRes = await pg
.with('TeamMembersUpdate', (qc) =>
qc
.updateTable('TeamMember')
.set({
email: sql`CONCAT(LEFT(email, POSITION('@' in email)), ${normalizedNewDomain}::VARCHAR)`
})
.where('userId', 'in', userIdsToUpdate)
)
.updateTable('User')
.set({
email: sql`CONCAT(LEFT(email, POSITION('@' in email)), ${normalizedNewDomain}::VARCHAR)`
})
.where('id', 'in', userIdsToUpdate)
.returning('id')
.execute()
const usersUpdatedIds = updatedUserRes.map(({id}) => id)
const data = {usersUpdatedIds, usersNotUpdatedIds}
return data
Expand Down

0 comments on commit 8c0c0db

Please sign in to comment.