@mccaskie-labs/workbench-agreements 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # @mccaskie-labs/workbench-agreements
2
+
3
+ ## 0.1.0
4
+
5
+ - First version: `createAgreements` (create, invite, share for Messenger and WhatsApp, get, list, void, file, verify, the signing route, tick, cleanup), `agreementsMigration`, the signed copy with its signing certificate (`buildSignedCopy`), the signing pages with `wb-agreement-*` classes and `AGREEMENT_STYLES`, words in English, Tagalog and Taglish, and `npx workbench-agreements init|generate [--check]|migration`.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @mccaskie-labs/workbench-agreements
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Send an agreement (a branded PDF made with `workbench-pdf`) to one to five people, let each sign it online on your own address (typed or drawn), keep a timestamped audit trail, and email the signed copy, with a signing certificate, to everyone. Runs in the app's own Worker, Supabase database and R2 bucket; emails go through the Gateway with `workbench-email`. What makes an e-signature valid in Australia and the Philippines, and what not to use it for: [`docs/agreements.md`](../../docs/agreements.md).
4
+
5
+ ```ts
6
+ import { createAgreements } from "@mccaskie-labs/workbench-agreements";
7
+ import { AGREEMENTS_CONFIG } from "./agreements.generated";
8
+
9
+ const agreements = createAgreements({ db: serviceClient, files: env.AGREEMENTS, mailer, config: AGREEMENTS_CONFIG, baseUrl: env.PUBLIC_URL, replyTo: "hello@example.com" });
10
+
11
+ // fetch(): before the app's own routes
12
+ const served = await agreements.handle(request, ctx);
13
+ if (served) return served;
14
+
15
+ // Send one
16
+ const sent = await agreements.create({
17
+ account: business.id,
18
+ title: "Venue hire agreement",
19
+ business: business.name,
20
+ document: pdfBytes,
21
+ signers: [{ name: "Maria Santos", email: "maria@example.com", role: "Hirer" }],
22
+ copyTo: [business.email],
23
+ });
24
+
25
+ // scheduled(): hourly and daily
26
+ ctx.waitUntil(agreements.tick());
27
+ ctx.waitUntil(agreements.cleanup());
28
+ ```
29
+
30
+ | Export | What it does |
31
+ | --- | --- |
32
+ | `createAgreements` | `create`, `invite`, `share` (a message and `wa.me` link for Messenger or WhatsApp), `get`, `list`, `void`, `file`, `verify`, `handle`, `tick`, `cleanup` |
33
+ | `agreementsMigration` | The three tables and their functions, service role only (`npx workbench-agreements migration`) |
34
+ | `buildSignedCopy`, `pdfPages` | The signed copy: original pages with a footer, then the signing certificate |
35
+ | `renderSigningPage`, `renderCodePage`, `renderDonePage`, `renderEndedPage` | The pages, every element with a `wb-agreement-*` class |
36
+ | `AGREEMENT_STYLES` (also `/styles`) | The classes and `--wb-agreement-*` variables with their defaults |
37
+ | `AGREEMENT_WORDS` | The words in English, Tagalog and Taglish (`ui.wbagreement.*`) |
38
+
39
+ Commands: `npx workbench-agreements init`, `generate [--check]`, `migration`.
@@ -0,0 +1,197 @@
1
+ import { type AgreementLanguage, type AgreementsConfig, type SigningMethod } from "./config.ts";
2
+ import { type AgreementStrings } from "./words.ts";
3
+ type DbError = {
4
+ message?: string;
5
+ code?: string;
6
+ details?: string;
7
+ } | null;
8
+ /** The part of a Supabase client createAgreements needs. Any supabase-js client fits. */
9
+ export interface AgreementsClient {
10
+ rpc(fn: string, args: Record<string, unknown>): PromiseLike<{
11
+ data: unknown;
12
+ error: DbError;
13
+ }>;
14
+ }
15
+ /** Where the PDFs and drawn signatures are kept. The app's R2 bucket binding fits. */
16
+ export interface FileStore {
17
+ put(key: string, value: Uint8Array, options?: {
18
+ httpMetadata?: {
19
+ contentType?: string;
20
+ };
21
+ }): Promise<unknown>;
22
+ get(key: string): Promise<{
23
+ arrayBuffer(): Promise<ArrayBuffer>;
24
+ } | null>;
25
+ delete(keys: string | string[]): Promise<unknown>;
26
+ }
27
+ /** The part of workbench-email's Mailer createAgreements needs. */
28
+ export interface MailerLike {
29
+ send(mail: {
30
+ to: string | string[];
31
+ subject: string;
32
+ text: string;
33
+ template: string;
34
+ replyTo?: string;
35
+ attachments?: {
36
+ filename: string;
37
+ mimeType: string;
38
+ bytes: Uint8Array;
39
+ }[];
40
+ idempotencyKey?: string;
41
+ }): Promise<unknown>;
42
+ }
43
+ export interface AgreementsOptions {
44
+ /** The service client. */
45
+ db: AgreementsClient;
46
+ /** The app's R2 bucket (its own, never shared). */
47
+ files: FileStore;
48
+ /** workbench-email's mailer, through the Gateway. */
49
+ mailer: MailerLike;
50
+ /** AGREEMENTS_CONFIG, from src/agreements.generated.ts. */
51
+ config: AgreementsConfig;
52
+ /** The project's own address in this environment, such as https://staging.example.app. Links are `<baseUrl>/<path>/<token>`. */
53
+ baseUrl: string;
54
+ /** The project's strings (`ui.wbagreement.*`); the package's words fill any gap. */
55
+ t?: AgreementStrings;
56
+ /** Stylesheets for the pages. Default /styles/workbench.css then /styles/project.css. */
57
+ styles?: readonly string[];
58
+ /** For dates on the pages and the certificate. Default en-AU. */
59
+ locale?: string;
60
+ /** Where replies to the emails go: the business's own address. */
61
+ replyTo?: string;
62
+ /** For tests. */
63
+ now?: () => Date;
64
+ }
65
+ export interface SignerRequest {
66
+ name: string;
67
+ email: string;
68
+ /** What they sign as: "Customer", "Director". */
69
+ role?: string;
70
+ }
71
+ export interface CreateAgreementRequest {
72
+ /** The app's own account (the business) this belongs to. */
73
+ account: string;
74
+ title: string;
75
+ /** The business's name, as signers see it. */
76
+ business: string;
77
+ /** The agreement's PDF, made with workbench-pdf. Kept as it is: its SHA-256 is what everyone signs. */
78
+ document: Uint8Array;
79
+ /** One to five people, in the order they're listed on the certificate. */
80
+ signers: readonly SignerRequest[];
81
+ /** The business's own addresses that get the signed copy (up to five). */
82
+ copyTo?: readonly string[];
83
+ /** The app's own row this is about (a booking, a job). */
84
+ subject?: {
85
+ type: string;
86
+ id: string;
87
+ };
88
+ language?: AgreementLanguage;
89
+ /** Default: config.expires_after_days from now. */
90
+ expiresAt?: Date;
91
+ /** Email each signer their link now. Default true. False when the business shares the links itself (share()). */
92
+ invite?: boolean;
93
+ }
94
+ export interface SignerLink {
95
+ id: string;
96
+ name: string;
97
+ email: string;
98
+ url: string;
99
+ }
100
+ export interface Signer {
101
+ id: string;
102
+ position: number;
103
+ name: string;
104
+ email: string;
105
+ role: string | null;
106
+ status: "waiting" | "signed" | "declined";
107
+ invitedAt: string | null;
108
+ verifiedAt: string | null;
109
+ signedAt: string | null;
110
+ method: SigningMethod | null;
111
+ typedName: string | null;
112
+ declinedAt: string | null;
113
+ declineReason: string | null;
114
+ }
115
+ export interface AgreementEvent {
116
+ kind: string;
117
+ at: string;
118
+ signerId: string | null;
119
+ ip: string | null;
120
+ userAgent: string | null;
121
+ detail: Record<string, unknown> | null;
122
+ }
123
+ export type AgreementStatus = "out" | "completed" | "declined" | "voided" | "expired";
124
+ export interface Agreement {
125
+ id: string;
126
+ account: string;
127
+ subject: {
128
+ type: string;
129
+ id: string;
130
+ } | null;
131
+ title: string;
132
+ business: string;
133
+ language: AgreementLanguage;
134
+ status: AgreementStatus;
135
+ documentSha256: string;
136
+ signedSha256: string | null;
137
+ pages: number;
138
+ copyTo: string[];
139
+ createdAt: string;
140
+ expiresAt: string;
141
+ completedAt: string | null;
142
+ copiesSentAt: string | null;
143
+ endedAt: string | null;
144
+ endReason: string | null;
145
+ signers: Signer[];
146
+ /** Only from get(). */
147
+ events?: AgreementEvent[];
148
+ }
149
+ /** For pasting into Messenger or WhatsApp by hand. Never SMS. */
150
+ export interface ShareLink extends SignerLink {
151
+ text: string;
152
+ /** Opens WhatsApp with the message written; the business picks the chat. */
153
+ whatsapp: string;
154
+ }
155
+ export interface Agreements {
156
+ create(req: CreateAgreementRequest): Promise<{
157
+ id: string;
158
+ documentSha256: string;
159
+ signers: SignerLink[];
160
+ }>;
161
+ /** Emails each waiting signer their link (again). Returns how many were emailed. */
162
+ invite(id: string): Promise<number>;
163
+ /** Each waiting signer's link with a message to paste into Messenger or WhatsApp. */
164
+ share(id: string): Promise<ShareLink[]>;
165
+ get(id: string): Promise<Agreement | null>;
166
+ list(account: string, limit?: number): Promise<Agreement[]>;
167
+ /** Withdraws an agreement nobody has finished. False when it is already closed. */
168
+ void(id: string, reason: string): Promise<boolean>;
169
+ /** The original PDF, or the signed copy once complete. */
170
+ file(id: string, which: "original" | "signed"): Promise<Uint8Array | null>;
171
+ /** Which agreement a PDF is, by its SHA-256, so anyone can check a copy. */
172
+ verify(pdf: Uint8Array): Promise<{
173
+ id: string;
174
+ title: string;
175
+ status: AgreementStatus;
176
+ which: "original" | "signed";
177
+ completedAt: string | null;
178
+ } | null>;
179
+ /** The Worker route. Null when the path isn't a signing link's. */
180
+ handle(request: Request, ctx: {
181
+ waitUntil(p: Promise<unknown>): void;
182
+ }): Promise<Response | null>;
183
+ /** From the app's cron (hourly is plenty): expires, finishes, sends copies and reminders. */
184
+ tick(): Promise<{
185
+ expired: number;
186
+ finished: number;
187
+ copies: number;
188
+ reminded: number;
189
+ failed: number;
190
+ }>;
191
+ /** Once a day: deletes agreements and their files past the retention. */
192
+ cleanup(): Promise<{
193
+ deleted: number;
194
+ }>;
195
+ }
196
+ export declare function createAgreements(opts: AgreementsOptions): Agreements;
197
+ export {};