Skip to content

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

feat(org): Add userJoinedAt field to organization response

Igor Correae18186f09bc5
diff --git a/backend/src/server/routes/v1/organization-router.ts b/backend/src/server/routes/v1/organization-router.ts
index 82afcd31c8..8ee6e46b58 100644
--- a/backend/src/server/routes/v1/organization-router.ts
+++ b/backend/src/server/routes/v1/organization-router.ts
@@ -18,7 +18,7 @@ import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { GenericResourceNameSchema, slugSchema } from "@app/server/lib/schemas";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { ActorType, AuthMode, MfaMethod } from "@app/services/auth/auth-type";
-import { orgWithSubOrgsSchema, sanitizedOrganizationSchema } from "@app/services/org/org-schema";
+import { OrgWithSubOrgsSchema, sanitizedOrganizationSchema } from "@app/services/org/org-schema";
import { integrationAuthPubSchema } from "../sanitizedSchemas";
@@ -63,7 +63,7 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
operationId: "listAccessibleOrganizationsWithSubOrgs",
response: {
200: z.object({
- organizations: orgWithSubOrgsSchema.array()
+ organizations: OrgWithSubOrgsSchema.array()
})
}
},
diff --git a/backend/src/services/org/org-dal.ts b/backend/src/services/org/org-dal.ts
index 076a0824a4..a2adef519b 100644
--- a/backend/src/services/org/org-dal.ts
+++ b/backend/src/services/org/org-dal.ts
@@ -28,7 +28,7 @@ import {
import { generateKnexQueryFromScim } from "@app/lib/knex/scim";
import { ActorType } from "../auth/auth-type";
-import { TOrgWithSubOrgs } from "./org-schema";
+import { TOrgWithSubOrgs } from "./org-types";
import { OrgAuthMethod } from "./org-types";
export type TOrgDALFactory = ReturnType<typeof orgDALFactory>;
@@ -240,12 +240,6 @@ export const orgDALFactory = (db: TDbClient) => {
actorId: string;
actorType: ActorType;
}): Promise<TOrgWithSubOrgs[]> => {
- if (dto.actorType !== ActorType.USER) {
- throw new BadRequestError({
- message: "listOrganizationsWithSubOrgs only supports user actors; identity/machine is not supported"
- });
- }
-
try {
const conn = db.replicaNode();
@@ -269,11 +263,18 @@ export const orgDALFactory = (db: TDbClient) => {
const rootOrgs = await conn(TableName.Organization)
.whereIn(`${TableName.Organization}.id`, rootOrgIdsSubquery)
+ .leftJoin(TableName.Membership, (qb) => {
+ void qb
+ .on(`${TableName.Membership}.scopeOrgId`, "=", db.ref("id").withSchema(TableName.Organization))
+ .andOn(`${TableName.Membership}.scope`, db.raw("?", [AccessScope.Organization]))
+ .andOn(`${TableName.Membership}.actorUserId`, db.raw("?", [dto.actorId]));
+ })
.select(
db.ref("id").withSchema(TableName.Organization),
db.ref("name").withSchema(TableName.Organization),
db.ref("slug").withSchema(TableName.Organization),
- db.ref("createdAt").withSchema(TableName.Organization)
+ db.ref("createdAt").withSchema(TableName.Organization),
+ db.ref("createdAt").withSchema(TableName.Membership).as("userJoinedAt")
);
const rootOrgIds = rootOrgs.map((o) => o.id);
@@ -313,6 +314,7 @@ export const orgDALFactory = (db: TDbClient) => {
name: org.name,
slug: org.slug,
createdAt: org.createdAt,
+ userJoinedAt: org.userJoinedAt ?? null,
subOrganizations: (subOrgsByRootId[org.id] ?? []).map((s) => ({
id: s.id,
name: s.name,
diff --git a/backend/src/services/org/org-schema.ts b/backend/src/services/org/org-schema.ts
index 82acb50042..c929b4f50c 100644
--- a/backend/src/services/org/org-schema.ts
+++ b/backend/src/services/org/org-schema.ts
@@ -9,18 +9,19 @@ const orgBasicSchema = OrganizationsSchema.pick({
slug: true
});
-type TOrgBasic = z.infer<typeof orgBasicSchema>;
-
-/** Root org with accessible sub-orgs (basic info only) */
-export type TOrgWithSubOrgs = TOrgBasic & {
- createdAt: Date;
- subOrganizations: TOrgBasic[];
-};
-
/** Zod schema for API response: org with sub-orgs */
-export const orgWithSubOrgsSchema = orgBasicSchema.extend({
- createdAt: z.date(),
- subOrganizations: z.array(orgBasicSchema)
+export const OrgWithSubOrgsSchema = OrganizationsSchema.pick({
+ id: true,
+ name: true,
+ slug: true,
+ createdAt: true
+}).extend({
+ userJoinedAt: z.date().optional().nullable(),
+ subOrganizations: OrganizationsSchema.pick({
+ id: true,
+ name: true,
+ slug: true
+ }).array()
});
export const sanitizedOrganizationSchema = OrganizationsSchema.pick({
diff --git a/backend/src/services/org/org-types.ts b/backend/src/services/org/org-types.ts
index c731a224e7..923ab29566 100644
--- a/backend/src/services/org/org-types.ts
+++ b/backend/src/services/org/org-types.ts
@@ -1,7 +1,14 @@
+import { z } from "zod";
+
import { TOrgPermission } from "@app/lib/types";
import { ActorAuthMethod, ActorType, MfaMethod } from "../auth/auth-type";
+import { OrgWithSubOrgsSchema } from "./org-schema";
+
+/** Root org with accessible sub-orgs (basic info only) */
+export type TOrgWithSubOrgs = z.infer<typeof OrgWithSubOrgsSchema>;
+
export type TUpdateOrgMembershipDTO = {
userId: string;
orgId: string;