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 6b1e634
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/core/src/libraries/organization-invitation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ export class OrganizationInvitationLibrary {
messagePayload: SendMessagePayload | false
) {
const { inviterId, invitee, organizationId, expiresAt, organizationRoleIds } = data;
const isMember = await this.queries.organizations.relations.users.isMember(
organizationId,
invitee
);

if (await this.queries.organizations.relations.users.isMember(organizationId, invitee)) {
if (isMember) {
throw new RequestError({
status: 422,
code: 'request.invalid_input',
Expand All @@ -66,6 +70,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 6b1e634

Please sign in to comment.