Add machine identities to /organization endpoint

This commit is contained in:
Carlos Monastyrski
2025-08-11 21:23:05 -07:00
parent f16dca45d9
commit 385c75c543
3 changed files with 52 additions and 10 deletions

View File

@@ -29,18 +29,30 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
},
schema: {
response: {
200: z.object({
organizations: sanitizedOrganizationSchema
.extend({
orgAuthMethod: z.string(),
userRole: z.string()
})
.array()
})
200: z.union([
z.object({
id: z.string(),
name: z.string(),
slug: z.string(),
role: z.string()
}),
z.object({
organizations: sanitizedOrganizationSchema
.extend({
orgAuthMethod: z.string(),
userRole: z.string()
})
.array()
})
])
}
},
onRequest: verifyAuth([AuthMode.JWT], { requireOrg: false }),
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN], { requireOrg: false }),
handler: async (req) => {
if (req.permission.type === ActorType.IDENTITY) {
const organization = await server.services.org.findIdentityOrganization(req.permission.id);
return organization;
}
const organizations = await server.services.org.findAllOrganizationOfUser(req.permission.id);
return { organizations };
}

View File

@@ -630,6 +630,25 @@ export const orgDALFactory = (db: TDbClient) => {
}
};
const findIdentityOrganization = async (
identityId: string
): Promise<{ id: string; name: string; slug: string; role: string }> => {
try {
const org = await db
.replicaNode()(TableName.IdentityOrgMembership)
.where({ identityId })
.join(TableName.Organization, `${TableName.IdentityOrgMembership}.orgId`, `${TableName.Organization}.id`)
.select(db.ref("id").withSchema(TableName.Organization).as("id"))
.select(db.ref("name").withSchema(TableName.Organization).as("name"))
.select(db.ref("slug").withSchema(TableName.Organization).as("slug"))
.select(db.ref("role").withSchema(TableName.IdentityOrgMembership).as("role"));
return org?.[0];
} catch (error) {
throw new DatabaseError({ error, name: "Find all org by user id" });
}
};
return withTransaction(db, {
...orgOrm,
findOrgByProjectId,
@@ -652,6 +671,7 @@ export const orgDALFactory = (db: TDbClient) => {
updateMembershipById,
deleteMembershipById,
deleteMembershipsById,
updateMembership
updateMembership,
findIdentityOrganization
});
};

View File

@@ -198,6 +198,15 @@ export const orgServiceFactory = ({
// Filter out orgs where the membership object is an invitation
return orgs.filter((org) => org.userStatus !== "invited");
};
/*
* Get all organization an identity is part of
* */
const findIdentityOrganization = async (identityId: string) => {
const org = await orgDAL.findIdentityOrganization(identityId);
return org;
};
/*
* Get all workspace members
* */
@@ -1403,6 +1412,7 @@ export const orgServiceFactory = ({
findOrganizationById,
findAllOrgMembers,
findAllOrganizationOfUser,
findIdentityOrganization,
inviteUserToOrganization,
verifyUserToOrg,
updateOrg,