mirror of
https://github.com/logto-io/logto.git
synced 2025-01-20 21:32:31 -05:00
fix(core): update expired invitation to expired before inserting a new one (#5609)
This commit is contained in:
parent
add78b77a4
commit
2b5e6d6fb6
2 changed files with 24 additions and 0 deletions
|
@ -66,6 +66,11 @@ export class OrganizationInvitationLibrary {
|
|||
|
||||
return this.queries.pool.transaction(async (connection) => {
|
||||
const organizationQueries = new OrganizationQueries(connection);
|
||||
// Check if any pending invitation has expired, if yes, update the invitation status to "Expired" first
|
||||
// Note: Even if the status may appear to be "Expired", the actual data in DB may still be "Pending".
|
||||
// Check `findEntities` in `OrganizationQueries` for more details.
|
||||
await organizationQueries.invitations.updateExpiredEntities({ invitee, organizationId });
|
||||
// Insert the new invitation
|
||||
const invitation = await organizationQueries.invitations.insert({
|
||||
id: generateStandardId(),
|
||||
inviterId,
|
||||
|
|
|
@ -116,6 +116,25 @@ class OrganizationInvitationsQueries extends SchemaQueries<
|
|||
return this.pool.any(this.#findEntity({ ...options, invitationId: undefined }));
|
||||
}
|
||||
|
||||
async updateExpiredEntities({
|
||||
organizationId,
|
||||
invitee,
|
||||
}: OrganizationInvitationSearchOptions): Promise<void> {
|
||||
const { table, fields } = convertToIdentifiers(OrganizationInvitations);
|
||||
await this.pool.query(sql`
|
||||
update ${table}
|
||||
set ${fields.status} = ${OrganizationInvitationStatus.Expired}
|
||||
where ${fields.status} = ${OrganizationInvitationStatus.Pending}
|
||||
and ${fields.expiresAt} < now()
|
||||
${conditionalSql(organizationId, (id) => {
|
||||
return sql`and ${fields.organizationId} = ${id}`;
|
||||
})}
|
||||
${conditionalSql(invitee, (email) => {
|
||||
return sql`and ${fields.invitee} = ${email}`;
|
||||
})}
|
||||
`);
|
||||
}
|
||||
|
||||
#findEntity({
|
||||
invitationId,
|
||||
organizationId,
|
||||
|
|
Loading…
Add table
Reference in a new issue