@hasna/microservices 0.0.2 → 0.0.4
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/bin/index.js +70 -0
- package/bin/mcp.js +71 -1
- package/dist/index.js +70 -0
- package/microservices/microservice-ads/package.json +27 -0
- package/microservices/microservice-ads/src/cli/index.ts +407 -0
- package/microservices/microservice-ads/src/db/campaigns.ts +493 -0
- package/microservices/microservice-ads/src/db/database.ts +93 -0
- package/microservices/microservice-ads/src/db/migrations.ts +60 -0
- package/microservices/microservice-ads/src/index.ts +39 -0
- package/microservices/microservice-ads/src/mcp/index.ts +320 -0
- package/microservices/microservice-contracts/package.json +27 -0
- package/microservices/microservice-contracts/src/cli/index.ts +383 -0
- package/microservices/microservice-contracts/src/db/contracts.ts +496 -0
- package/microservices/microservice-contracts/src/db/database.ts +93 -0
- package/microservices/microservice-contracts/src/db/migrations.ts +58 -0
- package/microservices/microservice-contracts/src/index.ts +43 -0
- package/microservices/microservice-contracts/src/mcp/index.ts +308 -0
- package/microservices/microservice-domains/package.json +27 -0
- package/microservices/microservice-domains/src/cli/index.ts +438 -0
- package/microservices/microservice-domains/src/db/database.ts +93 -0
- package/microservices/microservice-domains/src/db/domains.ts +551 -0
- package/microservices/microservice-domains/src/db/migrations.ts +60 -0
- package/microservices/microservice-domains/src/index.ts +44 -0
- package/microservices/microservice-domains/src/mcp/index.ts +368 -0
- package/microservices/microservice-hiring/package.json +27 -0
- package/microservices/microservice-hiring/src/cli/index.ts +431 -0
- package/microservices/microservice-hiring/src/db/database.ts +93 -0
- package/microservices/microservice-hiring/src/db/hiring.ts +582 -0
- package/microservices/microservice-hiring/src/db/migrations.ts +68 -0
- package/microservices/microservice-hiring/src/index.ts +51 -0
- package/microservices/microservice-hiring/src/mcp/index.ts +464 -0
- package/microservices/microservice-payments/package.json +27 -0
- package/microservices/microservice-payments/src/cli/index.ts +357 -0
- package/microservices/microservice-payments/src/db/database.ts +93 -0
- package/microservices/microservice-payments/src/db/migrations.ts +63 -0
- package/microservices/microservice-payments/src/db/payments.ts +652 -0
- package/microservices/microservice-payments/src/index.ts +51 -0
- package/microservices/microservice-payments/src/mcp/index.ts +460 -0
- package/microservices/microservice-payroll/package.json +27 -0
- package/microservices/microservice-payroll/src/cli/index.ts +374 -0
- package/microservices/microservice-payroll/src/db/database.ts +93 -0
- package/microservices/microservice-payroll/src/db/migrations.ts +69 -0
- package/microservices/microservice-payroll/src/db/payroll.ts +741 -0
- package/microservices/microservice-payroll/src/index.ts +48 -0
- package/microservices/microservice-payroll/src/mcp/index.ts +420 -0
- package/microservices/microservice-shipping/package.json +27 -0
- package/microservices/microservice-shipping/src/cli/index.ts +398 -0
- package/microservices/microservice-shipping/src/db/database.ts +93 -0
- package/microservices/microservice-shipping/src/db/migrations.ts +61 -0
- package/microservices/microservice-shipping/src/db/shipping.ts +643 -0
- package/microservices/microservice-shipping/src/index.ts +53 -0
- package/microservices/microservice-shipping/src/mcp/index.ts +385 -0
- package/microservices/microservice-social/package.json +27 -0
- package/microservices/microservice-social/src/cli/index.ts +447 -0
- package/microservices/microservice-social/src/db/database.ts +93 -0
- package/microservices/microservice-social/src/db/migrations.ts +55 -0
- package/microservices/microservice-social/src/db/social.ts +672 -0
- package/microservices/microservice-social/src/index.ts +46 -0
- package/microservices/microservice-social/src/mcp/index.ts +435 -0
- package/microservices/microservice-subscriptions/package.json +27 -0
- package/microservices/microservice-subscriptions/src/cli/index.ts +400 -0
- package/microservices/microservice-subscriptions/src/db/database.ts +93 -0
- package/microservices/microservice-subscriptions/src/db/migrations.ts +57 -0
- package/microservices/microservice-subscriptions/src/db/subscriptions.ts +692 -0
- package/microservices/microservice-subscriptions/src/index.ts +41 -0
- package/microservices/microservice-subscriptions/src/mcp/index.ts +365 -0
- package/microservices/microservice-transcriber/package.json +28 -0
- package/microservices/microservice-transcriber/src/cli/index.ts +1347 -0
- package/microservices/microservice-transcriber/src/db/annotations.ts +37 -0
- package/microservices/microservice-transcriber/src/db/database.ts +82 -0
- package/microservices/microservice-transcriber/src/db/migrations.ts +72 -0
- package/microservices/microservice-transcriber/src/db/transcripts.ts +395 -0
- package/microservices/microservice-transcriber/src/index.ts +43 -0
- package/microservices/microservice-transcriber/src/lib/config.ts +77 -0
- package/microservices/microservice-transcriber/src/lib/diff.ts +91 -0
- package/microservices/microservice-transcriber/src/lib/downloader.ts +570 -0
- package/microservices/microservice-transcriber/src/lib/feeds.ts +62 -0
- package/microservices/microservice-transcriber/src/lib/live.ts +94 -0
- package/microservices/microservice-transcriber/src/lib/notion.ts +129 -0
- package/microservices/microservice-transcriber/src/lib/providers.ts +713 -0
- package/microservices/microservice-transcriber/src/lib/summarizer.ts +147 -0
- package/microservices/microservice-transcriber/src/lib/translator.ts +75 -0
- package/microservices/microservice-transcriber/src/lib/webhook.ts +37 -0
- package/microservices/microservice-transcriber/src/mcp/index.ts +1070 -0
- package/microservices/microservice-transcriber/src/server/index.ts +199 -0
- package/package.json +1 -1
- package/microservices/microservice-invoices/dashboard/dist/assets/index-Bngq7FNM.css +0 -1
- package/microservices/microservice-invoices/dashboard/dist/assets/index-aHW4ARZR.js +0 -124
- package/microservices/microservice-invoices/dashboard/dist/index.html +0 -13
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import {
|
|
5
|
+
createPlan,
|
|
6
|
+
getPlan,
|
|
7
|
+
listPlans,
|
|
8
|
+
updatePlan,
|
|
9
|
+
deletePlan,
|
|
10
|
+
createSubscriber,
|
|
11
|
+
getSubscriber,
|
|
12
|
+
listSubscribers,
|
|
13
|
+
cancelSubscriber,
|
|
14
|
+
upgradeSubscriber,
|
|
15
|
+
downgradeSubscriber,
|
|
16
|
+
listEvents,
|
|
17
|
+
getMrr,
|
|
18
|
+
getArr,
|
|
19
|
+
getChurnRate,
|
|
20
|
+
listExpiring,
|
|
21
|
+
getSubscriberStats,
|
|
22
|
+
} from "../db/subscriptions.js";
|
|
23
|
+
|
|
24
|
+
const program = new Command();
|
|
25
|
+
|
|
26
|
+
program
|
|
27
|
+
.name("microservice-subscriptions")
|
|
28
|
+
.description("Subscription and recurring billing management microservice")
|
|
29
|
+
.version("0.0.1");
|
|
30
|
+
|
|
31
|
+
// --- Plans ---
|
|
32
|
+
|
|
33
|
+
const planCmd = program
|
|
34
|
+
.command("plan")
|
|
35
|
+
.description("Plan management");
|
|
36
|
+
|
|
37
|
+
planCmd
|
|
38
|
+
.command("create")
|
|
39
|
+
.description("Create a new plan")
|
|
40
|
+
.requiredOption("--name <name>", "Plan name")
|
|
41
|
+
.requiredOption("--price <price>", "Price")
|
|
42
|
+
.option("--interval <interval>", "Billing interval (monthly/yearly/lifetime)", "monthly")
|
|
43
|
+
.option("--features <features>", "Comma-separated features")
|
|
44
|
+
.option("--json", "Output as JSON", false)
|
|
45
|
+
.action((opts) => {
|
|
46
|
+
const plan = createPlan({
|
|
47
|
+
name: opts.name,
|
|
48
|
+
price: parseFloat(opts.price),
|
|
49
|
+
interval: opts.interval,
|
|
50
|
+
features: opts.features ? opts.features.split(",").map((f: string) => f.trim()) : undefined,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (opts.json) {
|
|
54
|
+
console.log(JSON.stringify(plan, null, 2));
|
|
55
|
+
} else {
|
|
56
|
+
console.log(`Created plan: ${plan.name} — $${plan.price}/${plan.interval} (${plan.id})`);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
planCmd
|
|
61
|
+
.command("list")
|
|
62
|
+
.description("List plans")
|
|
63
|
+
.option("--active", "Show active plans only")
|
|
64
|
+
.option("--interval <interval>", "Filter by interval")
|
|
65
|
+
.option("--json", "Output as JSON", false)
|
|
66
|
+
.action((opts) => {
|
|
67
|
+
const plans = listPlans({
|
|
68
|
+
active_only: opts.active,
|
|
69
|
+
interval: opts.interval,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (opts.json) {
|
|
73
|
+
console.log(JSON.stringify(plans, null, 2));
|
|
74
|
+
} else {
|
|
75
|
+
if (plans.length === 0) {
|
|
76
|
+
console.log("No plans found.");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
for (const p of plans) {
|
|
80
|
+
const status = p.active ? "" : " [inactive]";
|
|
81
|
+
console.log(` ${p.name} — $${p.price}/${p.interval}${status} (${p.id})`);
|
|
82
|
+
}
|
|
83
|
+
console.log(`\n${plans.length} plan(s)`);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
planCmd
|
|
88
|
+
.command("get")
|
|
89
|
+
.description("Get a plan by ID")
|
|
90
|
+
.argument("<id>", "Plan ID")
|
|
91
|
+
.option("--json", "Output as JSON", false)
|
|
92
|
+
.action((id, opts) => {
|
|
93
|
+
const plan = getPlan(id);
|
|
94
|
+
if (!plan) {
|
|
95
|
+
console.error(`Plan '${id}' not found.`);
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (opts.json) {
|
|
100
|
+
console.log(JSON.stringify(plan, null, 2));
|
|
101
|
+
} else {
|
|
102
|
+
console.log(`${plan.name}`);
|
|
103
|
+
console.log(` Price: $${plan.price}/${plan.interval}`);
|
|
104
|
+
console.log(` Active: ${plan.active}`);
|
|
105
|
+
if (plan.features.length) console.log(` Features: ${plan.features.join(", ")}`);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
planCmd
|
|
110
|
+
.command("update")
|
|
111
|
+
.description("Update a plan")
|
|
112
|
+
.argument("<id>", "Plan ID")
|
|
113
|
+
.option("--name <name>", "Plan name")
|
|
114
|
+
.option("--price <price>", "Price")
|
|
115
|
+
.option("--interval <interval>", "Billing interval")
|
|
116
|
+
.option("--features <features>", "Comma-separated features")
|
|
117
|
+
.option("--active <active>", "Active status (true/false)")
|
|
118
|
+
.option("--json", "Output as JSON", false)
|
|
119
|
+
.action((id, opts) => {
|
|
120
|
+
const input: Record<string, unknown> = {};
|
|
121
|
+
if (opts.name !== undefined) input.name = opts.name;
|
|
122
|
+
if (opts.price !== undefined) input.price = parseFloat(opts.price);
|
|
123
|
+
if (opts.interval !== undefined) input.interval = opts.interval;
|
|
124
|
+
if (opts.features !== undefined) input.features = opts.features.split(",").map((f: string) => f.trim());
|
|
125
|
+
if (opts.active !== undefined) input.active = opts.active === "true";
|
|
126
|
+
|
|
127
|
+
const plan = updatePlan(id, input);
|
|
128
|
+
if (!plan) {
|
|
129
|
+
console.error(`Plan '${id}' not found.`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (opts.json) {
|
|
134
|
+
console.log(JSON.stringify(plan, null, 2));
|
|
135
|
+
} else {
|
|
136
|
+
console.log(`Updated: ${plan.name} — $${plan.price}/${plan.interval}`);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// --- Subscribers ---
|
|
141
|
+
|
|
142
|
+
const subCmd = program
|
|
143
|
+
.command("subscriber")
|
|
144
|
+
.alias("sub")
|
|
145
|
+
.description("Subscriber management");
|
|
146
|
+
|
|
147
|
+
subCmd
|
|
148
|
+
.command("add")
|
|
149
|
+
.description("Add a new subscriber")
|
|
150
|
+
.requiredOption("--plan <id>", "Plan ID")
|
|
151
|
+
.requiredOption("--name <name>", "Customer name")
|
|
152
|
+
.requiredOption("--email <email>", "Customer email")
|
|
153
|
+
.option("--status <status>", "Initial status", "active")
|
|
154
|
+
.option("--trial-ends <date>", "Trial end date (YYYY-MM-DD)")
|
|
155
|
+
.option("--json", "Output as JSON", false)
|
|
156
|
+
.action((opts) => {
|
|
157
|
+
const subscriber = createSubscriber({
|
|
158
|
+
plan_id: opts.plan,
|
|
159
|
+
customer_name: opts.name,
|
|
160
|
+
customer_email: opts.email,
|
|
161
|
+
status: opts.status,
|
|
162
|
+
trial_ends_at: opts.trialEnds,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
if (opts.json) {
|
|
166
|
+
console.log(JSON.stringify(subscriber, null, 2));
|
|
167
|
+
} else {
|
|
168
|
+
console.log(`Added subscriber: ${subscriber.customer_name} <${subscriber.customer_email}> (${subscriber.id})`);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
subCmd
|
|
173
|
+
.command("list")
|
|
174
|
+
.description("List subscribers")
|
|
175
|
+
.option("--plan <id>", "Filter by plan ID")
|
|
176
|
+
.option("--status <status>", "Filter by status")
|
|
177
|
+
.option("--search <query>", "Search by name or email")
|
|
178
|
+
.option("--limit <n>", "Limit results")
|
|
179
|
+
.option("--json", "Output as JSON", false)
|
|
180
|
+
.action((opts) => {
|
|
181
|
+
const subscribers = listSubscribers({
|
|
182
|
+
plan_id: opts.plan,
|
|
183
|
+
status: opts.status,
|
|
184
|
+
search: opts.search,
|
|
185
|
+
limit: opts.limit ? parseInt(opts.limit) : undefined,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
if (opts.json) {
|
|
189
|
+
console.log(JSON.stringify(subscribers, null, 2));
|
|
190
|
+
} else {
|
|
191
|
+
if (subscribers.length === 0) {
|
|
192
|
+
console.log("No subscribers found.");
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
for (const s of subscribers) {
|
|
196
|
+
console.log(` ${s.customer_name} <${s.customer_email}> — ${s.status} (${s.id})`);
|
|
197
|
+
}
|
|
198
|
+
console.log(`\n${subscribers.length} subscriber(s)`);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
subCmd
|
|
203
|
+
.command("get")
|
|
204
|
+
.description("Get a subscriber by ID")
|
|
205
|
+
.argument("<id>", "Subscriber ID")
|
|
206
|
+
.option("--json", "Output as JSON", false)
|
|
207
|
+
.action((id, opts) => {
|
|
208
|
+
const subscriber = getSubscriber(id);
|
|
209
|
+
if (!subscriber) {
|
|
210
|
+
console.error(`Subscriber '${id}' not found.`);
|
|
211
|
+
process.exit(1);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (opts.json) {
|
|
215
|
+
console.log(JSON.stringify(subscriber, null, 2));
|
|
216
|
+
} else {
|
|
217
|
+
console.log(`${subscriber.customer_name} <${subscriber.customer_email}>`);
|
|
218
|
+
console.log(` Status: ${subscriber.status}`);
|
|
219
|
+
console.log(` Plan: ${subscriber.plan_id}`);
|
|
220
|
+
console.log(` Period: ${subscriber.current_period_start} — ${subscriber.current_period_end || "N/A"}`);
|
|
221
|
+
if (subscriber.canceled_at) console.log(` Canceled: ${subscriber.canceled_at}`);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
subCmd
|
|
226
|
+
.command("cancel")
|
|
227
|
+
.description("Cancel a subscription")
|
|
228
|
+
.argument("<id>", "Subscriber ID")
|
|
229
|
+
.option("--json", "Output as JSON", false)
|
|
230
|
+
.action((id, opts) => {
|
|
231
|
+
const subscriber = cancelSubscriber(id);
|
|
232
|
+
if (!subscriber) {
|
|
233
|
+
console.error(`Subscriber '${id}' not found.`);
|
|
234
|
+
process.exit(1);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (opts.json) {
|
|
238
|
+
console.log(JSON.stringify(subscriber, null, 2));
|
|
239
|
+
} else {
|
|
240
|
+
console.log(`Canceled subscription for ${subscriber.customer_name}`);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
subCmd
|
|
245
|
+
.command("upgrade")
|
|
246
|
+
.description("Upgrade a subscriber to a new plan")
|
|
247
|
+
.argument("<id>", "Subscriber ID")
|
|
248
|
+
.requiredOption("--plan <id>", "New plan ID")
|
|
249
|
+
.option("--json", "Output as JSON", false)
|
|
250
|
+
.action((id, opts) => {
|
|
251
|
+
const subscriber = upgradeSubscriber(id, opts.plan);
|
|
252
|
+
if (!subscriber) {
|
|
253
|
+
console.error(`Subscriber '${id}' not found or plan not found.`);
|
|
254
|
+
process.exit(1);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (opts.json) {
|
|
258
|
+
console.log(JSON.stringify(subscriber, null, 2));
|
|
259
|
+
} else {
|
|
260
|
+
console.log(`Upgraded ${subscriber.customer_name} to plan ${subscriber.plan_id}`);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
subCmd
|
|
265
|
+
.command("downgrade")
|
|
266
|
+
.description("Downgrade a subscriber to a new plan")
|
|
267
|
+
.argument("<id>", "Subscriber ID")
|
|
268
|
+
.requiredOption("--plan <id>", "New plan ID")
|
|
269
|
+
.option("--json", "Output as JSON", false)
|
|
270
|
+
.action((id, opts) => {
|
|
271
|
+
const subscriber = downgradeSubscriber(id, opts.plan);
|
|
272
|
+
if (!subscriber) {
|
|
273
|
+
console.error(`Subscriber '${id}' not found or plan not found.`);
|
|
274
|
+
process.exit(1);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (opts.json) {
|
|
278
|
+
console.log(JSON.stringify(subscriber, null, 2));
|
|
279
|
+
} else {
|
|
280
|
+
console.log(`Downgraded ${subscriber.customer_name} to plan ${subscriber.plan_id}`);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// --- Analytics ---
|
|
285
|
+
|
|
286
|
+
program
|
|
287
|
+
.command("mrr")
|
|
288
|
+
.description("Get monthly recurring revenue")
|
|
289
|
+
.option("--json", "Output as JSON", false)
|
|
290
|
+
.action((opts) => {
|
|
291
|
+
const mrr = getMrr();
|
|
292
|
+
if (opts.json) {
|
|
293
|
+
console.log(JSON.stringify({ mrr }));
|
|
294
|
+
} else {
|
|
295
|
+
console.log(`MRR: $${mrr.toFixed(2)}`);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
program
|
|
300
|
+
.command("arr")
|
|
301
|
+
.description("Get annual recurring revenue")
|
|
302
|
+
.option("--json", "Output as JSON", false)
|
|
303
|
+
.action((opts) => {
|
|
304
|
+
const arr = getArr();
|
|
305
|
+
if (opts.json) {
|
|
306
|
+
console.log(JSON.stringify({ arr }));
|
|
307
|
+
} else {
|
|
308
|
+
console.log(`ARR: $${arr.toFixed(2)}`);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
program
|
|
313
|
+
.command("churn")
|
|
314
|
+
.description("Get churn rate")
|
|
315
|
+
.option("--period <days>", "Period in days", "30")
|
|
316
|
+
.option("--json", "Output as JSON", false)
|
|
317
|
+
.action((opts) => {
|
|
318
|
+
const rate = getChurnRate(parseInt(opts.period));
|
|
319
|
+
if (opts.json) {
|
|
320
|
+
console.log(JSON.stringify({ churn_rate: rate, period_days: parseInt(opts.period) }));
|
|
321
|
+
} else {
|
|
322
|
+
console.log(`Churn rate (${opts.period}d): ${rate}%`);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
program
|
|
327
|
+
.command("events")
|
|
328
|
+
.description("List subscription events")
|
|
329
|
+
.option("--subscriber <id>", "Filter by subscriber ID")
|
|
330
|
+
.option("--type <type>", "Filter by event type")
|
|
331
|
+
.option("--limit <n>", "Limit results", "20")
|
|
332
|
+
.option("--json", "Output as JSON", false)
|
|
333
|
+
.action((opts) => {
|
|
334
|
+
const events = listEvents({
|
|
335
|
+
subscriber_id: opts.subscriber,
|
|
336
|
+
type: opts.type,
|
|
337
|
+
limit: parseInt(opts.limit),
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
if (opts.json) {
|
|
341
|
+
console.log(JSON.stringify(events, null, 2));
|
|
342
|
+
} else {
|
|
343
|
+
if (events.length === 0) {
|
|
344
|
+
console.log("No events found.");
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
for (const e of events) {
|
|
348
|
+
console.log(` [${e.occurred_at}] ${e.type} — subscriber: ${e.subscriber_id}`);
|
|
349
|
+
}
|
|
350
|
+
console.log(`\n${events.length} event(s)`);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
program
|
|
355
|
+
.command("expiring")
|
|
356
|
+
.description("List subscriptions expiring soon")
|
|
357
|
+
.option("--days <days>", "Days ahead to check", "7")
|
|
358
|
+
.option("--json", "Output as JSON", false)
|
|
359
|
+
.action((opts) => {
|
|
360
|
+
const expiring = listExpiring(parseInt(opts.days));
|
|
361
|
+
|
|
362
|
+
if (opts.json) {
|
|
363
|
+
console.log(JSON.stringify(expiring, null, 2));
|
|
364
|
+
} else {
|
|
365
|
+
if (expiring.length === 0) {
|
|
366
|
+
console.log(`No subscriptions expiring in the next ${opts.days} days.`);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
for (const s of expiring) {
|
|
370
|
+
console.log(` ${s.customer_name} <${s.customer_email}> — expires ${s.current_period_end}`);
|
|
371
|
+
}
|
|
372
|
+
console.log(`\n${expiring.length} expiring subscription(s)`);
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
program
|
|
377
|
+
.command("stats")
|
|
378
|
+
.description("Get subscriber statistics")
|
|
379
|
+
.option("--json", "Output as JSON", false)
|
|
380
|
+
.action((opts) => {
|
|
381
|
+
const stats = getSubscriberStats();
|
|
382
|
+
const mrr = getMrr();
|
|
383
|
+
const arr = getArr();
|
|
384
|
+
|
|
385
|
+
if (opts.json) {
|
|
386
|
+
console.log(JSON.stringify({ ...stats, mrr, arr }, null, 2));
|
|
387
|
+
} else {
|
|
388
|
+
console.log("Subscriber Statistics:");
|
|
389
|
+
console.log(` Total: ${stats.total}`);
|
|
390
|
+
console.log(` Active: ${stats.active}`);
|
|
391
|
+
console.log(` Trialing: ${stats.trialing}`);
|
|
392
|
+
console.log(` Past Due: ${stats.past_due}`);
|
|
393
|
+
console.log(` Canceled: ${stats.canceled}`);
|
|
394
|
+
console.log(` Expired: ${stats.expired}`);
|
|
395
|
+
console.log(` MRR: $${mrr.toFixed(2)}`);
|
|
396
|
+
console.log(` ARR: $${arr.toFixed(2)}`);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection for microservice-subscriptions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Database } from "bun:sqlite";
|
|
6
|
+
import { existsSync, mkdirSync } from "node:fs";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
import { MIGRATIONS } from "./migrations.js";
|
|
9
|
+
|
|
10
|
+
let _db: Database | null = null;
|
|
11
|
+
|
|
12
|
+
function getDbPath(): string {
|
|
13
|
+
// Environment variable override
|
|
14
|
+
if (process.env["MICROSERVICES_DIR"]) {
|
|
15
|
+
return join(process.env["MICROSERVICES_DIR"], "microservice-subscriptions", "data.db");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Check for .microservices in current or parent directories
|
|
19
|
+
let dir = resolve(process.cwd());
|
|
20
|
+
while (true) {
|
|
21
|
+
const candidate = join(dir, ".microservices", "microservice-subscriptions", "data.db");
|
|
22
|
+
const msDir = join(dir, ".microservices");
|
|
23
|
+
if (existsSync(msDir)) return candidate;
|
|
24
|
+
const parent = dirname(dir);
|
|
25
|
+
if (parent === dir) break;
|
|
26
|
+
dir = parent;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Global fallback
|
|
30
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
|
|
31
|
+
return join(home, ".microservices", "microservice-subscriptions", "data.db");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function ensureDir(filePath: string): void {
|
|
35
|
+
const dir = dirname(resolve(filePath));
|
|
36
|
+
if (!existsSync(dir)) {
|
|
37
|
+
mkdirSync(dir, { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getDatabase(): Database {
|
|
42
|
+
if (_db) return _db;
|
|
43
|
+
|
|
44
|
+
const dbPath = getDbPath();
|
|
45
|
+
ensureDir(dbPath);
|
|
46
|
+
|
|
47
|
+
_db = new Database(dbPath);
|
|
48
|
+
_db.exec("PRAGMA journal_mode = WAL");
|
|
49
|
+
_db.exec("PRAGMA foreign_keys = ON");
|
|
50
|
+
|
|
51
|
+
// Create migrations table
|
|
52
|
+
_db.exec(`
|
|
53
|
+
CREATE TABLE IF NOT EXISTS _migrations (
|
|
54
|
+
id INTEGER PRIMARY KEY,
|
|
55
|
+
name TEXT NOT NULL,
|
|
56
|
+
applied_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
57
|
+
)
|
|
58
|
+
`);
|
|
59
|
+
|
|
60
|
+
// Apply pending migrations
|
|
61
|
+
const applied = _db
|
|
62
|
+
.query("SELECT id FROM _migrations ORDER BY id")
|
|
63
|
+
.all() as { id: number }[];
|
|
64
|
+
const appliedIds = new Set(applied.map((r) => r.id));
|
|
65
|
+
|
|
66
|
+
for (const migration of MIGRATIONS) {
|
|
67
|
+
if (appliedIds.has(migration.id)) continue;
|
|
68
|
+
|
|
69
|
+
_db.exec("BEGIN");
|
|
70
|
+
try {
|
|
71
|
+
_db.exec(migration.sql);
|
|
72
|
+
_db.prepare("INSERT INTO _migrations (id, name) VALUES (?, ?)").run(
|
|
73
|
+
migration.id,
|
|
74
|
+
migration.name
|
|
75
|
+
);
|
|
76
|
+
_db.exec("COMMIT");
|
|
77
|
+
} catch (error) {
|
|
78
|
+
_db.exec("ROLLBACK");
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Migration ${migration.id} (${migration.name}) failed: ${error instanceof Error ? error.message : String(error)}`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return _db;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function closeDatabase(): void {
|
|
89
|
+
if (_db) {
|
|
90
|
+
_db.close();
|
|
91
|
+
_db = null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface MigrationEntry {
|
|
2
|
+
id: number;
|
|
3
|
+
name: string;
|
|
4
|
+
sql: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const MIGRATIONS: MigrationEntry[] = [
|
|
8
|
+
{
|
|
9
|
+
id: 1,
|
|
10
|
+
name: "initial_schema",
|
|
11
|
+
sql: `
|
|
12
|
+
CREATE TABLE IF NOT EXISTS plans (
|
|
13
|
+
id TEXT PRIMARY KEY,
|
|
14
|
+
name TEXT NOT NULL,
|
|
15
|
+
price REAL NOT NULL DEFAULT 0,
|
|
16
|
+
interval TEXT NOT NULL DEFAULT 'monthly' CHECK (interval IN ('monthly', 'yearly', 'lifetime')),
|
|
17
|
+
features TEXT NOT NULL DEFAULT '[]',
|
|
18
|
+
active INTEGER NOT NULL DEFAULT 1,
|
|
19
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
20
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
CREATE TABLE IF NOT EXISTS subscribers (
|
|
24
|
+
id TEXT PRIMARY KEY,
|
|
25
|
+
plan_id TEXT NOT NULL REFERENCES plans(id) ON DELETE RESTRICT,
|
|
26
|
+
customer_name TEXT NOT NULL,
|
|
27
|
+
customer_email TEXT NOT NULL,
|
|
28
|
+
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('trialing', 'active', 'past_due', 'canceled', 'expired')),
|
|
29
|
+
started_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
30
|
+
trial_ends_at TEXT,
|
|
31
|
+
current_period_start TEXT NOT NULL DEFAULT (datetime('now')),
|
|
32
|
+
current_period_end TEXT,
|
|
33
|
+
canceled_at TEXT,
|
|
34
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
35
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
36
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
40
|
+
id TEXT PRIMARY KEY,
|
|
41
|
+
subscriber_id TEXT NOT NULL REFERENCES subscribers(id) ON DELETE CASCADE,
|
|
42
|
+
type TEXT NOT NULL CHECK (type IN ('created', 'upgraded', 'downgraded', 'canceled', 'renewed', 'payment_failed')),
|
|
43
|
+
occurred_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
44
|
+
details TEXT NOT NULL DEFAULT '{}'
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
CREATE INDEX IF NOT EXISTS idx_plans_active ON plans(active);
|
|
48
|
+
CREATE INDEX IF NOT EXISTS idx_subscribers_plan ON subscribers(plan_id);
|
|
49
|
+
CREATE INDEX IF NOT EXISTS idx_subscribers_email ON subscribers(customer_email);
|
|
50
|
+
CREATE INDEX IF NOT EXISTS idx_subscribers_status ON subscribers(status);
|
|
51
|
+
CREATE INDEX IF NOT EXISTS idx_subscribers_period_end ON subscribers(current_period_end);
|
|
52
|
+
CREATE INDEX IF NOT EXISTS idx_events_subscriber ON events(subscriber_id);
|
|
53
|
+
CREATE INDEX IF NOT EXISTS idx_events_type ON events(type);
|
|
54
|
+
CREATE INDEX IF NOT EXISTS idx_events_occurred ON events(occurred_at);
|
|
55
|
+
`,
|
|
56
|
+
},
|
|
57
|
+
];
|