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 2705cbe
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 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,25 @@ export class OrganizationInvitationLibrary {

return this.queries.pool.transaction(async (connection) => {
const organizationQueries = new OrganizationQueries(connection);
// Fetch existing invitations to the invitee first
const existingInvitations = await organizationQueries.invitations.findEntities({ invitee });
// 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.
const expiringInvitations = existingInvitations.filter(
(invitation) => invitation.expiresAt < Date.now()
);
if (expiringInvitations.length > 0) {
await Promise.all(
expiringInvitations.map(async (invitation) =>
organizationQueries.invitations.updateById(invitation.id, {
status: OrganizationInvitationStatus.Expired,
updatedAt: Date.now(),
})
)
);
}
// Insert the new invitation
const invitation = await organizationQueries.invitations.insert({
id: generateStandardId(),
inviterId,
Expand Down

0 comments on commit 2705cbe

Please sign in to comment.