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
@@ -29,18 +29,30 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
}, },
schema: { schema: {
response: { response: {
200: z.object({ 200: z.union([
organizations: sanitizedOrganizationSchema z.object({
.extend({ id: z.string(),
orgAuthMethod: z.string(), name: z.string(),
userRole: z.string() slug: z.string(),
}) role: z.string()
.array() }),
}) 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) => { 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); const organizations = await server.services.org.findAllOrganizationOfUser(req.permission.id);
return { organizations }; return { organizations };
} }
+21 -1
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, { return withTransaction(db, {
...orgOrm, ...orgOrm,
findOrgByProjectId, findOrgByProjectId,
@@ -652,6 +671,7 @@ export const orgDALFactory = (db: TDbClient) => {
updateMembershipById, updateMembershipById,
deleteMembershipById, deleteMembershipById,
deleteMembershipsById, deleteMembershipsById,
updateMembership updateMembership,
findIdentityOrganization
}); });
}; };
+10
View File
@@ -198,6 +198,15 @@ export const orgServiceFactory = ({
// Filter out orgs where the membership object is an invitation // Filter out orgs where the membership object is an invitation
return orgs.filter((org) => org.userStatus !== "invited"); 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 * Get all workspace members
* */ * */
@@ -1403,6 +1412,7 @@ export const orgServiceFactory = ({
findOrganizationById, findOrganizationById,
findAllOrgMembers, findAllOrgMembers,
findAllOrganizationOfUser, findAllOrganizationOfUser,
findIdentityOrganization,
inviteUserToOrganization, inviteUserToOrganization,
verifyUserToOrg, verifyUserToOrg,
updateOrg, updateOrg,