Files
documenso/apps/remix/app/utils/analyze-ai-recipients.ts
Ephraim Atta-Duncan 13bd5815d9 fix: email place holder
2025-11-18 17:59:36 +00:00

64 lines
1.4 KiB
TypeScript

import { RecipientRole } from '@prisma/client';
import { AppError } from '@documenso/lib/errors/app-error';
export type AiRecipient = {
name: string;
email?: string;
role: 'SIGNER' | 'APPROVER' | 'CC';
signingOrder?: number;
};
export const analyzeRecipientsFromDocument = async (envelopeId: string): Promise<AiRecipient[]> => {
try {
const response = await fetch('/api/ai/analyze-recipients', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ envelopeId }),
});
if (!response.ok) {
throw new Error('Failed to analyze recipients');
}
return (await response.json()) as AiRecipient[];
} catch (error) {
throw AppError.parseError(error);
}
};
export type RecipientForCreation = {
name: string;
email: string;
role: RecipientRole;
signingOrder?: number;
};
export const ensureRecipientEmails = (
recipients: AiRecipient[],
envelopeId: string,
): RecipientForCreation[] => {
const allowedRoles: RecipientRole[] = [
RecipientRole.SIGNER,
RecipientRole.APPROVER,
RecipientRole.CC,
];
return recipients.map((recipient) => {
const email = recipient.email ?? '';
const candidateRole = recipient.role as RecipientRole;
const normalizedRole = allowedRoles.includes(candidateRole)
? candidateRole
: RecipientRole.SIGNER;
return {
...recipient,
email,
role: normalizedRole,
};
});
};