@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,715 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Command } from "commander";
4
+ import { readFileSync, writeFileSync } from "node:fs";
5
+ import {
6
+ createPlan,
7
+ getPlan,
8
+ listPlans,
9
+ updatePlan,
10
+ deletePlan,
11
+ createSubscriber,
12
+ getSubscriber,
13
+ listSubscribers,
14
+ cancelSubscriber,
15
+ upgradeSubscriber,
16
+ downgradeSubscriber,
17
+ listEvents,
18
+ getMrr,
19
+ getArr,
20
+ getChurnRate,
21
+ listExpiring,
22
+ getSubscriberStats,
23
+ pauseSubscriber,
24
+ resumeSubscriber,
25
+ extendTrial,
26
+ createDunning,
27
+ listDunning,
28
+ updateDunning,
29
+ bulkImportSubscribers,
30
+ exportSubscribers,
31
+ parseImportCsv,
32
+ getLtv,
33
+ getNrr,
34
+ getCohortReport,
35
+ comparePlans,
36
+ getExpiringRenewals,
37
+ } from "../db/subscriptions.js";
38
+
39
+ const program = new Command();
40
+
41
+ program
42
+ .name("microservice-subscriptions")
43
+ .description("Subscription and recurring billing management microservice")
44
+ .version("0.0.1");
45
+
46
+ // --- Plans ---
47
+
48
+ const planCmd = program
49
+ .command("plan")
50
+ .description("Plan management");
51
+
52
+ planCmd
53
+ .command("create")
54
+ .description("Create a new plan")
55
+ .requiredOption("--name <name>", "Plan name")
56
+ .requiredOption("--price <price>", "Price")
57
+ .option("--interval <interval>", "Billing interval (monthly/yearly/lifetime)", "monthly")
58
+ .option("--features <features>", "Comma-separated features")
59
+ .option("--json", "Output as JSON", false)
60
+ .action((opts) => {
61
+ const plan = createPlan({
62
+ name: opts.name,
63
+ price: parseFloat(opts.price),
64
+ interval: opts.interval,
65
+ features: opts.features ? opts.features.split(",").map((f: string) => f.trim()) : undefined,
66
+ });
67
+
68
+ if (opts.json) {
69
+ console.log(JSON.stringify(plan, null, 2));
70
+ } else {
71
+ console.log(`Created plan: ${plan.name} — $${plan.price}/${plan.interval} (${plan.id})`);
72
+ }
73
+ });
74
+
75
+ planCmd
76
+ .command("list")
77
+ .description("List plans")
78
+ .option("--active", "Show active plans only")
79
+ .option("--interval <interval>", "Filter by interval")
80
+ .option("--json", "Output as JSON", false)
81
+ .action((opts) => {
82
+ const plans = listPlans({
83
+ active_only: opts.active,
84
+ interval: opts.interval,
85
+ });
86
+
87
+ if (opts.json) {
88
+ console.log(JSON.stringify(plans, null, 2));
89
+ } else {
90
+ if (plans.length === 0) {
91
+ console.log("No plans found.");
92
+ return;
93
+ }
94
+ for (const p of plans) {
95
+ const status = p.active ? "" : " [inactive]";
96
+ console.log(` ${p.name} — $${p.price}/${p.interval}${status} (${p.id})`);
97
+ }
98
+ console.log(`\n${plans.length} plan(s)`);
99
+ }
100
+ });
101
+
102
+ planCmd
103
+ .command("get")
104
+ .description("Get a plan by ID")
105
+ .argument("<id>", "Plan ID")
106
+ .option("--json", "Output as JSON", false)
107
+ .action((id, opts) => {
108
+ const plan = getPlan(id);
109
+ if (!plan) {
110
+ console.error(`Plan '${id}' not found.`);
111
+ process.exit(1);
112
+ }
113
+
114
+ if (opts.json) {
115
+ console.log(JSON.stringify(plan, null, 2));
116
+ } else {
117
+ console.log(`${plan.name}`);
118
+ console.log(` Price: $${plan.price}/${plan.interval}`);
119
+ console.log(` Active: ${plan.active}`);
120
+ if (plan.features.length) console.log(` Features: ${plan.features.join(", ")}`);
121
+ }
122
+ });
123
+
124
+ planCmd
125
+ .command("update")
126
+ .description("Update a plan")
127
+ .argument("<id>", "Plan ID")
128
+ .option("--name <name>", "Plan name")
129
+ .option("--price <price>", "Price")
130
+ .option("--interval <interval>", "Billing interval")
131
+ .option("--features <features>", "Comma-separated features")
132
+ .option("--active <active>", "Active status (true/false)")
133
+ .option("--json", "Output as JSON", false)
134
+ .action((id, opts) => {
135
+ const input: Record<string, unknown> = {};
136
+ if (opts.name !== undefined) input.name = opts.name;
137
+ if (opts.price !== undefined) input.price = parseFloat(opts.price);
138
+ if (opts.interval !== undefined) input.interval = opts.interval;
139
+ if (opts.features !== undefined) input.features = opts.features.split(",").map((f: string) => f.trim());
140
+ if (opts.active !== undefined) input.active = opts.active === "true";
141
+
142
+ const plan = updatePlan(id, input);
143
+ if (!plan) {
144
+ console.error(`Plan '${id}' not found.`);
145
+ process.exit(1);
146
+ }
147
+
148
+ if (opts.json) {
149
+ console.log(JSON.stringify(plan, null, 2));
150
+ } else {
151
+ console.log(`Updated: ${plan.name} — $${plan.price}/${plan.interval}`);
152
+ }
153
+ });
154
+
155
+ // --- Subscribers ---
156
+
157
+ const subCmd = program
158
+ .command("subscriber")
159
+ .alias("sub")
160
+ .description("Subscriber management");
161
+
162
+ subCmd
163
+ .command("add")
164
+ .description("Add a new subscriber")
165
+ .requiredOption("--plan <id>", "Plan ID")
166
+ .requiredOption("--name <name>", "Customer name")
167
+ .requiredOption("--email <email>", "Customer email")
168
+ .option("--status <status>", "Initial status", "active")
169
+ .option("--trial-ends <date>", "Trial end date (YYYY-MM-DD)")
170
+ .option("--json", "Output as JSON", false)
171
+ .action((opts) => {
172
+ const subscriber = createSubscriber({
173
+ plan_id: opts.plan,
174
+ customer_name: opts.name,
175
+ customer_email: opts.email,
176
+ status: opts.status,
177
+ trial_ends_at: opts.trialEnds,
178
+ });
179
+
180
+ if (opts.json) {
181
+ console.log(JSON.stringify(subscriber, null, 2));
182
+ } else {
183
+ console.log(`Added subscriber: ${subscriber.customer_name} <${subscriber.customer_email}> (${subscriber.id})`);
184
+ }
185
+ });
186
+
187
+ subCmd
188
+ .command("list")
189
+ .description("List subscribers")
190
+ .option("--plan <id>", "Filter by plan ID")
191
+ .option("--status <status>", "Filter by status")
192
+ .option("--search <query>", "Search by name or email")
193
+ .option("--limit <n>", "Limit results")
194
+ .option("--json", "Output as JSON", false)
195
+ .action((opts) => {
196
+ const subscribers = listSubscribers({
197
+ plan_id: opts.plan,
198
+ status: opts.status,
199
+ search: opts.search,
200
+ limit: opts.limit ? parseInt(opts.limit) : undefined,
201
+ });
202
+
203
+ if (opts.json) {
204
+ console.log(JSON.stringify(subscribers, null, 2));
205
+ } else {
206
+ if (subscribers.length === 0) {
207
+ console.log("No subscribers found.");
208
+ return;
209
+ }
210
+ for (const s of subscribers) {
211
+ console.log(` ${s.customer_name} <${s.customer_email}> — ${s.status} (${s.id})`);
212
+ }
213
+ console.log(`\n${subscribers.length} subscriber(s)`);
214
+ }
215
+ });
216
+
217
+ subCmd
218
+ .command("get")
219
+ .description("Get a subscriber by ID")
220
+ .argument("<id>", "Subscriber ID")
221
+ .option("--json", "Output as JSON", false)
222
+ .action((id, opts) => {
223
+ const subscriber = getSubscriber(id);
224
+ if (!subscriber) {
225
+ console.error(`Subscriber '${id}' not found.`);
226
+ process.exit(1);
227
+ }
228
+
229
+ if (opts.json) {
230
+ console.log(JSON.stringify(subscriber, null, 2));
231
+ } else {
232
+ console.log(`${subscriber.customer_name} <${subscriber.customer_email}>`);
233
+ console.log(` Status: ${subscriber.status}`);
234
+ console.log(` Plan: ${subscriber.plan_id}`);
235
+ console.log(` Period: ${subscriber.current_period_start} — ${subscriber.current_period_end || "N/A"}`);
236
+ if (subscriber.canceled_at) console.log(` Canceled: ${subscriber.canceled_at}`);
237
+ }
238
+ });
239
+
240
+ subCmd
241
+ .command("cancel")
242
+ .description("Cancel a subscription")
243
+ .argument("<id>", "Subscriber ID")
244
+ .option("--json", "Output as JSON", false)
245
+ .action((id, opts) => {
246
+ const subscriber = cancelSubscriber(id);
247
+ if (!subscriber) {
248
+ console.error(`Subscriber '${id}' not found.`);
249
+ process.exit(1);
250
+ }
251
+
252
+ if (opts.json) {
253
+ console.log(JSON.stringify(subscriber, null, 2));
254
+ } else {
255
+ console.log(`Canceled subscription for ${subscriber.customer_name}`);
256
+ }
257
+ });
258
+
259
+ subCmd
260
+ .command("upgrade")
261
+ .description("Upgrade a subscriber to a new plan")
262
+ .argument("<id>", "Subscriber ID")
263
+ .requiredOption("--plan <id>", "New plan ID")
264
+ .option("--json", "Output as JSON", false)
265
+ .action((id, opts) => {
266
+ const subscriber = upgradeSubscriber(id, opts.plan);
267
+ if (!subscriber) {
268
+ console.error(`Subscriber '${id}' not found or plan not found.`);
269
+ process.exit(1);
270
+ }
271
+
272
+ if (opts.json) {
273
+ console.log(JSON.stringify(subscriber, null, 2));
274
+ } else {
275
+ console.log(`Upgraded ${subscriber.customer_name} to plan ${subscriber.plan_id}`);
276
+ }
277
+ });
278
+
279
+ subCmd
280
+ .command("downgrade")
281
+ .description("Downgrade a subscriber to a new plan")
282
+ .argument("<id>", "Subscriber ID")
283
+ .requiredOption("--plan <id>", "New plan ID")
284
+ .option("--json", "Output as JSON", false)
285
+ .action((id, opts) => {
286
+ const subscriber = downgradeSubscriber(id, opts.plan);
287
+ if (!subscriber) {
288
+ console.error(`Subscriber '${id}' not found or plan not found.`);
289
+ process.exit(1);
290
+ }
291
+
292
+ if (opts.json) {
293
+ console.log(JSON.stringify(subscriber, null, 2));
294
+ } else {
295
+ console.log(`Downgraded ${subscriber.customer_name} to plan ${subscriber.plan_id}`);
296
+ }
297
+ });
298
+
299
+ // --- Pause / Resume ---
300
+
301
+ subCmd
302
+ .command("pause")
303
+ .description("Pause a subscription")
304
+ .argument("<id>", "Subscriber ID")
305
+ .option("--resume-date <date>", "Scheduled resume date (YYYY-MM-DD)")
306
+ .option("--json", "Output as JSON", false)
307
+ .action((id, opts) => {
308
+ const subscriber = pauseSubscriber(id, opts.resumeDate);
309
+ if (!subscriber) {
310
+ console.error(`Subscriber '${id}' not found or cannot be paused.`);
311
+ process.exit(1);
312
+ }
313
+
314
+ if (opts.json) {
315
+ console.log(JSON.stringify(subscriber, null, 2));
316
+ } else {
317
+ console.log(`Paused subscription for ${subscriber.customer_name}`);
318
+ if (subscriber.resume_at) console.log(` Scheduled resume: ${subscriber.resume_at}`);
319
+ }
320
+ });
321
+
322
+ subCmd
323
+ .command("resume")
324
+ .description("Resume a paused subscription")
325
+ .argument("<id>", "Subscriber ID")
326
+ .option("--json", "Output as JSON", false)
327
+ .action((id, opts) => {
328
+ const subscriber = resumeSubscriber(id);
329
+ if (!subscriber) {
330
+ console.error(`Subscriber '${id}' not found or not paused.`);
331
+ process.exit(1);
332
+ }
333
+
334
+ if (opts.json) {
335
+ console.log(JSON.stringify(subscriber, null, 2));
336
+ } else {
337
+ console.log(`Resumed subscription for ${subscriber.customer_name}`);
338
+ }
339
+ });
340
+
341
+ // --- Trial Extension ---
342
+
343
+ subCmd
344
+ .command("extend-trial")
345
+ .description("Extend a subscriber's trial period")
346
+ .argument("<id>", "Subscriber ID")
347
+ .requiredOption("--days <days>", "Number of days to extend")
348
+ .option("--json", "Output as JSON", false)
349
+ .action((id, opts) => {
350
+ const subscriber = extendTrial(id, parseInt(opts.days));
351
+ if (!subscriber) {
352
+ console.error(`Subscriber '${id}' not found.`);
353
+ process.exit(1);
354
+ }
355
+
356
+ if (opts.json) {
357
+ console.log(JSON.stringify(subscriber, null, 2));
358
+ } else {
359
+ console.log(`Extended trial for ${subscriber.customer_name} by ${opts.days} days`);
360
+ console.log(` New trial end: ${subscriber.trial_ends_at}`);
361
+ }
362
+ });
363
+
364
+ // --- Bulk Import/Export ---
365
+
366
+ subCmd
367
+ .command("import")
368
+ .description("Bulk import subscribers from a CSV file")
369
+ .requiredOption("--file <path>", "Path to CSV file")
370
+ .option("--json", "Output as JSON", false)
371
+ .action((opts) => {
372
+ const csvContent = readFileSync(opts.file, "utf-8");
373
+ const data = parseImportCsv(csvContent);
374
+ const imported = bulkImportSubscribers(data);
375
+
376
+ if (opts.json) {
377
+ console.log(JSON.stringify({ imported: imported.length, subscribers: imported }, null, 2));
378
+ } else {
379
+ console.log(`Imported ${imported.length} subscriber(s) from ${opts.file}`);
380
+ for (const s of imported) {
381
+ console.log(` ${s.customer_name} <${s.customer_email}> (${s.id})`);
382
+ }
383
+ }
384
+ });
385
+
386
+ subCmd
387
+ .command("export")
388
+ .description("Export subscribers")
389
+ .option("--format <format>", "Output format (csv/json)", "csv")
390
+ .option("--file <path>", "Output file path (prints to stdout if omitted)")
391
+ .action((opts) => {
392
+ const output = exportSubscribers(opts.format as "csv" | "json");
393
+ if (opts.file) {
394
+ writeFileSync(opts.file, output, "utf-8");
395
+ console.log(`Exported to ${opts.file}`);
396
+ } else {
397
+ console.log(output);
398
+ }
399
+ });
400
+
401
+ // --- Dunning ---
402
+
403
+ const dunningCmd = program
404
+ .command("dunning")
405
+ .description("Dunning attempt management");
406
+
407
+ dunningCmd
408
+ .command("list")
409
+ .description("List dunning attempts")
410
+ .option("--subscriber <id>", "Filter by subscriber ID")
411
+ .option("--status <status>", "Filter by status")
412
+ .option("--limit <n>", "Limit results", "20")
413
+ .option("--json", "Output as JSON", false)
414
+ .action((opts) => {
415
+ const attempts = listDunning({
416
+ subscriber_id: opts.subscriber,
417
+ status: opts.status,
418
+ limit: parseInt(opts.limit),
419
+ });
420
+
421
+ if (opts.json) {
422
+ console.log(JSON.stringify(attempts, null, 2));
423
+ } else {
424
+ if (attempts.length === 0) {
425
+ console.log("No dunning attempts found.");
426
+ return;
427
+ }
428
+ for (const a of attempts) {
429
+ console.log(` [${a.created_at}] #${a.attempt_number} ${a.status} — subscriber: ${a.subscriber_id}`);
430
+ if (a.next_retry_at) console.log(` Next retry: ${a.next_retry_at}`);
431
+ }
432
+ console.log(`\n${attempts.length} attempt(s)`);
433
+ }
434
+ });
435
+
436
+ dunningCmd
437
+ .command("create")
438
+ .description("Create a dunning attempt")
439
+ .requiredOption("--subscriber <id>", "Subscriber ID")
440
+ .option("--attempt <n>", "Attempt number", "1")
441
+ .option("--status <status>", "Status", "pending")
442
+ .option("--next-retry <date>", "Next retry date")
443
+ .option("--json", "Output as JSON", false)
444
+ .action((opts) => {
445
+ const attempt = createDunning({
446
+ subscriber_id: opts.subscriber,
447
+ attempt_number: parseInt(opts.attempt),
448
+ status: opts.status,
449
+ next_retry_at: opts.nextRetry,
450
+ });
451
+
452
+ if (opts.json) {
453
+ console.log(JSON.stringify(attempt, null, 2));
454
+ } else {
455
+ console.log(`Created dunning attempt #${attempt.attempt_number} for subscriber ${attempt.subscriber_id}`);
456
+ }
457
+ });
458
+
459
+ dunningCmd
460
+ .command("update")
461
+ .description("Update a dunning attempt")
462
+ .argument("<id>", "Dunning attempt ID")
463
+ .option("--status <status>", "New status")
464
+ .option("--next-retry <date>", "Next retry date")
465
+ .option("--json", "Output as JSON", false)
466
+ .action((id, opts) => {
467
+ const input: Record<string, unknown> = {};
468
+ if (opts.status) input.status = opts.status;
469
+ if (opts.nextRetry !== undefined) input.next_retry_at = opts.nextRetry;
470
+
471
+ const attempt = updateDunning(id, input);
472
+ if (!attempt) {
473
+ console.error(`Dunning attempt '${id}' not found.`);
474
+ process.exit(1);
475
+ }
476
+
477
+ if (opts.json) {
478
+ console.log(JSON.stringify(attempt, null, 2));
479
+ } else {
480
+ console.log(`Updated dunning attempt ${attempt.id} — status: ${attempt.status}`);
481
+ }
482
+ });
483
+
484
+ // --- Analytics ---
485
+
486
+ program
487
+ .command("ltv")
488
+ .description("Show lifetime value per subscriber and average")
489
+ .option("--json", "Output as JSON", false)
490
+ .action((opts) => {
491
+ const result = getLtv();
492
+
493
+ if (opts.json) {
494
+ console.log(JSON.stringify(result, null, 2));
495
+ } else {
496
+ if (result.subscribers.length === 0) {
497
+ console.log("No subscribers found.");
498
+ return;
499
+ }
500
+ console.log("Lifetime Value Report:");
501
+ for (const s of result.subscribers) {
502
+ console.log(` ${s.customer_name} <${s.customer_email}> — $${s.ltv.toFixed(2)} (${s.months_active}mo on ${s.plan_name})`);
503
+ }
504
+ console.log(`\nAverage LTV: $${result.average_ltv.toFixed(2)}`);
505
+ }
506
+ });
507
+
508
+ program
509
+ .command("nrr")
510
+ .description("Calculate net revenue retention for a month")
511
+ .requiredOption("--month <month>", "Month in YYYY-MM format")
512
+ .option("--json", "Output as JSON", false)
513
+ .action((opts) => {
514
+ const result = getNrr(opts.month);
515
+
516
+ if (opts.json) {
517
+ console.log(JSON.stringify(result, null, 2));
518
+ } else {
519
+ console.log(`NRR for ${result.month}:`);
520
+ console.log(` Start MRR: $${result.start_mrr.toFixed(2)}`);
521
+ console.log(` Expansion: +$${result.expansion.toFixed(2)}`);
522
+ console.log(` Contraction: -$${result.contraction.toFixed(2)}`);
523
+ console.log(` Churn: -$${result.churn.toFixed(2)}`);
524
+ console.log(` NRR: ${result.nrr.toFixed(2)}%`);
525
+ }
526
+ });
527
+
528
+ program
529
+ .command("cohort-report")
530
+ .description("Show cohort retention analysis")
531
+ .option("--months <n>", "Number of months to analyze", "6")
532
+ .option("--json", "Output as JSON", false)
533
+ .action((opts) => {
534
+ const report = getCohortReport(parseInt(opts.months));
535
+
536
+ if (opts.json) {
537
+ console.log(JSON.stringify(report, null, 2));
538
+ } else {
539
+ if (report.length === 0) {
540
+ console.log("No cohort data available.");
541
+ return;
542
+ }
543
+ console.log("Cohort Retention Report:");
544
+ console.log(" Cohort | Total | Retained | Retention");
545
+ console.log(" -----------|-------|----------|----------");
546
+ for (const c of report) {
547
+ console.log(` ${c.cohort} | ${String(c.total).padStart(5)} | ${String(c.retained).padStart(8)} | ${c.retention_rate.toFixed(1)}%`);
548
+ }
549
+ }
550
+ });
551
+
552
+ planCmd
553
+ .command("compare")
554
+ .description("Compare two plans side by side")
555
+ .argument("<id1>", "First plan ID")
556
+ .argument("<id2>", "Second plan ID")
557
+ .option("--json", "Output as JSON", false)
558
+ .action((id1, id2, opts) => {
559
+ const result = comparePlans(id1, id2);
560
+ if (!result) {
561
+ console.error("One or both plans not found.");
562
+ process.exit(1);
563
+ }
564
+
565
+ if (opts.json) {
566
+ console.log(JSON.stringify(result, null, 2));
567
+ } else {
568
+ console.log(`Plan Comparison:`);
569
+ console.log(` ${result.plan1.name} vs ${result.plan2.name}`);
570
+ console.log(` Price: $${result.plan1.price}/${result.plan1.interval} vs $${result.plan2.price}/${result.plan2.interval}`);
571
+ console.log(` Price diff: $${result.price_diff} (${result.price_diff_pct > 0 ? "+" : ""}${result.price_diff_pct}%)`);
572
+ console.log(` Interval match: ${result.interval_match ? "Yes" : "No"}`);
573
+ if (result.common_features.length) console.log(` Common features: ${result.common_features.join(", ")}`);
574
+ if (result.features_only_in_plan1.length) console.log(` Only in ${result.plan1.name}: ${result.features_only_in_plan1.join(", ")}`);
575
+ if (result.features_only_in_plan2.length) console.log(` Only in ${result.plan2.name}: ${result.features_only_in_plan2.join(", ")}`);
576
+ }
577
+ });
578
+
579
+ program
580
+ .command("expiring-renewals")
581
+ .description("List subscribers with renewals expiring soon")
582
+ .option("--days <days>", "Days ahead to check", "7")
583
+ .option("--json", "Output as JSON", false)
584
+ .action((opts) => {
585
+ const expiring = getExpiringRenewals(parseInt(opts.days));
586
+
587
+ if (opts.json) {
588
+ console.log(JSON.stringify(expiring, null, 2));
589
+ } else {
590
+ if (expiring.length === 0) {
591
+ console.log(`No renewals expiring in the next ${opts.days} days.`);
592
+ return;
593
+ }
594
+ for (const s of expiring) {
595
+ console.log(` ${s.customer_name} <${s.customer_email}> — renews ${s.current_period_end}`);
596
+ }
597
+ console.log(`\n${expiring.length} upcoming renewal(s)`);
598
+ }
599
+ });
600
+
601
+ program
602
+ .command("mrr")
603
+ .description("Get monthly recurring revenue")
604
+ .option("--json", "Output as JSON", false)
605
+ .action((opts) => {
606
+ const mrr = getMrr();
607
+ if (opts.json) {
608
+ console.log(JSON.stringify({ mrr }));
609
+ } else {
610
+ console.log(`MRR: $${mrr.toFixed(2)}`);
611
+ }
612
+ });
613
+
614
+ program
615
+ .command("arr")
616
+ .description("Get annual recurring revenue")
617
+ .option("--json", "Output as JSON", false)
618
+ .action((opts) => {
619
+ const arr = getArr();
620
+ if (opts.json) {
621
+ console.log(JSON.stringify({ arr }));
622
+ } else {
623
+ console.log(`ARR: $${arr.toFixed(2)}`);
624
+ }
625
+ });
626
+
627
+ program
628
+ .command("churn")
629
+ .description("Get churn rate")
630
+ .option("--period <days>", "Period in days", "30")
631
+ .option("--json", "Output as JSON", false)
632
+ .action((opts) => {
633
+ const rate = getChurnRate(parseInt(opts.period));
634
+ if (opts.json) {
635
+ console.log(JSON.stringify({ churn_rate: rate, period_days: parseInt(opts.period) }));
636
+ } else {
637
+ console.log(`Churn rate (${opts.period}d): ${rate}%`);
638
+ }
639
+ });
640
+
641
+ program
642
+ .command("events")
643
+ .description("List subscription events")
644
+ .option("--subscriber <id>", "Filter by subscriber ID")
645
+ .option("--type <type>", "Filter by event type")
646
+ .option("--limit <n>", "Limit results", "20")
647
+ .option("--json", "Output as JSON", false)
648
+ .action((opts) => {
649
+ const events = listEvents({
650
+ subscriber_id: opts.subscriber,
651
+ type: opts.type,
652
+ limit: parseInt(opts.limit),
653
+ });
654
+
655
+ if (opts.json) {
656
+ console.log(JSON.stringify(events, null, 2));
657
+ } else {
658
+ if (events.length === 0) {
659
+ console.log("No events found.");
660
+ return;
661
+ }
662
+ for (const e of events) {
663
+ console.log(` [${e.occurred_at}] ${e.type} — subscriber: ${e.subscriber_id}`);
664
+ }
665
+ console.log(`\n${events.length} event(s)`);
666
+ }
667
+ });
668
+
669
+ program
670
+ .command("expiring")
671
+ .description("List subscriptions expiring soon")
672
+ .option("--days <days>", "Days ahead to check", "7")
673
+ .option("--json", "Output as JSON", false)
674
+ .action((opts) => {
675
+ const expiring = listExpiring(parseInt(opts.days));
676
+
677
+ if (opts.json) {
678
+ console.log(JSON.stringify(expiring, null, 2));
679
+ } else {
680
+ if (expiring.length === 0) {
681
+ console.log(`No subscriptions expiring in the next ${opts.days} days.`);
682
+ return;
683
+ }
684
+ for (const s of expiring) {
685
+ console.log(` ${s.customer_name} <${s.customer_email}> — expires ${s.current_period_end}`);
686
+ }
687
+ console.log(`\n${expiring.length} expiring subscription(s)`);
688
+ }
689
+ });
690
+
691
+ program
692
+ .command("stats")
693
+ .description("Get subscriber statistics")
694
+ .option("--json", "Output as JSON", false)
695
+ .action((opts) => {
696
+ const stats = getSubscriberStats();
697
+ const mrr = getMrr();
698
+ const arr = getArr();
699
+
700
+ if (opts.json) {
701
+ console.log(JSON.stringify({ ...stats, mrr, arr }, null, 2));
702
+ } else {
703
+ console.log("Subscriber Statistics:");
704
+ console.log(` Total: ${stats.total}`);
705
+ console.log(` Active: ${stats.active}`);
706
+ console.log(` Trialing: ${stats.trialing}`);
707
+ console.log(` Past Due: ${stats.past_due}`);
708
+ console.log(` Canceled: ${stats.canceled}`);
709
+ console.log(` Expired: ${stats.expired}`);
710
+ console.log(` MRR: $${mrr.toFixed(2)}`);
711
+ console.log(` ARR: $${arr.toFixed(2)}`);
712
+ }
713
+ });
714
+
715
+ program.parse(process.argv);