@hasna/microservices 0.0.3 → 0.0.5

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.
Files changed (68) hide show
  1. package/bin/index.js +63 -0
  2. package/bin/mcp.js +63 -0
  3. package/dist/index.js +63 -0
  4. package/microservices/microservice-ads/package.json +27 -0
  5. package/microservices/microservice-ads/src/cli/index.ts +605 -0
  6. package/microservices/microservice-ads/src/db/campaigns.ts +797 -0
  7. package/microservices/microservice-ads/src/db/database.ts +93 -0
  8. package/microservices/microservice-ads/src/db/migrations.ts +60 -0
  9. package/microservices/microservice-ads/src/index.ts +39 -0
  10. package/microservices/microservice-ads/src/mcp/index.ts +480 -0
  11. package/microservices/microservice-contracts/package.json +27 -0
  12. package/microservices/microservice-contracts/src/cli/index.ts +770 -0
  13. package/microservices/microservice-contracts/src/db/contracts.ts +925 -0
  14. package/microservices/microservice-contracts/src/db/database.ts +93 -0
  15. package/microservices/microservice-contracts/src/db/migrations.ts +141 -0
  16. package/microservices/microservice-contracts/src/index.ts +43 -0
  17. package/microservices/microservice-contracts/src/mcp/index.ts +617 -0
  18. package/microservices/microservice-domains/package.json +27 -0
  19. package/microservices/microservice-domains/src/cli/index.ts +691 -0
  20. package/microservices/microservice-domains/src/db/database.ts +93 -0
  21. package/microservices/microservice-domains/src/db/domains.ts +1164 -0
  22. package/microservices/microservice-domains/src/db/migrations.ts +60 -0
  23. package/microservices/microservice-domains/src/index.ts +65 -0
  24. package/microservices/microservice-domains/src/mcp/index.ts +536 -0
  25. package/microservices/microservice-hiring/package.json +27 -0
  26. package/microservices/microservice-hiring/src/cli/index.ts +741 -0
  27. package/microservices/microservice-hiring/src/db/database.ts +93 -0
  28. package/microservices/microservice-hiring/src/db/hiring.ts +1085 -0
  29. package/microservices/microservice-hiring/src/db/migrations.ts +89 -0
  30. package/microservices/microservice-hiring/src/index.ts +80 -0
  31. package/microservices/microservice-hiring/src/lib/scoring.ts +206 -0
  32. package/microservices/microservice-hiring/src/mcp/index.ts +709 -0
  33. package/microservices/microservice-payments/package.json +27 -0
  34. package/microservices/microservice-payments/src/cli/index.ts +609 -0
  35. package/microservices/microservice-payments/src/db/database.ts +93 -0
  36. package/microservices/microservice-payments/src/db/migrations.ts +81 -0
  37. package/microservices/microservice-payments/src/db/payments.ts +1204 -0
  38. package/microservices/microservice-payments/src/index.ts +51 -0
  39. package/microservices/microservice-payments/src/mcp/index.ts +683 -0
  40. package/microservices/microservice-payroll/package.json +27 -0
  41. package/microservices/microservice-payroll/src/cli/index.ts +643 -0
  42. package/microservices/microservice-payroll/src/db/database.ts +93 -0
  43. package/microservices/microservice-payroll/src/db/migrations.ts +95 -0
  44. package/microservices/microservice-payroll/src/db/payroll.ts +1377 -0
  45. package/microservices/microservice-payroll/src/index.ts +48 -0
  46. package/microservices/microservice-payroll/src/mcp/index.ts +666 -0
  47. package/microservices/microservice-shipping/package.json +27 -0
  48. package/microservices/microservice-shipping/src/cli/index.ts +606 -0
  49. package/microservices/microservice-shipping/src/db/database.ts +93 -0
  50. package/microservices/microservice-shipping/src/db/migrations.ts +69 -0
  51. package/microservices/microservice-shipping/src/db/shipping.ts +1093 -0
  52. package/microservices/microservice-shipping/src/index.ts +53 -0
  53. package/microservices/microservice-shipping/src/mcp/index.ts +533 -0
  54. package/microservices/microservice-social/package.json +27 -0
  55. package/microservices/microservice-social/src/cli/index.ts +689 -0
  56. package/microservices/microservice-social/src/db/database.ts +93 -0
  57. package/microservices/microservice-social/src/db/migrations.ts +88 -0
  58. package/microservices/microservice-social/src/db/social.ts +1046 -0
  59. package/microservices/microservice-social/src/index.ts +46 -0
  60. package/microservices/microservice-social/src/mcp/index.ts +655 -0
  61. package/microservices/microservice-subscriptions/package.json +27 -0
  62. package/microservices/microservice-subscriptions/src/cli/index.ts +715 -0
  63. package/microservices/microservice-subscriptions/src/db/database.ts +93 -0
  64. package/microservices/microservice-subscriptions/src/db/migrations.ts +125 -0
  65. package/microservices/microservice-subscriptions/src/db/subscriptions.ts +1256 -0
  66. package/microservices/microservice-subscriptions/src/index.ts +41 -0
  67. package/microservices/microservice-subscriptions/src/mcp/index.ts +631 -0
  68. package/package.json +1 -1
