@develit-services/bank 0.1.9 → 0.2.1

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.
Files changed (30) hide show
  1. package/dist/database/schema.cjs +1 -1
  2. package/dist/database/schema.d.cts +1 -1
  3. package/dist/database/schema.d.mts +1 -1
  4. package/dist/database/schema.d.ts +1 -1
  5. package/dist/database/schema.mjs +1 -1
  6. package/dist/export/worker.cjs +7 -123
  7. package/dist/export/worker.d.cts +14 -17
  8. package/dist/export/worker.d.mts +14 -17
  9. package/dist/export/worker.d.ts +14 -17
  10. package/dist/export/worker.mjs +7 -123
  11. package/dist/export/workflows.cjs +27 -20
  12. package/dist/export/workflows.mjs +27 -20
  13. package/dist/shared/{bank.DouutABZ.d.mts → bank.2BgwPCw_.d.ts} +2 -2
  14. package/dist/shared/{bank.-FurAs09.cjs → bank.B5dSMikM.cjs} +2 -7
  15. package/dist/shared/{bank.CsvBtQib.d.cts → bank.BGZYksmV.d.cts} +12 -8
  16. package/dist/shared/{bank.CsvBtQib.d.mts → bank.BGZYksmV.d.mts} +12 -8
  17. package/dist/shared/{bank.CsvBtQib.d.ts → bank.BGZYksmV.d.ts} +12 -8
  18. package/dist/shared/{bank.CRVH1wUH.d.cts → bank.BOe_Qmuw.d.mts} +2 -2
  19. package/dist/shared/{bank.BvXwHpBp.mjs → bank.BcDo3cbz.mjs} +1 -1
  20. package/dist/shared/{bank.DTdKHhGd.mjs → bank.CpoWaBu0.mjs} +4 -8
  21. package/dist/shared/{bank.W4h6Dlzi.mjs → bank.DdvSjgls.mjs} +17 -8
  22. package/dist/shared/{bank.z4VxSea3.cjs → bank.DhuHP2ky.cjs} +1 -1
  23. package/dist/shared/{bank.tLe0jhjo.cjs → bank.NAgd77sr.cjs} +17 -8
  24. package/dist/shared/{bank.Ckn_klvT.d.ts → bank.RD4xwHFf.d.cts} +2 -2
  25. package/dist/types.cjs +2 -2
  26. package/dist/types.d.cts +11 -9
  27. package/dist/types.d.mts +11 -9
  28. package/dist/types.d.ts +11 -9
  29. package/dist/types.mjs +2 -2
  30. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const database_schema = require('../shared/bank.tLe0jhjo.cjs');
3
+ const database_schema = require('../shared/bank.NAgd77sr.cjs');
4
4
  require('@develit-io/backend-sdk');
5
5
  require('drizzle-orm/sqlite-core');
6
6
  require('date-fns');
@@ -1,4 +1,4 @@
1
- export { f as account, g as accountCredentials, c as batch, o as ott, d as payment, e as paymentRelations } from '../shared/bank.CsvBtQib.cjs';
1
+ export { g as account, h as accountCredentials, d as batch, o as ott, e as payment, f as paymentRelations } from '../shared/bank.BGZYksmV.cjs';
2
2
  import 'drizzle-orm/sqlite-core';
3
3
  import 'drizzle-orm';
4
4
  import 'zod';
@@ -1,4 +1,4 @@
1
- export { f as account, g as accountCredentials, c as batch, o as ott, d as payment, e as paymentRelations } from '../shared/bank.CsvBtQib.mjs';
1
+ export { g as account, h as accountCredentials, d as batch, o as ott, e as payment, f as paymentRelations } from '../shared/bank.BGZYksmV.mjs';
2
2
  import 'drizzle-orm/sqlite-core';
3
3
  import 'drizzle-orm';
4
4
  import 'zod';
@@ -1,4 +1,4 @@
1
- export { f as account, g as accountCredentials, c as batch, o as ott, d as payment, e as paymentRelations } from '../shared/bank.CsvBtQib.js';
1
+ export { g as account, h as accountCredentials, d as batch, o as ott, e as payment, f as paymentRelations } from '../shared/bank.BGZYksmV.js';
2
2
  import 'drizzle-orm/sqlite-core';
3
3
  import 'drizzle-orm';
4
4
  import 'zod';
@@ -1,4 +1,4 @@
1
- export { D as account, G as accountCredentials, w as batch, z as ott, x as payment, y as paymentRelations } from '../shared/bank.W4h6Dlzi.mjs';
1
+ export { D as account, G as accountCredentials, w as batch, z as ott, x as payment, y as paymentRelations } from '../shared/bank.DdvSjgls.mjs';
2
2
  import '@develit-io/backend-sdk';
3
3
  import 'drizzle-orm/sqlite-core';
4
4
  import 'date-fns';
@@ -3,17 +3,17 @@
3
3
  const backendSdk = require('@develit-io/backend-sdk');
4
4
  const cloudflare_workers = require('cloudflare:workers');
