Files
pn-new-crm/src/lib/services/documenso-signers.ts
Matt 221ae5784e chore(autonomous-session): consolidate uncommitted work from prior session
Bundles the prior autonomous-session output that was sitting unstaged:

- Em-dash sweep across src/ + tests/ (en-dash/em-dash to hyphen, ~2280 instances)
- country-flag-icons rollout (CountryFlag component, replaces emoji glyphs that
  never rendered on Windows; lazy-loads the 3x2 SVG index as a single chunk
  after the per-subpath dynamic-import approach silently failed in webpack)
- Admin IA Phase 1+2: 7-domain regroup, 41 to 38 pages, /admin/berths index,
  redirects (ocr to ai, reports to dashboard, invitations to users),
  docs/admin-ia-proposal.md
- Per-template email tester (registry + endpoint + UI on Email admin page)
- Cancel-document mode picker (delete-from-Documenso vs keep-for-audit)
- Dashboard PDF report: 25 widgets, SVG charts, date-range picker, 11 resolvers
- Customize-widgets per-region sortables at xl+ (charts/rails/feed); single
  flat sortable below xl when the layout stacks; per-viewport saved orders
- Audit doc updates capturing each shipped item
- Lint fixes: react-compiler immutability in DonutChart (reduce instead of
  let-reassign), set-state-in-effect disables in CountryFlag and
  UploadForSigning preview-bytes effect, unused 'confirm' destructures in
  interest contract + reservation tabs, unescaped apostrophe in test-template
  card copy
2026-05-23 00:52:59 +02:00

62 lines
2.5 KiB
TypeScript

/**
* Small helpers shared between the document-signing flows and the
* Documenso webhook handlers. Kept in their own file so the two
* heavyweight callers (`documents.service.ts` + the webhook handler
* routes) don't have to depend on each other for a 30-line utility.
*/
import type { DocumentLabel } from '@/lib/services/document-signing-emails.service';
import type { DocumentSigner } from '@/lib/db/schema/documents';
/**
* Pull the per-recipient signing token out of a Documenso signing URL.
* Both v1.13 and v2 emit URLs of the form
* `https://signatures.example.com/sign/<token>`
* so the token is the last non-empty path segment. Returns null when
* the URL is empty / unparseable / doesn't carry a token-shaped tail.
*
* Used at document-send time to populate `document_signers.signing_token`
* so subsequent webhook deliveries can match recipients by token
* (more robust than email match when one address serves multiple roles).
*/
export function extractSigningToken(url: string | null | undefined): string | null {
if (!url) return null;
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return null;
}
const segments = parsed.pathname.split('/').filter(Boolean);
const last = segments[segments.length - 1];
if (!last) return null;
// A token must be at least 8 chars and contain only URL-safe chars -
// discriminates real tokens from generic words like "sign" or "embed"
// that some Documenso 2.x deployments append.
if (last.length < 8) return null;
if (!/^[A-Za-z0-9_-]+$/.test(last)) return null;
return last;
}
/** Map of internal documentType to the customer-facing label used in
* email subjects + body copy. Duplicated in send-invitation/route.ts
* but consolidated here so the webhook cascade picks up the same
* label without re-defining the map. */
export const DOC_TYPE_LABEL: Record<string, DocumentLabel> = {
eoi: 'Expression of Interest',
contract: 'Sales Contract',
reservation_agreement: 'Reservation Agreement',
};
/** Pick the next pending signer in signing order. Returns null when
* every signer in the list has signed (or declined). */
export function nextPendingSigner<T extends Pick<DocumentSigner, 'status' | 'signingOrder'>>(
signers: T[],
): T | null {
// signers list is assumed pre-sorted asc by signingOrder; we
// defensively pick the lowest-order pending row regardless.
const pending = signers.filter((s) => s.status === 'pending');
if (pending.length === 0) return null;
return pending.reduce((lo, s) => (s.signingOrder < lo.signingOrder ? s : lo));
}