0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

Merge pull request #5523 from logto-io/gao-allow-filter-invitation-by-invitee

feat: allow get invitations by invitee
This commit is contained in:
Gao Sun 2024-03-20 11:29:33 +08:00 committed by GitHub
commit bc110843e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 2 deletions

View file

@ -92,6 +92,7 @@ type OrganizationInvitationSearchOptions = {
invitationId?: string;
organizationId?: string;
inviterId?: string;
invitee?: string;
};
class OrganizationInvitationsQueries extends SchemaQueries<
@ -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);
@ -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`}`;

View file

@ -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],
}),

View file

@ -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(