5
5
  const d1 = require('drizzle-orm/d1');
6
- const database_schema = require('../shared/bank.tLe0jhjo.cjs');
6
+ const database_schema = require('../shared/bank.NAgd77sr.cjs');
7
7
  require('jose');
8
8
  const generalCodes = require('@develit-io/general-codes');
9
9
  const zod = require('zod');
10
- const bank = require('../shared/bank.-FurAs09.cjs');
10
+ const bank = require('../shared/bank.B5dSMikM.cjs');
11
11
  const drizzleOrm = require('drizzle-orm');
12
12
  require('drizzle-orm/sqlite-core');
13
13
  require('date-fns');
14
14
  require('drizzle-zod');
15
15
  require('drizzle-orm/relations');
16
- require('../shared/bank.z4VxSea3.cjs');
16
+ require('../shared/bank.DhuHP2ky.cjs');
17
17
 
18
18
  const upsertAccountCommand = (db, { account }) => {
19
19
  const id = account.id || backendSdk.uuidv4();
@@ -385,102 +385,6 @@ let BankServiceBase = class extends backendSdk.develitWorker(cloudflare_workers.
385
385
  async _getAccounts() {
386
386
  return await getAllAccountsQuery(this.db);
387
387
  }
388
- async _saveOrUpdatePayments(accounts) {
389
- const allFetchedPayments = [];
390
- const connectorKeys = new Set(
391
- accounts.map(({ connectorKey }) => connectorKey)
392
- );
393
- for (const connectorKey of connectorKeys) {
394
- await this._initiateBankConnector({ connectorKey });
395
- for (const account of accounts.filter(
396
- (acc) => acc.connectorKey === connectorKey
397
- )) {
398
- if (!account.lastSyncedAt) {
399
- this.log({
400
- message: `Skipping account ${account.id} as it has no lastSyncedAt date.`
401
- });
402
- continue;
403
- }
404
- const payments = await this.bankConnector.getAllAccountPayments({
405
- db: this.db,
406
- environment: this.env.ENVIRONMENT,
407
- account,
408
- filter: {
409
- dateFrom: account.lastSyncedAt
410
- }
411
- });
412
- if (!payments || payments.length === 0) continue;
413
- payments.forEach((payment) => {
414
- payment.direction = database_schema.getPaymentDirection(payment, account.iban);
415
- });
416
- allFetchedPayments.push(...payments);
417
- }
418
- }
419
- if (allFetchedPayments.length < 1) return;
420
- const bankRefIds = allFetchedPayments.map((payment) => payment.bankRefId).filter(Boolean);
421
- const alreadyExistingPayments = await bank.getPaymentsByBankRefIdsQuery(
422
- this.db,
423
- {
424
- ids: bankRefIds
425
- }
426
- );
427
- const eventsToPush = [];
428
- const paymentsCommands = allFetchedPayments.map((payment) => {
429
- const isAlreadyExisting = alreadyExistingPayments.some(
430
- (existingPayment) => existingPayment.bankRefId === payment.bankRefId
431
- );
432
- if (isAlreadyExisting) {
433
- eventsToPush.push({
434
- eventType: "BANK_PAYMENT",
435
- eventSignal: "paymentUpdated",
436
- bankPayment: payment,
437
- metadata: {
438
- correlationId: payment.correlationId,
439
- entityId: payment.id,
440
- idempotencySuffix: payment.status,
441
- timestamp: (/* @__PURE__ */ new Date()).toDateString()
442
- }
443
- });
444
- return bank.updatePaymentCommand(this.db, { payment }).command;
445
- }
446
- eventsToPush.push({
447
- eventType: "BANK_PAYMENT",
448
- eventSignal: "paymentCreated",
449
- bankPayment: payment,
450
- metadata: {
451
- correlationId: payment.correlationId,
452
- entityId: payment.id,
453
- timestamp: (/* @__PURE__ */ new Date()).toDateString()
454
- }
455
- });
456
- return bank.createPaymentCommand(this.db, { payment }).command;
457
- });
458
- await this.db.batch([paymentsCommands[0], ...paymentsCommands.slice(1)]);
459
- await this.pushToQueue(
460
- this.env.QUEUE_BUS_QUEUE,
461
- eventsToPush
462
- );
463
- for (const account of accounts) {
464
- const paymentsForAccount = allFetchedPayments.filter(
465
- (payment) => (payment.direction === "OUTGOING" ? payment.debtorIban : payment.creditorIban) === account.iban
466
- );
467
- let lastSyncPayment;
468
- lastSyncPayment = paymentsForAccount.filter(({ status }) => status !== "COMPLETED").sort(
469
- (a, b) => (a.createdAt?.getTime() || 0) - (b.createdAt?.getTime() || 0)
470
- )[0];
471
- if (!lastSyncPayment) {
472
- lastSyncPayment = paymentsForAccount.sort(
473
- (a, b) => (b.createdAt?.getTime() || 0) - (a.createdAt?.getTime() || 0)
474
- )[0];
475
- }
476
- if (lastSyncPayment.createdAt) {
477
- await bank.updateAccountLastSyncCommand(this.db, {
478
- accountId: account.id,
479
- lastSyncedAt: lastSyncPayment.createdAt
480
- }).command.execute();
481
- }
482
- }
483
- }
484
388
  async _initiateBankConnector({
485
389
  connectorKey,
486
390
  withAuth
@@ -600,34 +504,14 @@ let BankServiceBase = class extends backendSdk.develitWorker(cloudflare_workers.
600
504
  }
601
505
  );
602
506
  }
603
- async syncAccounts(options) {
507
+ async syncAccounts() {
604
508
  return this.handleAction(
605
509
  null,
606
510
  { successMessage: "Accounts synchronized successfully" },
607
511
  async () => {
608
- const accountsToSync = [];
609
- let accounts = options?.accounts || [];
610
- if (!accounts.length) {
611
- accounts = await this._getAccounts();
612
- }
613
- for (const account of accounts.filter(
614
- (acc) => !!!acc.connectorKey.includes("MOCK")
615
- )) {
616
- const accountFetchInterval = this._accountFetchInterval(
617
- account.connectorKey
618
- );
619
- const now = Date.now();
620
- const lastSyncTime = account.lastSyncedAt.getTime();
621
- const intervalMs = accountFetchInterval * 1e3;
622
- const shouldFetch = now - lastSyncTime >= intervalMs;
623
- if (!shouldFetch) continue;
624
- accountsToSync.push({
625
- ...account
626
- });
627
- }
628
- console.log("ACCS TO SYNC", accountsToSync.length);
629
- if (accountsToSync.length <= 0) return;
630
- await this._saveOrUpdatePayments(accounts);
512
+ throw backendSdk.createInternalError(null, {
513
+ message: "Not implemented yet"
514
+ });
631
515
  }
632
516
  );
633
517
  }
@@ -2,8 +2,8 @@ import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { WorkflowInstanceStatus, IRPCResponse } from '@develit-io/backend-sdk';
3
3
  import { WorkerEntrypoint } from 'cloudflare:workers';
4
4
  import { DrizzleD1Database } from 'drizzle-orm/d1';
5
- import { P as PaymentSelectType, B as BatchSelectType, t as tables, a as PaymentInsertType } from '../shared/bank.CsvBtQib.cjs';
6
- import { A as AccountSelectType, C as ConfigEnvironmentBank, I as IBankConnector, a as ConnectorKey } from '../shared/bank.CRVH1wUH.cjs';
5
+ import { P as PaymentSelectType, B as BatchSelectType, t as tables, a as PaymentInsertType } from '../shared/bank.BGZYksmV.cjs';
6
+ import { A as AccountSelectType, C as ConfigEnvironmentBank, I as IBankConnector, a as ConnectorKey } from '../shared/bank.RD4xwHFf.cjs';
7
7
  import { z } from 'zod';
8
8
  import * as drizzle_zod from 'drizzle-zod';
9
9
  import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
@@ -1538,21 +1538,21 @@ declare const getBatchesInputSchema: z.ZodObject<{
1538
1538
  }, z.core.$strip>;
1539
1539
  filterBatchAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
1540
1540
  filterBatchStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
1541
- PREPARED: "PREPARED";
1542
- FAILED: "FAILED";
1543
- COMPLETED: "COMPLETED";
1544
1541
  OPEN: "OPEN";
1545
1542
  PROCESSING: "PROCESSING";
1546
1543
  READY_TO_SIGN: "READY_TO_SIGN";
1547
1544
  WAITING_FOR_PROCESSING: "WAITING_FOR_PROCESSING";
1548
- }>, z.ZodArray<z.ZodEnum<{
1549
1545
  PREPARED: "PREPARED";
1550
- FAILED: "FAILED";
1551
1546
  COMPLETED: "COMPLETED";
1547
+ FAILED: "FAILED";
1548
+ }>, z.ZodArray<z.ZodEnum<{
1552
1549
  OPEN: "OPEN";
1553
1550
  PROCESSING: "PROCESSING";
1554
1551
  READY_TO_SIGN: "READY_TO_SIGN";
1555
1552
  WAITING_FOR_PROCESSING: "WAITING_FOR_PROCESSING";
1553
+ PREPARED: "PREPARED";
1554
+ COMPLETED: "COMPLETED";
1555
+ FAILED: "FAILED";
1556
1556
  }>>]>>;
1557
1557
  }, z.core.$strip>;
