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

feat: allow get invitations by invitee #5523

Merged
merged 1 commit into from
Mar 20, 2024
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
11 changes: 10 additions & 1 deletion packages/core/src/queries/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type OrganizationInvitationSearchOptions = {
invitationId?: string;
organizationId?: string;
inviterId?: string;
invitee?: string;
};

class OrganizationInvitationsQueries extends SchemaQueries<
Expand All @@ -115,7 +116,12 @@ class OrganizationInvitationsQueries extends SchemaQueries<
return this.pool.any(this.#findEntity({ ...options, invitationId: undefined }));
}

#findEntity({ invitationId, organizationId, inviterId }: OrganizationInvitationSearchOptions) {
#findEntity({
invitationId,
organizationId,
inviterId,
invitee,
}: OrganizationInvitationSearchOptions) {
const { table, fields } = convertToIdentifiers(OrganizationInvitations, true);
const roleRelations = convertToIdentifiers(OrganizationInvitationRoleRelations, true);
const roles = convertToIdentifiers(OrganizationRoles, true);
Expand Down Expand Up @@ -159,6 +165,9 @@ class OrganizationInvitationsQueries extends SchemaQueries<
${conditionalSql(inviterId, (id) => {
return sql`and ${fields.inviterId} = ${id}`;
})}
${conditionalSql(invitee, (email) => {
return sql`and ${fields.invitee} = ${email}`;
})}
group by ${fields.id}
${conditionalSql(this.orderBy, ({ field, order }) => {
return sql`order by ${fields[field]} ${order === 'desc' ? sql`desc` : sql`asc`}`;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/routes/organization/invitations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export default function organizationInvitationRoutes<T extends AuthedRouter>(
router.get(
'/',
koaGuard({
query: z.object({ organizationId: z.string().optional(), inviterId: z.string().optional() }),
query: z
.object({ organizationId: z.string(), inviterId: z.string(), invitee: z.string() })
.partial(),
response: organizationInvitationEntityGuard.array(),
status: [200],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ describe('organization invitation creation', () => {
await Promise.all([deleteUser(inviter.id), deleteUser(inviter2.id)]);
});

it('should be able to get invitations by invitee', async () => {
const organization = await organizationApi.create({ name: 'test' });
const invitee = `${randomId()}@example.com`;
await invitationApi.create({
organizationId: organization.id,
invitee,
expiresAt: Date.now() + 1_000_000,
});

const invitations = await invitationApi.getList(new URLSearchParams({ invitee }));
expect(invitations.length).toBe(1);
expect(invitations[0]?.invitee).toBe(invitee);
});

it('should have no pagination', async () => {
const organization = await organizationApi.create({ name: 'test' });
await Promise.all(
Expand Down
Loading