Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(sub-org): add server-side search, ordering, and totalCount to list endpoint

Igor Correab5c3080fb1a4
diff --git a/backend/src/ee/routes/v1/sub-org-router.ts b/backend/src/ee/routes/v1/sub-org-router.ts
index 9339138451..c8723e08d7 100644
--- a/backend/src/ee/routes/v1/sub-org-router.ts
+++ b/backend/src/ee/routes/v1/sub-org-router.ts
@@ -86,6 +86,9 @@ export const registerSubOrgRouter = async (server: FastifyZodProvider) => {
querystring: z.object({
limit: z.coerce.number().min(1).max(1000).default(25).describe(SUB_ORGANIZATIONS.LIST.limit),
offset: z.coerce.number().min(0).default(0).describe(SUB_ORGANIZATIONS.LIST.offset),
+ search: z.string().trim().optional().describe(SUB_ORGANIZATIONS.LIST.search),
+ orderBy: z.enum(["name"]).default("name").describe(SUB_ORGANIZATIONS.LIST.orderBy),
+ orderDirection: z.enum(["asc", "desc"]).default("asc").describe(SUB_ORGANIZATIONS.LIST.orderDirection),
isAccessible: z
.enum(["true", "false"])
.optional()
@@ -94,22 +97,26 @@ export const registerSubOrgRouter = async (server: FastifyZodProvider) => {
}),
response: {
200: z.object({
- organizations: sanitizedSubOrganizationSchema.array()
+ organizations: sanitizedSubOrganizationSchema.array(),
+ totalCount: z.number()
})
}
},
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
handler: async (req) => {
- const { organizations } = await server.services.subOrganization.listSubOrgs({
+ const { organizations, totalCount } = await server.services.subOrganization.listSubOrgs({
permissionActor: req.permission,
data: {
limit: req.query.limit,
offset: req.query.offset,
+ search: req.query.search,
+ orderBy: req.query.orderBy,
+ orderDirection: req.query.orderDirection,
isAccessible: req.query.isAccessible
}
});
- return { organizations };
+ return { organizations, totalCount };
}
});
diff --git a/backend/src/ee/services/sub-org/sub-org-service.ts b/backend/src/ee/services/sub-org/sub-org-service.ts
index 3b7919f17e..42961e7de6 100644
--- a/backend/src/ee/services/sub-org/sub-org-service.ts
+++ b/backend/src/ee/services/sub-org/sub-org-service.ts
@@ -106,17 +106,21 @@ export const subOrgServiceFactory = ({
scope: OrganizationActionScope.Any
});
- const organizations = await orgDAL.listSubOrganizations({
+ const { orgs: organizations, totalCount } = await orgDAL.listSubOrganizations({
actorId: permissionActor.id,
actorType: permissionActor.type,
orgId: permissionActor.rootOrgId,
isAccessible: data?.isAccessible,
+ search: data?.search,
+ orderBy: data?.orderBy,
+ orderDirection: data?.orderDirection,
limit: data?.limit,
offset: data?.offset
});
return {
- organizations
+ organizations,
+ totalCount
};
};
diff --git a/backend/src/ee/services/sub-org/sub-org-types.ts b/backend/src/ee/services/sub-org/sub-org-types.ts
index 796e8be424..d711cccda1 100644
--- a/backend/src/ee/services/sub-org/sub-org-types.ts
+++ b/backend/src/ee/services/sub-org/sub-org-types.ts
@@ -12,6 +12,8 @@ export type TListSubOrgDTO = {
limit?: number;
offset?: number;
search?: string;
+ orderBy?: string;
+ orderDirection?: "asc" | "desc";
isAccessible?: boolean;
}>;
};
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 5e7f9502e9..bcc19b8a33 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -845,6 +845,9 @@ export const SUB_ORGANIZATIONS = {
LIST: {
limit: "The number of sub organizations to return.",
offset: "The offset to start from. If you enter 10, it will start from the 10th sub organization.",
+ search: "Optional. Filter sub organizations by name (case-insensitive substring match).",
+ orderBy: "The field to order sub organizations by. Currently only 'name' is supported.",
+ orderDirection: "The direction to order sub organizations. Either 'asc' or 'desc'.",
isAccessible: "Filter to only return sub organizations that the actor has access to."
},
DELETE: {
diff --git a/backend/src/services/org/org-dal.ts b/backend/src/services/org/org-dal.ts
index 5415858436..6f0e2c2788 100644
--- a/backend/src/services/org/org-dal.ts
+++ b/backend/src/services/org/org-dal.ts
@@ -162,70 +162,75 @@ export const orgDALFactory = (db: TDbClient) => {
actorType: ActorType;
orgId: string;
isAccessible?: boolean;
+ search?: string;
+ orderBy?: string;
+ orderDirection?: "asc" | "desc";
limit?: number;
offset?: number;
}) => {
try {
const conn = db.replicaNode();
+ const orderBy = dto.orderBy ?? "name";
+ const orderDirection = dto.orderDirection ?? "asc";
+
+ const buildBaseQuery = () => {
+ if (dto.isAccessible) {
+ const subOrgIdsSubquery = conn(TableName.Organization)
+ .where(`${TableName.Organization}.rootOrgId`, dto.orgId)
+ .select(db.ref("id").withSchema(TableName.Organization));
+
+ const userGroupIdsSubquery = conn(TableName.Groups)
+ .join(TableName.UserGroupMembership, `${TableName.UserGroupMembership}.groupId`, `${TableName.Groups}.id`)
+ .where(`${TableName.UserGroupMembership}.userId`, dto.actorId)
+ .select(db.ref("id").withSchema(TableName.Groups));
+
+ const identityGroupIdsSubquery = conn(TableName.Groups)
+ .join(
+ TableName.IdentityGroupMembership,
+ `${TableName.IdentityGroupMembership}.groupId`,
+ `${TableName.Groups}.id`
+ )
+ .where(`${TableName.IdentityGroupMembership}.identityId`, dto.actorId)
+ .select(db.ref("id").withSchema(TableName.Groups));
+
+ const accessibleSubOrgIdsSubquery = conn(TableName.Membership)
+ .where(`${TableName.Membership}.scope`, AccessScope.Organization)
+ .whereIn(`${TableName.Membership}.scopeOrgId`, subOrgIdsSubquery)
+ .andWhere((qb) => {
+ if (dto.actorType === ActorType.USER) {
+ void qb
+ .where(`${TableName.Membership}.actorUserId`, dto.actorId)
+ .orWhereIn(`${TableName.Membership}.actorGroupId`, userGroupIdsSubquery);
+ } else {
+ void qb
+ .where(`${TableName.Membership}.actorIdentityId`, dto.actorId)
+ .orWhereIn(`${TableName.Membership}.actorGroupId`, identityGroupIdsSubquery);
+ }
+ })
+ .select(db.ref("scopeOrgId").withSchema(TableName.Membership));
- if (dto.isAccessible) {
- // Subquery: sub-org ids (children of root)
- const subOrgIdsSubquery = conn(TableName.Organization)
- .where(`${TableName.Organization}.rootOrgId`, dto.orgId)
- .select(db.ref("id").withSchema(TableName.Organization));
-
- // Group IDs the actor belongs to
- const userGroupIdsSubquery = conn(TableName.Groups)
- .join(TableName.UserGroupMembership, `${TableName.UserGroupMembership}.groupId`, `${TableName.Groups}.id`)
- .where(`${TableName.UserGroupMembership}.userId`, dto.actorId)
- .select(db.ref("id").withSchema(TableName.Groups));
-
- const identityGroupIdsSubquery = conn(TableName.Groups)
- .join(
- TableName.IdentityGroupMembership,
- `${TableName.IdentityGroupMembership}.groupId`,
- `${TableName.Groups}.id`
- )
- .where(`${TableName.IdentityGroupMembership}.identityId`, dto.actorId)
- .select(db.ref("id").withSchema(TableName.Groups));
+ return conn(TableName.Organization)
+ .where(`${TableName.Organization}.rootOrgId`, dto.orgId)
+ .whereIn(`${TableName.Organization}.id`, accessibleSubOrgIdsSubquery);
+ }
- // Accessible sub-org ids: memberships in those orgs where actor is direct or via group
- const accessibleSubOrgIdsSubquery = conn(TableName.Membership)
- .where(`${TableName.Membership}.scope`, AccessScope.Organization)
- .whereIn(`${TableName.Membership}.scopeOrgId`, subOrgIdsSubquery)
- .andWhere((qb) => {
- if (dto.actorType === ActorType.USER) {
- void qb
- .where(`${TableName.Membership}.actorUserId`, dto.actorId)
- .orWhereIn(`${TableName.Membership}.actorGroupId`, userGroupIdsSubquery);
- } else {
- void qb
- .where(`${TableName.Membership}.actorIdentityId`, dto.actorId)
- .orWhereIn(`${TableName.Membership}.actorGroupId`, identityGroupIdsSubquery);
- }
- })
- .select(db.ref("scopeOrgId").withSchema(TableName.Membership));
-
- const query = conn(TableName.Organization)
- .where(`${TableName.Organization}.rootOrgId`, dto.orgId)
- .whereIn(`${TableName.Organization}.id`, accessibleSubOrgIdsSubquery)
- .select(selectAllTableCols(TableName.Organization));
-
- if (dto.limit) void query.limit(dto.limit);
- if (dto.offset) void query.offset(dto.offset);
-
- const orgs = await query;
- return orgs;
- }
+ return conn(TableName.Organization).where(`${TableName.Organization}.rootOrgId`, dto.orgId);
+ };
- const query = conn(TableName.Organization)
- .where(`${TableName.Organization}.rootOrgId`, dto.orgId)
- .select(selectAllTableCols(TableName.Organization));
- if (dto.limit) void query.limit(dto.limit);
- if (dto.offset) void query.offset(dto.offset);
+ const baseQuery = buildBaseQuery();
+ if (dto.search) void baseQuery.whereILike(`${TableName.Organization}.name`, `%${dto.search}%`);
+
+ const [totalResult, orgs] = await Promise.all([
+ baseQuery.clone().count({ count: "*" }).first(),
+ baseQuery
+ .clone()
+ .select(selectAllTableCols(TableName.Organization))
+ .orderBy(`${TableName.Organization}.${orderBy}`, orderDirection)
+ .limit(dto.limit ?? 25)
+ .offset(dto.offset ?? 0)
+ ]);
- const orgs = await query;
- return orgs;
+ return { orgs, totalCount: Number(totalResult?.count ?? 0) };
} catch (error) {
throw new DatabaseError({ error, name: "List sub organization" });
}