0
Fork 0
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:
Charles Zhao 2024-04-01 20:18:03 +08:00 committed by GitHub
parent add78b77a4
commit 2b5e6d6fb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View file

@ -66,6 +66,11 @@ export class OrganizationInvitationLibrary {
return this.queries.pool.transaction(async (connection) => { return this.queries.pool.transaction(async (connection) => {
const organizationQueries = new OrganizationQueries(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({ const invitation = await organizationQueries.invitations.insert({
id: generateStandardId(), id: generateStandardId(),
inviterId, inviterId,

View file

@ -116,6 +116,25 @@ class OrganizationInvitationsQueries extends SchemaQueries<
return this.pool.any(this.#findEntity({ ...options, invitationId: undefined })); 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({ #findEntity({
invitationId, invitationId,
organizationId, organizationId,