Merge pull request #3580 from Infisical/feat/return-metadata-with-identity-create

Return metadata with identity post endpoints
This commit is contained in:
x032205
2025-05-13 14:22:34 -04:00
committed by GitHub
2 changed files with 48 additions and 23 deletions
@@ -52,7 +52,8 @@ export const registerIdentityRouter = async (server: FastifyZodProvider) => {
response: { response: {
200: z.object({ 200: z.object({
identity: IdentitiesSchema.extend({ identity: IdentitiesSchema.extend({
authMethods: z.array(z.string()) authMethods: z.array(z.string()),
metadata: z.object({ id: z.string(), key: z.string(), value: z.string() }).array()
}) })
}) })
} }
@@ -123,7 +124,9 @@ export const registerIdentityRouter = async (server: FastifyZodProvider) => {
}), }),
response: { response: {
200: z.object({ 200: z.object({
identity: IdentitiesSchema identity: IdentitiesSchema.extend({
metadata: z.object({ id: z.string(), key: z.string(), value: z.string() }).array()
})
}) })
} }
}, },
@@ -227,8 +230,8 @@ export const registerIdentityRouter = async (server: FastifyZodProvider) => {
identity: IdentityOrgMembershipsSchema.extend({ identity: IdentityOrgMembershipsSchema.extend({
metadata: z metadata: z
.object({ .object({
key: z.string().trim().min(1),
id: z.string().trim().min(1), id: z.string().trim().min(1),
key: z.string().trim().min(1),
value: z.string().trim().min(1) value: z.string().trim().min(1)
}) })
.array() .array()
@@ -106,18 +106,29 @@ export const identityServiceFactory = ({
}, },
tx tx
); );
let insertedMetadata: Array<{
id: string;
key: string;
value: string;
}> = [];
if (metadata && metadata.length) { if (metadata && metadata.length) {
await identityMetadataDAL.insertMany( const rowsToInsert = metadata.map(({ key, value }) => ({
metadata.map(({ key, value }) => ({ identityId: newIdentity.id,
identityId: newIdentity.id, orgId,
orgId, key,
key, value
value }));
})),
tx insertedMetadata = await identityMetadataDAL.insertMany(rowsToInsert, tx);
);
} }
return { ...newIdentity, authMethods: [] };
return {
...newIdentity,
authMethods: [],
metadata: insertedMetadata
};
}); });
await licenseService.updateSubscriptionOrgMemberCount(orgId); await licenseService.updateSubscriptionOrgMemberCount(orgId);
@@ -189,21 +200,31 @@ export const identityServiceFactory = ({
tx tx
); );
} }
let insertedMetadata: Array<{
id: string;
key: string;
value: string;
}> = [];
if (metadata) { if (metadata) {
await identityMetadataDAL.delete({ orgId: identityOrgMembership.orgId, identityId: id }, tx); await identityMetadataDAL.delete({ orgId: identityOrgMembership.orgId, identityId: id }, tx);
if (metadata.length) { if (metadata.length) {
await identityMetadataDAL.insertMany( const rowsToInsert = metadata.map(({ key, value }) => ({
metadata.map(({ key, value }) => ({ identityId: newIdentity.id,
identityId: newIdentity.id, orgId: identityOrgMembership.orgId,
orgId: identityOrgMembership.orgId, key,
key, value
value }));
})),
tx insertedMetadata = await identityMetadataDAL.insertMany(rowsToInsert, tx);
);
} }
} }
return newIdentity;
return {
...newIdentity,
metadata: insertedMetadata
};
}); });
return { ...identity, orgId: identityOrgMembership.orgId }; return { ...identity, orgId: identityOrgMembership.orgId };
@@ -224,6 +245,7 @@ export const identityServiceFactory = ({
actorOrgId actorOrgId
); );
ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Read, OrgPermissionSubjects.Identity); ForbiddenError.from(permission).throwUnlessCan(OrgPermissionIdentityActions.Read, OrgPermissionSubjects.Identity);
return identity; return identity;
}; };