Skip to content

Commit

Permalink
fix(core): update expired invitation to expired before inserting a ne…
Browse files Browse the repository at this point in the history
…w one
  • Loading branch information
charIeszhao committed Apr 1, 2024
1 parent add78b7 commit 008577e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/libraries/organization-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/queries/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ class OrganizationInvitationsQueries extends SchemaQueries<
return this.pool.any(this.#findEntity({ ...options, invitationId: undefined }));
}

async updateExpiredEntities({
invitationId,
organizationId,
inviterId,
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(invitationId, (id) => {
return sql`and ${fields.id} = ${id}`;
})}
${conditionalSql(organizationId, (id) => {
return sql`and ${fields.organizationId} = ${id}`;
})}
${conditionalSql(inviterId, (id) => {
return sql`and ${fields.inviterId} = ${id}`;
})}
${conditionalSql(invitee, (email) => {
return sql`and ${fields.invitee} = ${email}`;
})}
`);
}

#findEntity({
invitationId,
organizationId,
Expand Down

0 comments on commit 008577e

Please sign in to comment.