@hasna/microservices 0.0.3 → 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 +63 -0
- package/bin/mcp.js +63 -0
- package/dist/index.js +63 -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/package.json +1 -1
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { Command } from "commander";
|
|
4
|
+
import {
|
|
5
|
+
createEmployee,
|
|
6
|
+
getEmployee,
|
|
7
|
+
listEmployees,
|
|
8
|
+
updateEmployee,
|
|
9
|
+
terminateEmployee,
|
|
10
|
+
createPayPeriod,
|
|
11
|
+
listPayPeriods,
|
|
12
|
+
getPayPeriod,
|
|
13
|
+
processPayroll,
|
|
14
|
+
listPayStubs,
|
|
15
|
+
getPayStub,
|
|
16
|
+
getPayrollReport,
|
|
17
|
+
getYtdReport,
|
|
18
|
+
getTaxSummary,
|
|
19
|
+
} from "../db/payroll.js";
|
|
20
|
+
|
|
21
|
+
const program = new Command();
|
|
22
|
+
|
|
23
|
+
program
|
|
24
|
+
.name("microservice-payroll")
|
|
25
|
+
.description("Payroll management microservice")
|
|
26
|
+
.version("0.0.1");
|
|
27
|
+
|
|
28
|
+
// --- Employees ---
|
|
29
|
+
|
|
30
|
+
const employeeCmd = program
|
|
31
|
+
.command("employee")
|
|
32
|
+
.description("Employee management");
|
|
33
|
+
|
|
34
|
+
employeeCmd
|
|
35
|
+
.command("add")
|
|
36
|
+
.description("Add a new employee")
|
|
37
|
+
.requiredOption("--name <name>", "Full name")
|
|
38
|
+
.requiredOption("--pay-rate <rate>", "Pay rate (annual salary or hourly rate)")
|
|
39
|
+
.option("--email <email>", "Email address")
|
|
40
|
+
.option("--type <type>", "Type: employee or contractor", "employee")
|
|
41
|
+
.option("--department <dept>", "Department")
|
|
42
|
+
.option("--title <title>", "Job title")
|
|
43
|
+
.option("--pay-type <type>", "Pay type: salary or hourly", "salary")
|
|
44
|
+
.option("--currency <currency>", "Currency code", "USD")
|
|
45
|
+
.option("--start-date <date>", "Start date (YYYY-MM-DD)")
|
|
46
|
+
.option("--json", "Output as JSON", false)
|
|
47
|
+
.action((opts) => {
|
|
48
|
+
const employee = createEmployee({
|
|
49
|
+
name: opts.name,
|
|
50
|
+
email: opts.email,
|
|
51
|
+
type: opts.type,
|
|
52
|
+
department: opts.department,
|
|
53
|
+
title: opts.title,
|
|
54
|
+
pay_rate: parseFloat(opts.payRate),
|
|
55
|
+
pay_type: opts.payType,
|
|
56
|
+
currency: opts.currency,
|
|
57
|
+
start_date: opts.startDate,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (opts.json) {
|
|
61
|
+
console.log(JSON.stringify(employee, null, 2));
|
|
62
|
+
} else {
|
|
63
|
+
console.log(`Created employee: ${employee.name} (${employee.id})`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
employeeCmd
|
|
68
|
+
.command("list")
|
|
69
|
+
.description("List employees")
|
|
70
|
+
.option("--status <status>", "Filter by status: active or terminated")
|
|
71
|
+
.option("--department <dept>", "Filter by department")
|
|
72
|
+
.option("--type <type>", "Filter by type: employee or contractor")
|
|
73
|
+
.option("--search <query>", "Search by name, email, or department")
|
|
74
|
+
.option("--json", "Output as JSON", false)
|
|
75
|
+
.action((opts) => {
|
|
76
|
+
const employees = listEmployees({
|
|
77
|
+
status: opts.status,
|
|
78
|
+
department: opts.department,
|
|
79
|
+
type: opts.type,
|
|
80
|
+
search: opts.search,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (opts.json) {
|
|
84
|
+
console.log(JSON.stringify(employees, null, 2));
|
|
85
|
+
} else {
|
|
86
|
+
if (employees.length === 0) {
|
|
87
|
+
console.log("No employees found.");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
for (const e of employees) {
|
|
91
|
+
const dept = e.department ? ` (${e.department})` : "";
|
|
92
|
+
const status = e.status === "terminated" ? " [TERMINATED]" : "";
|
|
93
|
+
console.log(` ${e.name}${dept} — ${e.pay_type} $${e.pay_rate}${status}`);
|
|
94
|
+
}
|
|
95
|
+
console.log(`\n${employees.length} employee(s)`);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
employeeCmd
|
|
100
|
+
.command("get")
|
|
101
|
+
.description("Get an employee by ID")
|
|
102
|
+
.argument("<id>", "Employee ID")
|
|
103
|
+
.option("--json", "Output as JSON", false)
|
|
104
|
+
.action((id, opts) => {
|
|
105
|
+
const employee = getEmployee(id);
|
|
106
|
+
if (!employee) {
|
|
107
|
+
console.error(`Employee '${id}' not found.`);
|
|
108
|
+
process.exit(1);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (opts.json) {
|
|
112
|
+
console.log(JSON.stringify(employee, null, 2));
|
|
113
|
+
} else {
|
|
114
|
+
console.log(`${employee.name}`);
|
|
115
|
+
if (employee.email) console.log(` Email: ${employee.email}`);
|
|
116
|
+
console.log(` Type: ${employee.type}`);
|
|
117
|
+
console.log(` Status: ${employee.status}`);
|
|
118
|
+
if (employee.department) console.log(` Department: ${employee.department}`);
|
|
119
|
+
if (employee.title) console.log(` Title: ${employee.title}`);
|
|
120
|
+
console.log(` Pay: ${employee.pay_type} $${employee.pay_rate} ${employee.currency}`);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
employeeCmd
|
|
125
|
+
.command("update")
|
|
126
|
+
.description("Update an employee")
|
|
127
|
+
.argument("<id>", "Employee ID")
|
|
128
|
+
.option("--name <name>", "Full name")
|
|
129
|
+
.option("--email <email>", "Email")
|
|
130
|
+
.option("--department <dept>", "Department")
|
|
131
|
+
.option("--title <title>", "Job title")
|
|
132
|
+
.option("--pay-rate <rate>", "Pay rate")
|
|
133
|
+
.option("--pay-type <type>", "Pay type")
|
|
134
|
+
.option("--json", "Output as JSON", false)
|
|
135
|
+
.action((id, opts) => {
|
|
136
|
+
const input: Record<string, unknown> = {};
|
|
137
|
+
if (opts.name !== undefined) input.name = opts.name;
|
|
138
|
+
if (opts.email !== undefined) input.email = opts.email;
|
|
139
|
+
if (opts.department !== undefined) input.department = opts.department;
|
|
140
|
+
if (opts.title !== undefined) input.title = opts.title;
|
|
141
|
+
if (opts.payRate !== undefined) input.pay_rate = parseFloat(opts.payRate);
|
|
142
|
+
if (opts.payType !== undefined) input.pay_type = opts.payType;
|
|
143
|
+
|
|
144
|
+
const employee = updateEmployee(id, input);
|
|
145
|
+
if (!employee) {
|
|
146
|
+
console.error(`Employee '${id}' not found.`);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (opts.json) {
|
|
151
|
+
console.log(JSON.stringify(employee, null, 2));
|
|
152
|
+
} else {
|
|
153
|
+
console.log(`Updated: ${employee.name}`);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
employeeCmd
|
|
158
|
+
.command("terminate")
|
|
159
|
+
.description("Terminate an employee")
|
|
160
|
+
.argument("<id>", "Employee ID")
|
|
161
|
+
.option("--end-date <date>", "End date (YYYY-MM-DD)")
|
|
162
|
+
.option("--json", "Output as JSON", false)
|
|
163
|
+
.action((id, opts) => {
|
|
164
|
+
const employee = terminateEmployee(id, opts.endDate);
|
|
165
|
+
if (!employee) {
|
|
166
|
+
console.error(`Employee '${id}' not found.`);
|
|
167
|
+
process.exit(1);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (opts.json) {
|
|
171
|
+
console.log(JSON.stringify(employee, null, 2));
|
|
172
|
+
} else {
|
|
173
|
+
console.log(`Terminated: ${employee.name} (end date: ${employee.end_date})`);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// --- Pay Periods ---
|
|
178
|
+
|
|
179
|
+
const periodCmd = program
|
|
180
|
+
.command("payperiod")
|
|
181
|
+
.description("Pay period management");
|
|
182
|
+
|
|
183
|
+
periodCmd
|
|
184
|
+
.command("create")
|
|
185
|
+
.description("Create a new pay period")
|
|
186
|
+
.requiredOption("--start <date>", "Start date (YYYY-MM-DD)")
|
|
187
|
+
.requiredOption("--end <date>", "End date (YYYY-MM-DD)")
|
|
188
|
+
.option("--json", "Output as JSON", false)
|
|
189
|
+
.action((opts) => {
|
|
190
|
+
const period = createPayPeriod({
|
|
191
|
+
start_date: opts.start,
|
|
192
|
+
end_date: opts.end,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
if (opts.json) {
|
|
196
|
+
console.log(JSON.stringify(period, null, 2));
|
|
197
|
+
} else {
|
|
198
|
+
console.log(`Created pay period: ${period.start_date} to ${period.end_date} (${period.id})`);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
periodCmd
|
|
203
|
+
.command("list")
|
|
204
|
+
.description("List pay periods")
|
|
205
|
+
.option("--status <status>", "Filter by status")
|
|
206
|
+
.option("--json", "Output as JSON", false)
|
|
207
|
+
.action((opts) => {
|
|
208
|
+
const periods = listPayPeriods(opts.status);
|
|
209
|
+
|
|
210
|
+
if (opts.json) {
|
|
211
|
+
console.log(JSON.stringify(periods, null, 2));
|
|
212
|
+
} else {
|
|
213
|
+
if (periods.length === 0) {
|
|
214
|
+
console.log("No pay periods found.");
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
for (const p of periods) {
|
|
218
|
+
console.log(` ${p.start_date} to ${p.end_date} [${p.status}] (${p.id})`);
|
|
219
|
+
}
|
|
220
|
+
console.log(`\n${periods.length} pay period(s)`);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
periodCmd
|
|
225
|
+
.command("process")
|
|
226
|
+
.description("Process payroll for a pay period")
|
|
227
|
+
.argument("<id>", "Pay period ID")
|
|
228
|
+
.option("--json", "Output as JSON", false)
|
|
229
|
+
.action((id, opts) => {
|
|
230
|
+
try {
|
|
231
|
+
const stubs = processPayroll(id);
|
|
232
|
+
if (opts.json) {
|
|
233
|
+
console.log(JSON.stringify(stubs, null, 2));
|
|
234
|
+
} else {
|
|
235
|
+
console.log(`Processed payroll: ${stubs.length} pay stub(s) generated.`);
|
|
236
|
+
for (const s of stubs) {
|
|
237
|
+
console.log(` Employee ${s.employee_id}: gross=$${s.gross_pay} net=$${s.net_pay}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// --- Pay Stubs ---
|
|
247
|
+
|
|
248
|
+
const stubCmd = program
|
|
249
|
+
.command("paystub")
|
|
250
|
+
.description("Pay stub management");
|
|
251
|
+
|
|
252
|
+
stubCmd
|
|
253
|
+
.command("list")
|
|
254
|
+
.description("List pay stubs")
|
|
255
|
+
.option("--employee <id>", "Filter by employee ID")
|
|
256
|
+
.option("--period <id>", "Filter by pay period ID")
|
|
257
|
+
.option("--json", "Output as JSON", false)
|
|
258
|
+
.action((opts) => {
|
|
259
|
+
const stubs = listPayStubs({
|
|
260
|
+
employee_id: opts.employee,
|
|
261
|
+
pay_period_id: opts.period,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
if (opts.json) {
|
|
265
|
+
console.log(JSON.stringify(stubs, null, 2));
|
|
266
|
+
} else {
|
|
267
|
+
if (stubs.length === 0) {
|
|
268
|
+
console.log("No pay stubs found.");
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
for (const s of stubs) {
|
|
272
|
+
console.log(` ${s.id} — Employee ${s.employee_id}: gross=$${s.gross_pay} net=$${s.net_pay}`);
|
|
273
|
+
}
|
|
274
|
+
console.log(`\n${stubs.length} pay stub(s)`);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
stubCmd
|
|
279
|
+
.command("get")
|
|
280
|
+
.description("Get a pay stub by ID")
|
|
281
|
+
.argument("<id>", "Pay stub ID")
|
|
282
|
+
.option("--json", "Output as JSON", false)
|
|
283
|
+
.action((id, opts) => {
|
|
284
|
+
const stub = getPayStub(id);
|
|
285
|
+
if (!stub) {
|
|
286
|
+
console.error(`Pay stub '${id}' not found.`);
|
|
287
|
+
process.exit(1);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (opts.json) {
|
|
291
|
+
console.log(JSON.stringify(stub, null, 2));
|
|
292
|
+
} else {
|
|
293
|
+
console.log(`Pay Stub: ${stub.id}`);
|
|
294
|
+
console.log(` Employee: ${stub.employee_id}`);
|
|
295
|
+
console.log(` Period: ${stub.pay_period_id}`);
|
|
296
|
+
console.log(` Gross Pay: $${stub.gross_pay}`);
|
|
297
|
+
console.log(` Deductions: ${JSON.stringify(stub.deductions)}`);
|
|
298
|
+
console.log(` Net Pay: $${stub.net_pay}`);
|
|
299
|
+
if (stub.hours_worked !== null) console.log(` Hours: ${stub.hours_worked}`);
|
|
300
|
+
if (stub.overtime_hours) console.log(` Overtime: ${stub.overtime_hours}`);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// --- Run (process payroll shortcut) ---
|
|
305
|
+
|
|
306
|
+
program
|
|
307
|
+
.command("run")
|
|
308
|
+
.description("Process payroll for a pay period (shortcut)")
|
|
309
|
+
.argument("<period-id>", "Pay period ID")
|
|
310
|
+
.option("--json", "Output as JSON", false)
|
|
311
|
+
.action((periodId, opts) => {
|
|
312
|
+
try {
|
|
313
|
+
const stubs = processPayroll(periodId);
|
|
314
|
+
if (opts.json) {
|
|
315
|
+
console.log(JSON.stringify(stubs, null, 2));
|
|
316
|
+
} else {
|
|
317
|
+
console.log(`Payroll processed: ${stubs.length} pay stub(s) generated.`);
|
|
318
|
+
}
|
|
319
|
+
} catch (error) {
|
|
320
|
+
console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
|
|
321
|
+
process.exit(1);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// --- Report ---
|
|
326
|
+
|
|
327
|
+
program
|
|
328
|
+
.command("report")
|
|
329
|
+
.description("Get payroll report for a pay period")
|
|
330
|
+
.argument("<period-id>", "Pay period ID")
|
|
331
|
+
.option("--json", "Output as JSON", false)
|
|
332
|
+
.action((periodId, opts) => {
|
|
333
|
+
const report = getPayrollReport(periodId);
|
|
334
|
+
if (!report) {
|
|
335
|
+
console.error(`Pay period '${periodId}' not found.`);
|
|
336
|
+
process.exit(1);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (opts.json) {
|
|
340
|
+
console.log(JSON.stringify(report, null, 2));
|
|
341
|
+
} else {
|
|
342
|
+
console.log(`Payroll Report: ${report.period.start_date} to ${report.period.end_date}`);
|
|
343
|
+
console.log(` Status: ${report.period.status}`);
|
|
344
|
+
console.log(` Employees: ${report.employee_count}`);
|
|
345
|
+
console.log(` Total Gross: $${report.total_gross}`);
|
|
346
|
+
console.log(` Total Deductions: $${report.total_deductions}`);
|
|
347
|
+
console.log(` Total Net: $${report.total_net}`);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// --- Taxes ---
|
|
352
|
+
|
|
353
|
+
program
|
|
354
|
+
.command("taxes")
|
|
355
|
+
.description("Get tax summary for a year")
|
|
356
|
+
.argument("<year>", "Tax year")
|
|
357
|
+
.option("--json", "Output as JSON", false)
|
|
358
|
+
.action((year, opts) => {
|
|
359
|
+
const summary = getTaxSummary(parseInt(year));
|
|
360
|
+
|
|
361
|
+
if (opts.json) {
|
|
362
|
+
console.log(JSON.stringify(summary, null, 2));
|
|
363
|
+
} else {
|
|
364
|
+
if (summary.length === 0) {
|
|
365
|
+
console.log(`No tax data for ${year}.`);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
for (const entry of summary) {
|
|
369
|
+
console.log(` ${entry.employee_name}: gross=$${entry.total_gross} fed=$${entry.total_federal_tax} state=$${entry.total_state_tax} net=$${entry.total_net}`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database connection for microservice-payroll
|
|
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-payroll", "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-payroll", "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-payroll", "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,69 @@
|
|
|
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 employees (
|
|
13
|
+
id TEXT PRIMARY KEY,
|
|
14
|
+
name TEXT NOT NULL,
|
|
15
|
+
email TEXT,
|
|
16
|
+
type TEXT NOT NULL DEFAULT 'employee' CHECK (type IN ('employee', 'contractor')),
|
|
17
|
+
status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'terminated')),
|
|
18
|
+
department TEXT,
|
|
19
|
+
title TEXT,
|
|
20
|
+
pay_rate REAL NOT NULL,
|
|
21
|
+
pay_type TEXT NOT NULL DEFAULT 'salary' CHECK (pay_type IN ('salary', 'hourly')),
|
|
22
|
+
currency TEXT NOT NULL DEFAULT 'USD',
|
|
23
|
+
tax_info TEXT NOT NULL DEFAULT '{}',
|
|
24
|
+
start_date TEXT,
|
|
25
|
+
end_date TEXT,
|
|
26
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
27
|
+
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
CREATE TABLE IF NOT EXISTS pay_periods (
|
|
31
|
+
id TEXT PRIMARY KEY,
|
|
32
|
+
start_date TEXT NOT NULL,
|
|
33
|
+
end_date TEXT NOT NULL,
|
|
34
|
+
status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'processing', 'completed')),
|
|
35
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
CREATE TABLE IF NOT EXISTS pay_stubs (
|
|
39
|
+
id TEXT PRIMARY KEY,
|
|
40
|
+
employee_id TEXT NOT NULL REFERENCES employees(id) ON DELETE CASCADE,
|
|
41
|
+
pay_period_id TEXT NOT NULL REFERENCES pay_periods(id) ON DELETE CASCADE,
|
|
42
|
+
gross_pay REAL NOT NULL DEFAULT 0,
|
|
43
|
+
deductions TEXT NOT NULL DEFAULT '{}',
|
|
44
|
+
net_pay REAL NOT NULL DEFAULT 0,
|
|
45
|
+
hours_worked REAL,
|
|
46
|
+
overtime_hours REAL DEFAULT 0,
|
|
47
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
CREATE TABLE IF NOT EXISTS payments (
|
|
51
|
+
id TEXT PRIMARY KEY,
|
|
52
|
+
pay_stub_id TEXT NOT NULL REFERENCES pay_stubs(id) ON DELETE CASCADE,
|
|
53
|
+
method TEXT NOT NULL DEFAULT 'direct_deposit' CHECK (method IN ('direct_deposit', 'check', 'wire')),
|
|
54
|
+
status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'paid', 'failed')),
|
|
55
|
+
paid_at TEXT,
|
|
56
|
+
reference TEXT,
|
|
57
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
CREATE INDEX IF NOT EXISTS idx_employees_email ON employees(email);
|
|
61
|
+
CREATE INDEX IF NOT EXISTS idx_employees_status ON employees(status);
|
|
62
|
+
CREATE INDEX IF NOT EXISTS idx_employees_department ON employees(department);
|
|
63
|
+
CREATE INDEX IF NOT EXISTS idx_pay_stubs_employee ON pay_stubs(employee_id);
|
|
64
|
+
CREATE INDEX IF NOT EXISTS idx_pay_stubs_period ON pay_stubs(pay_period_id);
|
|
65
|
+
CREATE INDEX IF NOT EXISTS idx_payments_stub ON payments(pay_stub_id);
|
|
66
|
+
CREATE INDEX IF NOT EXISTS idx_payments_status ON payments(status);
|
|
67
|
+
`,
|
|
68
|
+
},
|
|
69
|
+
];
|