mirror of
https://github.com/documenso/documenso.git
synced 2025-11-19 19:21:39 +10:00
chore: refactor
This commit is contained in:
67
apps/remix/app/utils/detect-document-recipients.ts
Normal file
67
apps/remix/app/utils/detect-document-recipients.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import { RecipientRole } from '@prisma/client';
|
||||
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
|
||||
export type SuggestedRecipient = {
|
||||
name: string;
|
||||
email?: string;
|
||||
role: 'SIGNER' | 'APPROVER' | 'CC';
|
||||
signingOrder?: number;
|
||||
};
|
||||
|
||||
export const detectRecipientsInDocument = async (
|
||||
envelopeId: string,
|
||||
): Promise<SuggestedRecipient[]> => {
|
||||
try {
|
||||
const response = await fetch('/api/ai/detect-recipients', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ envelopeId }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||
message: 'Failed to detect recipients',
|
||||
});
|
||||
}
|
||||
|
||||
return (await response.json()) as SuggestedRecipient[];
|
||||
} catch (error) {
|
||||
throw AppError.parseError(error);
|
||||
}
|
||||
};
|
||||
|
||||
export type RecipientForCreation = {
|
||||
name: string;
|
||||
email: string;
|
||||
role: RecipientRole;
|
||||
signingOrder?: number;
|
||||
};
|
||||
|
||||
export const ensureRecipientEmails = (
|
||||
recipients: SuggestedRecipient[],
|
||||
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,
|
||||
};
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user