mirror of
https://github.com/documenso/documenso.git
synced 2026-08-24 23:32:29 +10:00
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
|
import type { TFieldSignature } from '@documenso/lib/types/field';
|
|
import type { TSignEnvelopeFieldValue } from '@documenso/trpc/server/envelope-router/sign-envelope-field.types';
|
|
import { FieldType } from '@prisma/client';
|
|
|
|
import { SignFieldSignatureDialog } from '~/components/dialogs/sign-field-signature-dialog';
|
|
|
|
type HandleSignatureFieldClickOptions = {
|
|
field: TFieldSignature;
|
|
fullName?: string;
|
|
signature: string | null;
|
|
typedSignatureEnabled?: boolean;
|
|
uploadSignatureEnabled?: boolean;
|
|
drawSignatureEnabled?: boolean;
|
|
qrSignatureEnabled?: boolean;
|
|
};
|
|
|
|
export const handleSignatureFieldClick = async (
|
|
options: HandleSignatureFieldClickOptions,
|
|
): Promise<Extract<TSignEnvelopeFieldValue, { type: typeof FieldType.SIGNATURE }> | null> => {
|
|
const {
|
|
field,
|
|
fullName,
|
|
signature,
|
|
typedSignatureEnabled,
|
|
uploadSignatureEnabled,
|
|
drawSignatureEnabled,
|
|
qrSignatureEnabled,
|
|
} = options;
|
|
|
|
if (field.type !== FieldType.SIGNATURE) {
|
|
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
|
message: 'Invalid field type',
|
|
});
|
|
}
|
|
|
|
if (field.inserted) {
|
|
return {
|
|
type: FieldType.SIGNATURE,
|
|
value: null,
|
|
};
|
|
}
|
|
|
|
let signatureToInsert = signature;
|
|
|
|
if (!signatureToInsert) {
|
|
signatureToInsert = await SignFieldSignatureDialog.call({
|
|
fullName,
|
|
typedSignatureEnabled,
|
|
uploadSignatureEnabled,
|
|
drawSignatureEnabled,
|
|
qrSignatureEnabled,
|
|
});
|
|
}
|
|
|
|
if (!signatureToInsert) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
type: FieldType.SIGNATURE,
|
|
value: signatureToInsert,
|
|
};
|
|
};
|