@voyantjs/finance 0.52.2 → 0.52.3
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/dist/action-ledger-drift.d.ts +29 -0
- package/dist/action-ledger-drift.d.ts.map +1 -0
- package/dist/action-ledger-drift.js +163 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -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-documents.d.ts +8 -8
- package/dist/routes-public.d.ts +48 -48
- package/dist/routes-settlement.d.ts +1 -1
- package/dist/routes-shared.d.ts +12 -0
- package/dist/routes-shared.d.ts.map +1 -1
- package/dist/routes.d.ts +224 -224
- package/dist/routes.d.ts.map +1 -1
- package/dist/routes.js +252 -46
- package/dist/schema.d.ts +22 -22
- package/dist/service-action-ledger-accounting.d.ts +77 -0
- package/dist/service-action-ledger-accounting.d.ts.map +1 -0
- package/dist/service-action-ledger-accounting.js +307 -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-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-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 +70 -0
- package/dist/service-action-ledger.d.ts +6 -0
- package/dist/service-action-ledger.d.ts.map +1 -0
- package/dist/service-action-ledger.js +5 -0
- package/dist/service-booking-create.d.ts +12 -12
- package/dist/service-bookings-dual-create.d.ts +12 -12
- package/dist/service-issue.d.ts +15 -12
- package/dist/service-issue.d.ts.map +1 -1
- package/dist/service-issue.js +24 -3
- package/dist/service-public.d.ts +13 -13
- package/dist/service-vouchers.d.ts +10 -10
- package/dist/service.d.ts +291 -279
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +731 -234
- package/dist/validation-billing.d.ts +37 -37
- package/dist/validation-payments.d.ts +102 -102
- package/dist/validation-public.d.ts +45 -45
- package/dist/validation-shared.d.ts +32 -32
- package/dist/validation-vouchers.d.ts +2 -2
- package/package.json +13 -7
package/dist/service.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { appendActionLedgerMutation, } from "@voyantjs/action-ledger";
|
|
1
2
|
import { bookingItems, bookings } from "@voyantjs/bookings/schema";
|
|
2
3
|
import { renderStructuredTemplate } from "@voyantjs/utils/template-renderer";
|
|
3
4
|
import { and, asc, desc, eq, gte, ilike, lte, ne, or, sql } from "drizzle-orm";
|
|
4
5
|
import { bookingGuarantees, bookingItemCommissions, bookingItemTaxLines, bookingPaymentSchedules, creditNoteLineItems, creditNotes, financeNotes, invoiceAttachments, invoiceExternalRefs, invoiceLineItems, invoiceNumberSeries, invoiceRenditions, invoices, invoiceTemplates, paymentAuthorizations, paymentCaptures, paymentInstruments, paymentSessions, payments, supplierPayments, taxClasses, taxPolicyProfiles, taxPolicyRules, taxRegimes, } from "./schema.js";
|
|
6
|
+
import { buildBookingGuaranteeCreateActionLedgerInput, buildBookingGuaranteeDeleteActionLedgerInput, buildBookingGuaranteeUpdateActionLedgerInput, buildBookingPaymentScheduleCreateActionLedgerInput, buildBookingPaymentScheduleDeleteActionLedgerInput, buildBookingPaymentScheduleUpdateActionLedgerInput, buildCreditNoteCreationActionLedgerInput, buildCreditNoteLineItemCreateActionLedgerInput, buildCreditNoteUpdateActionLedgerInput, buildInvoiceDeleteActionLedgerInput, buildInvoiceLineItemCreateActionLedgerInput, buildInvoiceLineItemDeleteActionLedgerInput, buildInvoiceLineItemUpdateActionLedgerInput, buildInvoiceUpdateActionLedgerInput, buildPaymentAuthorizationCreateActionLedgerInput, buildPaymentAuthorizationDeleteActionLedgerInput, buildPaymentAuthorizationUpdateActionLedgerInput, buildPaymentCaptureCreateActionLedgerInput, buildPaymentCaptureDeleteActionLedgerInput, buildPaymentCaptureUpdateActionLedgerInput, buildPaymentInstrumentCreateActionLedgerInput, buildPaymentInstrumentDeleteActionLedgerInput, buildPaymentInstrumentUpdateActionLedgerInput, buildPaymentSessionCancelledActionLedgerInput, buildPaymentSessionCompletionActionLedgerInput, buildPaymentSessionCreateActionLedgerInput, buildPaymentSessionExpiredActionLedgerInput, buildPaymentSessionFailedActionLedgerInput, buildPaymentSessionRequiresRedirectActionLedgerInput, buildPaymentSessionUpdateActionLedgerInput, buildRecordPaymentActionLedgerInput, buildSupplierPaymentCreateActionLedgerInput, buildSupplierPaymentUpdateActionLedgerInput, } from "./service-action-ledger.js";
|
|
5
7
|
import { getFinanceAggregates } from "./service-aggregates.js";
|
|
6
8
|
import { vouchersService } from "./service-vouchers.js";
|
|
9
|
+
export { buildBookingGuaranteeCreateActionLedgerInput, buildBookingGuaranteeDeleteActionLedgerInput, buildBookingGuaranteeUpdateActionLedgerInput, buildBookingPaymentScheduleCreateActionLedgerInput, buildBookingPaymentScheduleDeleteActionLedgerInput, buildBookingPaymentScheduleUpdateActionLedgerInput, buildCreditNoteCreationActionLedgerInput, buildCreditNoteLineItemCreateActionLedgerInput, buildCreditNoteUpdateActionLedgerInput, buildInvoiceDeleteActionLedgerInput, buildInvoiceIssuedActionLedgerInput, buildInvoiceLineItemCreateActionLedgerInput, buildInvoiceLineItemDeleteActionLedgerInput, buildInvoiceLineItemUpdateActionLedgerInput, buildInvoiceUpdateActionLedgerInput, buildPaymentAuthorizationCreateActionLedgerInput, buildPaymentAuthorizationDeleteActionLedgerInput, buildPaymentAuthorizationUpdateActionLedgerInput, buildPaymentCaptureCreateActionLedgerInput, buildPaymentCaptureDeleteActionLedgerInput, buildPaymentCaptureUpdateActionLedgerInput, buildPaymentInstrumentCreateActionLedgerInput, buildPaymentInstrumentDeleteActionLedgerInput, buildPaymentInstrumentUpdateActionLedgerInput, buildPaymentSessionCancelledActionLedgerInput, buildPaymentSessionCompletionActionLedgerInput, buildPaymentSessionCreateActionLedgerInput, buildPaymentSessionExpiredActionLedgerInput, buildPaymentSessionFailedActionLedgerInput, buildPaymentSessionRequiresRedirectActionLedgerInput, buildPaymentSessionUpdateActionLedgerInput, buildRecordPaymentActionLedgerInput, buildSupplierPaymentCreateActionLedgerInput, buildSupplierPaymentUpdateActionLedgerInput, } from "./service-action-ledger.js";
|
|
7
10
|
export class PaymentValidationError extends Error {
|
|
8
11
|
status = 400;
|
|
9
12
|
code = "invalid_request";
|
|
@@ -253,19 +256,59 @@ export const financeService = {
|
|
|
253
256
|
.limit(1);
|
|
254
257
|
return row ?? null;
|
|
255
258
|
},
|
|
256
|
-
async createPaymentInstrument(db, data) {
|
|
257
|
-
const
|
|
259
|
+
async createPaymentInstrument(db, data, runtime = {}) {
|
|
260
|
+
const createInstrument = (writer) => writer.insert(paymentInstruments).values(data).returning();
|
|
261
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
262
|
+
if (actionLedgerContext) {
|
|
263
|
+
const [row] = await db.transaction(async (tx) => {
|
|
264
|
+
const created = await createInstrument(tx);
|
|
265
|
+
if (created[0]) {
|
|
266
|
+
await appendActionLedgerMutation(tx, buildPaymentInstrumentCreateActionLedgerInput(actionLedgerContext, { instrument: created[0] }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
267
|
+
}
|
|
268
|
+
return created;
|
|
269
|
+
});
|
|
270
|
+
return row ?? null;
|
|
271
|
+
}
|
|
272
|
+
const [row] = await createInstrument(db);
|
|
258
273
|
return row ?? null;
|
|
259
274
|
},
|
|
260
|
-
async updatePaymentInstrument(db, id, data) {
|
|
261
|
-
const
|
|
275
|
+
async updatePaymentInstrument(db, id, data, runtime = {}) {
|
|
276
|
+
const updateInstrument = (writer) => writer
|
|
262
277
|
.update(paymentInstruments)
|
|
263
278
|
.set({ ...data, updatedAt: new Date() })
|
|
264
279
|
.where(eq(paymentInstruments.id, id))
|
|
265
280
|
.returning();
|
|
281
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
282
|
+
if (actionLedgerContext) {
|
|
283
|
+
const [row] = await db.transaction(async (tx) => {
|
|
284
|
+
const updated = await updateInstrument(tx);
|
|
285
|
+
if (updated[0]) {
|
|
286
|
+
await appendActionLedgerMutation(tx, buildPaymentInstrumentUpdateActionLedgerInput(actionLedgerContext, { instrument: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
287
|
+
}
|
|
288
|
+
return updated;
|
|
289
|
+
});
|
|
290
|
+
return row ?? null;
|
|
291
|
+
}
|
|
292
|
+
const [row] = await updateInstrument(db);
|
|
266
293
|
return row ?? null;
|
|
267
294
|
},
|
|
268
|
-
async deletePaymentInstrument(db, id) {
|
|
295
|
+
async deletePaymentInstrument(db, id, runtime = {}) {
|
|
296
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
297
|
+
if (actionLedgerContext) {
|
|
298
|
+
return db.transaction(async (tx) => {
|
|
299
|
+
const [existing] = await tx
|
|
300
|
+
.select()
|
|
301
|
+
.from(paymentInstruments)
|
|
302
|
+
.where(eq(paymentInstruments.id, id))
|
|
303
|
+
.limit(1);
|
|
304
|
+
if (!existing) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
await tx.delete(paymentInstruments).where(eq(paymentInstruments.id, id));
|
|
308
|
+
await appendActionLedgerMutation(tx, buildPaymentInstrumentDeleteActionLedgerInput(actionLedgerContext, { instrument: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
309
|
+
return { id: existing.id };
|
|
310
|
+
});
|
|
311
|
+
}
|
|
269
312
|
const [row] = await db
|
|
270
313
|
.delete(paymentInstruments)
|
|
271
314
|
.where(eq(paymentInstruments.id, id))
|
|
@@ -320,7 +363,7 @@ export const financeService = {
|
|
|
320
363
|
const [row] = await db.select().from(paymentSessions).where(eq(paymentSessions.id, id)).limit(1);
|
|
321
364
|
return row ?? null;
|
|
322
365
|
},
|
|
323
|
-
async createPaymentSession(db, data) {
|
|
366
|
+
async createPaymentSession(db, data, runtime = {}) {
|
|
324
367
|
if (data.idempotencyKey) {
|
|
325
368
|
const [existing] = await db
|
|
326
369
|
.select()
|
|
@@ -332,7 +375,7 @@ export const financeService = {
|
|
|
332
375
|
}
|
|
333
376
|
}
|
|
334
377
|
const target = derivePaymentSessionTarget(data);
|
|
335
|
-
const
|
|
378
|
+
const createSession = (writer) => writer
|
|
336
379
|
.insert(paymentSessions)
|
|
337
380
|
.values({
|
|
338
381
|
...data,
|
|
@@ -348,11 +391,23 @@ export const financeService = {
|
|
|
348
391
|
expiresAt: toTimestamp(data.expiresAt),
|
|
349
392
|
})
|
|
350
393
|
.returning();
|
|
394
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
395
|
+
if (actionLedgerContext) {
|
|
396
|
+
const [row] = await db.transaction(async (tx) => {
|
|
397
|
+
const created = await createSession(tx);
|
|
398
|
+
if (created[0]) {
|
|
399
|
+
await appendActionLedgerMutation(tx, await buildPaymentSessionCreateActionLedgerInput(actionLedgerContext, { session: created[0] }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
400
|
+
}
|
|
401
|
+
return created;
|
|
402
|
+
});
|
|
403
|
+
return row ?? null;
|
|
404
|
+
}
|
|
405
|
+
const [row] = await createSession(db);
|
|
351
406
|
return row ?? null;
|
|
352
407
|
},
|
|
353
|
-
async updatePaymentSession(db, id, data) {
|
|
408
|
+
async updatePaymentSession(db, id, data, runtime = {}) {
|
|
354
409
|
const target = derivePaymentSessionTarget(data);
|
|
355
|
-
const
|
|
410
|
+
const updateSession = (writer) => writer
|
|
356
411
|
.update(paymentSessions)
|
|
357
412
|
.set({
|
|
358
413
|
...data,
|
|
@@ -372,10 +427,22 @@ export const financeService = {
|
|
|
372
427
|
})
|
|
373
428
|
.where(eq(paymentSessions.id, id))
|
|
374
429
|
.returning();
|
|
430
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
431
|
+
if (actionLedgerContext) {
|
|
432
|
+
const [row] = await db.transaction(async (tx) => {
|
|
433
|
+
const updated = await updateSession(tx);
|
|
434
|
+
if (updated[0]) {
|
|
435
|
+
await appendActionLedgerMutation(tx, buildPaymentSessionUpdateActionLedgerInput(actionLedgerContext, { session: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
436
|
+
}
|
|
437
|
+
return updated;
|
|
438
|
+
});
|
|
439
|
+
return row ?? null;
|
|
440
|
+
}
|
|
441
|
+
const [row] = await updateSession(db);
|
|
375
442
|
return row ?? null;
|
|
376
443
|
},
|
|
377
|
-
async markPaymentSessionRequiresRedirect(db, id, data) {
|
|
378
|
-
const
|
|
444
|
+
async markPaymentSessionRequiresRedirect(db, id, data, runtime = {}) {
|
|
445
|
+
const markRequiresRedirect = (writer) => writer
|
|
379
446
|
.update(paymentSessions)
|
|
380
447
|
.set({
|
|
381
448
|
status: "requires_redirect",
|
|
@@ -395,10 +462,22 @@ export const financeService = {
|
|
|
395
462
|
})
|
|
396
463
|
.where(eq(paymentSessions.id, id))
|
|
397
464
|
.returning();
|
|
465
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
466
|
+
if (actionLedgerContext) {
|
|
467
|
+
const [row] = await db.transaction(async (tx) => {
|
|
468
|
+
const updated = await markRequiresRedirect(tx);
|
|
469
|
+
if (updated[0]) {
|
|
470
|
+
await appendActionLedgerMutation(tx, buildPaymentSessionRequiresRedirectActionLedgerInput(actionLedgerContext, { session: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
471
|
+
}
|
|
472
|
+
return updated;
|
|
473
|
+
});
|
|
474
|
+
return row ?? null;
|
|
475
|
+
}
|
|
476
|
+
const [row] = await markRequiresRedirect(db);
|
|
398
477
|
return row ?? null;
|
|
399
478
|
},
|
|
400
|
-
async failPaymentSession(db, id, data) {
|
|
401
|
-
const
|
|
479
|
+
async failPaymentSession(db, id, data, runtime = {}) {
|
|
480
|
+
const failSession = (writer) => writer
|
|
402
481
|
.update(paymentSessions)
|
|
403
482
|
.set({
|
|
404
483
|
status: "failed",
|
|
@@ -415,10 +494,22 @@ export const financeService = {
|
|
|
415
494
|
})
|
|
416
495
|
.where(eq(paymentSessions.id, id))
|
|
417
496
|
.returning();
|
|
497
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
498
|
+
if (actionLedgerContext) {
|
|
499
|
+
const [row] = await db.transaction(async (tx) => {
|
|
500
|
+
const updated = await failSession(tx);
|
|
501
|
+
if (updated[0]) {
|
|
502
|
+
await appendActionLedgerMutation(tx, buildPaymentSessionFailedActionLedgerInput(actionLedgerContext, { session: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
503
|
+
}
|
|
504
|
+
return updated;
|
|
505
|
+
});
|
|
506
|
+
return row ?? null;
|
|
507
|
+
}
|
|
508
|
+
const [row] = await failSession(db);
|
|
418
509
|
return row ?? null;
|
|
419
510
|
},
|
|
420
|
-
async cancelPaymentSession(db, id, data) {
|
|
421
|
-
const
|
|
511
|
+
async cancelPaymentSession(db, id, data, runtime = {}) {
|
|
512
|
+
const cancelSession = (writer) => writer
|
|
422
513
|
.update(paymentSessions)
|
|
423
514
|
.set({
|
|
424
515
|
status: "cancelled",
|
|
@@ -430,10 +521,22 @@ export const financeService = {
|
|
|
430
521
|
})
|
|
431
522
|
.where(eq(paymentSessions.id, id))
|
|
432
523
|
.returning();
|
|
524
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
525
|
+
if (actionLedgerContext) {
|
|
526
|
+
const [row] = await db.transaction(async (tx) => {
|
|
527
|
+
const updated = await cancelSession(tx);
|
|
528
|
+
if (updated[0]) {
|
|
529
|
+
await appendActionLedgerMutation(tx, buildPaymentSessionCancelledActionLedgerInput(actionLedgerContext, { session: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
530
|
+
}
|
|
531
|
+
return updated;
|
|
532
|
+
});
|
|
533
|
+
return row ?? null;
|
|
534
|
+
}
|
|
535
|
+
const [row] = await cancelSession(db);
|
|
433
536
|
return row ?? null;
|
|
434
537
|
},
|
|
435
|
-
async expirePaymentSession(db, id, data) {
|
|
436
|
-
const
|
|
538
|
+
async expirePaymentSession(db, id, data, runtime = {}) {
|
|
539
|
+
const expireSession = (writer) => writer
|
|
437
540
|
.update(paymentSessions)
|
|
438
541
|
.set({
|
|
439
542
|
status: "expired",
|
|
@@ -445,6 +548,18 @@ export const financeService = {
|
|
|
445
548
|
})
|
|
446
549
|
.where(eq(paymentSessions.id, id))
|
|
447
550
|
.returning();
|
|
551
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
552
|
+
if (actionLedgerContext) {
|
|
553
|
+
const [row] = await db.transaction(async (tx) => {
|
|
554
|
+
const updated = await expireSession(tx);
|
|
555
|
+
if (updated[0]) {
|
|
556
|
+
await appendActionLedgerMutation(tx, buildPaymentSessionExpiredActionLedgerInput(actionLedgerContext, { session: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
557
|
+
}
|
|
558
|
+
return updated;
|
|
559
|
+
});
|
|
560
|
+
return row ?? null;
|
|
561
|
+
}
|
|
562
|
+
const [row] = await expireSession(db);
|
|
448
563
|
return row ?? null;
|
|
449
564
|
},
|
|
450
565
|
async completePaymentSession(db, id, data, runtime = {}) {
|
|
@@ -628,6 +743,15 @@ export const financeService = {
|
|
|
628
743
|
})
|
|
629
744
|
.where(eq(paymentSessions.id, id))
|
|
630
745
|
.returning();
|
|
746
|
+
if (updated && runtime.actionLedgerContext) {
|
|
747
|
+
await appendActionLedgerMutation(tx, await buildPaymentSessionCompletionActionLedgerInput(runtime.actionLedgerContext, {
|
|
748
|
+
session: updated,
|
|
749
|
+
status: data.status,
|
|
750
|
+
paymentId,
|
|
751
|
+
}, {
|
|
752
|
+
authorizationSource: runtime.actionLedgerAuthorizationSource,
|
|
753
|
+
}));
|
|
754
|
+
}
|
|
631
755
|
return {
|
|
632
756
|
updated: updated ?? null,
|
|
633
757
|
settlement: settlementForEmit,
|
|
@@ -692,8 +816,8 @@ export const financeService = {
|
|
|
692
816
|
.limit(1);
|
|
693
817
|
return row ?? null;
|
|
694
818
|
},
|
|
695
|
-
async createPaymentAuthorization(db, data) {
|
|
696
|
-
const
|
|
819
|
+
async createPaymentAuthorization(db, data, runtime = {}) {
|
|
820
|
+
const createAuthorization = (writer) => writer
|
|
697
821
|
.insert(paymentAuthorizations)
|
|
698
822
|
.values({
|
|
699
823
|
...data,
|
|
@@ -702,10 +826,22 @@ export const financeService = {
|
|
|
702
826
|
voidedAt: toTimestamp(data.voidedAt),
|
|
703
827
|
})
|
|
704
828
|
.returning();
|
|
829
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
830
|
+
if (actionLedgerContext) {
|
|
831
|
+
const [row] = await db.transaction(async (tx) => {
|
|
832
|
+
const created = await createAuthorization(tx);
|
|
833
|
+
if (created[0]) {
|
|
834
|
+
await appendActionLedgerMutation(tx, await buildPaymentAuthorizationCreateActionLedgerInput(actionLedgerContext, { authorization: created[0] }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
835
|
+
}
|
|
836
|
+
return created;
|
|
837
|
+
});
|
|
838
|
+
return row ?? null;
|
|
839
|
+
}
|
|
840
|
+
const [row] = await createAuthorization(db);
|
|
705
841
|
return row ?? null;
|
|
706
842
|
},
|
|
707
|
-
async updatePaymentAuthorization(db, id, data) {
|
|
708
|
-
const
|
|
843
|
+
async updatePaymentAuthorization(db, id, data, runtime = {}) {
|
|
844
|
+
const updateAuthorization = (writer) => writer
|
|
709
845
|
.update(paymentAuthorizations)
|
|
710
846
|
.set({
|
|
711
847
|
...data,
|
|
@@ -716,9 +852,37 @@ export const financeService = {
|
|
|
716
852
|
})
|
|
717
853
|
.where(eq(paymentAuthorizations.id, id))
|
|
718
854
|
.returning();
|
|
855
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
856
|
+
if (actionLedgerContext) {
|
|
857
|
+
const [row] = await db.transaction(async (tx) => {
|
|
858
|
+
const updated = await updateAuthorization(tx);
|
|
859
|
+
if (updated[0]) {
|
|
860
|
+
await appendActionLedgerMutation(tx, buildPaymentAuthorizationUpdateActionLedgerInput(actionLedgerContext, { authorization: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
861
|
+
}
|
|
862
|
+
return updated;
|
|
863
|
+
});
|
|
864
|
+
return row ?? null;
|
|
865
|
+
}
|
|
866
|
+
const [row] = await updateAuthorization(db);
|
|
719
867
|
return row ?? null;
|
|
720
868
|
},
|
|
721
|
-
async deletePaymentAuthorization(db, id) {
|
|
869
|
+
async deletePaymentAuthorization(db, id, runtime = {}) {
|
|
870
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
871
|
+
if (actionLedgerContext) {
|
|
872
|
+
return db.transaction(async (tx) => {
|
|
873
|
+
const [existing] = await tx
|
|
874
|
+
.select()
|
|
875
|
+
.from(paymentAuthorizations)
|
|
876
|
+
.where(eq(paymentAuthorizations.id, id))
|
|
877
|
+
.limit(1);
|
|
878
|
+
if (!existing) {
|
|
879
|
+
return null;
|
|
880
|
+
}
|
|
881
|
+
await tx.delete(paymentAuthorizations).where(eq(paymentAuthorizations.id, id));
|
|
882
|
+
await appendActionLedgerMutation(tx, buildPaymentAuthorizationDeleteActionLedgerInput(actionLedgerContext, { authorization: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
883
|
+
return { id: existing.id };
|
|
884
|
+
});
|
|
885
|
+
}
|
|
722
886
|
const [row] = await db
|
|
723
887
|
.delete(paymentAuthorizations)
|
|
724
888
|
.where(eq(paymentAuthorizations.id, id))
|
|
@@ -746,8 +910,8 @@ export const financeService = {
|
|
|
746
910
|
const [row] = await db.select().from(paymentCaptures).where(eq(paymentCaptures.id, id)).limit(1);
|
|
747
911
|
return row ?? null;
|
|
748
912
|
},
|
|
749
|
-
async createPaymentCapture(db, data) {
|
|
750
|
-
const
|
|
913
|
+
async createPaymentCapture(db, data, runtime = {}) {
|
|
914
|
+
const createCapture = (writer) => writer
|
|
751
915
|
.insert(paymentCaptures)
|
|
752
916
|
.values({
|
|
753
917
|
...data,
|
|
@@ -755,10 +919,22 @@ export const financeService = {
|
|
|
755
919
|
settledAt: toTimestamp(data.settledAt),
|
|
756
920
|
})
|
|
757
921
|
.returning();
|
|
922
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
923
|
+
if (actionLedgerContext) {
|
|
924
|
+
const [row] = await db.transaction(async (tx) => {
|
|
925
|
+
const created = await createCapture(tx);
|
|
926
|
+
if (created[0]) {
|
|
927
|
+
await appendActionLedgerMutation(tx, await buildPaymentCaptureCreateActionLedgerInput(actionLedgerContext, { capture: created[0] }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
928
|
+
}
|
|
929
|
+
return created;
|
|
930
|
+
});
|
|
931
|
+
return row ?? null;
|
|
932
|
+
}
|
|
933
|
+
const [row] = await createCapture(db);
|
|
758
934
|
return row ?? null;
|
|
759
935
|
},
|
|
760
|
-
async updatePaymentCapture(db, id, data) {
|
|
761
|
-
const
|
|
936
|
+
async updatePaymentCapture(db, id, data, runtime = {}) {
|
|
937
|
+
const updateCapture = (writer) => writer
|
|
762
938
|
.update(paymentCaptures)
|
|
763
939
|
.set({
|
|
764
940
|
...data,
|
|
@@ -768,9 +944,37 @@ export const financeService = {
|
|
|
768
944
|
})
|
|
769
945
|
.where(eq(paymentCaptures.id, id))
|
|
770
946
|
.returning();
|
|
947
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
948
|
+
if (actionLedgerContext) {
|
|
949
|
+
const [row] = await db.transaction(async (tx) => {
|
|
950
|
+
const updated = await updateCapture(tx);
|
|
951
|
+
if (updated[0]) {
|
|
952
|
+
await appendActionLedgerMutation(tx, buildPaymentCaptureUpdateActionLedgerInput(actionLedgerContext, { capture: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
953
|
+
}
|
|
954
|
+
return updated;
|
|
955
|
+
});
|
|
956
|
+
return row ?? null;
|
|
957
|
+
}
|
|
958
|
+
const [row] = await updateCapture(db);
|
|
771
959
|
return row ?? null;
|
|
772
960
|
},
|
|
773
|
-
async deletePaymentCapture(db, id) {
|
|
961
|
+
async deletePaymentCapture(db, id, runtime = {}) {
|
|
962
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
963
|
+
if (actionLedgerContext) {
|
|
964
|
+
return db.transaction(async (tx) => {
|
|
965
|
+
const [existing] = await tx
|
|
966
|
+
.select()
|
|
967
|
+
.from(paymentCaptures)
|
|
968
|
+
.where(eq(paymentCaptures.id, id))
|
|
969
|
+
.limit(1);
|
|
970
|
+
if (!existing) {
|
|
971
|
+
return null;
|
|
972
|
+
}
|
|
973
|
+
await tx.delete(paymentCaptures).where(eq(paymentCaptures.id, id));
|
|
974
|
+
await appendActionLedgerMutation(tx, buildPaymentCaptureDeleteActionLedgerInput(actionLedgerContext, { capture: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
975
|
+
return { id: existing.id };
|
|
976
|
+
});
|
|
977
|
+
}
|
|
774
978
|
const [row] = await db
|
|
775
979
|
.delete(paymentCaptures)
|
|
776
980
|
.where(eq(paymentCaptures.id, id))
|
|
@@ -784,20 +988,33 @@ export const financeService = {
|
|
|
784
988
|
.where(eq(bookingPaymentSchedules.bookingId, bookingId))
|
|
785
989
|
.orderBy(asc(bookingPaymentSchedules.dueDate), asc(bookingPaymentSchedules.createdAt));
|
|
786
990
|
},
|
|
787
|
-
async createBookingPaymentSchedule(db, bookingId, data) {
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
991
|
+
async createBookingPaymentSchedule(db, bookingId, data, runtime = {}) {
|
|
992
|
+
const createSchedule = async (writer) => {
|
|
993
|
+
const [booking] = await writer
|
|
994
|
+
.select({ id: bookings.id })
|
|
995
|
+
.from(bookings)
|
|
996
|
+
.where(eq(bookings.id, bookingId))
|
|
997
|
+
.limit(1);
|
|
998
|
+
if (!booking) {
|
|
999
|
+
return null;
|
|
1000
|
+
}
|
|
1001
|
+
const [row] = await writer
|
|
1002
|
+
.insert(bookingPaymentSchedules)
|
|
1003
|
+
.values({ ...data, bookingId })
|
|
1004
|
+
.returning();
|
|
1005
|
+
return row ?? null;
|
|
1006
|
+
};
|
|
1007
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1008
|
+
if (actionLedgerContext) {
|
|
1009
|
+
return db.transaction(async (tx) => {
|
|
1010
|
+
const row = await createSchedule(tx);
|
|
1011
|
+
if (row) {
|
|
1012
|
+
await appendActionLedgerMutation(tx, buildBookingPaymentScheduleCreateActionLedgerInput(actionLedgerContext, { schedule: row }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1013
|
+
}
|
|
1014
|
+
return row;
|
|
1015
|
+
});
|
|
795
1016
|
}
|
|
796
|
-
|
|
797
|
-
.insert(bookingPaymentSchedules)
|
|
798
|
-
.values({ ...data, bookingId })
|
|
799
|
-
.returning();
|
|
800
|
-
return row ?? null;
|
|
1017
|
+
return createSchedule(db);
|
|
801
1018
|
},
|
|
802
1019
|
/**
|
|
803
1020
|
* Persist a payment schedule that was already computed elsewhere
|
|
@@ -849,116 +1066,183 @@ export const financeService = {
|
|
|
849
1066
|
});
|
|
850
1067
|
return db.insert(bookingPaymentSchedules).values(rows).returning();
|
|
851
1068
|
},
|
|
852
|
-
async applyDefaultBookingPaymentPlan(db, bookingId, data) {
|
|
853
|
-
const
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
}
|
|
891
|
-
scheduleRows
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
scheduleRows.push({
|
|
904
|
-
bookingItemId: null,
|
|
905
|
-
scheduleType: "balance",
|
|
906
|
-
status: singleDueDate <= today ? "due" : "pending",
|
|
907
|
-
dueDate: toDateString(singleDueDate),
|
|
908
|
-
currency: booking.sellCurrency,
|
|
909
|
-
amountCents: totalAmountCents,
|
|
910
|
-
notes: data.notes ?? null,
|
|
911
|
-
});
|
|
912
|
-
}
|
|
913
|
-
const createdSchedules = await db
|
|
914
|
-
.insert(bookingPaymentSchedules)
|
|
915
|
-
.values(scheduleRows.map((row) => ({
|
|
916
|
-
...row,
|
|
917
|
-
bookingId,
|
|
918
|
-
bookingItemId: row.bookingItemId ?? null,
|
|
919
|
-
notes: row.notes ?? null,
|
|
920
|
-
})))
|
|
921
|
-
.returning();
|
|
922
|
-
if (data.createGuarantee) {
|
|
923
|
-
const depositSchedule = createdSchedules.find((schedule) => schedule.scheduleType === "deposit");
|
|
924
|
-
if (depositSchedule) {
|
|
925
|
-
await db.insert(bookingGuarantees).values({
|
|
926
|
-
bookingId,
|
|
927
|
-
bookingPaymentScheduleId: depositSchedule.id,
|
|
1069
|
+
async applyDefaultBookingPaymentPlan(db, bookingId, data, runtime = {}) {
|
|
1070
|
+
const applyPlan = async (writer) => {
|
|
1071
|
+
const [booking] = await writer
|
|
1072
|
+
.select()
|
|
1073
|
+
.from(bookings)
|
|
1074
|
+
.where(eq(bookings.id, bookingId))
|
|
1075
|
+
.limit(1);
|
|
1076
|
+
if (!booking) {
|
|
1077
|
+
return null;
|
|
1078
|
+
}
|
|
1079
|
+
const totalAmountCents = booking.sellAmountCents ?? 0;
|
|
1080
|
+
if (totalAmountCents <= 0) {
|
|
1081
|
+
return {
|
|
1082
|
+
createdSchedules: [],
|
|
1083
|
+
deletedSchedules: [],
|
|
1084
|
+
createdGuarantee: null,
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
const today = startOfUtcDay(new Date());
|
|
1088
|
+
const depositDueDate = data.depositDueDate ? parseDateString(data.depositDueDate) : today;
|
|
1089
|
+
const startDate = booking.startDate ? parseDateString(booking.startDate) : null;
|
|
1090
|
+
const rawBalanceDueDate = startDate
|
|
1091
|
+
? new Date(startDate.getTime() - data.balanceDueDaysBeforeStart * 24 * 60 * 60 * 1000)
|
|
1092
|
+
: today;
|
|
1093
|
+
const balanceDueDate = rawBalanceDueDate < today ? today : rawBalanceDueDate;
|
|
1094
|
+
let depositAmountCents = 0;
|
|
1095
|
+
if (data.depositMode === "fixed_amount") {
|
|
1096
|
+
depositAmountCents = Math.min(totalAmountCents, data.depositValue);
|
|
1097
|
+
}
|
|
1098
|
+
else if (data.depositMode === "percentage") {
|
|
1099
|
+
depositAmountCents = Math.min(totalAmountCents, Math.round((totalAmountCents * data.depositValue) / 100));
|
|
1100
|
+
}
|
|
1101
|
+
const clearableScheduleWhere = and(eq(bookingPaymentSchedules.bookingId, bookingId), or(eq(bookingPaymentSchedules.status, "pending"), eq(bookingPaymentSchedules.status, "due")));
|
|
1102
|
+
const deletedSchedules = data.clearExistingPending
|
|
1103
|
+
? await writer.select().from(bookingPaymentSchedules).where(clearableScheduleWhere)
|
|
1104
|
+
: [];
|
|
1105
|
+
if (data.clearExistingPending) {
|
|
1106
|
+
await writer.delete(bookingPaymentSchedules).where(clearableScheduleWhere);
|
|
1107
|
+
}
|
|
1108
|
+
const scheduleRows = [];
|
|
1109
|
+
if (depositAmountCents > 0 && depositAmountCents < totalAmountCents) {
|
|
1110
|
+
scheduleRows.push({
|
|
1111
|
+
bookingItemId: null,
|
|
1112
|
+
scheduleType: "deposit",
|
|
1113
|
+
status: depositDueDate <= today ? "due" : "pending",
|
|
1114
|
+
dueDate: toDateString(depositDueDate),
|
|
1115
|
+
currency: booking.sellCurrency,
|
|
1116
|
+
amountCents: depositAmountCents,
|
|
1117
|
+
notes: data.notes ?? null,
|
|
1118
|
+
});
|
|
1119
|
+
scheduleRows.push({
|
|
928
1120
|
bookingItemId: null,
|
|
929
|
-
|
|
930
|
-
status: "pending",
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
amountCents: depositSchedule.amountCents,
|
|
935
|
-
provider: null,
|
|
936
|
-
referenceNumber: null,
|
|
937
|
-
guaranteedAt: null,
|
|
938
|
-
expiresAt: null,
|
|
939
|
-
releasedAt: null,
|
|
1121
|
+
scheduleType: "balance",
|
|
1122
|
+
status: balanceDueDate <= today ? "due" : "pending",
|
|
1123
|
+
dueDate: toDateString(balanceDueDate),
|
|
1124
|
+
currency: booking.sellCurrency,
|
|
1125
|
+
amountCents: Math.max(0, totalAmountCents - depositAmountCents),
|
|
940
1126
|
notes: data.notes ?? null,
|
|
941
1127
|
});
|
|
942
1128
|
}
|
|
1129
|
+
else {
|
|
1130
|
+
const singleDueDate = balanceDueDate <= today ? today : balanceDueDate;
|
|
1131
|
+
scheduleRows.push({
|
|
1132
|
+
bookingItemId: null,
|
|
1133
|
+
scheduleType: "balance",
|
|
1134
|
+
status: singleDueDate <= today ? "due" : "pending",
|
|
1135
|
+
dueDate: toDateString(singleDueDate),
|
|
1136
|
+
currency: booking.sellCurrency,
|
|
1137
|
+
amountCents: totalAmountCents,
|
|
1138
|
+
notes: data.notes ?? null,
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
const createdSchedules = await writer
|
|
1142
|
+
.insert(bookingPaymentSchedules)
|
|
1143
|
+
.values(scheduleRows.map((row) => ({
|
|
1144
|
+
...row,
|
|
1145
|
+
bookingId,
|
|
1146
|
+
bookingItemId: row.bookingItemId ?? null,
|
|
1147
|
+
notes: row.notes ?? null,
|
|
1148
|
+
})))
|
|
1149
|
+
.returning();
|
|
1150
|
+
let createdGuarantee = null;
|
|
1151
|
+
if (data.createGuarantee) {
|
|
1152
|
+
const depositSchedule = createdSchedules.find((schedule) => schedule.scheduleType === "deposit");
|
|
1153
|
+
if (depositSchedule) {
|
|
1154
|
+
const [guarantee] = await writer
|
|
1155
|
+
.insert(bookingGuarantees)
|
|
1156
|
+
.values({
|
|
1157
|
+
bookingId,
|
|
1158
|
+
bookingPaymentScheduleId: depositSchedule.id,
|
|
1159
|
+
bookingItemId: null,
|
|
1160
|
+
guaranteeType: data.guaranteeType,
|
|
1161
|
+
status: "pending",
|
|
1162
|
+
paymentInstrumentId: null,
|
|
1163
|
+
paymentAuthorizationId: null,
|
|
1164
|
+
currency: depositSchedule.currency,
|
|
1165
|
+
amountCents: depositSchedule.amountCents,
|
|
1166
|
+
provider: null,
|
|
1167
|
+
referenceNumber: null,
|
|
1168
|
+
guaranteedAt: null,
|
|
1169
|
+
expiresAt: null,
|
|
1170
|
+
releasedAt: null,
|
|
1171
|
+
notes: data.notes ?? null,
|
|
1172
|
+
})
|
|
1173
|
+
.returning();
|
|
1174
|
+
createdGuarantee = guarantee ?? null;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
return { createdSchedules, deletedSchedules, createdGuarantee };
|
|
1178
|
+
};
|
|
1179
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1180
|
+
if (actionLedgerContext) {
|
|
1181
|
+
const result = await db.transaction(async (tx) => {
|
|
1182
|
+
const applied = await applyPlan(tx);
|
|
1183
|
+
if (!applied) {
|
|
1184
|
+
return null;
|
|
1185
|
+
}
|
|
1186
|
+
for (const schedule of applied.deletedSchedules) {
|
|
1187
|
+
await appendActionLedgerMutation(tx, buildBookingPaymentScheduleDeleteActionLedgerInput(actionLedgerContext, { schedule }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1188
|
+
}
|
|
1189
|
+
for (const schedule of applied.createdSchedules) {
|
|
1190
|
+
await appendActionLedgerMutation(tx, buildBookingPaymentScheduleCreateActionLedgerInput(actionLedgerContext, { schedule }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1191
|
+
}
|
|
1192
|
+
if (applied.createdGuarantee) {
|
|
1193
|
+
await appendActionLedgerMutation(tx, await buildBookingGuaranteeCreateActionLedgerInput(actionLedgerContext, { guarantee: applied.createdGuarantee }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1194
|
+
}
|
|
1195
|
+
return applied.createdSchedules;
|
|
1196
|
+
});
|
|
1197
|
+
return result;
|
|
943
1198
|
}
|
|
944
|
-
|
|
1199
|
+
const result = await applyPlan(db);
|
|
1200
|
+
return result?.createdSchedules ?? null;
|
|
945
1201
|
},
|
|
946
|
-
async updateBookingPaymentSchedule(db, scheduleId, data) {
|
|
947
|
-
const
|
|
1202
|
+
async updateBookingPaymentSchedule(db, scheduleId, data, runtime = {}) {
|
|
1203
|
+
const updateSchedule = (writer) => writer
|
|
948
1204
|
.update(bookingPaymentSchedules)
|
|
949
1205
|
.set({ ...data, updatedAt: new Date() })
|
|
950
1206
|
.where(eq(bookingPaymentSchedules.id, scheduleId))
|
|
951
1207
|
.returning();
|
|
1208
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1209
|
+
if (actionLedgerContext) {
|
|
1210
|
+
const [row] = await db.transaction(async (tx) => {
|
|
1211
|
+
const updated = await updateSchedule(tx);
|
|
1212
|
+
if (updated[0]) {
|
|
1213
|
+
await appendActionLedgerMutation(tx, buildBookingPaymentScheduleUpdateActionLedgerInput(actionLedgerContext, { schedule: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1214
|
+
}
|
|
1215
|
+
return updated;
|
|
1216
|
+
});
|
|
1217
|
+
return row ?? null;
|
|
1218
|
+
}
|
|
1219
|
+
const [row] = await updateSchedule(db);
|
|
952
1220
|
return row ?? null;
|
|
953
1221
|
},
|
|
954
|
-
async deleteBookingPaymentSchedule(db, scheduleId) {
|
|
1222
|
+
async deleteBookingPaymentSchedule(db, scheduleId, runtime = {}) {
|
|
1223
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1224
|
+
if (actionLedgerContext) {
|
|
1225
|
+
return db.transaction(async (tx) => {
|
|
1226
|
+
const [existing] = await tx
|
|
1227
|
+
.select()
|
|
1228
|
+
.from(bookingPaymentSchedules)
|
|
1229
|
+
.where(eq(bookingPaymentSchedules.id, scheduleId))
|
|
1230
|
+
.limit(1);
|
|
1231
|
+
if (!existing) {
|
|
1232
|
+
return null;
|
|
1233
|
+
}
|
|
1234
|
+
await tx.delete(bookingPaymentSchedules).where(eq(bookingPaymentSchedules.id, scheduleId));
|
|
1235
|
+
await appendActionLedgerMutation(tx, buildBookingPaymentScheduleDeleteActionLedgerInput(actionLedgerContext, { schedule: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1236
|
+
return { id: existing.id };
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
955
1239
|
const [row] = await db
|
|
956
1240
|
.delete(bookingPaymentSchedules)
|
|
957
1241
|
.where(eq(bookingPaymentSchedules.id, scheduleId))
|
|
958
1242
|
.returning({ id: bookingPaymentSchedules.id });
|
|
959
1243
|
return row ?? null;
|
|
960
1244
|
},
|
|
961
|
-
async createPaymentSessionFromBookingSchedule(db, scheduleId, data) {
|
|
1245
|
+
async createPaymentSessionFromBookingSchedule(db, scheduleId, data, runtime = {}) {
|
|
962
1246
|
const [schedule] = await db
|
|
963
1247
|
.select()
|
|
964
1248
|
.from(bookingPaymentSchedules)
|
|
@@ -999,9 +1283,9 @@ export const financeService = {
|
|
|
999
1283
|
scheduleType: schedule.scheduleType,
|
|
1000
1284
|
dueDate: schedule.dueDate,
|
|
1001
1285
|
},
|
|
1002
|
-
});
|
|
1286
|
+
}, runtime);
|
|
1003
1287
|
},
|
|
1004
|
-
async createPaymentSessionFromInvoice(db, invoiceId, data) {
|
|
1288
|
+
async createPaymentSessionFromInvoice(db, invoiceId, data, runtime = {}) {
|
|
1005
1289
|
const [invoice] = await db.select().from(invoices).where(eq(invoices.id, invoiceId)).limit(1);
|
|
1006
1290
|
if (!invoice) {
|
|
1007
1291
|
return null;
|
|
@@ -1040,7 +1324,7 @@ export const financeService = {
|
|
|
1040
1324
|
invoiceType: invoice.invoiceType,
|
|
1041
1325
|
dueDate: invoice.dueDate,
|
|
1042
1326
|
},
|
|
1043
|
-
});
|
|
1327
|
+
}, runtime);
|
|
1044
1328
|
},
|
|
1045
1329
|
listBookingGuarantees(db, bookingId) {
|
|
1046
1330
|
return db
|
|
@@ -1049,38 +1333,51 @@ export const financeService = {
|
|
|
1049
1333
|
.where(eq(bookingGuarantees.bookingId, bookingId))
|
|
1050
1334
|
.orderBy(desc(bookingGuarantees.createdAt));
|
|
1051
1335
|
},
|
|
1052
|
-
async createBookingGuarantee(db, bookingId, data) {
|
|
1053
|
-
const
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1336
|
+
async createBookingGuarantee(db, bookingId, data, runtime = {}) {
|
|
1337
|
+
const createGuarantee = async (writer) => {
|
|
1338
|
+
const [booking] = await writer
|
|
1339
|
+
.select({ id: bookings.id })
|
|
1340
|
+
.from(bookings)
|
|
1341
|
+
.where(eq(bookings.id, bookingId))
|
|
1342
|
+
.limit(1);
|
|
1343
|
+
if (!booking) {
|
|
1344
|
+
return null;
|
|
1345
|
+
}
|
|
1346
|
+
const [row] = await writer
|
|
1347
|
+
.insert(bookingGuarantees)
|
|
1348
|
+
.values({
|
|
1349
|
+
bookingId,
|
|
1350
|
+
bookingPaymentScheduleId: data.bookingPaymentScheduleId ?? null,
|
|
1351
|
+
bookingItemId: data.bookingItemId ?? null,
|
|
1352
|
+
guaranteeType: data.guaranteeType,
|
|
1353
|
+
status: data.status,
|
|
1354
|
+
paymentInstrumentId: data.paymentInstrumentId ?? null,
|
|
1355
|
+
paymentAuthorizationId: data.paymentAuthorizationId ?? null,
|
|
1356
|
+
currency: data.currency ?? null,
|
|
1357
|
+
amountCents: data.amountCents ?? null,
|
|
1358
|
+
provider: data.provider ?? null,
|
|
1359
|
+
referenceNumber: data.referenceNumber ?? null,
|
|
1360
|
+
guaranteedAt: toTimestamp(data.guaranteedAt),
|
|
1361
|
+
expiresAt: toTimestamp(data.expiresAt),
|
|
1362
|
+
releasedAt: toTimestamp(data.releasedAt),
|
|
1363
|
+
notes: data.notes ?? null,
|
|
1364
|
+
})
|
|
1365
|
+
.returning();
|
|
1366
|
+
return row ?? null;
|
|
1367
|
+
};
|
|
1368
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1369
|
+
if (actionLedgerContext) {
|
|
1370
|
+
return db.transaction(async (tx) => {
|
|
1371
|
+
const row = await createGuarantee(tx);
|
|
1372
|
+
if (row) {
|
|
1373
|
+
await appendActionLedgerMutation(tx, await buildBookingGuaranteeCreateActionLedgerInput(actionLedgerContext, { guarantee: row }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1374
|
+
}
|
|
1375
|
+
return row;
|
|
1376
|
+
});
|
|
1060
1377
|
}
|
|
1061
|
-
|
|
1062
|
-
.insert(bookingGuarantees)
|
|
1063
|
-
.values({
|
|
1064
|
-
bookingId,
|
|
1065
|
-
bookingPaymentScheduleId: data.bookingPaymentScheduleId ?? null,
|
|
1066
|
-
bookingItemId: data.bookingItemId ?? null,
|
|
1067
|
-
guaranteeType: data.guaranteeType,
|
|
1068
|
-
status: data.status,
|
|
1069
|
-
paymentInstrumentId: data.paymentInstrumentId ?? null,
|
|
1070
|
-
paymentAuthorizationId: data.paymentAuthorizationId ?? null,
|
|
1071
|
-
currency: data.currency ?? null,
|
|
1072
|
-
amountCents: data.amountCents ?? null,
|
|
1073
|
-
provider: data.provider ?? null,
|
|
1074
|
-
referenceNumber: data.referenceNumber ?? null,
|
|
1075
|
-
guaranteedAt: toTimestamp(data.guaranteedAt),
|
|
1076
|
-
expiresAt: toTimestamp(data.expiresAt),
|
|
1077
|
-
releasedAt: toTimestamp(data.releasedAt),
|
|
1078
|
-
notes: data.notes ?? null,
|
|
1079
|
-
})
|
|
1080
|
-
.returning();
|
|
1081
|
-
return row ?? null;
|
|
1378
|
+
return createGuarantee(db);
|
|
1082
1379
|
},
|
|
1083
|
-
async createPaymentSessionFromBookingGuarantee(db, guaranteeId, data) {
|
|
1380
|
+
async createPaymentSessionFromBookingGuarantee(db, guaranteeId, data, runtime = {}) {
|
|
1084
1381
|
const [guarantee] = await db
|
|
1085
1382
|
.select()
|
|
1086
1383
|
.from(bookingGuarantees)
|
|
@@ -1127,10 +1424,10 @@ export const financeService = {
|
|
|
1127
1424
|
metadata: data.metadata ?? {
|
|
1128
1425
|
guaranteeType: guarantee.guaranteeType,
|
|
1129
1426
|
},
|
|
1130
|
-
});
|
|
1427
|
+
}, runtime);
|
|
1131
1428
|
},
|
|
1132
|
-
async updateBookingGuarantee(db, guaranteeId, data) {
|
|
1133
|
-
const
|
|
1429
|
+
async updateBookingGuarantee(db, guaranteeId, data, runtime = {}) {
|
|
1430
|
+
const updateGuarantee = (writer) => writer
|
|
1134
1431
|
.update(bookingGuarantees)
|
|
1135
1432
|
.set({
|
|
1136
1433
|
...data,
|
|
@@ -1141,9 +1438,37 @@ export const financeService = {
|
|
|
1141
1438
|
})
|
|
1142
1439
|
.where(eq(bookingGuarantees.id, guaranteeId))
|
|
1143
1440
|
.returning();
|
|
1441
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1442
|
+
if (actionLedgerContext) {
|
|
1443
|
+
const [row] = await db.transaction(async (tx) => {
|
|
1444
|
+
const updated = await updateGuarantee(tx);
|
|
1445
|
+
if (updated[0]) {
|
|
1446
|
+
await appendActionLedgerMutation(tx, buildBookingGuaranteeUpdateActionLedgerInput(actionLedgerContext, { guarantee: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1447
|
+
}
|
|
1448
|
+
return updated;
|
|
1449
|
+
});
|
|
1450
|
+
return row ?? null;
|
|
1451
|
+
}
|
|
1452
|
+
const [row] = await updateGuarantee(db);
|
|
1144
1453
|
return row ?? null;
|
|
1145
1454
|
},
|
|
1146
|
-
async deleteBookingGuarantee(db, guaranteeId) {
|
|
1455
|
+
async deleteBookingGuarantee(db, guaranteeId, runtime = {}) {
|
|
1456
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1457
|
+
if (actionLedgerContext) {
|
|
1458
|
+
return db.transaction(async (tx) => {
|
|
1459
|
+
const [existing] = await tx
|
|
1460
|
+
.select()
|
|
1461
|
+
.from(bookingGuarantees)
|
|
1462
|
+
.where(eq(bookingGuarantees.id, guaranteeId))
|
|
1463
|
+
.limit(1);
|
|
1464
|
+
if (!existing) {
|
|
1465
|
+
return null;
|
|
1466
|
+
}
|
|
1467
|
+
await tx.delete(bookingGuarantees).where(eq(bookingGuarantees.id, guaranteeId));
|
|
1468
|
+
await appendActionLedgerMutation(tx, buildBookingGuaranteeDeleteActionLedgerInput(actionLedgerContext, { guarantee: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1469
|
+
return { id: existing.id };
|
|
1470
|
+
});
|
|
1471
|
+
}
|
|
1147
1472
|
const [row] = await db
|
|
1148
1473
|
.delete(bookingGuarantees)
|
|
1149
1474
|
.where(eq(bookingGuarantees.id, guaranteeId))
|
|
@@ -1329,19 +1654,43 @@ export const financeService = {
|
|
|
1329
1654
|
offset: query.offset,
|
|
1330
1655
|
};
|
|
1331
1656
|
},
|
|
1332
|
-
async createSupplierPayment(db, data) {
|
|
1333
|
-
const
|
|
1657
|
+
async createSupplierPayment(db, data, runtime = {}) {
|
|
1658
|
+
const createPayment = (writer) => writer
|
|
1334
1659
|
.insert(supplierPayments)
|
|
1335
1660
|
.values({ ...data, paymentInstrumentId: data.paymentInstrumentId ?? null })
|
|
1336
1661
|
.returning();
|
|
1662
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1663
|
+
if (actionLedgerContext) {
|
|
1664
|
+
const [row] = await db.transaction(async (tx) => {
|
|
1665
|
+
const created = await createPayment(tx);
|
|
1666
|
+
if (created[0]) {
|
|
1667
|
+
await appendActionLedgerMutation(tx, await buildSupplierPaymentCreateActionLedgerInput(actionLedgerContext, { payment: created[0] }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1668
|
+
}
|
|
1669
|
+
return created;
|
|
1670
|
+
});
|
|
1671
|
+
return row;
|
|
1672
|
+
}
|
|
1673
|
+
const [row] = await createPayment(db);
|
|
1337
1674
|
return row;
|
|
1338
1675
|
},
|
|
1339
|
-
async updateSupplierPayment(db, id, data) {
|
|
1340
|
-
const
|
|
1676
|
+
async updateSupplierPayment(db, id, data, runtime = {}) {
|
|
1677
|
+
const updatePayment = (writer) => writer
|
|
1341
1678
|
.update(supplierPayments)
|
|
1342
1679
|
.set({ ...data, updatedAt: new Date() })
|
|
1343
1680
|
.where(eq(supplierPayments.id, id))
|
|
1344
1681
|
.returning();
|
|
1682
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1683
|
+
if (actionLedgerContext) {
|
|
1684
|
+
const [row] = await db.transaction(async (tx) => {
|
|
1685
|
+
const updated = await updatePayment(tx);
|
|
1686
|
+
if (updated[0]) {
|
|
1687
|
+
await appendActionLedgerMutation(tx, buildSupplierPaymentUpdateActionLedgerInput(actionLedgerContext, { payment: updated[0], changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1688
|
+
}
|
|
1689
|
+
return updated;
|
|
1690
|
+
});
|
|
1691
|
+
return row ?? null;
|
|
1692
|
+
}
|
|
1693
|
+
const [row] = await updatePayment(db);
|
|
1345
1694
|
return row ?? null;
|
|
1346
1695
|
},
|
|
1347
1696
|
async listInvoices(db, query) {
|
|
@@ -1522,15 +1871,41 @@ export const financeService = {
|
|
|
1522
1871
|
const [row] = await db.select().from(invoices).where(eq(invoices.id, id)).limit(1);
|
|
1523
1872
|
return row ?? null;
|
|
1524
1873
|
},
|
|
1525
|
-
async updateInvoice(db, id, data) {
|
|
1526
|
-
const
|
|
1874
|
+
async updateInvoice(db, id, data, runtime = {}) {
|
|
1875
|
+
const updateInvoiceRow = (writer) => writer
|
|
1527
1876
|
.update(invoices)
|
|
1528
1877
|
.set({ ...data, updatedAt: new Date() })
|
|
1529
1878
|
.where(eq(invoices.id, id))
|
|
1530
1879
|
.returning();
|
|
1880
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1881
|
+
if (actionLedgerContext) {
|
|
1882
|
+
return db.transaction(async (tx) => {
|
|
1883
|
+
const [row] = await updateInvoiceRow(tx);
|
|
1884
|
+
if (row) {
|
|
1885
|
+
await appendActionLedgerMutation(tx, buildInvoiceUpdateActionLedgerInput(actionLedgerContext, { invoice: row, changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1886
|
+
}
|
|
1887
|
+
return row ?? null;
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1890
|
+
const [row] = await updateInvoiceRow(db);
|
|
1531
1891
|
return row ?? null;
|
|
1532
1892
|
},
|
|
1533
|
-
async deleteInvoice(db, id) {
|
|
1893
|
+
async deleteInvoice(db, id, runtime = {}) {
|
|
1894
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1895
|
+
if (actionLedgerContext) {
|
|
1896
|
+
return db.transaction(async (tx) => {
|
|
1897
|
+
const [existing] = await tx.select().from(invoices).where(eq(invoices.id, id)).limit(1);
|
|
1898
|
+
if (!existing) {
|
|
1899
|
+
return { status: "not_found" };
|
|
1900
|
+
}
|
|
1901
|
+
if (existing.status !== "draft") {
|
|
1902
|
+
return { status: "not_draft" };
|
|
1903
|
+
}
|
|
1904
|
+
await tx.delete(invoices).where(eq(invoices.id, id));
|
|
1905
|
+
await appendActionLedgerMutation(tx, buildInvoiceDeleteActionLedgerInput(actionLedgerContext, { invoice: existing }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1906
|
+
return { status: "deleted" };
|
|
1907
|
+
});
|
|
1908
|
+
}
|
|
1534
1909
|
const [existing] = await db
|
|
1535
1910
|
.select({ id: invoices.id, status: invoices.status })
|
|
1536
1911
|
.from(invoices)
|
|
@@ -1552,35 +1927,97 @@ export const financeService = {
|
|
|
1552
1927
|
.where(eq(invoiceLineItems.invoiceId, invoiceId))
|
|
1553
1928
|
.orderBy(asc(invoiceLineItems.sortOrder));
|
|
1554
1929
|
},
|
|
1555
|
-
async createInvoiceLineItem(db, invoiceId, data) {
|
|
1556
|
-
const
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1930
|
+
async createInvoiceLineItem(db, invoiceId, data, runtime = {}) {
|
|
1931
|
+
const createLineItem = async (writer) => {
|
|
1932
|
+
const [invoice] = await writer
|
|
1933
|
+
.select()
|
|
1934
|
+
.from(invoices)
|
|
1935
|
+
.where(eq(invoices.id, invoiceId))
|
|
1936
|
+
.limit(1);
|
|
1937
|
+
if (!invoice) {
|
|
1938
|
+
return null;
|
|
1939
|
+
}
|
|
1940
|
+
const [row] = await writer
|
|
1941
|
+
.insert(invoiceLineItems)
|
|
1942
|
+
.values({ ...data, invoiceId })
|
|
1943
|
+
.returning();
|
|
1944
|
+
return row ? { invoice, lineItem: row } : null;
|
|
1945
|
+
};
|
|
1946
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1947
|
+
if (actionLedgerContext) {
|
|
1948
|
+
const result = await db.transaction(async (tx) => {
|
|
1949
|
+
const created = await createLineItem(tx);
|
|
1950
|
+
if (created) {
|
|
1951
|
+
await appendActionLedgerMutation(tx, buildInvoiceLineItemCreateActionLedgerInput(actionLedgerContext, created, {
|
|
1952
|
+
authorizationSource: runtime.actionLedgerAuthorizationSource,
|
|
1953
|
+
}));
|
|
1954
|
+
}
|
|
1955
|
+
return created;
|
|
1956
|
+
});
|
|
1957
|
+
return result?.lineItem ?? null;
|
|
1563
1958
|
}
|
|
1564
|
-
|
|
1565
|
-
.insert(invoiceLineItems)
|
|
1566
|
-
.values({ ...data, invoiceId })
|
|
1567
|
-
.returning();
|
|
1568
|
-
return row;
|
|
1959
|
+
return (await createLineItem(db))?.lineItem ?? null;
|
|
1569
1960
|
},
|
|
1570
|
-
async updateInvoiceLineItem(db, lineId, data) {
|
|
1571
|
-
const
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1961
|
+
async updateInvoiceLineItem(db, lineId, data, runtime = {}) {
|
|
1962
|
+
const updateLineItem = async (writer) => {
|
|
1963
|
+
const [row] = await writer
|
|
1964
|
+
.update(invoiceLineItems)
|
|
1965
|
+
.set(data)
|
|
1966
|
+
.where(eq(invoiceLineItems.id, lineId))
|
|
1967
|
+
.returning();
|
|
1968
|
+
if (!row) {
|
|
1969
|
+
return null;
|
|
1970
|
+
}
|
|
1971
|
+
const [invoice] = await writer
|
|
1972
|
+
.select()
|
|
1973
|
+
.from(invoices)
|
|
1974
|
+
.where(eq(invoices.id, row.invoiceId))
|
|
1975
|
+
.limit(1);
|
|
1976
|
+
return invoice ? { invoice, lineItem: row } : null;
|
|
1977
|
+
};
|
|
1978
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
1979
|
+
if (actionLedgerContext) {
|
|
1980
|
+
const result = await db.transaction(async (tx) => {
|
|
1981
|
+
const updated = await updateLineItem(tx);
|
|
1982
|
+
if (updated) {
|
|
1983
|
+
await appendActionLedgerMutation(tx, buildInvoiceLineItemUpdateActionLedgerInput(actionLedgerContext, { ...updated, changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
1984
|
+
}
|
|
1985
|
+
return updated;
|
|
1986
|
+
});
|
|
1987
|
+
return result?.lineItem ?? null;
|
|
1988
|
+
}
|
|
1989
|
+
return (await updateLineItem(db))?.lineItem ?? null;
|
|
1577
1990
|
},
|
|
1578
|
-
async deleteInvoiceLineItem(db, lineId) {
|
|
1579
|
-
const
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1991
|
+
async deleteInvoiceLineItem(db, lineId, runtime = {}) {
|
|
1992
|
+
const deleteLineItem = async (writer) => {
|
|
1993
|
+
const [row] = await writer
|
|
1994
|
+
.delete(invoiceLineItems)
|
|
1995
|
+
.where(eq(invoiceLineItems.id, lineId))
|
|
1996
|
+
.returning();
|
|
1997
|
+
if (!row) {
|
|
1998
|
+
return null;
|
|
1999
|
+
}
|
|
2000
|
+
const [invoice] = await writer
|
|
2001
|
+
.select()
|
|
2002
|
+
.from(invoices)
|
|
2003
|
+
.where(eq(invoices.id, row.invoiceId))
|
|
2004
|
+
.limit(1);
|
|
2005
|
+
return invoice ? { invoice, lineItem: row } : null;
|
|
2006
|
+
};
|
|
2007
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
2008
|
+
if (actionLedgerContext) {
|
|
2009
|
+
const result = await db.transaction(async (tx) => {
|
|
2010
|
+
const deleted = await deleteLineItem(tx);
|
|
2011
|
+
if (deleted) {
|
|
2012
|
+
await appendActionLedgerMutation(tx, buildInvoiceLineItemDeleteActionLedgerInput(actionLedgerContext, deleted, {
|
|
2013
|
+
authorizationSource: runtime.actionLedgerAuthorizationSource,
|
|
2014
|
+
}));
|
|
2015
|
+
}
|
|
2016
|
+
return deleted;
|
|
2017
|
+
});
|
|
2018
|
+
return result?.lineItem ?? null;
|
|
2019
|
+
}
|
|
2020
|
+
return (await deleteLineItem(db))?.lineItem ?? null;
|
|
1584
2021
|
},
|
|
1585
2022
|
listPayments(db, invoiceId) {
|
|
1586
2023
|
return db
|
|
@@ -1815,7 +2252,7 @@ export const financeService = {
|
|
|
1815
2252
|
const row = toRows(result)[0];
|
|
1816
2253
|
return row ? mapRawPayment(row) : null;
|
|
1817
2254
|
},
|
|
1818
|
-
async createPayment(db, invoiceId, data) {
|
|
2255
|
+
async createPayment(db, invoiceId, data, runtime = {}) {
|
|
1819
2256
|
const [invoice] = await db.select().from(invoices).where(eq(invoices.id, invoiceId)).limit(1);
|
|
1820
2257
|
if (!invoice) {
|
|
1821
2258
|
return null;
|
|
@@ -1849,6 +2286,14 @@ export const financeService = {
|
|
|
1849
2286
|
.update(invoices)
|
|
1850
2287
|
.set({ paidCents, balanceDueCents, status: newStatus, updatedAt: new Date() })
|
|
1851
2288
|
.where(eq(invoices.id, invoiceId));
|
|
2289
|
+
if (payment && runtime.actionLedgerContext) {
|
|
2290
|
+
await appendActionLedgerMutation(tx, await buildRecordPaymentActionLedgerInput(runtime.actionLedgerContext, {
|
|
2291
|
+
invoice,
|
|
2292
|
+
payment,
|
|
2293
|
+
}, {
|
|
2294
|
+
authorizationSource: runtime.actionLedgerAuthorizationSource,
|
|
2295
|
+
}));
|
|
2296
|
+
}
|
|
1852
2297
|
return payment;
|
|
1853
2298
|
});
|
|
1854
2299
|
},
|
|
@@ -1859,28 +2304,56 @@ export const financeService = {
|
|
|
1859
2304
|
.where(eq(creditNotes.invoiceId, invoiceId))
|
|
1860
2305
|
.orderBy(desc(creditNotes.createdAt));
|
|
1861
2306
|
},
|
|
1862
|
-
async createCreditNote(db, invoiceId, data) {
|
|
1863
|
-
const [invoice] = await db
|
|
1864
|
-
.select({ id: invoices.id })
|
|
1865
|
-
.from(invoices)
|
|
1866
|
-
.where(eq(invoices.id, invoiceId))
|
|
1867
|
-
.limit(1);
|
|
2307
|
+
async createCreditNote(db, invoiceId, data, runtime = {}) {
|
|
2308
|
+
const [invoice] = await db.select().from(invoices).where(eq(invoices.id, invoiceId)).limit(1);
|
|
1868
2309
|
if (!invoice) {
|
|
1869
2310
|
return null;
|
|
1870
2311
|
}
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
2312
|
+
return db.transaction(async (tx) => {
|
|
2313
|
+
const [row] = await tx
|
|
2314
|
+
.insert(creditNotes)
|
|
2315
|
+
.values({ ...data, invoiceId })
|
|
2316
|
+
.returning();
|
|
2317
|
+
if (row && runtime.actionLedgerContext) {
|
|
2318
|
+
await appendActionLedgerMutation(tx, await buildCreditNoteCreationActionLedgerInput(runtime.actionLedgerContext, {
|
|
2319
|
+
invoice,
|
|
2320
|
+
creditNote: row,
|
|
2321
|
+
}, {
|
|
2322
|
+
authorizationSource: runtime.actionLedgerAuthorizationSource,
|
|
2323
|
+
}));
|
|
2324
|
+
}
|
|
2325
|
+
return row;
|
|
2326
|
+
});
|
|
1876
2327
|
},
|
|
1877
|
-
async updateCreditNote(db, creditNoteId, data) {
|
|
1878
|
-
const
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
2328
|
+
async updateCreditNote(db, creditNoteId, data, runtime = {}) {
|
|
2329
|
+
const updateCreditNoteRow = async (writer) => {
|
|
2330
|
+
const [row] = await writer
|
|
2331
|
+
.update(creditNotes)
|
|
2332
|
+
.set({ ...data, updatedAt: new Date() })
|
|
2333
|
+
.where(eq(creditNotes.id, creditNoteId))
|
|
2334
|
+
.returning();
|
|
2335
|
+
if (!row) {
|
|
2336
|
+
return null;
|
|
2337
|
+
}
|
|
2338
|
+
const [invoice] = await writer
|
|
2339
|
+
.select()
|
|
2340
|
+
.from(invoices)
|
|
2341
|
+
.where(eq(invoices.id, row.invoiceId))
|
|
2342
|
+
.limit(1);
|
|
2343
|
+
return invoice ? { invoice, creditNote: row } : null;
|
|
2344
|
+
};
|
|
2345
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
2346
|
+
if (actionLedgerContext) {
|
|
2347
|
+
const result = await db.transaction(async (tx) => {
|
|
2348
|
+
const updated = await updateCreditNoteRow(tx);
|
|
2349
|
+
if (updated) {
|
|
2350
|
+
await appendActionLedgerMutation(tx, buildCreditNoteUpdateActionLedgerInput(actionLedgerContext, { ...updated, changes: data }, { authorizationSource: runtime.actionLedgerAuthorizationSource }));
|
|
2351
|
+
}
|
|
2352
|
+
return updated;
|
|
2353
|
+
});
|
|
2354
|
+
return result?.creditNote ?? null;
|
|
2355
|
+
}
|
|
2356
|
+
return (await updateCreditNoteRow(db))?.creditNote ?? null;
|
|
1884
2357
|
},
|
|
1885
2358
|
listCreditNoteLineItems(db, creditNoteId) {
|
|
1886
2359
|
return db
|
|
@@ -1889,20 +2362,44 @@ export const financeService = {
|
|
|
1889
2362
|
.where(eq(creditNoteLineItems.creditNoteId, creditNoteId))
|
|
1890
2363
|
.orderBy(asc(creditNoteLineItems.sortOrder));
|
|
1891
2364
|
},
|
|
1892
|
-
async createCreditNoteLineItem(db, creditNoteId, data) {
|
|
1893
|
-
const
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
2365
|
+
async createCreditNoteLineItem(db, creditNoteId, data, runtime = {}) {
|
|
2366
|
+
const createLineItem = async (writer) => {
|
|
2367
|
+
const [creditNote] = await writer
|
|
2368
|
+
.select()
|
|
2369
|
+
.from(creditNotes)
|
|
2370
|
+
.where(eq(creditNotes.id, creditNoteId))
|
|
2371
|
+
.limit(1);
|
|
2372
|
+
if (!creditNote) {
|
|
2373
|
+
return null;
|
|
2374
|
+
}
|
|
2375
|
+
const [invoice] = await writer
|
|
2376
|
+
.select()
|
|
2377
|
+
.from(invoices)
|
|
2378
|
+
.where(eq(invoices.id, creditNote.invoiceId))
|
|
2379
|
+
.limit(1);
|
|
2380
|
+
if (!invoice) {
|
|
2381
|
+
return null;
|
|
2382
|
+
}
|
|
2383
|
+
const [row] = await writer
|
|
2384
|
+
.insert(creditNoteLineItems)
|
|
2385
|
+
.values({ ...data, creditNoteId })
|
|
2386
|
+
.returning();
|
|
2387
|
+
return row ? { invoice, creditNote, lineItem: row } : null;
|
|
2388
|
+
};
|
|
2389
|
+
const actionLedgerContext = runtime.actionLedgerContext;
|
|
2390
|
+
if (actionLedgerContext) {
|
|
2391
|
+
const result = await db.transaction(async (tx) => {
|
|
2392
|
+
const created = await createLineItem(tx);
|
|
2393
|
+
if (created) {
|
|
2394
|
+
await appendActionLedgerMutation(tx, buildCreditNoteLineItemCreateActionLedgerInput(actionLedgerContext, created, {
|
|
2395
|
+
authorizationSource: runtime.actionLedgerAuthorizationSource,
|
|
2396
|
+
}));
|
|
2397
|
+
}
|
|
2398
|
+
return created;
|
|
2399
|
+
});
|
|
2400
|
+
return result?.lineItem ?? null;
|
|
1900
2401
|
}
|
|
1901
|
-
|
|
1902
|
-
.insert(creditNoteLineItems)
|
|
1903
|
-
.values({ ...data, creditNoteId })
|
|
1904
|
-
.returning();
|
|
1905
|
-
return row;
|
|
2402
|
+
return (await createLineItem(db))?.lineItem ?? null;
|
|
1906
2403
|
},
|
|
1907
2404
|
listNotes(db, invoiceId) {
|
|
1908
2405
|
return db
|