Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): update expired invitation to expired before inserting a new one #5609

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 Expand Up @@ -123,7 +128,7 @@
status: OrganizationInvitationStatus.Accepted,
acceptedUserId: string
): Promise<OrganizationInvitationEntity>;
// TODO: Error i18n

Check warning on line 131 in packages/core/src/libraries/organization-invitation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/core/src/libraries/organization-invitation.ts#L131

[no-warning-comments] Unexpected 'todo' comment: 'TODO: Error i18n'.
async updateStatus(
id: string,
status: OrganizationInvitationStatus,
Expand Down
19 changes: 19 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,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,
Expand Down
Loading