This commit is contained in:
Bereket Engida
2025-11-08 18:59:39 -08:00
parent 2b27b66a14
commit f1f3fb9e25
3 changed files with 53 additions and 12 deletions

View File

@@ -114,7 +114,6 @@ export const getOrgAdapter = <O extends OrganizationOptions>(
organizationId?: string | undefined;
limit?: number | undefined;
offset?: number | undefined;
organizationSlug?: string | undefined;
sortBy?: string | undefined;
sortOrder?: ("asc" | "desc") | undefined;
filter?:
@@ -130,10 +129,7 @@ export const getOrgAdapter = <O extends OrganizationOptions>(
adapter.findMany<Member>({
model: "member",
where: [
{
field: data.organizationSlug ? "slug" : "id",
value: data.organizationSlug ?? data.organizationId,
},
{ field: "organizationId", value: data.organizationId },
...(data.filter?.field
? [
{

View File

@@ -73,6 +73,19 @@ describe("listMembers", async () => {
expect(members.data?.total).toBe(11);
});
it("should return all members by organization slug", async () => {
const members = await client.organization.listMembers({
fetchOptions: {
headers,
},
query: {
organizationSlug: "test-second",
},
});
expect(members.data?.members.length).toBe(1);
expect(members.data?.total).toBe(1);
});
it("should limit the number of members", async () => {
const members = await client.organization.listMembers({
fetchOptions: {

View File

@@ -880,14 +880,28 @@ export const listMembers = <O extends OrganizationOptions>(options: O) =>
},
async (ctx) => {
const session = ctx.context.session;
const organizationId =
ctx.query?.organizationId || session.session.activeOrganizationId;
let organizationId =
ctx.query?.organizationId ||
ctx.query?.organizationSlug ||
session.session.activeOrganizationId;
if (!organizationId) {
throw new APIError("BAD_REQUEST", {
message: ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION,
});
}
const adapter = getOrgAdapter<O>(ctx.context, options);
if (ctx.query?.organizationSlug) {
const organization = await adapter.findOrganizationBySlug(
ctx.query?.organizationSlug,
);
if (!organization) {
throw new APIError("BAD_REQUEST", {
message: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,
});
}
organizationId = organization.id;
}
const isMember = await adapter.findMemberByOrgId({
userId: session.user.id,
organizationId,
@@ -911,7 +925,6 @@ export const listMembers = <O extends OrganizationOptions>(options: O) =>
value: ctx.query.filterValue,
}
: undefined,
organizationSlug: ctx.query?.organizationSlug,
});
return ctx.json({
members,
@@ -943,22 +956,41 @@ export const getActiveMemberRole = <O extends OrganizationOptions>(
'The organization ID to list members for. If not provided, will default to the user\'s active organization. Eg: "organization-id"',
})
.optional(),
organizationSlug: z
.string()
.meta({
description:
'The organization slug to list members for. If not provided, will default to the user\'s active organization. Eg: "organization-slug"',
})
.optional(),
})
.optional(),
use: [orgMiddleware, orgSessionMiddleware],
},
async (ctx) => {
const session = ctx.context.session;
const organizationId =
ctx.query?.organizationId || session.session.activeOrganizationId;
let organizationId =
ctx.query?.organizationId ||
ctx.query?.organizationSlug ||
session.session.activeOrganizationId;
if (!organizationId) {
throw new APIError("BAD_REQUEST", {
message: ORGANIZATION_ERROR_CODES.NO_ACTIVE_ORGANIZATION,
});
}
const userId = ctx.query?.userId || session.user.id;
const adapter = getOrgAdapter<O>(ctx.context, options);
if (ctx.query?.organizationSlug) {
const organization = await adapter.findOrganizationBySlug(
ctx.query?.organizationSlug,
);
if (!organization) {
throw new APIError("BAD_REQUEST", {
message: ORGANIZATION_ERROR_CODES.ORGANIZATION_NOT_FOUND,
});
}
organizationId = organization.id;
}
const userId = ctx.query?.userId || session.user.id || organizationId;
const member = await adapter.findMemberByOrgId({
userId,