Files
sign/packages/lib/server-only/recipient/get-is-recipient-turn.ts
Ephraim Duncan 3d644db286 feat: signing order (#1290)
Adds the ability to specify an optional signing order for documents.
When specified a document will be considered sequential with recipients
only being allowed to sign in the order that they were specified in.
2024-09-16 22:36:45 +10:00

47 lines
1023 B
TypeScript

import { prisma } from '@documenso/prisma';
import { DocumentSigningOrder, SigningStatus } from '@documenso/prisma/client';
export type GetIsRecipientTurnOptions = {
token: string;
};
export async function getIsRecipientsTurnToSign({ token }: GetIsRecipientTurnOptions) {
const document = await prisma.document.findFirstOrThrow({
where: {
Recipient: {
some: {
token,
},
},
},
include: {
documentMeta: true,
Recipient: {
orderBy: {
signingOrder: 'asc',
},
},
},
});
if (document.documentMeta?.signingOrder !== DocumentSigningOrder.SEQUENTIAL) {
return true;
}
const recipients = document.Recipient;
const currentRecipientIndex = recipients.findIndex((r) => r.token === token);
if (currentRecipientIndex === -1) {
return false;
}
for (let i = 0; i < currentRecipientIndex; i++) {
if (recipients[i].signingStatus !== SigningStatus.SIGNED) {
return false;
}
}
return true;
}