mirror of
https://github.com/fosrl/pangolin.git
synced 2026-08-15 16:59:35 +02:00
send email upon generate virtual api key
This commit is contained in:
@@ -4,6 +4,43 @@ export const virtualApiKeyResourceIdsSchema = z
|
||||
.array(z.coerce.number().int().positive())
|
||||
.optional();
|
||||
|
||||
const virtualApiKeyEmailFieldsSchema = {
|
||||
sendEmail: z.boolean().optional().default(false),
|
||||
sendToAttributedUser: z.boolean().optional().default(false),
|
||||
emails: z.array(z.email().toLowerCase()).max(20).optional().default([])
|
||||
};
|
||||
|
||||
function refineVirtualApiKeyEmailFields(
|
||||
data: {
|
||||
sendEmail: boolean;
|
||||
sendToAttributedUser: boolean;
|
||||
emails: string[];
|
||||
userId?: string | null;
|
||||
},
|
||||
ctx: z.RefinementCtx
|
||||
) {
|
||||
if (!data.sendEmail) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.sendToAttributedUser && data.emails.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message:
|
||||
"Select the associated user or add at least one email address",
|
||||
path: ["sendEmail"]
|
||||
});
|
||||
}
|
||||
|
||||
if (data.sendToAttributedUser && !data.userId) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Associate a user to email the key to that user",
|
||||
path: ["sendToAttributedUser"]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const createVirtualApiKeyBodySchema = z
|
||||
.strictObject({
|
||||
name: z.string().nonempty(),
|
||||
@@ -11,7 +48,8 @@ export const createVirtualApiKeyBodySchema = z
|
||||
userId: z.string().optional().nullable(),
|
||||
allResources: z.boolean().optional().default(false),
|
||||
resourceIds: virtualApiKeyResourceIdsSchema,
|
||||
validForSeconds: z.int().positive().optional()
|
||||
validForSeconds: z.int().positive().optional(),
|
||||
...virtualApiKeyEmailFieldsSchema
|
||||
})
|
||||
.refine(
|
||||
(data) => data.allResources || (data.resourceIds?.length ?? 0) > 0,
|
||||
@@ -20,13 +58,30 @@ export const createVirtualApiKeyBodySchema = z
|
||||
"Select at least one public inference resource, or enable all public inference resources",
|
||||
path: ["resourceIds"]
|
||||
}
|
||||
);
|
||||
)
|
||||
.superRefine(refineVirtualApiKeyEmailFields);
|
||||
|
||||
export const updateVirtualApiKeyBodySchema = z.strictObject({
|
||||
name: z.string().nonempty().optional(),
|
||||
description: z.string().optional().nullable(),
|
||||
userId: z.string().optional().nullable(),
|
||||
allResources: z.boolean().optional(),
|
||||
resourceIds: virtualApiKeyResourceIdsSchema,
|
||||
validForSeconds: z.int().positive().optional().nullable()
|
||||
});
|
||||
export const updateVirtualApiKeyBodySchema = z
|
||||
.strictObject({
|
||||
name: z.string().nonempty().optional(),
|
||||
description: z.string().optional().nullable(),
|
||||
userId: z.string().optional().nullable(),
|
||||
allResources: z.boolean().optional(),
|
||||
resourceIds: virtualApiKeyResourceIdsSchema,
|
||||
validForSeconds: z.int().positive().optional().nullable(),
|
||||
...virtualApiKeyEmailFieldsSchema
|
||||
})
|
||||
.superRefine((data, ctx) => {
|
||||
if (!data.sendEmail) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.sendToAttributedUser && data.emails.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message:
|
||||
"Select the associated user or add at least one email address",
|
||||
path: ["sendEmail"]
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user