1558
1558
  type GetBatchesInput = z.input<typeof getBatchesInputSchema>;
@@ -1581,9 +1581,9 @@ declare const getPaymentsInputSchema: z.ZodObject<{
1581
1581
  limit: z.ZodNumber;
1582
1582
  sort: z.ZodObject<{
1583
1583
  column: z.ZodEnum<{
1584
- amount: "amount";
1585
1584
  createdAt: "createdAt";
1586
1585
  updatedAt: "updatedAt";
1586
+ amount: "amount";
1587
1587
  }>;
1588
1588
  direction: z.ZodEnum<{
1589
1589
  asc: "asc";
@@ -1712,17 +1712,17 @@ declare const getPaymentsInputSchema: z.ZodObject<{
1712
1712
  filterPaymentDateTo: z.ZodOptional<z.ZodDate>;
1713
1713
  filterPaymentStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
1714
1714
  PREPARED: "PREPARED";
1715
- INITIALIZED: "INITIALIZED";
1715
+ COMPLETED: "COMPLETED";
1716
1716
  FAILED: "FAILED";
1717
+ INITIALIZED: "INITIALIZED";
1717
1718
  PENDING: "PENDING";
1718
- COMPLETED: "COMPLETED";
1719
1719
  CREATED: "CREATED";
1720
1720
  }>, z.ZodArray<z.ZodEnum<{
1721
1721
  PREPARED: "PREPARED";
1722
- INITIALIZED: "INITIALIZED";
1722
+ COMPLETED: "COMPLETED";
1723
1723
  FAILED: "FAILED";
1724
+ INITIALIZED: "INITIALIZED";
1724
1725
  PENDING: "PENDING";
1725
- COMPLETED: "COMPLETED";
1726
1726
  CREATED: "CREATED";
1727
1727
  }>>]>>;
1728
1728
  }, z.core.$strip>;
@@ -2314,7 +2314,6 @@ declare class BankServiceBase extends BankServiceBase_base {
2314
2314
  updatedAt: Date | null;
2315
2315
  deletedAt: Date | null;
2316
2316
  }[]>;
2317
- _saveOrUpdatePayments(accounts: AccountSelectType[]): Promise<void>;
2318
2317
  _initiateBankConnector({ connectorKey, withAuth, }: {
2319
2318
  connectorKey: ConnectorKey;
2320
2319
  withAuth?: boolean;
@@ -2325,15 +2324,13 @@ declare class BankServiceBase extends BankServiceBase_base {
2325
2324
  syncAccountStatus(input: SyncAccountStatusInput): Promise<IRPCResponse<SyncAccountStatusOutput>>;
2326
2325
  syncAccountRestart(input: SyncAccountRestartInput): Promise<IRPCResponse<SyncAccountRestartOutput>>;
2327
2326
  syncAccountTerminate(input: SyncAccountTerminateInput): Promise<IRPCResponse<SyncAccountTerminateOutput>>;
2328
- syncAccounts(options?: {
2329
- accounts?: AccountSelectType[];
2330
- }): Promise<IRPCResponse<void>>;
2327
+ syncAccounts(): Promise<IRPCResponse<never>>;
2331
2328
  scheduled(controller: ScheduledController): Promise<void>;
2332
2329
  updateBatchStatuses(): Promise<void>;
2333
2330
  addPaymentsToBatch({ paymentsToBatch, }: {
2334
2331
  paymentsToBatch: SendPaymentInput[];
2335
2332
  }): Promise<IRPCResponse<{
2336
- status: "PREPARED" | "FAILED" | "COMPLETED" | "OPEN" | "PROCESSING" | "READY_TO_SIGN" | "WAITING_FOR_PROCESSING" | null;
2333
+ status: "OPEN" | "PROCESSING" | "READY_TO_SIGN" | "WAITING_FOR_PROCESSING" | "PREPARED" | "COMPLETED" | "FAILED" | null;
2337
2334
  id: string;
2338
2335
  createdAt: Date | null;
2339
2336
  updatedAt: Date | null;
@@ -2,8 +2,8 @@ import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { WorkflowInstanceStatus, IRPCResponse } from '@develit-io/backend-sdk';
3
3
  import { WorkerEntrypoint } from 'cloudflare:workers';
4
4
  import { DrizzleD1Database } from 'drizzle-orm/d1';
5
- import { P as PaymentSelectType, B as BatchSelectType, t as tables, a as PaymentInsertType } from '../shared/bank.CsvBtQib.mjs';
6
- import { A as AccountSelectType, C as ConfigEnvironmentBank, I as IBankConnector, a as ConnectorKey } from '../shared/bank.DouutABZ.mjs';
5
+ import { P as PaymentSelectType, B as BatchSelectType, t as tables, a as PaymentInsertType } from '../shared/bank.BGZYksmV.mjs';
6
+ import { A as AccountSelectType, C as ConfigEnvironmentBank, I as IBankConnector, a as ConnectorKey } from '../shared/bank.BOe_Qmuw.mjs';
7
7
  import { z } from 'zod';
8
8
  import * as drizzle_zod from 'drizzle-zod';
9
9
  import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
@@ -1538,21 +1538,21 @@ declare const getBatchesInputSchema: z.ZodObject<{
1538
1538
  }, z.core.$strip>;
1539
1539
  filterBatchAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
1540
1540
  filterBatchStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
1541
- PREPARED: "PREPARED";
1542
- FAILED: "FAILED";
1543
- COMPLETED: "COMPLETED";
1544
1541
  OPEN: "OPEN";
1545
1542
  PROCESSING: "PROCESSING";
1546
1543
  READY_TO_SIGN: "READY_TO_SIGN";
1547
1544
  WAITING_FOR_PROCESSING: "WAITING_FOR_PROCESSING";
1548
- }>, z.ZodArray<z.ZodEnum<{
1549
1545
  PREPARED: "PREPARED";
1550
- FAILED: "FAILED";
1551
1546
  COMPLETED: "COMPLETED";
1547
+ FAILED: "FAILED";
1548
+ }>, z.ZodArray<z.ZodEnum<{
1552
1549
  OPEN: "OPEN";
1553
1550
  PROCESSING: "PROCESSING";
1554
1551
  READY_TO_SIGN: "READY_TO_SIGN";
1555
1552
  WAITING_FOR_PROCESSING: "WAITING_FOR_PROCESSING";
1553
+ PREPARED: "PREPARED";
1554
+ COMPLETED: "COMPLETED";
1555
+ FAILED: "FAILED";
1556
1556
  }>>]>>;
1557
1557
  }, z.core.$strip>;
1558
1558
  type GetBatchesInput = z.input<typeof getBatchesInputSchema>;
@@ -1581,9 +1581,9 @@ declare const getPaymentsInputSchema: z.ZodObject<{
1581
1581
  limit: z.ZodNumber;
1582
1582
  sort: z.ZodObject<{
1583
1583
  column: z.ZodEnum<{
1584
- amount: "amount";
1585
1584
  createdAt: "createdAt";
1586
1585
  updatedAt: "updatedAt";
1586
+ amount: "amount";
1587
1587
  }>;
1588
1588
  direction: z.ZodEnum<{
1589
1589
  asc: "asc";
@@ -1712,17 +1712,17 @@ declare const getPaymentsInputSchema: z.ZodObject<{
1712
1712
  filterPaymentDateTo: z.ZodOptional<z.ZodDate>;
1713
1713
  filterPaymentStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
1714
1714
  PREPARED: "PREPARED";
1715
- INITIALIZED: "INITIALIZED";
1715
+ COMPLETED: "COMPLETED";
1716
1716
  FAILED: "FAILED";
1717
+ INITIALIZED: "INITIALIZED";
1717
1718
  PENDING: "PENDING";
1718
- COMPLETED: "COMPLETED";
1719
1719
  CREATED: "CREATED";
1720
1720
  }>, z.ZodArray<z.ZodEnum<{
1721
1721
  PREPARED: "PREPARED";
1722
- INITIALIZED: "INITIALIZED";
1722
+ COMPLETED: "COMPLETED";
1723
1723
  FAILED: "FAILED";
1724
+ INITIALIZED: "INITIALIZED";
1724
1725
  PENDING: "PENDING";
1725
- COMPLETED: "COMPLETED";
1726
1726
  CREATED: "CREATED";
1727
1727
  }>>]>>;
1728
1728
  }, z.core.$strip>;
@@ -2314,7 +2314,6 @@ declare class BankServiceBase extends BankServiceBase_base {
2314
2314
  updatedAt: Date | null;
2315
2315
  deletedAt: Date | null;
2316
2316
  }[]>;
2317
- _saveOrUpdatePayments(accounts: AccountSelectType[]): Promise<void>;
2318
2317
  _initiateBankConnector({ connectorKey, withAuth, }: {
2319
2318
  connectorKey: ConnectorKey;
2320
2319
  withAuth?: boolean;
@@ -2325,15 +2324,13 @@ declare class BankServiceBase extends BankServiceBase_base {
2325
2324
  syncAccountStatus(input: SyncAccountStatusInput): Promise<IRPCResponse<SyncAccountStatusOutput>>;
2326
2325
  syncAccountRestart(input: SyncAccountRestartInput): Promise<IRPCResponse<SyncAccountRestartOutput>>;
2327
2326
  syncAccountTerminate(input: SyncAccountTerminateInput): Promise<IRPCResponse<SyncAccountTerminateOutput>>;
2328
- syncAccounts(options?: {
2329
- accounts?: AccountSelectType[];
2330
- }): Promise<IRPCResponse<void>>;
2327
+ syncAccounts(): Promise<IRPCResponse<never>>;
2331
2328
  scheduled(controller: ScheduledController): Promise<void>;
2332
2329
  updateBatchStatuses(): Promise<void>;
2333
2330
  addPaymentsToBatch({ paymentsToBatch, }: {
2334
2331
  paymentsToBatch: SendPaymentInput[];
2335
2332
  }): Promise<IRPCResponse<{
2336
- status: "PREPARED" | "FAILED" | "COMPLETED" | "OPEN" | "PROCESSING" | "READY_TO_SIGN" | "WAITING_FOR_PROCESSING" | null;
2333
+ status: "OPEN" | "PROCESSING" | "READY_TO_SIGN" | "WAITING_FOR_PROCESSING" | "PREPARED" | "COMPLETED" | "FAILED" | null;
2337
2334
  id: string;
2338
2335
  createdAt: Date | null;
2339
2336
  updatedAt: Date | null;
@@ -2,8 +2,8 @@ import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { WorkflowInstanceStatus, IRPCResponse } from '@develit-io/backend-sdk';
3
3
  import { WorkerEntrypoint } from 'cloudflare:workers';
4
4
  import { DrizzleD1Database } from 'drizzle-orm/d1';
5
- import { P as PaymentSelectType, B as BatchSelectType, t as tables, a as PaymentInsertType } from '../shared/bank.CsvBtQib.js';
6
- import { A as AccountSelectType, C as ConfigEnvironmentBank, I as IBankConnector, a as ConnectorKey } from '../shared/bank.Ckn_klvT.js';
5
+ import { P as PaymentSelectType, B as BatchSelectType, t as tables, a as PaymentInsertType } from '../shared/bank.BGZYksmV.js';
6
+ import { A as AccountSelectType, C as ConfigEnvironmentBank, I as IBankConnector, a as ConnectorKey } from '../shared/bank.2BgwPCw_.js';
7
7
  import { z } from 'zod';
8
8
  import * as drizzle_zod from 'drizzle-zod';
9
9
  import * as drizzle_orm_sqlite_core from 'drizzle-orm/sqlite-core';
@@ -1538,21 +1538,21 @@ declare const getBatchesInputSchema: z.ZodObject<{
1538
1538
  }, z.core.$strip>;
1539
1539
  filterBatchAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
1540
1540
  filterBatchStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
1541
- PREPARED: "PREPARED";
1542
- FAILED: "FAILED";
1543
- COMPLETED: "COMPLETED";
1544
1541
  OPEN: "OPEN";
1545
1542
  PROCESSING: "PROCESSING";
1546
1543
  READY_TO_SIGN: "READY_TO_SIGN";
1547
1544
  WAITING_FOR_PROCESSING: "WAITING_FOR_PROCESSING";
1548
- }>, z.ZodArray<z.ZodEnum<{
1549
1545
  PREPARED: "PREPARED";
1550
- FAILED: "FAILED";
1551
1546
  COMPLETED: "COMPLETED";
1547
+ FAILED: "FAILED";
1548
+ }>, z.ZodArray<z.ZodEnum<{
1552
1549
  OPEN: "OPEN";
1553
1550
  PROCESSING: "PROCESSING";
1554
1551
  READY_TO_SIGN: "READY_TO_SIGN";
1555
1552
  WAITING_FOR_PROCESSING: "WAITING_FOR_PROCESSING";
1553
+ PREPARED: "PREPARED";
1554
+ COMPLETED: "COMPLETED";
1555
+ FAILED: "FAILED";
1556
1556
  }>>]>>;
1557
1557
  }, z.core.$strip>;
1558
1558
  type GetBatchesInput = z.input<typeof getBatchesInputSchema>;
@@ -1581,9 +1581,9 @@ declare const getPaymentsInputSchema: z.ZodObject<{
1581
1581
  limit: z.ZodNumber;
1582
1582
  sort: z.ZodObject<{
1583
1583
  column: z.ZodEnum<{
1584
- amount: "amount";
1585
1584
  createdAt: "createdAt";
1586
1585
  updatedAt: "updatedAt";
1586
+ amount: "amount";
1587
1587
  }>;
1588
1588
  direction: z.ZodEnum<{
1589
1589
  asc: "asc";
@@ -1712,17 +1712,17 @@ declare const getPaymentsInputSchema: z.ZodObject<{
1712
1712
  filterPaymentDateTo: z.ZodOptional<z.ZodDate>;
1713
1713
  filterPaymentStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
1714
1714
  PREPARED: "PREPARED";
1715
- INITIALIZED: "INITIALIZED";
1715
+ COMPLETED: "COMPLETED";
1716
1716
  FAILED: "FAILED";
1717
+ INITIALIZED: "INITIALIZED";
1717
1718
  PENDING: "PENDING";
1718
- COMPLETED: "COMPLETED";
1719
1719
  CREATED: "CREATED";
1720
1720
  }>, z.ZodArray<z.ZodEnum<{
1721
1721
  PREPARED: "PREPARED";
1722
- INITIALIZED: "INITIALIZED";
1722
+ COMPLETED: "COMPLETED";
1723
1723
  FAILED: "FAILED";
1724
+ INITIALIZED: "INITIALIZED";
1724
1725
  PENDING: "PENDING";
1725
- COMPLETED: "COMPLETED";
1726
1726
  CREATED: "CREATED";
1727
1727
  }>>]>>;
1728
1728
  }, z.core.$strip>;
@@ -2314,7 +2314,6 @@ declare class BankServiceBase extends BankServiceBase_base {
2314
2314
  updatedAt: Date | null;
2315
2315
  deletedAt: Date | null;
2316
2316
  }[]>;
2317
- _saveOrUpdatePayments(accounts: AccountSelectType[]): Promise<void>;
2318
2317
  _initiateBankConnector({ connectorKey, withAuth, }: {
2319
2318
  connectorKey: ConnectorKey;
2320
2319
  withAuth?: boolean;
@@ -2325,15 +2324,13 @@ declare class BankServiceBase extends BankServiceBase_base {
2325
2324
  syncAccountStatus(input: SyncAccountStatusInput): Promise<IRPCResponse<SyncAccountStatusOutput>>;
2326
2325
  syncAccountRestart(input: SyncAccountRestartInput): Promise<IRPCResponse<SyncAccountRestartOutput>>;
2327
2326
  syncAccountTerminate(input: SyncAccountTerminateInput): Promise<IRPCResponse<SyncAccountTerminateOutput>>;
2328
- syncAccounts(options?: {
2329
- accounts?: AccountSelectType[];
2330
- }): Promise<IRPCResponse<void>>;
2327
+ syncAccounts(): Promise<IRPCResponse<never>>;
2331
2328
  scheduled(controller: ScheduledController): Promise<void>;
2332
2329
  updateBatchStatuses(): Promise<void>;
2333
2330
  addPaymentsToBatch({ paymentsToBatch, }: {
2334
2331
  paymentsToBatch: SendPaymentInput[];
2335
2332
  }): Promise<IRPCResponse<{
2336
- status: "PREPARED" | "FAILED" | "COMPLETED" | "OPEN" | "PROCESSING" | "READY_TO_SIGN" | "WAITING_FOR_PROCESSING" | null;
2333
+ status: "OPEN" | "PROCESSING" | "READY_TO_SIGN" | "WAITING_FOR_PROCESSING" | "PREPARED" | "COMPLETED" | "FAILED" | null;
2337
2334
  id: string;
2338
2335
  createdAt: Date | null;
2339
2336
  updatedAt: Date | null;
@@ -1,17 +1,17 @@
1
1
  import { uuidv4, bankAccountMetadataSchema, workflowInstanceStatusSchema, develitWorker, createInternalError, first, action, service } from '@develit-io/backend-sdk';
2
2
  import { WorkerEntrypoint } from 'cloudflare:workers';
3
3
  import { drizzle } from 'drizzle-orm/d1';
4
- import { t as tables, c as INSTRUCTION_PRIORITIES, C as CHARGE_BEARERS, P as PAYMENT_TYPES, g as CONNECTOR_KEYS, B as BATCH_STATUSES, d as PAYMENT_STATUSES, e as PAYMENT_DIRECTIONS, i as accountInsertSchema, v as getPaymentDirection, M as MockConnector, F as FinbricksConnector, E as ErsteConnector } from '../shared/bank.W4h6Dlzi.mjs';
4
+ import { t as tables, c as INSTRUCTION_PRIORITIES, C as CHARGE_BEARERS, P as PAYMENT_TYPES, g as CONNECTOR_KEYS, B as BATCH_STATUSES, d as PAYMENT_STATUSES, e as PAYMENT_DIRECTIONS, i as accountInsertSchema, M as MockConnector, F as FinbricksConnector, E as ErsteConnector } from '../shared/bank.DdvSjgls.mjs';
5
5
  import 'jose';
6
6
  import { CURRENCY_CODES } from '@develit-io/general-codes';
7
7
  import { z } from 'zod';
8
- import { a as getPaymentsByBankRefIdsQuery, u as updatePaymentCommand, c as createPaymentCommand, b as updateAccountLastSyncCommand, g as getCredentialsByAccountId, i as initiateConnector } from '../shared/bank.DTdKHhGd.mjs';
8
+ import { g as getCredentialsByAccountId, i as initiateConnector, c as createPaymentCommand, u as updatePaymentCommand, a as updateAccountLastSyncCommand } from '../shared/bank.CpoWaBu0.mjs';
9
9
  import { eq, inArray, and, sql, asc, desc, gte, lte } from 'drizzle-orm';
10
10
  import 'drizzle-orm/sqlite-core';
11
11
  import 'date-fns';
12
12
  import 'drizzle-zod';
13
13
  import 'drizzle-orm/relations';
14
- import '../shared/bank.BvXwHpBp.mjs';
14
+ import '../shared/bank.BcDo3cbz.mjs';
15
15
 
16
16
  const upsertAccountCommand = (db, { account }) => {
17
17
  const id = account.id || uuidv4();
@@ -383,102 +383,6 @@ let BankServiceBase = class extends develitWorker(WorkerEntrypoint) {
383
383
  async _getAccounts() {
384
384
  return await getAllAccountsQuery(this.db);
385
385
  }
386
- async _saveOrUpdatePayments(accounts) {
387
- const allFetchedPayments = [];
388
- const connectorKeys = new Set(
389
- accounts.map(({ connectorKey }) => connectorKey)
390
- );
391
- for (const connectorKey of connectorKeys) {
392
- await this._initiateBankConnector({ connectorKey });
393
- for (const account of accounts.filter(
394
- (acc) => acc.connectorKey === connectorKey
395
- )) {
396
- if (!account.lastSyncedAt) {
397
- this.log({
398
- message: `Skipping account ${account.id} as it has no lastSyncedAt date.`
399
- });
400
- continue;
401
- }
402
- const payments = await this.bankConnector.getAllAccountPayments({
403
- db: this.db,
404
- environment: this.env.ENVIRONMENT,
405
- account,
406
- filter: {
407
- dateFrom: account.lastSyncedAt
408
- }
409
- });
410
- if (!payments || payments.length === 0) continue;
411
- payments.forEach((payment) => {
412
- payment.direction = getPaymentDirection(payment, account.iban);
413
- });
414
- allFetchedPayments.push(...payments);
415
- }
416
- }
417
- if (allFetchedPayments.length < 1) return;
418
- const bankRefIds = allFetchedPayments.map((payment) => payment.bankRefId).filter(Boolean);
419
- const alreadyExistingPayments = await getPaymentsByBankRefIdsQuery(
420
- this.db,
421
- {
422
- ids: bankRefIds
423
- }
424
- );
425
- const eventsToPush = [];
426
- const paymentsCommands = allFetchedPayments.map((payment) => {
427
- const isAlreadyExisting = alreadyExistingPayments.some(
428
- (existingPayment) => existingPayment.bankRefId === payment.bankRefId
429
- );
430
- if (isAlreadyExisting) {
431
- eventsToPush.push({
432
- eventType: "BANK_PAYMENT",
433
- eventSignal: "paymentUpdated",
434
- bankPayment: payment,
435
- metadata: {
436
- correlationId: payment.correlationId,
437
- entityId: payment.id,
438
- idempotencySuffix: payment.status,
439
- timestamp: (/* @__PURE__ */ new Date()).toDateString()
440
- }
441
- });
442
- return updatePaymentCommand(this.db, { payment }).command;
443
- }
444
- eventsToPush.push({
445
- eventType: "BANK_PAYMENT",
446
- eventSignal: "paymentCreated",
447
- bankPayment: payment,
448
- metadata: {
449
- correlationId: payment.correlationId,
450
- entityId: payment.id,
451
- timestamp: (/* @__PURE__ */ new Date()).toDateString()
452
- }
453
- });
454
- return createPaymentCommand(this.db, { payment }).command;
455
- });
456
- await this.db.batch([paymentsCommands[0], ...paymentsCommands.slice(1)]);
457
- await this.pushToQueue(
458
- this.env.QUEUE_BUS_QUEUE,
459
- eventsToPush
460
- );
461
- for (const account of accounts) {
462
- const paymentsForAccount = allFetchedPayments.filter(
463
- (payment) => (payment.direction === "OUTGOING" ? payment.debtorIban : payment.creditorIban) === account.iban
464
- );
465
- let lastSyncPayment;
466
- lastSyncPayment = paymentsForAccount.filter(({ status }) => status !== "COMPLETED").sort(
467
- (a, b) => (a.createdAt?.getTime() || 0) - (b.createdAt?.getTime() || 0)
468
- )[0];
469
- if (!lastSyncPayment) {
470
- lastSyncPayment = paymentsForAccount.sort(
471
- (a, b) => (b.createdAt?.getTime() || 0) - (a.createdAt?.getTime() || 0)
472
- )[0];
473
- }
474
- if (lastSyncPayment.createdAt) {
475
- await updateAccountLastSyncCommand(this.db, {
476
- accountId: account.id,
477
- lastSyncedAt: lastSyncPayment.createdAt
478
- }).command.execute();
479
- }
480
- }
481
- }
482
386
  async _initiateBankConnector({
483
387
  connectorKey,
484
388
  withAuth
@@ -598,34 +502,14 @@ let BankServiceBase = class extends develitWorker(WorkerEntrypoint) {
598
502
  }
599
503
  );
600
504
  }
601
- async syncAccounts(options) {
505
+ async syncAccounts() {
602
506
  return this.handleAction(
603
507
  null,
604
508
  { successMessage: "Accounts synchronized successfully" },
605
509
  async () => {
606
- const accountsToSync = [];
607
- let accounts = options?.accounts || [];
608
- if (!accounts.length) {
609
- accounts = await this._getAccounts();
610
- }
611
- for (const account of accounts.filter(
612
- (acc) => !!!acc.connectorKey.includes("MOCK")
613
- )) {
614
- const accountFetchInterval = this._accountFetchInterval(
615
- account.connectorKey
616
- );
617
- const now = Date.now();
618
- const lastSyncTime = account.lastSyncedAt.getTime();
619
- const intervalMs = accountFetchInterval * 1e3;
620
- const shouldFetch = now - lastSyncTime >= intervalMs;
621
- if (!shouldFetch) continue;
622
- accountsToSync.push({
623
- ...account
624
- });
625
- }
626
- console.log("ACCS TO SYNC", accountsToSync.length);
627
- if (accountsToSync.length <= 0) return;
628
- await this._saveOrUpdatePayments(accounts);
510
+ throw createInternalError(null, {
511
+ message: "Not implemented yet"
512
+ });
629
513
  }
630
514
  );
631
515
  }