@voyant-travel/finance 0.119.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.
- package/LICENSE +201 -0
- package/README.md +192 -0
- package/dist/action-ledger-drift.d.ts +29 -0
- package/dist/action-ledger-drift.d.ts.map +1 -0
- package/dist/action-ledger-drift.js +166 -0
- package/dist/booking-tax.d.ts +124 -0
- package/dist/booking-tax.d.ts.map +1 -0
- package/dist/booking-tax.js +264 -0
- package/dist/checkout-routes.d.ts +1154 -0
- package/dist/checkout-routes.d.ts.map +1 -0
- package/dist/checkout-routes.js +116 -0
- package/dist/checkout-service-plan.d.ts +137 -0
- package/dist/checkout-service-plan.d.ts.map +1 -0
- package/dist/checkout-service-plan.js +119 -0
- package/dist/checkout-service.d.ts +9 -0
- package/dist/checkout-service.d.ts.map +1 -0
- package/dist/checkout-service.js +324 -0
- package/dist/checkout-validation.d.ts +1682 -0
- package/dist/checkout-validation.d.ts.map +1 -0
- package/dist/checkout-validation.js +228 -0
- package/dist/document-download.d.ts +3 -0
- package/dist/document-download.d.ts.map +1 -0
- package/dist/document-download.js +1 -0
- package/dist/fx-money.d.ts +17 -0
- package/dist/fx-money.d.ts.map +1 -0
- package/dist/fx-money.js +194 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -0
- package/dist/invoice-fx.d.ts +134 -0
- package/dist/invoice-fx.d.ts.map +1 -0
- package/dist/invoice-fx.js +240 -0
- package/dist/invoice-number-errors.d.ts +2 -0
- package/dist/invoice-number-errors.d.ts.map +1 -0
- package/dist/invoice-number-errors.js +58 -0
- package/dist/markets-ref.d.ts +149 -0
- package/dist/markets-ref.d.ts.map +1 -0
- package/dist/markets-ref.js +17 -0
- package/dist/payment-link.d.ts +23 -0
- package/dist/payment-link.d.ts.map +1 -0
- package/dist/payment-link.js +30 -0
- package/dist/payment-policy.d.ts +113 -0
- package/dist/payment-policy.d.ts.map +1 -0
- package/dist/payment-policy.js +193 -0
- package/dist/route-runtime.d.ts +22 -0
- package/dist/route-runtime.d.ts.map +1 -0
- package/dist/route-runtime.js +18 -0
- package/dist/routes-action-ledger.d.ts +181 -0
- package/dist/routes-action-ledger.d.ts.map +1 -0
- package/dist/routes-action-ledger.js +142 -0
- package/dist/routes-booking-billing.d.ts +852 -0
- package/dist/routes-booking-billing.d.ts.map +1 -0
- package/dist/routes-booking-billing.js +223 -0
- package/dist/routes-booking-create.d.ts +3 -0
- package/dist/routes-booking-create.d.ts.map +1 -0
- package/dist/routes-booking-create.js +194 -0
- package/dist/routes-booking-reads.d.ts +46 -0
- package/dist/routes-booking-reads.d.ts.map +1 -0
- package/dist/routes-booking-reads.js +20 -0
- package/dist/routes-documents.d.ts +195 -0
- package/dist/routes-documents.d.ts.map +1 -0
- package/dist/routes-documents.js +93 -0
- package/dist/routes-invoice-core.d.ts +794 -0
- package/dist/routes-invoice-core.d.ts.map +1 -0
- package/dist/routes-invoice-core.js +238 -0
- package/dist/routes-invoice-documents.d.ts +401 -0
- package/dist/routes-invoice-documents.d.ts.map +1 -0
- package/dist/routes-invoice-documents.js +91 -0
- package/dist/routes-invoice-issue.d.ts +384 -0
- package/dist/routes-invoice-issue.d.ts.map +1 -0
- package/dist/routes-invoice-issue.js +208 -0
- package/dist/routes-payment-processing.d.ts +1193 -0
- package/dist/routes-payment-processing.d.ts.map +1 -0
- package/dist/routes-payment-processing.js +238 -0
- package/dist/routes-payments.d.ts +309 -0
- package/dist/routes-payments.d.ts.map +1 -0
- package/dist/routes-payments.js +94 -0
- package/dist/routes-public.d.ts +1948 -0
- package/dist/routes-public.d.ts.map +1 -0
- package/dist/routes-public.js +275 -0
- package/dist/routes-reference-data.d.ts +977 -0
- package/dist/routes-reference-data.d.ts.map +1 -0
- package/dist/routes-reference-data.js +191 -0
- package/dist/routes-reports.d.ts +344 -0
- package/dist/routes-reports.d.ts.map +1 -0
- package/dist/routes-reports.js +93 -0
- package/dist/routes-runtime.d.ts +71 -0
- package/dist/routes-runtime.d.ts.map +1 -0
- package/dist/routes-runtime.js +59 -0
- package/dist/routes-settlement.d.ts +67 -0
- package/dist/routes-settlement.d.ts.map +1 -0
- package/dist/routes-settlement.js +23 -0
- package/dist/routes-shared.d.ts +35 -0
- package/dist/routes-shared.d.ts.map +1 -0
- package/dist/routes-shared.js +10 -0
- package/dist/routes-supplier-invoices.d.ts +778 -0
- package/dist/routes-supplier-invoices.d.ts.map +1 -0
- package/dist/routes-supplier-invoices.js +159 -0
- package/dist/routes-vouchers.d.ts +228 -0
- package/dist/routes-vouchers.d.ts.map +1 -0
- package/dist/routes-vouchers.js +54 -0
- package/dist/routes.d.ts +5577 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +44 -0
- package/dist/schema/booking-billing.d.ts +1006 -0
- package/dist/schema/booking-billing.d.ts.map +1 -0
- package/dist/schema/booking-billing.js +106 -0
- package/dist/schema/enums.d.ts +48 -0
- package/dist/schema/enums.d.ts.map +1 -0
- package/dist/schema/enums.js +237 -0
- package/dist/schema/invoice-documents.d.ts +1245 -0
- package/dist/schema/invoice-documents.d.ts.map +1 -0
- package/dist/schema/invoice-documents.js +140 -0
- package/dist/schema/payment-instruments.d.ts +418 -0
- package/dist/schema/payment-instruments.d.ts.map +1 -0
- package/dist/schema/payment-instruments.js +45 -0
- package/dist/schema/payment-processing.d.ts +563 -0
- package/dist/schema/payment-processing.d.ts.map +1 -0
- package/dist/schema/payment-processing.js +65 -0
- package/dist/schema/payment-sessions.d.ts +728 -0
- package/dist/schema/payment-sessions.d.ts.map +1 -0
- package/dist/schema/payment-sessions.js +79 -0
- package/dist/schema/receivables.d.ts +1474 -0
- package/dist/schema/receivables.d.ts.map +1 -0
- package/dist/schema/receivables.js +179 -0
- package/dist/schema/relations.d.ts +82 -0
- package/dist/schema/relations.d.ts.map +1 -0
- package/dist/schema/relations.js +144 -0
- package/dist/schema/supplier-invoices.d.ts +1619 -0
- package/dist/schema/supplier-invoices.d.ts.map +1 -0
- package/dist/schema/supplier-invoices.js +228 -0
- package/dist/schema/tax.d.ts +712 -0
- package/dist/schema/tax.d.ts.map +1 -0
- package/dist/schema/tax.js +98 -0
- package/dist/schema/vouchers.d.ts +444 -0
- package/dist/schema/vouchers.d.ts.map +1 -0
- package/dist/schema/vouchers.js +64 -0
- package/dist/schema.d.ts +12 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +11 -0
- package/dist/service-accountant-shares.d.ts +106 -0
- package/dist/service-accountant-shares.d.ts.map +1 -0
- package/dist/service-accountant-shares.js +331 -0
- package/dist/service-action-ledger-accounting.d.ts +104 -0
- package/dist/service-action-ledger-accounting.d.ts.map +1 -0
- package/dist/service-action-ledger-accounting.js +386 -0
- package/dist/service-action-ledger-booking-payments.d.ts +48 -0
- package/dist/service-action-ledger-booking-payments.d.ts.map +1 -0
- package/dist/service-action-ledger-booking-payments.js +178 -0
- package/dist/service-action-ledger-bookings.d.ts +44 -0
- package/dist/service-action-ledger-bookings.d.ts.map +1 -0
- package/dist/service-action-ledger-bookings.js +81 -0
- package/dist/service-action-ledger-payment-authorizations.d.ts +48 -0
- package/dist/service-action-ledger-payment-authorizations.d.ts.map +1 -0
- package/dist/service-action-ledger-payment-authorizations.js +209 -0
- package/dist/service-action-ledger-payment-sessions.d.ts +83 -0
- package/dist/service-action-ledger-payment-sessions.d.ts.map +1 -0
- package/dist/service-action-ledger-payment-sessions.js +294 -0
- package/dist/service-action-ledger-supplier-invoices.d.ts +27 -0
- package/dist/service-action-ledger-supplier-invoices.d.ts.map +1 -0
- package/dist/service-action-ledger-supplier-invoices.js +111 -0
- package/dist/service-action-ledger-supplier-payments.d.ts +21 -0
- package/dist/service-action-ledger-supplier-payments.d.ts.map +1 -0
- package/dist/service-action-ledger-supplier-payments.js +97 -0
- package/dist/service-action-ledger.d.ts +7 -0
- package/dist/service-action-ledger.d.ts.map +1 -0
- package/dist/service-action-ledger.js +6 -0
- package/dist/service-aggregates.d.ts +96 -0
- package/dist/service-aggregates.d.ts.map +1 -0
- package/dist/service-aggregates.js +294 -0
- package/dist/service-booking-billing.d.ts +2322 -0
- package/dist/service-booking-billing.d.ts.map +1 -0
- package/dist/service-booking-billing.js +8 -0
- package/dist/service-booking-create.d.ts +410 -0
- package/dist/service-booking-create.d.ts.map +1 -0
- package/dist/service-booking-create.js +1256 -0
- package/dist/service-booking-guarantees.d.ts +725 -0
- package/dist/service-booking-guarantees.d.ts.map +1 -0
- package/dist/service-booking-guarantees.js +153 -0
- package/dist/service-booking-item-billing.d.ts +1062 -0
- package/dist/service-booking-item-billing.d.ts.map +1 -0
- package/dist/service-booking-item-billing.js +77 -0
- package/dist/service-booking-payment-schedules.d.ts +557 -0
- package/dist/service-booking-payment-schedules.d.ts.map +1 -0
- package/dist/service-booking-payment-schedules.js +372 -0
- package/dist/service-bookings-dual-create.d.ts +308 -0
- package/dist/service-bookings-dual-create.d.ts.map +1 -0
- package/dist/service-bookings-dual-create.js +131 -0
- package/dist/service-boundary-sql.d.ts +6 -0
- package/dist/service-boundary-sql.d.ts.map +1 -0
- package/dist/service-boundary-sql.js +15 -0
- package/dist/service-cost-categories.d.ts +26 -0
- package/dist/service-cost-categories.d.ts.map +1 -0
- package/dist/service-cost-categories.js +76 -0
- package/dist/service-documents.d.ts +80 -0
- package/dist/service-documents.d.ts.map +1 -0
- package/dist/service-documents.js +228 -0
- package/dist/service-invoice-artifacts.d.ts +246 -0
- package/dist/service-invoice-artifacts.d.ts.map +1 -0
- package/dist/service-invoice-artifacts.js +277 -0
- package/dist/service-invoice-core.d.ts +405 -0
- package/dist/service-invoice-core.d.ts.map +1 -0
- package/dist/service-invoice-core.js +290 -0
- package/dist/service-invoice-credit-notes.d.ts +973 -0
- package/dist/service-invoice-credit-notes.d.ts.map +1 -0
- package/dist/service-invoice-credit-notes.js +142 -0
- package/dist/service-invoice-from-booking.d.ts +41 -0
- package/dist/service-invoice-from-booking.d.ts.map +1 -0
- package/dist/service-invoice-from-booking.js +267 -0
- package/dist/service-invoice-line-items.d.ts +432 -0
- package/dist/service-invoice-line-items.d.ts.map +1 -0
- package/dist/service-invoice-line-items.js +102 -0
- package/dist/service-invoice-numbering.d.ts +227 -0
- package/dist/service-invoice-numbering.d.ts.map +1 -0
- package/dist/service-invoice-numbering.js +260 -0
- package/dist/service-invoice-payments.d.ts +673 -0
- package/dist/service-invoice-payments.d.ts.map +1 -0
- package/dist/service-invoice-payments.js +398 -0
- package/dist/service-invoices.d.ts +2501 -0
- package/dist/service-invoices.d.ts.map +1 -0
- package/dist/service-invoices.js +12 -0
- package/dist/service-issue.d.ts +207 -0
- package/dist/service-issue.d.ts.map +1 -0
- package/dist/service-issue.js +431 -0
- package/dist/service-payment-authorizations.d.ts +164 -0
- package/dist/service-payment-authorizations.d.ts.map +1 -0
- package/dist/service-payment-authorizations.js +227 -0
- package/dist/service-payment-instruments.d.ts +116 -0
- package/dist/service-payment-instruments.d.ts.map +1 -0
- package/dist/service-payment-instruments.js +99 -0
- package/dist/service-payment-processing.d.ts +676 -0
- package/dist/service-payment-processing.d.ts.map +1 -0
- package/dist/service-payment-processing.js +10 -0
- package/dist/service-payment-session-completion.d.ts +48 -0
- package/dist/service-payment-session-completion.d.ts.map +1 -0
- package/dist/service-payment-session-completion.js +238 -0
- package/dist/service-payment-sessions.d.ts +361 -0
- package/dist/service-payment-sessions.d.ts.map +1 -0
- package/dist/service-payment-sessions.js +280 -0
- package/dist/service-profitability.d.ts +114 -0
- package/dist/service-profitability.d.ts.map +1 -0
- package/dist/service-profitability.js +794 -0
- package/dist/service-public.d.ts +553 -0
- package/dist/service-public.d.ts.map +1 -0
- package/dist/service-public.js +583 -0
- package/dist/service-reference-data.d.ts +272 -0
- package/dist/service-reference-data.d.ts.map +1 -0
- package/dist/service-reference-data.js +280 -0
- package/dist/service-rendition-wait.d.ts +38 -0
- package/dist/service-rendition-wait.d.ts.map +1 -0
- package/dist/service-rendition-wait.js +67 -0
- package/dist/service-reports.d.ts +37 -0
- package/dist/service-reports.d.ts.map +1 -0
- package/dist/service-reports.js +62 -0
- package/dist/service-settlement.d.ts +46 -0
- package/dist/service-settlement.d.ts.map +1 -0
- package/dist/service-settlement.js +185 -0
- package/dist/service-shared.d.ts +541 -0
- package/dist/service-shared.d.ts.map +1 -0
- package/dist/service-shared.js +764 -0
- package/dist/service-supplier-invoices.d.ts +871 -0
- package/dist/service-supplier-invoices.d.ts.map +1 -0
- package/dist/service-supplier-invoices.js +744 -0
- package/dist/service-supplier-payments.d.ts +69 -0
- package/dist/service-supplier-payments.d.ts.map +1 -0
- package/dist/service-supplier-payments.js +136 -0
- package/dist/service-vouchers-migration.d.ts +44 -0
- package/dist/service-vouchers-migration.d.ts.map +1 -0
- package/dist/service-vouchers-migration.js +148 -0
- package/dist/service-vouchers.d.ts +157 -0
- package/dist/service-vouchers.d.ts.map +1 -0
- package/dist/service-vouchers.js +191 -0
- package/dist/service.d.ts +6490 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +29 -0
- package/dist/validation-billing.d.ts +2 -0
- package/dist/validation-billing.d.ts.map +1 -0
- package/dist/validation-billing.js +1 -0
- package/dist/validation-payments.d.ts +2 -0
- package/dist/validation-payments.d.ts.map +1 -0
- package/dist/validation-payments.js +1 -0
- package/dist/validation-public.d.ts +2 -0
- package/dist/validation-public.d.ts.map +1 -0
- package/dist/validation-public.js +1 -0
- package/dist/validation-shared.d.ts +2 -0
- package/dist/validation-shared.d.ts.map +1 -0
- package/dist/validation-shared.js +1 -0
- package/dist/validation-vouchers.d.ts +2 -0
- package/dist/validation-vouchers.d.ts.map +1 -0
- package/dist/validation-vouchers.js +1 -0
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +1 -0
- package/package.json +121 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { and, asc, desc, eq, gt, ilike, or, sql } from "drizzle-orm";
|
|
2
|
+
import { voucherRedemptions, vouchers } from "./schema.js";
|
|
3
|
+
/**
|
|
4
|
+
* Raised by the voucher service. Code + message; route handlers map to HTTP.
|
|
5
|
+
* Reasons the route layer cares about:
|
|
6
|
+
* - `code_in_use` — supplied code collides with an existing voucher
|
|
7
|
+
* - `voucher_not_found` — id-not-found / code-not-found read path
|
|
8
|
+
* - `voucher_inactive` — redeem attempted against non-active status
|
|
9
|
+
* - `voucher_not_started`— validFrom is set and hasn't happened yet
|
|
10
|
+
* - `voucher_expired` — expiresAt has passed
|
|
11
|
+
* - `insufficient_balance` — requested amount > remainingAmountCents
|
|
12
|
+
*/
|
|
13
|
+
export class VoucherServiceError extends Error {
|
|
14
|
+
code;
|
|
15
|
+
constructor(code, message) {
|
|
16
|
+
super(message ?? code);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.name = "VoucherServiceError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const CODE_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
22
|
+
/**
|
|
23
|
+
* Generate a short, human-friendly voucher code. Crockford-style alphabet
|
|
24
|
+
* (no 0/O/1/I) so codes stay readable when typed from a receipt or email.
|
|
25
|
+
* 12 chars from a 32-symbol alphabet ≈ 60 bits of entropy; unique-index on
|
|
26
|
+
* `code` catches the astronomically-unlikely collision.
|
|
27
|
+
*/
|
|
28
|
+
function generateVoucherCode() {
|
|
29
|
+
const bytes = new Uint8Array(12);
|
|
30
|
+
crypto.getRandomValues(bytes);
|
|
31
|
+
let out = "";
|
|
32
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
33
|
+
const index = (bytes[i] ?? 0) % CODE_ALPHABET.length;
|
|
34
|
+
out += CODE_ALPHABET[index];
|
|
35
|
+
if (i === 3 || i === 7)
|
|
36
|
+
out += "-";
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
export const vouchersService = {
|
|
41
|
+
async list(db, query) {
|
|
42
|
+
const conditions = [];
|
|
43
|
+
if (query.status)
|
|
44
|
+
conditions.push(eq(vouchers.status, query.status));
|
|
45
|
+
if (query.seriesCode)
|
|
46
|
+
conditions.push(eq(vouchers.seriesCode, query.seriesCode));
|
|
47
|
+
if (query.issuedToPersonId) {
|
|
48
|
+
conditions.push(eq(vouchers.issuedToPersonId, query.issuedToPersonId));
|
|
49
|
+
}
|
|
50
|
+
if (query.issuedToOrganizationId) {
|
|
51
|
+
conditions.push(eq(vouchers.issuedToOrganizationId, query.issuedToOrganizationId));
|
|
52
|
+
}
|
|
53
|
+
if (query.hasBalance) {
|
|
54
|
+
conditions.push(gt(vouchers.remainingAmountCents, 0));
|
|
55
|
+
}
|
|
56
|
+
if (query.search) {
|
|
57
|
+
const term = `%${query.search}%`;
|
|
58
|
+
conditions.push(or(ilike(vouchers.code, term), ilike(vouchers.notes, term)));
|
|
59
|
+
}
|
|
60
|
+
const where = conditions.length ? and(...conditions) : undefined;
|
|
61
|
+
const [rows, countResult] = await Promise.all([
|
|
62
|
+
db
|
|
63
|
+
.select()
|
|
64
|
+
.from(vouchers)
|
|
65
|
+
.where(where)
|
|
66
|
+
.limit(query.limit)
|
|
67
|
+
.offset(query.offset)
|
|
68
|
+
.orderBy(desc(vouchers.createdAt)),
|
|
69
|
+
db.select({ count: sql `count(*)::int` }).from(vouchers).where(where),
|
|
70
|
+
]);
|
|
71
|
+
return {
|
|
72
|
+
data: rows,
|
|
73
|
+
total: countResult[0]?.count ?? 0,
|
|
74
|
+
limit: query.limit,
|
|
75
|
+
offset: query.offset,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
async getById(db, id) {
|
|
79
|
+
const [row] = await db.select().from(vouchers).where(eq(vouchers.id, id)).limit(1);
|
|
80
|
+
if (!row)
|
|
81
|
+
return null;
|
|
82
|
+
const redemptions = await db
|
|
83
|
+
.select()
|
|
84
|
+
.from(voucherRedemptions)
|
|
85
|
+
.where(eq(voucherRedemptions.voucherId, id))
|
|
86
|
+
.orderBy(asc(voucherRedemptions.createdAt));
|
|
87
|
+
return { ...row, redemptions };
|
|
88
|
+
},
|
|
89
|
+
async create(db, input, issuedByUserId) {
|
|
90
|
+
const code = input.code?.trim() || generateVoucherCode();
|
|
91
|
+
const [existing] = await db
|
|
92
|
+
.select({ id: vouchers.id })
|
|
93
|
+
.from(vouchers)
|
|
94
|
+
.where(eq(vouchers.code, code))
|
|
95
|
+
.limit(1);
|
|
96
|
+
if (existing) {
|
|
97
|
+
throw new VoucherServiceError("code_in_use");
|
|
98
|
+
}
|
|
99
|
+
const [row] = await db
|
|
100
|
+
.insert(vouchers)
|
|
101
|
+
.values({
|
|
102
|
+
code,
|
|
103
|
+
seriesCode: input.seriesCode ?? null,
|
|
104
|
+
currency: input.currency,
|
|
105
|
+
initialAmountCents: input.amountCents,
|
|
106
|
+
remainingAmountCents: input.amountCents,
|
|
107
|
+
issuedToPersonId: input.issuedToPersonId ?? null,
|
|
108
|
+
issuedToOrganizationId: input.issuedToOrganizationId ?? null,
|
|
109
|
+
sourceType: input.sourceType,
|
|
110
|
+
sourceBookingId: input.sourceBookingId ?? null,
|
|
111
|
+
sourcePaymentId: input.sourcePaymentId ?? null,
|
|
112
|
+
validFrom: input.validFrom ? new Date(input.validFrom) : null,
|
|
113
|
+
expiresAt: input.expiresAt ? new Date(input.expiresAt) : null,
|
|
114
|
+
notes: input.notes ?? null,
|
|
115
|
+
issuedByUserId: issuedByUserId ?? null,
|
|
116
|
+
})
|
|
117
|
+
.returning();
|
|
118
|
+
return row ?? null;
|
|
119
|
+
},
|
|
120
|
+
async update(db, id, input) {
|
|
121
|
+
const [row] = await db
|
|
122
|
+
.update(vouchers)
|
|
123
|
+
.set({
|
|
124
|
+
...(input.status !== undefined ? { status: input.status } : {}),
|
|
125
|
+
...(input.seriesCode !== undefined ? { seriesCode: input.seriesCode } : {}),
|
|
126
|
+
...(input.validFrom !== undefined
|
|
127
|
+
? { validFrom: input.validFrom ? new Date(input.validFrom) : null }
|
|
128
|
+
: {}),
|
|
129
|
+
...(input.expiresAt !== undefined
|
|
130
|
+
? { expiresAt: input.expiresAt ? new Date(input.expiresAt) : null }
|
|
131
|
+
: {}),
|
|
132
|
+
...(input.notes !== undefined ? { notes: input.notes } : {}),
|
|
133
|
+
...(input.issuedToPersonId !== undefined
|
|
134
|
+
? { issuedToPersonId: input.issuedToPersonId }
|
|
135
|
+
: {}),
|
|
136
|
+
...(input.issuedToOrganizationId !== undefined
|
|
137
|
+
? { issuedToOrganizationId: input.issuedToOrganizationId }
|
|
138
|
+
: {}),
|
|
139
|
+
updatedAt: new Date(),
|
|
140
|
+
})
|
|
141
|
+
.where(eq(vouchers.id, id))
|
|
142
|
+
.returning();
|
|
143
|
+
return row ?? null;
|
|
144
|
+
},
|
|
145
|
+
/**
|
|
146
|
+
* Apply a voucher against a booking. Runs in a transaction so
|
|
147
|
+
* `remainingAmountCents` and the redemption row either both land or neither.
|
|
148
|
+
* Guards: voucher must exist, be active, not expired, and have enough
|
|
149
|
+
* balance for the requested amount. When remaining hits zero the voucher
|
|
150
|
+
* flips to `status = 'redeemed'`.
|
|
151
|
+
*/
|
|
152
|
+
async redeem(db, voucherId, input, userId) {
|
|
153
|
+
return db.transaction(async (tx) => {
|
|
154
|
+
const [voucher] = await tx.select().from(vouchers).where(eq(vouchers.id, voucherId)).limit(1);
|
|
155
|
+
if (!voucher)
|
|
156
|
+
throw new VoucherServiceError("voucher_not_found");
|
|
157
|
+
if (voucher.status !== "active")
|
|
158
|
+
throw new VoucherServiceError("voucher_inactive");
|
|
159
|
+
if (voucher.validFrom && voucher.validFrom.getTime() > Date.now()) {
|
|
160
|
+
throw new VoucherServiceError("voucher_not_started");
|
|
161
|
+
}
|
|
162
|
+
if (voucher.expiresAt && voucher.expiresAt.getTime() < Date.now()) {
|
|
163
|
+
throw new VoucherServiceError("voucher_expired");
|
|
164
|
+
}
|
|
165
|
+
if (input.amountCents > voucher.remainingAmountCents) {
|
|
166
|
+
throw new VoucherServiceError("insufficient_balance");
|
|
167
|
+
}
|
|
168
|
+
const [redemption] = await tx
|
|
169
|
+
.insert(voucherRedemptions)
|
|
170
|
+
.values({
|
|
171
|
+
voucherId: voucher.id,
|
|
172
|
+
bookingId: input.bookingId,
|
|
173
|
+
paymentId: input.paymentId ?? null,
|
|
174
|
+
amountCents: input.amountCents,
|
|
175
|
+
createdByUserId: userId ?? null,
|
|
176
|
+
})
|
|
177
|
+
.returning();
|
|
178
|
+
const newRemaining = voucher.remainingAmountCents - input.amountCents;
|
|
179
|
+
const [updated] = await tx
|
|
180
|
+
.update(vouchers)
|
|
181
|
+
.set({
|
|
182
|
+
remainingAmountCents: newRemaining,
|
|
183
|
+
status: newRemaining === 0 ? "redeemed" : voucher.status,
|
|
184
|
+
updatedAt: new Date(),
|
|
185
|
+
})
|
|
186
|
+
.where(eq(vouchers.id, voucher.id))
|
|
187
|
+
.returning();
|
|
188
|
+
return { voucher: updated ?? voucher, redemption: redemption ?? null };
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
};
|