diff --git a/packages/lib/utils/recipients.test.ts b/packages/lib/utils/recipients.test.ts index 5d969f0ff..d76f262ee 100644 --- a/packages/lib/utils/recipients.test.ts +++ b/packages/lib/utils/recipients.test.ts @@ -51,6 +51,24 @@ describe('recipient signing order helpers', () => { ]); }); + it('detects an assistant anywhere in the last signing step (groups)', () => { + expect( + isAssistantLastSigner([ + { role: RecipientRole.SIGNER, signingOrder: 1 }, + { role: RecipientRole.ASSISTANT, signingOrder: 2 }, + { role: RecipientRole.SIGNER, signingOrder: 2 }, + ]), + ).toBe(true); + + expect( + isAssistantLastSigner([ + { role: RecipientRole.ASSISTANT, signingOrder: 1 }, + { role: RecipientRole.SIGNER, signingOrder: 1 }, + { role: RecipientRole.SIGNER, signingOrder: 2 }, + ]), + ).toBe(false); + }); + it('checks whether the last non-CC recipient is an assistant', () => { expect( isAssistantLastSigner([ diff --git a/packages/lib/utils/recipients.ts b/packages/lib/utils/recipients.ts index f51d02c78..a82a1e251 100644 --- a/packages/lib/utils/recipients.ts +++ b/packages/lib/utils/recipients.ts @@ -22,11 +22,32 @@ export const isCcRecipient = (recipient: Pick) => { return recipient.role === RecipientRole.CC; }; -export const isAssistantLastSigner = (recipients: Pick[]) => { +/** + * Whether an assistant sits in the last signing step (nobody after them to assist). + * + * Falls back to a positional check when no recipient carries a signing order. + */ +export const isAssistantLastSigner = ( + recipients: Array & { signingOrder?: number | null }>, +) => { const nonCcRecipients = recipients.filter((recipient) => !isCcRecipient(recipient)); - const lastNonCcRecipient = nonCcRecipients[nonCcRecipients.length - 1]; - return lastNonCcRecipient?.role === RecipientRole.ASSISTANT; + if (nonCcRecipients.length === 0) { + return false; + } + + const hasAnySigningOrder = nonCcRecipients.some((recipient) => typeof recipient.signingOrder === 'number'); + + if (!hasAnySigningOrder) { + return nonCcRecipients[nonCcRecipients.length - 1]?.role === RecipientRole.ASSISTANT; + } + + const maxOrder = Math.max(...nonCcRecipients.map((recipient) => recipient.signingOrder ?? Number.MAX_SAFE_INTEGER)); + + return nonCcRecipients.some( + (recipient) => + (recipient.signingOrder ?? Number.MAX_SAFE_INTEGER) === maxOrder && recipient.role === RecipientRole.ASSISTANT, + ); }; export const sortRecipientsForSigningOrder = (recipients: T[]): T[] => {