@@ -0,0 +1,770 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Command } from "commander";
4
+ import {
5
+ createContract,
6
+ getContract,
7
+ listContracts,
8
+ updateContract,
9
+ deleteContract,
10
+ searchContracts,
11
+ listExpiring,
12
+ renewContract,
13
+ getContractStats,
14
+ submitForReview,
15
+ approveContract,
16
+ getContractHistory,
17
+ recordSignature,
18
+ listSignatures,
19
+ compareContracts,
20
+ exportContract,
21
+ } from "../db/contracts.js";
22
+ import {
23
+ createClause,
24
+ listClauses,
25
+ deleteClause,
26
+ addClauseFromTemplate,
27
+ saveClauseTemplate,
28
+ listClauseTemplates,
29
+ } from "../db/contracts.js";
30
+ import {
31
+ createReminder,
32
+ listReminders,
33
+ deleteReminder,
34
+ setMultiReminders,
35
+ } from "../db/contracts.js";
36
+ import {
37
+ createObligation,
38
+ listObligations,
39
+ completeObligation,
40
+ listOverdueObligations,
41
+ } from "../db/contracts.js";
42
+
43
+ const program = new Command();
44
+
45
+ program
46
+ .name("microservice-contracts")
47
+ .description("Contract and agreement management microservice")
48
+ .version("0.0.1");
49
+
50
+ // --- Contracts ---
51
+
52
+ const contractCmd = program
53
+ .command("contract")
54
+ .description("Contract management");
55
+
56
+ contractCmd
57
+ .command("create")
58
+ .description("Create a new contract")
59
+ .requiredOption("--title <title>", "Contract title")
60
+ .option("--type <type>", "Contract type (nda/service/employment/license/other)", "other")
61
+ .option("--status <status>", "Status (draft/pending_review/pending_signature/active/expired/terminated)", "draft")
62
+ .option("--counterparty <name>", "Counterparty name")
63
+ .option("--counterparty-email <email>", "Counterparty email")
64
+ .option("--start-date <date>", "Start date (YYYY-MM-DD)")
65
+ .option("--end-date <date>", "End date (YYYY-MM-DD)")
66
+ .option("--auto-renew", "Enable auto-renewal", false)
67
+ .option("--renewal-period <period>", "Renewal period (e.g. '1 year')")
68
+ .option("--value <amount>", "Contract value")
69
+ .option("--currency <code>", "Currency code", "USD")
70
+ .option("--file-path <path>", "Path to contract file")
71
+ .option("--json", "Output as JSON", false)
72
+ .action((opts) => {
73
+ const contract = createContract({
74
+ title: opts.title,
75
+ type: opts.type,
76
+ status: opts.status,
77
+ counterparty: opts.counterparty,
78
+ counterparty_email: opts.counterpartyEmail,
79
+ start_date: opts.startDate,
80
+ end_date: opts.endDate,
81
+ auto_renew: opts.autoRenew,
82
+ renewal_period: opts.renewalPeriod,
83
+ value: opts.value ? parseFloat(opts.value) : undefined,
84
+ currency: opts.currency,
85
+ file_path: opts.filePath,
86
+ });
87
+
88
+ if (opts.json) {
89
+ console.log(JSON.stringify(contract, null, 2));
90
+ } else {
91
+ console.log(`Created contract: ${contract.title} (${contract.id})`);
92
+ }
93
+ });
94
+
95
+ contractCmd
96
+ .command("list")
97
+ .description("List contracts")
98
+ .option("--search <query>", "Search by title, counterparty, or email")
99
+ .option("--type <type>", "Filter by type")
100
+ .option("--status <status>", "Filter by status")
101
+ .option("--counterparty <name>", "Filter by counterparty")
102
+ .option("--limit <n>", "Limit results")
103
+ .option("--json", "Output as JSON", false)
104
+ .action((opts) => {
105
+ const contracts = listContracts({
106
+ search: opts.search,
107
+ type: opts.type,
108
+ status: opts.status,
109
+ counterparty: opts.counterparty,
110
+ limit: opts.limit ? parseInt(opts.limit) : undefined,
111
+ });
112
+
113
+ if (opts.json) {
114
+ console.log(JSON.stringify(contracts, null, 2));
115
+ } else {
116
+ if (contracts.length === 0) {
117
+ console.log("No contracts found.");
118
+ return;
119
+ }
120
+ for (const c of contracts) {
121
+ const cp = c.counterparty ? ` — ${c.counterparty}` : "";
122
+ const status = ` [${c.status}]`;
123
+ console.log(` ${c.title}${cp}${status} (${c.id})`);
124
+ }
125
+ console.log(`\n${contracts.length} contract(s)`);
126
+ }
127
+ });
128
+
129
+ contractCmd
130
+ .command("get")
131
+ .description("Get a contract by ID")
132
+ .argument("<id>", "Contract ID")
133
+ .option("--json", "Output as JSON", false)
134
+ .action((id, opts) => {
135
+ const contract = getContract(id);
136
+ if (!contract) {
137
+ console.error(`Contract '${id}' not found.`);
138
+ process.exit(1);
139
+ }
140
+
141
+ if (opts.json) {
142
+ console.log(JSON.stringify(contract, null, 2));
143
+ } else {
144
+ console.log(`${contract.title}`);
145
+ console.log(` Type: ${contract.type}`);
146
+ console.log(` Status: ${contract.status}`);
147
+ if (contract.counterparty) console.log(` Counterparty: ${contract.counterparty}`);
148
+ if (contract.counterparty_email) console.log(` Email: ${contract.counterparty_email}`);
149
+ if (contract.start_date) console.log(` Start: ${contract.start_date}`);
150
+ if (contract.end_date) console.log(` End: ${contract.end_date}`);
151
+ if (contract.value !== null) console.log(` Value: ${contract.value} ${contract.currency}`);
152
+ if (contract.auto_renew) console.log(` Auto-renew: ${contract.renewal_period || "1 year"}`);
153
+ if (contract.file_path) console.log(` File: ${contract.file_path}`);
154
+ }
155
+ });
156
+
157
+ contractCmd
158
+ .command("update")
159
+ .description("Update a contract")
160
+ .argument("<id>", "Contract ID")
161
+ .option("--title <title>", "Title")
162
+ .option("--type <type>", "Type")
163
+ .option("--status <status>", "Status")
164
+ .option("--counterparty <name>", "Counterparty name")
165
+ .option("--counterparty-email <email>", "Counterparty email")
166
+ .option("--start-date <date>", "Start date")
167
+ .option("--end-date <date>", "End date")
168
+ .option("--auto-renew", "Enable auto-renewal")
169
+ .option("--no-auto-renew", "Disable auto-renewal")
170
+ .option("--renewal-period <period>", "Renewal period")
171
+ .option("--value <amount>", "Value")
172
+ .option("--currency <code>", "Currency")
173
+ .option("--file-path <path>", "File path")
174
+ .option("--json", "Output as JSON", false)
175
+ .action((id, opts) => {
176
+ const input: Record<string, unknown> = {};
177
+ if (opts.title !== undefined) input.title = opts.title;
178
+ if (opts.type !== undefined) input.type = opts.type;
179
+ if (opts.status !== undefined) input.status = opts.status;
180
+ if (opts.counterparty !== undefined) input.counterparty = opts.counterparty;
181
+ if (opts.counterpartyEmail !== undefined) input.counterparty_email = opts.counterpartyEmail;
182
+ if (opts.startDate !== undefined) input.start_date = opts.startDate;
183
+ if (opts.endDate !== undefined) input.end_date = opts.endDate;
184
+ if (opts.autoRenew !== undefined) input.auto_renew = opts.autoRenew;
185
+ if (opts.renewalPeriod !== undefined) input.renewal_period = opts.renewalPeriod;
186
+ if (opts.value !== undefined) input.value = parseFloat(opts.value);
187
+ if (opts.currency !== undefined) input.currency = opts.currency;
188
+ if (opts.filePath !== undefined) input.file_path = opts.filePath;
189
+
190
+ const contract = updateContract(id, input);
191
+ if (!contract) {
192
+ console.error(`Contract '${id}' not found.`);
193
+ process.exit(1);
194
+ }
195
+
196
+ if (opts.json) {
197
+ console.log(JSON.stringify(contract, null, 2));
198
+ } else {
199
+ console.log(`Updated: ${contract.title}`);
200
+ }
201
+ });
202
+
203
+ contractCmd
204
+ .command("delete")
205
+ .description("Delete a contract")
206
+ .argument("<id>", "Contract ID")
207
+ .action((id) => {
208
+ const deleted = deleteContract(id);
209
+ if (deleted) {
210
+ console.log(`Deleted contract ${id}`);
211
+ } else {
212
+ console.error(`Contract '${id}' not found.`);
213
+ process.exit(1);
214
+ }
215
+ });
216
+
217
+ // --- Approval workflow ---
218
+
219
+ contractCmd
220
+ .command("submit")
221
+ .description("Submit a draft contract for review (draft -> pending_review)")
222
+ .argument("<id>", "Contract ID")
223
+ .option("--json", "Output as JSON", false)
224
+ .action((id, opts) => {
225
+ try {
226
+ const contract = submitForReview(id);
227
+ if (!contract) {
228
+ console.error(`Contract '${id}' not found.`);
229
+ process.exit(1);
230
+ }
231
+ if (opts.json) {
232
+ console.log(JSON.stringify(contract, null, 2));
233
+ } else {
234
+ console.log(`Submitted for review: ${contract.title} [${contract.status}]`);
235
+ }
236
+ } catch (err) {
237
+ console.error((err as Error).message);
238
+ process.exit(1);
239
+ }
240
+ });
241
+
242
+ contractCmd
243
+ .command("approve")
244
+ .description("Approve a contract (advances through approval workflow)")
245
+ .argument("<id>", "Contract ID")
246
+ .option("--json", "Output as JSON", false)
247
+ .action((id, opts) => {
248
+ try {
249
+ const contract = approveContract(id);
250
+ if (!contract) {
251
+ console.error(`Contract '${id}' not found.`);
252
+ process.exit(1);
253
+ }
254
+ if (opts.json) {
255
+ console.log(JSON.stringify(contract, null, 2));
256
+ } else {
257
+ console.log(`Approved: ${contract.title} [${contract.status}]`);
258
+ }
259
+ } catch (err) {
260
+ console.error((err as Error).message);
261
+ process.exit(1);
262
+ }
263
+ });
264
+
265
+ // --- Version history ---
266
+
267
+ contractCmd
268
+ .command("history")
269
+ .description("Show version history for a contract")
270
+ .argument("<id>", "Contract ID")
271
+ .option("--json", "Output as JSON", false)
272
+ .action((id, opts) => {
273
+ const history = getContractHistory(id);
274
+ if (opts.json) {
275
+ console.log(JSON.stringify(history, null, 2));
276
+ } else {
277
+ if (history.length === 0) {
278
+ console.log("No version history.");
279
+ return;
280
+ }
281
+ for (const v of history) {
282
+ console.log(` ${v.changed_at} — "${v.title}" [${v.status}] value=${v.value ?? "N/A"}`);
283
+ }
284
+ console.log(`\n${history.length} version(s)`);
285
+ }
286
+ });
287
+
288
+ // --- Signature logging ---
289
+
290
+ contractCmd
291
+ .command("sign")
292
+ .description("Record a signature for a contract")
293
+ .argument("<id>", "Contract ID")
294
+ .requiredOption("--signer <name>", "Signer name")
295
+ .option("--email <email>", "Signer email")
296
+ .option("--method <method>", "Signature method (digital/wet/docusign)", "digital")
297
+ .option("--json", "Output as JSON", false)
298
+ .action((id, opts) => {
299
+ const sig = recordSignature({
300
+ contract_id: id,
301
+ signer_name: opts.signer,
302
+ signer_email: opts.email,
303
+ method: opts.method,
304
+ });
305
+
306
+ if (opts.json) {
307
+ console.log(JSON.stringify(sig, null, 2));
308
+ } else {
309
+ const email = sig.signer_email ? ` (${sig.signer_email})` : "";
310
+ console.log(`Recorded signature: ${sig.signer_name}${email} via ${sig.method} (${sig.id})`);
311
+ }
312
+ });
313
+
314
+ contractCmd
315
+ .command("signatures")
316
+ .description("List signatures for a contract")
317
+ .argument("<id>", "Contract ID")
318
+ .option("--json", "Output as JSON", false)
319
+ .action((id, opts) => {
320
+ const sigs = listSignatures(id);
321
+ if (opts.json) {
322
+ console.log(JSON.stringify(sigs, null, 2));
323
+ } else {
324
+ if (sigs.length === 0) {
325
+ console.log("No signatures found.");
326
+ return;
327
+ }
328
+ for (const s of sigs) {
329
+ const email = s.signer_email ? ` (${s.signer_email})` : "";
330
+ console.log(` ${s.signer_name}${email} — ${s.method} — ${s.signed_at}`);
331
+ }
332
+ }
333
+ });
334
+
335
+ // --- Contract comparison ---
336
+
337
+ contractCmd
338
+ .command("compare")
339
+ .description("Compare two contracts showing clause differences")
340
+ .argument("<id1>", "First contract ID")
341
+ .argument("<id2>", "Second contract ID")
342
+ .option("--json", "Output as JSON", false)
343
+ .action((id1, id2, opts) => {
344
+ try {
345
+ const diff = compareContracts(id1, id2);
346
+ if (opts.json) {
347
+ console.log(JSON.stringify(diff, null, 2));
348
+ } else {
349
+ console.log(`Comparing: "${diff.contract1.title}" vs "${diff.contract2.title}"\n`);
350
+
351
+ if (diff.field_differences.length > 0) {
352
+ console.log("Field differences:");
353
+ for (const d of diff.field_differences) {
354
+ console.log(` ${d.field}: ${JSON.stringify(d.contract1_value)} vs ${JSON.stringify(d.contract2_value)}`);
355
+ }
356
+ console.log("");
357
+ }
358
+
359
+ if (diff.clause_only_in_1.length > 0) {
360
+ console.log(`Clauses only in "${diff.contract1.title}":`);
361
+ for (const c of diff.clause_only_in_1) {
362
+ console.log(` - ${c.name}`);
363
+ }
364
+ console.log("");
365
+ }
366
+
367
+ if (diff.clause_only_in_2.length > 0) {
368
+ console.log(`Clauses only in "${diff.contract2.title}":`);
369
+ for (const c of diff.clause_only_in_2) {
370
+ console.log(` - ${c.name}`);
371
+ }
372
+ console.log("");
373
+ }
374
+
375
+ if (diff.clause_differences.length > 0) {
376
+ console.log("Clause text differences:");
377
+ for (const d of diff.clause_differences) {
378
+ console.log(` ${d.name}:`);
379
+ console.log(` Contract 1: ${d.contract1_text.substring(0, 80)}${d.contract1_text.length > 80 ? "..." : ""}`);
380
+ console.log(` Contract 2: ${d.contract2_text.substring(0, 80)}${d.contract2_text.length > 80 ? "..." : ""}`);
381
+ }
382
+ console.log("");
383
+ }
384
+
385
+ if (diff.field_differences.length === 0 && diff.clause_only_in_1.length === 0 && diff.clause_only_in_2.length === 0 && diff.clause_differences.length === 0) {
386
+ console.log("No differences found.");
387
+ }
388
+ }
389
+ } catch (err) {
390
+ console.error((err as Error).message);
391
+ process.exit(1);
392
+ }
393
+ });
394
+
395
+ // --- Markdown export ---
396
+
397
+ contractCmd
398
+ .command("export")
399
+ .description("Export a contract in markdown or JSON format")
400
+ .argument("<id>", "Contract ID")
401
+ .option("--format <format>", "Export format (md/json)", "md")
402
+ .action((id, opts) => {
403
+ try {
404
+ const output = exportContract(id, opts.format);
405
+ console.log(output);
406
+ } catch (err) {
407
+ console.error((err as Error).message);
408
+ process.exit(1);
409
+ }
410
+ });
411
+
412
+ // --- Expiring & Renew ---
413
+
414
+ program
415
+ .command("expiring")
416
+ .description("List contracts expiring within N days")
417
+ .option("--days <n>", "Number of days to look ahead", "30")
418
+ .option("--json", "Output as JSON", false)
419
+ .action((opts) => {
420
+ const days = parseInt(opts.days);
421
+ const contracts = listExpiring(days);
422
+
423
+ if (opts.json) {
424
+ console.log(JSON.stringify(contracts, null, 2));
425
+ } else {
426
+ if (contracts.length === 0) {
427
+ console.log(`No contracts expiring within ${days} days.`);
428
+ return;
429
+ }
430
+ for (const c of contracts) {
431
+ console.log(` ${c.title} — expires ${c.end_date} [${c.status}]`);
432
+ }
433
+ console.log(`\n${contracts.length} contract(s) expiring within ${days} days`);
434
+ }
435
+ });
436
+
437
+ program
438
+ .command("renew")
439
+ .description("Renew a contract")
440
+ .argument("<id>", "Contract ID")
441
+ .option("--json", "Output as JSON", false)
442
+ .action((id, opts) => {
443
+ const contract = renewContract(id);
444
+ if (!contract) {
445
+ console.error(`Contract '${id}' not found.`);
446
+ process.exit(1);
447
+ }
448
+
449
+ if (opts.json) {
450
+ console.log(JSON.stringify(contract, null, 2));
451
+ } else {
452
+ console.log(`Renewed: ${contract.title} — new end date: ${contract.end_date}`);
453
+ }
454
+ });
455
+
456
+ // --- Clauses ---
457
+
458
+ const clauseCmd = program
459
+ .command("clause")
460
+ .description("Clause management");
461
+
462
+ clauseCmd
463
+ .command("add")
464
+ .description("Add a clause to a contract")
465
+ .requiredOption("--contract <id>", "Contract ID")
466
+ .requiredOption("--name <name>", "Clause name")
467
+ .option("--text <text>", "Clause text")
468
+ .option("--from-template <templateName>", "Add clause from a template by name")
469
+ .option("--type <type>", "Clause type (standard/custom/negotiated)", "standard")
470
+ .option("--json", "Output as JSON", false)
471
+ .action((opts) => {
472
+ try {
473
+ let clause;
474
+ if (opts.fromTemplate) {
475
+ clause = addClauseFromTemplate(opts.contract, opts.fromTemplate);
476
+ } else {
477
+ if (!opts.text) {
478
+ console.error("Either --text or --from-template is required.");
479
+ process.exit(1);
480
+ }
481
+ clause = createClause({
482
+ contract_id: opts.contract,
483
+ name: opts.name,
484
+ text: opts.text,
485
+ type: opts.type,
486
+ });
487
+ }
488
+
489
+ if (opts.json) {
490
+ console.log(JSON.stringify(clause, null, 2));
491
+ } else {
492
+ console.log(`Added clause: ${clause.name} (${clause.id})`);
493
+ }
494
+ } catch (err) {
495
+ console.error((err as Error).message);
496
+ process.exit(1);
497
+ }
498
+ });
499
+
500
+ clauseCmd
501
+ .command("list")
502
+ .description("List clauses for a contract")
503
+ .argument("<contract-id>", "Contract ID")
504
+ .option("--json", "Output as JSON", false)
505
+ .action((contractId, opts) => {
506
+ const clauses = listClauses(contractId);
507
+
508
+ if (opts.json) {
509
+ console.log(JSON.stringify(clauses, null, 2));
510
+ } else {
511
+ if (clauses.length === 0) {
512
+ console.log("No clauses found.");
513
+ return;
514
+ }
515
+ for (const c of clauses) {
516
+ console.log(` ${c.name} [${c.type}]: ${c.text.substring(0, 80)}${c.text.length > 80 ? "..." : ""}`);
517
+ }
518
+ }
519
+ });
520
+
521
+ clauseCmd
522
+ .command("remove")
523
+ .description("Remove a clause")
524
+ .argument("<id>", "Clause ID")
525
+ .action((id) => {
526
+ const deleted = deleteClause(id);
527
+ if (deleted) {
528
+ console.log(`Removed clause ${id}`);
529
+ } else {
530
+ console.error(`Clause '${id}' not found.`);
531
+ process.exit(1);
532
+ }
533
+ });
534
+
535
+ // --- Clause templates ---
536
+
537
+ clauseCmd
538
+ .command("save-template")
539
+ .description("Save a clause as a reusable template")
540
+ .requiredOption("--name <name>", "Template name")
541
+ .requiredOption("--text <text>", "Template text")
542
+ .option("--type <type>", "Clause type (standard/custom/negotiated)", "standard")
543
+ .option("--json", "Output as JSON", false)
544
+ .action((opts) => {
545
+ const template = saveClauseTemplate({
546
+ name: opts.name,
547
+ text: opts.text,
548
+ type: opts.type,
549
+ });
550
+
551
+ if (opts.json) {
552
+ console.log(JSON.stringify(template, null, 2));
553
+ } else {
554
+ console.log(`Saved template: ${template.name} (${template.id})`);
555
+ }
556
+ });
557
+
558
+ clauseCmd
559
+ .command("list-templates")
560
+ .description("List all clause templates")
561
+ .option("--json", "Output as JSON", false)
562
+ .action((opts) => {
563
+ const templates = listClauseTemplates();
564
+
565
+ if (opts.json) {
566
+ console.log(JSON.stringify(templates, null, 2));
567
+ } else {
568
+ if (templates.length === 0) {
569
+ console.log("No clause templates found.");
570
+ return;
571
+ }
572
+ for (const t of templates) {
573
+ console.log(` ${t.name} [${t.type}]: ${t.text.substring(0, 80)}${t.text.length > 80 ? "..." : ""}`);
574
+ }
575
+ }
576
+ });
577
+
578
+ // --- Obligations ---
579
+
580
+ const obligationCmd = program
581
+ .command("obligation")
582
+ .description("Obligation tracking");
583
+
584
+ obligationCmd
585
+ .command("add")
586
+ .description("Add an obligation to a clause")
587
+ .requiredOption("--clause <id>", "Clause ID")
588
+ .requiredOption("--description <desc>", "Obligation description")
589
+ .option("--due-date <date>", "Due date (YYYY-MM-DD)")
590
+ .option("--assigned-to <name>", "Person assigned to this obligation")
591
+ .option("--json", "Output as JSON", false)
592
+ .action((opts) => {
593
+ const obligation = createObligation({
594
+ clause_id: opts.clause,
595
+ description: opts.description,
596
+ due_date: opts.dueDate,
597
+ assigned_to: opts.assignedTo,
598
+ });
599
+
600
+ if (opts.json) {
601
+ console.log(JSON.stringify(obligation, null, 2));
602
+ } else {
603
+ console.log(`Added obligation: ${obligation.description} (${obligation.id})`);
604
+ }
605
+ });
606
+
607
+ obligationCmd
608
+ .command("list")
609
+ .description("List obligations for a clause")
610
+ .argument("<clause-id>", "Clause ID")
611
+ .option("--json", "Output as JSON", false)
612
+ .action((clauseId, opts) => {
613
+ const obligations = listObligations(clauseId);
614
+
615
+ if (opts.json) {
616
+ console.log(JSON.stringify(obligations, null, 2));
617
+ } else {
618
+ if (obligations.length === 0) {
619
+ console.log("No obligations found.");
620
+ return;
621
+ }
622
+ for (const o of obligations) {
623
+ const due = o.due_date ? ` due ${o.due_date}` : "";
624
+ const assigned = o.assigned_to ? ` (${o.assigned_to})` : "";
625
+ console.log(` [${o.status}] ${o.description}${due}${assigned}`);
626
+ }
627
+ }
628
+ });
629
+
630
+ obligationCmd
631
+ .command("complete")
632
+ .description("Mark an obligation as completed")
633
+ .argument("<id>", "Obligation ID")
634
+ .option("--json", "Output as JSON", false)
635
+ .action((id, opts) => {
636
+ const obligation = completeObligation(id);
637
+ if (!obligation) {
638
+ console.error(`Obligation '${id}' not found.`);
639
+ process.exit(1);
640
+ }
641
+
642
+ if (opts.json) {
643
+ console.log(JSON.stringify(obligation, null, 2));
644
+ } else {
645
+ console.log(`Completed: ${obligation.description}`);
646
+ }
647
+ });
648
+
649
+ obligationCmd
650
+ .command("overdue")
651
+ .description("List all overdue obligations")
652
+ .option("--json", "Output as JSON", false)
653
+ .action((opts) => {
654
+ const obligations = listOverdueObligations();
655
+
656
+ if (opts.json) {
657
+ console.log(JSON.stringify(obligations, null, 2));
658
+ } else {
659
+ if (obligations.length === 0) {
660
+ console.log("No overdue obligations.");
661
+ return;
662
+ }
663
+ for (const o of obligations) {
664
+ const due = o.due_date ? ` due ${o.due_date}` : "";
665
+ const assigned = o.assigned_to ? ` (${o.assigned_to})` : "";
666
+ console.log(` [${o.status}] ${o.description}${due}${assigned}`);
667
+ }
668
+ console.log(`\n${obligations.length} overdue obligation(s)`);
669
+ }
670
+ });
671
+
672
+ // --- Reminders ---
673
+
674
+ const remindCmd = program
675
+ .command("remind")
676
+ .description("Reminder management");
677
+
678
+ remindCmd
679
+ .command("set")
680
+ .description("Set a reminder for a contract")
681
+ .requiredOption("--contract <id>", "Contract ID")
682
+ .option("--at <datetime>", "Reminder datetime (ISO 8601)")
683
+ .option("--message <msg>", "Reminder message")
684
+ .option("--days-before <days>", "Set multi-stage reminders (comma-separated, e.g. 60,30,7)")
685
+ .option("--json", "Output as JSON", false)
686
+ .action((opts) => {
687
+ if (opts.daysBefore) {
688
+ try {
689
+ const days = opts.daysBefore.split(",").map((d: string) => parseInt(d.trim()));
690
+ const reminders = setMultiReminders(opts.contract, days);
691
+ if (opts.json) {
692
+ console.log(JSON.stringify(reminders, null, 2));
693
+ } else {
694
+ for (const r of reminders) {
695
+ console.log(`Set reminder: ${r.message} at ${r.remind_at} (${r.id})`);
696
+ }
697
+ console.log(`\n${reminders.length} reminder(s) created`);
698
+ }
699
+ } catch (err) {
700
+ console.error((err as Error).message);
701
+ process.exit(1);
702
+ }
703
+ } else {
704
+ if (!opts.at || !opts.message) {
705
+ console.error("Either --at and --message are required, or use --days-before for multi-stage reminders.");
706
+ process.exit(1);
707
+ }
708
+ const reminder = createReminder({
709
+ contract_id: opts.contract,
710
+ remind_at: opts.at,
711
+ message: opts.message,
712
+ });
713
+
714
+ if (opts.json) {
715
+ console.log(JSON.stringify(reminder, null, 2));
716
+ } else {
717
+ console.log(`Set reminder: ${reminder.message} at ${reminder.remind_at} (${reminder.id})`);
718
+ }
719
+ }
720
+ });
721
+
722
+ remindCmd
723
+ .command("list")
724
+ .description("List reminders for a contract")
725
+ .argument("<contract-id>", "Contract ID")
726
+ .option("--json", "Output as JSON", false)
727
+ .action((contractId, opts) => {
728
+ const reminders = listReminders(contractId);
729
+
730
+ if (opts.json) {
731
+ console.log(JSON.stringify(reminders, null, 2));
732
+ } else {
733
+ if (reminders.length === 0) {
734
+ console.log("No reminders found.");
735
+ return;
736
+ }
737
+ for (const r of reminders) {
738
+ const sent = r.sent ? " (sent)" : "";
739
+ console.log(` ${r.remind_at} — ${r.message}${sent}`);
740
+ }
741
+ }
742
+ });
743
+
744
+ // --- Stats ---
745
+
746
+ program
747
+ .command("stats")
748
+ .description("Show contract statistics")
749
+ .option("--json", "Output as JSON", false)
750
+ .action((opts) => {
751
+ const stats = getContractStats();
752
+
753
+ if (opts.json) {
754
+ console.log(JSON.stringify(stats, null, 2));
755
+ } else {
756
+ console.log(`Total contracts: ${stats.total}`);
757
+ console.log("\nBy status:");
758
+ for (const [status, count] of Object.entries(stats.by_status)) {
759
+ console.log(` ${status}: ${count}`);
760
+ }
761
+ console.log("\nBy type:");
762
+ for (const [type, count] of Object.entries(stats.by_type)) {
763
+ console.log(` ${type}: ${count}`);
764
+ }
765
+ console.log(`\nTotal active value: ${stats.total_value} USD`);
766
+ console.log(`Expiring within 30 days: ${stats.expiring_30_days}`);
767
+ }
768
+ });
769
+
770
+ program.parse(process.argv);