@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,357 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import {
|
|
5
|
+
createPayment,
|
|
6
|
+
getPayment,
|
|
7
|
+
listPayments,
|
|
8
|
+
updatePayment,
|
|
9
|
+
deletePayment,
|
|
10
|
+
refundPayment,
|
|
11
|
+
searchPayments,
|
|
12
|
+
getRevenueReport,
|
|
13
|
+
getRevenueByCustomer,
|
|
14
|
+
reconcileWithInvoice,
|
|
15
|
+
getPaymentStats,
|
|
16
|
+
getBalanceByProvider,
|
|
17
|
+
type PaymentType,
|
|
18
|
+
type PaymentStatus,
|
|
19
|
+
type PaymentProvider,
|
|
20
|
+
} from "../db/payments.js";
|
|
21
|
+
import {
|
|
22
|
+
listDisputes,
|
|
23
|
+
respondDispute,
|
|
24
|
+
type DisputeStatus,
|
|
25
|
+
} from "../db/payments.js";
|
|
26
|
+
import {
|
|
27
|
+
createPayout,
|
|
28
|
+
listPayouts,
|
|
29
|
+
type PayoutStatus,
|
|
30
|
+
} from "../db/payments.js";
|
|
31
|
+
|
|
32
|
+
const program = new Command();
|
|
33
|
+
|
|
34
|
+
program
|
|
35
|
+
.name("microservice-payments")
|
|
36
|
+
.description("Payment processing and tracking microservice")
|
|
37
|
+
.version("0.0.1");
|
|
38
|
+
|
|
39
|
+
// --- Payments ---
|
|
40
|
+
|
|
41
|
+
const paymentCmd = program
|
|
42
|
+
.command("payment")
|
|
43
|
+
.description("Payment management");
|
|
44
|
+
|
|
45
|
+
paymentCmd
|
|
46
|
+
.command("create")
|
|
47
|
+
.description("Create a new payment")
|
|
48
|
+
.requiredOption("--type <type>", "Payment type (charge/refund/transfer/payout)")
|
|
49
|
+
.requiredOption("--amount <amount>", "Amount")
|
|
50
|
+
.option("--currency <currency>", "Currency code", "USD")
|
|
51
|
+
.option("--status <status>", "Status")
|
|
52
|
+
.option("--customer-name <name>", "Customer name")
|
|
53
|
+
.option("--customer-email <email>", "Customer email")
|
|
54
|
+
.option("--description <desc>", "Description")
|
|
55
|
+
.option("--provider <provider>", "Provider (stripe/square/mercury/manual)")
|
|
56
|
+
.option("--provider-id <id>", "Provider transaction ID")
|
|
57
|
+
.option("--invoice-id <id>", "Invoice ID")
|
|
58
|
+
.option("--json", "Output as JSON", false)
|
|
59
|
+
.action((opts) => {
|
|
60
|
+
const payment = createPayment({
|
|
61
|
+
type: opts.type as PaymentType,
|
|
62
|
+
amount: parseFloat(opts.amount),
|
|
63
|
+
currency: opts.currency,
|
|
64
|
+
status: opts.status as PaymentStatus | undefined,
|
|
65
|
+
customer_name: opts.customerName,
|
|
66
|
+
customer_email: opts.customerEmail,
|
|
67
|
+
description: opts.description,
|
|
68
|
+
provider: opts.provider as PaymentProvider | undefined,
|
|
69
|
+
provider_id: opts.providerId,
|
|
70
|
+
invoice_id: opts.invoiceId,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
if (opts.json) {
|
|
74
|
+
console.log(JSON.stringify(payment, null, 2));
|
|
75
|
+
} else {
|
|
76
|
+
console.log(`Created payment: ${payment.type} $${payment.amount} ${payment.currency} (${payment.id})`);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
paymentCmd
|
|
81
|
+
.command("list")
|
|
82
|
+
.description("List payments")
|
|
83
|
+
.option("--status <status>", "Filter by status")
|
|
84
|
+
.option("--type <type>", "Filter by type")
|
|
85
|
+
.option("--provider <provider>", "Filter by provider")
|
|
86
|
+
.option("--limit <n>", "Limit results")
|
|
87
|
+
.option("--json", "Output as JSON", false)
|
|
88
|
+
.action((opts) => {
|
|
89
|
+
const payments = listPayments({
|
|
90
|
+
status: opts.status as PaymentStatus | undefined,
|
|
91
|
+
type: opts.type as PaymentType | undefined,
|
|
92
|
+
provider: opts.provider as PaymentProvider | undefined,
|
|
93
|
+
limit: opts.limit ? parseInt(opts.limit) : undefined,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (opts.json) {
|
|
97
|
+
console.log(JSON.stringify(payments, null, 2));
|
|
98
|
+
} else {
|
|
99
|
+
if (payments.length === 0) {
|
|
100
|
+
console.log("No payments found.");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
for (const p of payments) {
|
|
104
|
+
const customer = p.customer_name || p.customer_email || "—";
|
|
105
|
+
console.log(` ${p.type} $${p.amount} ${p.currency} [${p.status}] ${customer} (${p.id})`);
|
|
106
|
+
}
|
|
107
|
+
console.log(`\n${payments.length} payment(s)`);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
paymentCmd
|
|
112
|
+
.command("get")
|
|
113
|
+
.description("Get a payment by ID")
|
|
114
|
+
.argument("<id>", "Payment ID")
|
|
115
|
+
.option("--json", "Output as JSON", false)
|
|
116
|
+
.action((id, opts) => {
|
|
117
|
+
const payment = getPayment(id);
|
|
118
|
+
if (!payment) {
|
|
119
|
+
console.error(`Payment '${id}' not found.`);
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (opts.json) {
|
|
124
|
+
console.log(JSON.stringify(payment, null, 2));
|
|
125
|
+
} else {
|
|
126
|
+
console.log(`${payment.type} — $${payment.amount} ${payment.currency}`);
|
|
127
|
+
console.log(` Status: ${payment.status}`);
|
|
128
|
+
if (payment.customer_name) console.log(` Customer: ${payment.customer_name}`);
|
|
129
|
+
if (payment.customer_email) console.log(` Email: ${payment.customer_email}`);
|
|
130
|
+
if (payment.description) console.log(` Description: ${payment.description}`);
|
|
131
|
+
if (payment.provider) console.log(` Provider: ${payment.provider}`);
|
|
132
|
+
if (payment.invoice_id) console.log(` Invoice: ${payment.invoice_id}`);
|
|
133
|
+
console.log(` Created: ${payment.created_at}`);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
paymentCmd
|
|
138
|
+
.command("refund")
|
|
139
|
+
.description("Refund a payment")
|
|
140
|
+
.argument("<id>", "Payment ID to refund")
|
|
141
|
+
.option("--amount <amount>", "Partial refund amount")
|
|
142
|
+
.option("--json", "Output as JSON", false)
|
|
143
|
+
.action((id, opts) => {
|
|
144
|
+
const refund = refundPayment(id, opts.amount ? parseFloat(opts.amount) : undefined);
|
|
145
|
+
if (!refund) {
|
|
146
|
+
console.error(`Cannot refund payment '${id}' — not found or not succeeded.`);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (opts.json) {
|
|
151
|
+
console.log(JSON.stringify(refund, null, 2));
|
|
152
|
+
} else {
|
|
153
|
+
console.log(`Refunded $${refund.amount} ${refund.currency} (${refund.id})`);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// --- Disputes ---
|
|
158
|
+
|
|
159
|
+
const disputeCmd = program
|
|
160
|
+
.command("dispute")
|
|
161
|
+
.description("Dispute management");
|
|
162
|
+
|
|
163
|
+
disputeCmd
|
|
164
|
+
.command("list")
|
|
165
|
+
.description("List disputes")
|
|
166
|
+
.option("--status <status>", "Filter by status (open/under_review/won/lost)")
|
|
167
|
+
.option("--json", "Output as JSON", false)
|
|
168
|
+
.action((opts) => {
|
|
169
|
+
const disputes = listDisputes(opts.status as DisputeStatus | undefined);
|
|
170
|
+
|
|
171
|
+
if (opts.json) {
|
|
172
|
+
console.log(JSON.stringify(disputes, null, 2));
|
|
173
|
+
} else {
|
|
174
|
+
if (disputes.length === 0) {
|
|
175
|
+
console.log("No disputes found.");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
for (const d of disputes) {
|
|
179
|
+
const amount = d.amount != null ? ` $${d.amount}` : "";
|
|
180
|
+
console.log(` [${d.status}]${amount} — ${d.reason || "No reason"} (${d.id})`);
|
|
181
|
+
}
|
|
182
|
+
console.log(`\n${disputes.length} dispute(s)`);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
disputeCmd
|
|
187
|
+
.command("respond")
|
|
188
|
+
.description("Respond to a dispute")
|
|
189
|
+
.argument("<id>", "Dispute ID")
|
|
190
|
+
.requiredOption("--status <status>", "New status (open/under_review/won/lost)")
|
|
191
|
+
.option("--json", "Output as JSON", false)
|
|
192
|
+
.action((id, opts) => {
|
|
193
|
+
const dispute = respondDispute(id, { status: opts.status as DisputeStatus });
|
|
194
|
+
if (!dispute) {
|
|
195
|
+
console.error(`Dispute '${id}' not found.`);
|
|
196
|
+
process.exit(1);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (opts.json) {
|
|
200
|
+
console.log(JSON.stringify(dispute, null, 2));
|
|
201
|
+
} else {
|
|
202
|
+
console.log(`Dispute ${id} updated to: ${dispute.status}`);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// --- Payouts ---
|
|
207
|
+
|
|
208
|
+
const payoutCmd = program
|
|
209
|
+
.command("payout")
|
|
210
|
+
.description("Payout management");
|
|
211
|
+
|
|
212
|
+
payoutCmd
|
|
213
|
+
.command("list")
|
|
214
|
+
.description("List payouts")
|
|
215
|
+
.option("--status <status>", "Filter by status (pending/in_transit/paid/failed)")
|
|
216
|
+
.option("--json", "Output as JSON", false)
|
|
217
|
+
.action((opts) => {
|
|
218
|
+
const payouts = listPayouts(opts.status as PayoutStatus | undefined);
|
|
219
|
+
|
|
220
|
+
if (opts.json) {
|
|
221
|
+
console.log(JSON.stringify(payouts, null, 2));
|
|
222
|
+
} else {
|
|
223
|
+
if (payouts.length === 0) {
|
|
224
|
+
console.log("No payouts found.");
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
for (const p of payouts) {
|
|
228
|
+
const dest = p.destination || "—";
|
|
229
|
+
console.log(` $${p.amount} ${p.currency} [${p.status}] -> ${dest} (${p.id})`);
|
|
230
|
+
}
|
|
231
|
+
console.log(`\n${payouts.length} payout(s)`);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
payoutCmd
|
|
236
|
+
.command("initiate")
|
|
237
|
+
.description("Initiate a new payout")
|
|
238
|
+
.requiredOption("--amount <amount>", "Amount")
|
|
239
|
+
.option("--currency <currency>", "Currency code", "USD")
|
|
240
|
+
.option("--destination <dest>", "Destination (bank account, etc.)")
|
|
241
|
+
.option("--json", "Output as JSON", false)
|
|
242
|
+
.action((opts) => {
|
|
243
|
+
const payout = createPayout({
|
|
244
|
+
amount: parseFloat(opts.amount),
|
|
245
|
+
currency: opts.currency,
|
|
246
|
+
destination: opts.destination,
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
if (opts.json) {
|
|
250
|
+
console.log(JSON.stringify(payout, null, 2));
|
|
251
|
+
} else {
|
|
252
|
+
console.log(`Initiated payout: $${payout.amount} ${payout.currency} (${payout.id})`);
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// --- Balance ---
|
|
257
|
+
|
|
258
|
+
program
|
|
259
|
+
.command("balance")
|
|
260
|
+
.description("Show balance by provider")
|
|
261
|
+
.option("--json", "Output as JSON", false)
|
|
262
|
+
.action((opts) => {
|
|
263
|
+
const balances = getBalanceByProvider();
|
|
264
|
+
|
|
265
|
+
if (opts.json) {
|
|
266
|
+
console.log(JSON.stringify(balances, null, 2));
|
|
267
|
+
} else {
|
|
268
|
+
if (balances.length === 0) {
|
|
269
|
+
console.log("No payment data.");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
for (const b of balances) {
|
|
273
|
+
console.log(` ${b.provider}: charges $${b.total_charges}, refunds $${b.total_refunds}, net $${b.net_balance}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// --- Revenue ---
|
|
279
|
+
|
|
280
|
+
program
|
|
281
|
+
.command("revenue")
|
|
282
|
+
.description("Revenue report for a period")
|
|
283
|
+
.requiredOption("--period <YYYY-MM>", "Month period (e.g. 2024-01)")
|
|
284
|
+
.option("--json", "Output as JSON", false)
|
|
285
|
+
.action((opts) => {
|
|
286
|
+
const [year, month] = opts.period.split("-").map(Number);
|
|
287
|
+
const startDate = `${year}-${String(month).padStart(2, "0")}-01`;
|
|
288
|
+
// Last day of month
|
|
289
|
+
const lastDay = new Date(year, month, 0).getDate();
|
|
290
|
+
const endDate = `${year}-${String(month).padStart(2, "0")}-${String(lastDay).padStart(2, "0")} 23:59:59`;
|
|
291
|
+
|
|
292
|
+
const report = getRevenueReport(startDate, endDate);
|
|
293
|
+
|
|
294
|
+
if (opts.json) {
|
|
295
|
+
console.log(JSON.stringify(report, null, 2));
|
|
296
|
+
} else {
|
|
297
|
+
console.log(`Revenue Report: ${opts.period}`);
|
|
298
|
+
console.log(` Total Revenue: $${report.total_revenue.toFixed(2)}`);
|
|
299
|
+
console.log(` Total Refunds: $${report.total_refunds.toFixed(2)}`);
|
|
300
|
+
console.log(` Net Revenue: $${report.net_revenue.toFixed(2)}`);
|
|
301
|
+
console.log(` Payments: ${report.payment_count} Refunds: ${report.refund_count}`);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// --- Reconcile ---
|
|
306
|
+
|
|
307
|
+
program
|
|
308
|
+
.command("reconcile")
|
|
309
|
+
.description("Link a payment to an invoice")
|
|
310
|
+
.requiredOption("--payment <id>", "Payment ID")
|
|
311
|
+
.requiredOption("--invoice <id>", "Invoice ID")
|
|
312
|
+
.option("--json", "Output as JSON", false)
|
|
313
|
+
.action((opts) => {
|
|
314
|
+
const payment = reconcileWithInvoice(opts.payment, opts.invoice);
|
|
315
|
+
if (!payment) {
|
|
316
|
+
console.error(`Payment '${opts.payment}' not found.`);
|
|
317
|
+
process.exit(1);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (opts.json) {
|
|
321
|
+
console.log(JSON.stringify(payment, null, 2));
|
|
322
|
+
} else {
|
|
323
|
+
console.log(`Linked payment ${payment.id} to invoice ${payment.invoice_id}`);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// --- Stats ---
|
|
328
|
+
|
|
329
|
+
program
|
|
330
|
+
.command("stats")
|
|
331
|
+
.description("Payment statistics")
|
|
332
|
+
.option("--json", "Output as JSON", false)
|
|
333
|
+
.action((opts) => {
|
|
334
|
+
const stats = getPaymentStats();
|
|
335
|
+
|
|
336
|
+
if (opts.json) {
|
|
337
|
+
console.log(JSON.stringify(stats, null, 2));
|
|
338
|
+
} else {
|
|
339
|
+
console.log("Payment Statistics:");
|
|
340
|
+
console.log(` Total Payments: ${stats.total_payments}`);
|
|
341
|
+
console.log(` Charges: ${stats.total_charges}`);
|
|
342
|
+
console.log(` Refunds: ${stats.total_refunds}`);
|
|
343
|
+
console.log(` Transfers: ${stats.total_transfers}`);
|
|
344
|
+
console.log(` Payouts: ${stats.total_payouts}`);
|
|
345
|
+
console.log(` Total Amount: $${stats.total_amount.toFixed(2)}`);
|
|
346
|
+
console.log(" By Status:");
|
|
347
|
+
for (const [s, c] of Object.entries(stats.by_status)) {
|
|
348
|
+
console.log(` ${s}: ${c}`);
|
|
349
|
+
}
|
|
350
|
+
console.log(" By Provider:");
|
|
351
|
+
for (const [p, c] of Object.entries(stats.by_provider)) {
|
|
352
|
+
console.log(` ${p}: ${c}`);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection for microservice-payments
|
|
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-payments", "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-payments", "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-payments", "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,63 @@
|
|
|
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 payments (
|
|
13
|
+
id TEXT PRIMARY KEY,
|
|
14
|
+
type TEXT NOT NULL CHECK (type IN ('charge', 'refund', 'transfer', 'payout')),
|
|
15
|
+
amount REAL NOT NULL,
|
|
16
|
+
currency TEXT NOT NULL DEFAULT 'USD',
|
|
17
|
+
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'succeeded', 'failed', 'disputed', 'refunded')),
|
|
18
|
+
customer_name TEXT,
|
|
19
|
+
customer_email TEXT,
|
|
20
|
+
description TEXT,
|
|
21
|
+
provider TEXT CHECK (provider IN ('stripe', 'square', 'mercury', 'manual')),
|
|
22
|
+
provider_id TEXT,
|
|
23
|
+
invoice_id TEXT,
|
|
24
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
25
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
26
|
+
completed_at TEXT
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
CREATE TABLE IF NOT EXISTS disputes (
|
|
30
|
+
id TEXT PRIMARY KEY,
|
|
31
|
+
payment_id TEXT NOT NULL REFERENCES payments(id) ON DELETE CASCADE,
|
|
32
|
+
reason TEXT,
|
|
33
|
+
status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'under_review', 'won', 'lost')),
|
|
34
|
+
amount REAL,
|
|
35
|
+
evidence TEXT NOT NULL DEFAULT '{}',
|
|
36
|
+
opened_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
37
|
+
resolved_at TEXT,
|
|
38
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
CREATE TABLE IF NOT EXISTS payouts (
|
|
42
|
+
id TEXT PRIMARY KEY,
|
|
43
|
+
amount REAL NOT NULL,
|
|
44
|
+
currency TEXT NOT NULL DEFAULT 'USD',
|
|
45
|
+
destination TEXT,
|
|
46
|
+
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'in_transit', 'paid', 'failed')),
|
|
47
|
+
initiated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
48
|
+
arrived_at TEXT,
|
|
49
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
CREATE INDEX IF NOT EXISTS idx_payments_status ON payments(status);
|
|
53
|
+
CREATE INDEX IF NOT EXISTS idx_payments_customer_email ON payments(customer_email);
|
|
54
|
+
CREATE INDEX IF NOT EXISTS idx_payments_provider ON payments(provider);
|
|
55
|
+
CREATE INDEX IF NOT EXISTS idx_payments_invoice_id ON payments(invoice_id);
|
|
56
|
+
CREATE INDEX IF NOT EXISTS idx_payments_created_at ON payments(created_at);
|
|
57
|
+
CREATE INDEX IF NOT EXISTS idx_payments_type ON payments(type);
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_disputes_payment_id ON disputes(payment_id);
|
|
59
|
+
CREATE INDEX IF NOT EXISTS idx_disputes_status ON disputes(status);
|
|
60
|
+
CREATE INDEX IF NOT EXISTS idx_payouts_status ON payouts(status);
|
|
61
|
+
`,
|
|
62
|
+
},
|
|
63
|
+
];
|