@ambushsoftworks/nestjs-esign
v0.1.1
Published
E-signatures for NestJS: contracts sent through DocuSeal or PandaDoc, signed by both parties, verified webhooks, our own executed copy and audit certificate, retention
Maintainers
Readme
@ambushsoftworks/nestjs-esign
Contracts sent for e-signature through a vendor, for NestJS and GraphQL.
- Lifecycle: issue, sign (client and firm, in order), decline, void, supersede with an amendment, complete — a status machine that only moves forward, with every event kept.
- Vendors behind one port: DocuSeal (cloud or self-hosted) and PandaDoc, and a fake for tests and laptops. Each tenant brings its own vendor account, and a retired account is still heard from.
- Webhooks: one route per tenant; the signature is checked against each of the tenant's accounts before the body is read. A redelivered event is skipped.
- Our own copy: the executed PDF and the audit certificate, fetched, hashed, checked against the vendor and stored, so the record outlives the vendor seat.
- Recovery: status and signing progress read straight from the vendor when a webhook never came; the host's sweep drives it.
- Retention: how long a signed copy is kept and when an abandoned one may go (pure rules); the host's sweep disposes.
- Templates: a tenant's templates and their merge tokens, handles written onto the vendor's record so a contract is found again after a restore or a vendor switch, and tolerant matching of token spellings.
No web half: the signing page is the vendor's, embedded by the host.
Words
The engine speaks of a tenant (an organisation, or a part of one), an
agreement for a reference (whatever it is for: an appointment, an
engagement), two parties — CLIENT and FIRM, stored as such — a
vendor (the e-signature service) and the tenant's vendor account.
Staff act for the firm.
Layout
core/ framework-free: AgreementService, retention rules, template
handles, token matching, ports, data types, errors
docuseal/ the DocuSeal vendor (entry: /docuseal)
pandadoc/ the PandaDoc vendor (entry: /pandadoc)
nest/ EsignModule.forRoot, the webhook controller, the signing-session
operations, request identity, the guard map
testing/ the fake vendor, in-memory ports, a whole fake host, the
repository contractcore/ imports only itself and Node's crypto; the vendors import the core
and crypto; nest/ adds NestJS and GraphQL. Nothing imports a host:
__tests__/core-boundary.spec.ts fails otherwise, and on any word of the
first host's in the source, comments and tests.
Hosting it
EsignModule.forRoot({
imports: [/* modules your port providers need */],
providers: [
{ provide: AGREEMENT_REPOSITORY, useClass: MyAgreements },
{
provide: AGREEMENT_PROVIDER,
useFactory: (configs, logger) =>
new DocuSealAgreementProvider(configs, logger, {
roleNames: { FIRM: 'Firm', CLIENT: 'Client' },
}),
inject: [MyVendorAccounts, MyLogger],
},
// … one provider per port, below
],
guards: MY_GUARDS, // every operation, [] for none
webhook: [Public(), NoTenant(), NoThrottle()], // your class decorators
exports: [], // host providers to export beside the engine's
});Ports
| Token | Port |
|---|---|
| AGREEMENT_REPOSITORY | IAgreementRepository — incl. updateWhereStatus (the atomic transition) and the cross-tenant sweeps |
| AGREEMENT_EVENT_REPOSITORY | IAgreementEventRepository |
| AGREEMENT_CONFIG_REPOSITORY | IAgreementConfigRepository — a tenant's vendor accounts, current first |
| AGREEMENT_PROVIDER | IAgreementProvider — the vendor: DocuSealAgreementProvider, PandaDocAgreementProvider or FakeAgreementProvider |
| AGREEMENT_DOCUMENT_STORE | IAgreementDocumentStore — where our copies go, and signed links to them |
| AGREEMENT_LOGGER | IAgreementLogger |
| AGREEMENT_REFUND_HANDLER | IAgreementRefundHandler — a declined agreement's refund (bind a no-op without payments) |
| AGREEMENT_NOTIFIER | IAgreementNotifier — the firm's turn, the client's turn, a copy ready |
| AGREEMENT_RECORDING_SCHEDULER | IAgreementRecordingScheduler — fetch and store our copy, off the request |
| AGREEMENT_COMPLETION_LISTENER | IAgreementCompletionListener — what follows a signed agreement |
| ESIGN_TENANT_RESOLVER, ESIGN_CALLER_RESOLVER | who asks, in which tenant, from the GraphQL context |
| ESIGN_SIGNING_ACCESS | IEsignSigningAccess.assertMaySign(caller, party, agreement, tenant) |
All are required. The four host adapters are bound explicitly rather than optionally: an optional injection that quietly resolved to nothing is how a refund or a client's copy goes missing.
Vendors
Each takes (configs, logger, options). roleNames are what the tenant's
templates call each party — they are written into firms' templates, so a
host keeps whatever its templates already say. wording.templateNotConfigured
is the sentence for a tenant with no template. PandaDoc also takes
requiredTokens, checked against the live template. PandaDoc signs its
webhook in the URL (?signature=); DocuSeal in a header, with a replay
window.
Guards and the webhook
The resolvers carry no guards of their own. guards names the host's method
decorators for every operation in ESIGN_OPERATIONS —
createAgreementSigningSession and createFirmSigningSession — and an
operation missing from the map fails the boot. webhook are the class
decorators that make POST /webhooks/agreements/:tenantId public, outside
any tenant and unthrottled; the vendor's signature is its authentication.
The host must create its Nest app with { rawBody: true }.
Signing sessions
A client's session refuses staff, so a firm never signs in a client's place;
the firm's refuses anyone else. Neither takes a recipient from the caller.
ESIGN_SIGNING_ACCESS is the host's check on top (that this client is the
agreement's, that this staff member may act on that file).
Template handles and tokens
TemplateHandleResolver adopts a template a tenant picks (writing a handle,
prefix + a uuid, onto the vendor's record) and resolves it later,
refusing — never falling back — when the contract is gone or belongs to a
vendor the tenant left. createTokenMatcher(fields, aliases) matches
client.email, CLIENT EMAIL and Client.Email\ to one field and spreads
values across a template's own spellings. The fields are the host's.
What must never change
- Stored values: statuses
DRAFT,AWAITING_SIGNATURE,COMPLETED,DECLINED,VOIDED,SUPERSEDED; partiesCLIENT,FIRM; event types; retention codes; vendor namesdocuseal,pandadoc,fake. - What vendors hold: the handle prefix and role names a host chose.
- The webhook route, and its answers: 401 on a bad signature, 200 otherwise (a failure to apply is logged; reconciliation is the retry).
- Storage keys:
agreements/<tenant>/<id>/executed.pdfandaudit-certificate.pdf. - GraphQL names:
createAgreementSigningSession,createFirmSigningSession,AgreementSigningSession. - Error codes (
.code):AGREEMENTS_NOT_CONFIGURED,AGREEMENT_NOT_FOUND,INVALID_AGREEMENT_STATE,NOT_THE_SIGNER,WEBHOOK_VERIFICATION_FAILED,AGREEMENT_PROVIDER_ERROR,AGREEMENT_AUDIT_CERTIFICATE_PENDING,AGREEMENT_PROVIDER_MISMATCH.
Testing
inMemoryEsignHost() gives every port in memory with the fake vendor, for
forRoot or as service(). Run describeAgreementRepositoryContract against
your own repositories on a real store, each case in a transaction you roll
back; the in-memory ones meet it here, and the first host's Prisma ones meet
it on Postgres.
