@develit-services/bank 4.1.0 → 4.2.0

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 (32) hide show
  1. package/dist/database/schema.d.cts +1 -1
  2. package/dist/database/schema.d.mts +1 -1
  3. package/dist/database/schema.d.ts +1 -1
  4. package/dist/export/worker.cjs +99 -2
  5. package/dist/export/worker.d.cts +15 -14
  6. package/dist/export/worker.d.mts +15 -14
  7. package/dist/export/worker.d.ts +15 -14
  8. package/dist/export/worker.mjs +99 -2
  9. package/dist/export/workflows.cjs +102 -87
  10. package/dist/export/workflows.mjs +102 -87
  11. package/dist/export/wrangler.d.cts +2 -1
  12. package/dist/export/wrangler.d.mts +2 -1
  13. package/dist/export/wrangler.d.ts +2 -1
  14. package/dist/shared/{bank.C0JeMUxQ.d.cts → bank.BOI0GA-f.d.cts} +5 -3
  15. package/dist/shared/{bank.C0JeMUxQ.d.mts → bank.BOI0GA-f.d.mts} +5 -3
  16. package/dist/shared/{bank.C0JeMUxQ.d.ts → bank.BOI0GA-f.d.ts} +5 -3
  17. package/dist/shared/{bank.xrNekjj9.cjs → bank.C2QV_SMx.cjs} +24 -4
  18. package/dist/shared/{bank.BxZARqNE.mjs → bank.CL02VO9K.mjs} +24 -4
  19. package/dist/shared/{bank.CjPpPiJF.d.mts → bank.CT-uUhZB.d.mts} +1 -1
  20. package/dist/shared/{bank.DNFep60v.cjs → bank.Cfu7ZqyP.cjs} +1 -1
  21. package/dist/shared/{bank.DflRiCrT.d.ts → bank.DDmes7Gx.d.ts} +1 -1
  22. package/dist/shared/{bank.CwB3cDIG.d.cts → bank.DUd_XvqB.d.cts} +1 -1
  23. package/dist/shared/{bank.BydmpvCs.d.ts → bank.Dq24vYU7.d.cts} +1 -0
  24. package/dist/shared/{bank.BydmpvCs.d.cts → bank.Dq24vYU7.d.mts} +1 -0
  25. package/dist/shared/{bank.BydmpvCs.d.mts → bank.Dq24vYU7.d.ts} +1 -0
  26. package/dist/shared/{bank.BScD3GXI.mjs → bank.qcrBpYcu.mjs} +1 -1
  27. package/dist/types.cjs +1 -1
  28. package/dist/types.d.cts +5 -5
  29. package/dist/types.d.mts +5 -5
  30. package/dist/types.d.ts +5 -5
  31. package/dist/types.mjs +1 -1
  32. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- export { aA as account, aB as accountCredentials, aC as batch, aD as ott, aE as payment, aF as paymentRequest } from '../shared/bank.C0JeMUxQ.cjs';
1
+ export { aA as account, aB as accountCredentials, aC as batch, aD as ott, aE as payment, aF as paymentRequest } from '../shared/bank.BOI0GA-f.cjs';
2
2
  import 'drizzle-orm';
3
3
  import 'drizzle-orm/sqlite-core';
4
4
  import '@develit-io/general-codes';
@@ -1,4 +1,4 @@
1
- export { aA as account, aB as accountCredentials, aC as batch, aD as ott, aE as payment, aF as paymentRequest } from '../shared/bank.C0JeMUxQ.mjs';
1
+ export { aA as account, aB as accountCredentials, aC as batch, aD as ott, aE as payment, aF as paymentRequest } from '../shared/bank.BOI0GA-f.mjs';
2
2
  import 'drizzle-orm';
3
3
  import 'drizzle-orm/sqlite-core';
4
4
  import '@develit-io/general-codes';
@@ -1,4 +1,4 @@
1
- export { aA as account, aB as accountCredentials, aC as batch, aD as ott, aE as payment, aF as paymentRequest } from '../shared/bank.C0JeMUxQ.js';
1
+ export { aA as account, aB as accountCredentials, aC as batch, aD as ott, aE as payment, aF as paymentRequest } from '../shared/bank.BOI0GA-f.js';
2
2
  import 'drizzle-orm';
3
3
  import 'drizzle-orm/sqlite-core';
4
4
  import '@develit-io/general-codes';
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const backendSdk = require('@develit-io/backend-sdk');
4
- const ott_zod = require('../shared/bank.xrNekjj9.cjs');
4
+ const ott_zod = require('../shared/bank.C2QV_SMx.cjs');
5
5
  const drizzleOrm = require('drizzle-orm');
6
6
  const cloudflare_workers = require('cloudflare:workers');
7
7
  const d1 = require('drizzle-orm/d1');
@@ -11,10 +11,69 @@ const database_schema = require('../shared/bank.9Yw4KHyl.cjs');
11
11
  const generalCodes = require('@develit-io/general-codes');
12
12
  require('date-fns');
13
13
  require('node:crypto');
14
- const credentialsResolver = require('../shared/bank.DNFep60v.cjs');
14
+ const credentialsResolver = require('../shared/bank.Cfu7ZqyP.cjs');
15
15
  require('drizzle-orm/zod');
16
16
  require('drizzle-orm/sqlite-core');
17
17
 
18
+ const SYNC_WORKFLOW_HEARTBEAT_CRON = "0 */8 * * *";
19
+ const DEAD_STATUSES = /* @__PURE__ */ new Set([
20
+ "complete",
21
+ "terminated",
22
+ "errored",
23
+ "unknown"
24
+ ]);
25
+ async function heartbeatSyncWorkflows({
26
+ entities,
27
+ resetAfterIterations,
28
+ getInstance,
29
+ createInstance,
30
+ logger
31
+ }) {
32
+ await Promise.all(
33
+ entities.map(async ({ id, iterationCount }) => {
34
+ try {
35
+ const instance = await getInstance(id);
36
+ const { status } = await instance.status();
37
+ if (DEAD_STATUSES.has(status)) {
38
+ await instance.restart();
39
+ logger.info("sync-workflow.heartbeat.zombie-recovery", {
40
+ id,
41
+ previousStatus: status
42
+ });
43
+ return;
44
+ }
45
+ if (iterationCount == null) {
46
+ logger.warn("sync-workflow.heartbeat.iteration-count-missing", {
47
+ id,
48
+ status
49
+ });
50
+ return;
51
+ }
52
+ if (iterationCount >= resetAfterIterations) {
53
+ await instance.restart();
54
+ logger.info("sync-workflow.heartbeat.scheduled-reset", {
55
+ id,
56
+ iterationCount
57
+ });
58
+ }
59
+ } catch (err) {
60
+ try {
61
+ await createInstance(id);
62
+ logger.info("sync-workflow.heartbeat.created-missing-instance", {
63
+ id
64
+ });
65
+ } catch (createErr) {
66
+ logger.error("sync-workflow.heartbeat.failed", {
67
+ id,
68
+ error: err instanceof Error ? err.message : String(err),
69
+ createError: createErr instanceof Error ? createErr.message : String(createErr)
70
+ });
71
+ }
72
+ }
73
+ })
74
+ );
75
+ }
76
+
18
77
  const sendPaymentInputSchema = zod.z.object({
19
78
  correlationId: zod.z.string().min(1),
20
79
  refId: zod.z.string().optional(),
@@ -874,8 +933,46 @@ let BankServiceBase = class extends backendSdk.develitWorker(cloudflare_workers.
874
933
  if (controller.cron === this.env.CRON_PAYMENT_STATUSES) {
875
934
  console.log("Scheduled CRON payment request statuses");
876
935
  await this.updatePaymentRequestStatuses();
936
+ return;
937
+ }
938
+ if (controller.cron === SYNC_WORKFLOW_HEARTBEAT_CRON) {
939
+ console.log("Scheduled CRON sync workflow heartbeat");
940
+ await this.heartbeatSyncWorkflows();
877
941
  }
878
942
  }
943
+ async heartbeatSyncWorkflows() {
944
+ const accounts = await this.db.select({
945
+ id: ott_zod.tables.account.id,
946
+ lastSyncMetadata: ott_zod.tables.account.lastSyncMetadata
947
+ }).from(ott_zod.tables.account).where(drizzleOrm.eq(ott_zod.tables.account.status, "AUTHORIZED")).all();
948
+ const resetAfterIterations = Number(
949
+ this.env.SYNC_WORKFLOW_RESET_AFTER_ITERATIONS
950
+ );
951
+ if (!Number.isFinite(resetAfterIterations) || resetAfterIterations <= 0) {
952
+ console.error(
953
+ "Invalid SYNC_WORKFLOW_RESET_AFTER_ITERATIONS, skipping heartbeat",
954
+ { value: this.env.SYNC_WORKFLOW_RESET_AFTER_ITERATIONS }
955
+ );
956
+ return;
957
+ }
958
+ await heartbeatSyncWorkflows({
959
+ entities: accounts.map((a) => ({
960
+ id: a.id,
961
+ iterationCount: a.lastSyncMetadata?.iterationCount
962
+ })),
963
+ resetAfterIterations,
964
+ getInstance: (id) => this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.get(id),
965
+ createInstance: (id) => this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.create({
966
+ id,
967
+ params: { accountId: id }
968
+ }),
969
+ logger: {
970
+ info: (event, data) => console.log(JSON.stringify({ level: "info", event, ...data })),
971
+ warn: (event, data) => console.warn(JSON.stringify({ level: "warn", event, ...data })),
972
+ error: (event, data) => console.error(JSON.stringify({ level: "error", event, ...data }))
973
+ }
974
+ });
975
+ }
879
976
  async handleAuthorizationCallback(input) {
880
977
  return this.handleAction(
881
978
  { data: input, schema: handleAuthorizationCallbackInputSchema },
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm from 'drizzle-orm';
2
- import { B as BatchSelectType, P as PaymentRequestSelectType, A as AccountSelectType, a as PaymentSelectType, L as LastSyncMetadata, C as ConnectorConfig, b as ConfigEnvironmentBank, t as tables, c as ConnectorKey, I as IBankConnector, d as PaymentType, e as CurrencyCode, H as HandleAuthorizationCallbackInput, f as HandleAuthorizationCallbackOutput } from '../shared/bank.C0JeMUxQ.cjs';
3
- import { S as SendPaymentInput, a as SendPaymentOutput, b as SendPaymentSyncInput, c as SendPaymentSyncOutput, F as FinbricksSupportedBanksResponse } from '../shared/bank.CwB3cDIG.cjs';
2
+ import { B as BatchSelectType, P as PaymentRequestSelectType, A as AccountSelectType, a as PaymentSelectType, L as LastSyncMetadata, C as ConnectorConfig, b as ConfigEnvironmentBank, t as tables, c as ConnectorKey, I as IBankConnector, d as PaymentType, e as CurrencyCode, H as HandleAuthorizationCallbackInput, f as HandleAuthorizationCallbackOutput } from '../shared/bank.BOI0GA-f.cjs';
3
+ import { S as SendPaymentInput, a as SendPaymentOutput, b as SendPaymentSyncInput, c as SendPaymentSyncOutput, F as FinbricksSupportedBanksResponse } from '../shared/bank.DUd_XvqB.cjs';
4
4
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
5
5
  import { WorkflowInstanceStatus, IRPCResponse } from '@develit-io/backend-sdk';
6
6
  import { WorkerEntrypoint } from 'cloudflare:workers';
@@ -2703,15 +2703,15 @@ declare const getBatchesInputSchema: z.ZodObject<{
2703
2703
  filterBatchAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
2704
2704
  filterBatchStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
2705
2705
  AUTHORIZED: "AUTHORIZED";
2706
- COMPLETED: "COMPLETED";
2707
2706
  PROCESSING: "PROCESSING";
2708
2707
  READY_TO_SIGN: "READY_TO_SIGN";
2708
+ COMPLETED: "COMPLETED";
2709
2709
  FAILED: "FAILED";
2710
2710
  }>, z.ZodArray<z.ZodEnum<{
2711
2711
  AUTHORIZED: "AUTHORIZED";
2712
- COMPLETED: "COMPLETED";
2713
2712
  PROCESSING: "PROCESSING";
2714
2713
  READY_TO_SIGN: "READY_TO_SIGN";
2714
+ COMPLETED: "COMPLETED";
2715
2715
  FAILED: "FAILED";
2716
2716
  }>>]>>;
2717
2717
  }, z.core.$strip>;
@@ -2948,20 +2948,20 @@ declare const getPaymentsInputSchema: z.ZodObject<{
2948
2948
  filterPaymentDateFrom: z.ZodOptional<z.ZodDate>;
2949
2949
  filterPaymentDateTo: z.ZodOptional<z.ZodDate>;
2950
2950
  filterPaymentStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
2951
- BOOKED: "BOOKED";
2952
- REJECTED: "REJECTED";
2953
- PENDING: "PENDING";
2954
2951
  PROCESSING: "PROCESSING";
2952
+ PENDING: "PENDING";
2953
+ BOOKED: "BOOKED";
2955
2954
  CANCELLED: "CANCELLED";
2955
+ REJECTED: "REJECTED";
2956
2956
  SCHEDULED: "SCHEDULED";
2957
2957
  HOLD: "HOLD";
2958
2958
  INFO: "INFO";
2959
2959
  }>, z.ZodArray<z.ZodEnum<{
2960
- BOOKED: "BOOKED";
2961
- REJECTED: "REJECTED";
2962
- PENDING: "PENDING";
2963
2960
  PROCESSING: "PROCESSING";
2961
+ PENDING: "PENDING";
2962
+ BOOKED: "BOOKED";
2964
2963
  CANCELLED: "CANCELLED";
2964
+ REJECTED: "REJECTED";
2965
2965
  SCHEDULED: "SCHEDULED";
2966
2966
  HOLD: "HOLD";
2967
2967
  INFO: "INFO";
@@ -3597,19 +3597,19 @@ declare const getPaymentRequestsInputSchema: z.ZodObject<{
3597
3597
  filterAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
3598
3598
  filterStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
3599
3599
  AUTHORIZED: "AUTHORIZED";
3600
- OPENED: "OPENED";
3601
3600
  COMPLETED: "COMPLETED";
3602
3601
  BOOKED: "BOOKED";
3603
- SETTLED: "SETTLED";
3604
3602
  REJECTED: "REJECTED";
3603
+ OPENED: "OPENED";
3604
+ SETTLED: "SETTLED";
3605
3605
  CLOSED: "CLOSED";
3606
3606
  }>, z.ZodArray<z.ZodEnum<{
3607
3607
  AUTHORIZED: "AUTHORIZED";
3608
- OPENED: "OPENED";
3609
3608
  COMPLETED: "COMPLETED";
3610
3609
  BOOKED: "BOOKED";
3611
- SETTLED: "SETTLED";
3612
3610
  REJECTED: "REJECTED";
3611
+ OPENED: "OPENED";
3612
+ SETTLED: "SETTLED";
3613
3613
  CLOSED: "CLOSED";
3614
3614
  }>>]>>;
3615
3615
  filterPaymentType: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
@@ -3886,6 +3886,7 @@ declare class BankServiceBase extends BankServiceBase_base {
3886
3886
  statusChanged: number;
3887
3887
  }>>;
3888
3888
  scheduled(controller: ScheduledController): Promise<void>;
3889
+ private heartbeatSyncWorkflows;
3889
3890
  handleAuthorizationCallback(input: HandleAuthorizationCallbackInput): Promise<IRPCResponse<HandleAuthorizationCallbackOutput>>;
3890
3891
  processBatch(input: ProcessBatchInput): Promise<IRPCResponse<ProcessBatchOutput>>;
3891
3892
  processBatchStatus(input: ProcessBatchStatusInput): Promise<IRPCResponse<ProcessBatchStatusOutput>>;
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm from 'drizzle-orm';
2
- import { B as BatchSelectType, P as PaymentRequestSelectType, A as AccountSelectType, a as PaymentSelectType, L as LastSyncMetadata, C as ConnectorConfig, b as ConfigEnvironmentBank, t as tables, c as ConnectorKey, I as IBankConnector, d as PaymentType, e as CurrencyCode, H as HandleAuthorizationCallbackInput, f as HandleAuthorizationCallbackOutput } from '../shared/bank.C0JeMUxQ.mjs';
3
- import { S as SendPaymentInput, a as SendPaymentOutput, b as SendPaymentSyncInput, c as SendPaymentSyncOutput, F as FinbricksSupportedBanksResponse } from '../shared/bank.CjPpPiJF.mjs';
2
+ import { B as BatchSelectType, P as PaymentRequestSelectType, A as AccountSelectType, a as PaymentSelectType, L as LastSyncMetadata, C as ConnectorConfig, b as ConfigEnvironmentBank, t as tables, c as ConnectorKey, I as IBankConnector, d as PaymentType, e as CurrencyCode, H as HandleAuthorizationCallbackInput, f as HandleAuthorizationCallbackOutput } from '../shared/bank.BOI0GA-f.mjs';
3
+ import { S as SendPaymentInput, a as SendPaymentOutput, b as SendPaymentSyncInput, c as SendPaymentSyncOutput, F as FinbricksSupportedBanksResponse } from '../shared/bank.CT-uUhZB.mjs';
4
4
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
5
5
  import { WorkflowInstanceStatus, IRPCResponse } from '@develit-io/backend-sdk';
6
6
  import { WorkerEntrypoint } from 'cloudflare:workers';
@@ -2703,15 +2703,15 @@ declare const getBatchesInputSchema: z.ZodObject<{
2703
2703
  filterBatchAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
2704
2704
  filterBatchStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
2705
2705
  AUTHORIZED: "AUTHORIZED";
2706
- COMPLETED: "COMPLETED";
2707
2706
  PROCESSING: "PROCESSING";
2708
2707
  READY_TO_SIGN: "READY_TO_SIGN";
2708
+ COMPLETED: "COMPLETED";
2709
2709
  FAILED: "FAILED";
2710
2710
  }>, z.ZodArray<z.ZodEnum<{
2711
2711
  AUTHORIZED: "AUTHORIZED";
2712
- COMPLETED: "COMPLETED";
2713
2712
  PROCESSING: "PROCESSING";
2714
2713
  READY_TO_SIGN: "READY_TO_SIGN";
2714
+ COMPLETED: "COMPLETED";
2715
2715
  FAILED: "FAILED";
2716
2716
  }>>]>>;
2717
2717
  }, z.core.$strip>;
@@ -2948,20 +2948,20 @@ declare const getPaymentsInputSchema: z.ZodObject<{
2948
2948
  filterPaymentDateFrom: z.ZodOptional<z.ZodDate>;
2949
2949
  filterPaymentDateTo: z.ZodOptional<z.ZodDate>;
2950
2950
  filterPaymentStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
2951
- BOOKED: "BOOKED";
2952
- REJECTED: "REJECTED";
2953
- PENDING: "PENDING";
2954
2951
  PROCESSING: "PROCESSING";
2952
+ PENDING: "PENDING";
2953
+ BOOKED: "BOOKED";
2955
2954
  CANCELLED: "CANCELLED";
2955
+ REJECTED: "REJECTED";
2956
2956
  SCHEDULED: "SCHEDULED";
2957
2957
  HOLD: "HOLD";
2958
2958
  INFO: "INFO";
2959
2959
  }>, z.ZodArray<z.ZodEnum<{
2960
- BOOKED: "BOOKED";
2961
- REJECTED: "REJECTED";
2962
- PENDING: "PENDING";
2963
2960
  PROCESSING: "PROCESSING";
2961
+ PENDING: "PENDING";
2962
+ BOOKED: "BOOKED";
2964
2963
  CANCELLED: "CANCELLED";
2964
+ REJECTED: "REJECTED";
2965
2965
  SCHEDULED: "SCHEDULED";
2966
2966
  HOLD: "HOLD";
2967
2967
  INFO: "INFO";
@@ -3597,19 +3597,19 @@ declare const getPaymentRequestsInputSchema: z.ZodObject<{
3597
3597
  filterAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
3598
3598
  filterStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
3599
3599
  AUTHORIZED: "AUTHORIZED";
3600
- OPENED: "OPENED";
3601
3600
  COMPLETED: "COMPLETED";
3602
3601
  BOOKED: "BOOKED";
3603
- SETTLED: "SETTLED";
3604
3602
  REJECTED: "REJECTED";
3603
+ OPENED: "OPENED";
3604
+ SETTLED: "SETTLED";
3605
3605
  CLOSED: "CLOSED";
3606
3606
  }>, z.ZodArray<z.ZodEnum<{
3607
3607
  AUTHORIZED: "AUTHORIZED";
3608
- OPENED: "OPENED";
3609
3608
  COMPLETED: "COMPLETED";
3610
3609
  BOOKED: "BOOKED";
3611
- SETTLED: "SETTLED";
3612
3610
  REJECTED: "REJECTED";
3611
+ OPENED: "OPENED";
3612
+ SETTLED: "SETTLED";
3613
3613
  CLOSED: "CLOSED";
3614
3614
  }>>]>>;
3615
3615
  filterPaymentType: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
@@ -3886,6 +3886,7 @@ declare class BankServiceBase extends BankServiceBase_base {
3886
3886
  statusChanged: number;
3887
3887
  }>>;
3888
3888
  scheduled(controller: ScheduledController): Promise<void>;
3889
+ private heartbeatSyncWorkflows;
3889
3890
  handleAuthorizationCallback(input: HandleAuthorizationCallbackInput): Promise<IRPCResponse<HandleAuthorizationCallbackOutput>>;
3890
3891
  processBatch(input: ProcessBatchInput): Promise<IRPCResponse<ProcessBatchOutput>>;
3891
3892
  processBatchStatus(input: ProcessBatchStatusInput): Promise<IRPCResponse<ProcessBatchStatusOutput>>;
@@ -1,6 +1,6 @@
1
1
  import * as drizzle_orm from 'drizzle-orm';
2
- import { B as BatchSelectType, P as PaymentRequestSelectType, A as AccountSelectType, a as PaymentSelectType, L as LastSyncMetadata, C as ConnectorConfig, b as ConfigEnvironmentBank, t as tables, c as ConnectorKey, I as IBankConnector, d as PaymentType, e as CurrencyCode, H as HandleAuthorizationCallbackInput, f as HandleAuthorizationCallbackOutput } from '../shared/bank.C0JeMUxQ.js';
3
- import { S as SendPaymentInput, a as SendPaymentOutput, b as SendPaymentSyncInput, c as SendPaymentSyncOutput, F as FinbricksSupportedBanksResponse } from '../shared/bank.DflRiCrT.js';
2
+ import { B as BatchSelectType, P as PaymentRequestSelectType, A as AccountSelectType, a as PaymentSelectType, L as LastSyncMetadata, C as ConnectorConfig, b as ConfigEnvironmentBank, t as tables, c as ConnectorKey, I as IBankConnector, d as PaymentType, e as CurrencyCode, H as HandleAuthorizationCallbackInput, f as HandleAuthorizationCallbackOutput } from '../shared/bank.BOI0GA-f.js';
3
+ import { S as SendPaymentInput, a as SendPaymentOutput, b as SendPaymentSyncInput, c as SendPaymentSyncOutput, F as FinbricksSupportedBanksResponse } from '../shared/bank.DDmes7Gx.js';
4
4
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
5
5
  import { WorkflowInstanceStatus, IRPCResponse } from '@develit-io/backend-sdk';
6
6
  import { WorkerEntrypoint } from 'cloudflare:workers';
@@ -2703,15 +2703,15 @@ declare const getBatchesInputSchema: z.ZodObject<{
2703
2703
  filterBatchAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
2704
2704
  filterBatchStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
2705
2705
  AUTHORIZED: "AUTHORIZED";
2706
- COMPLETED: "COMPLETED";
2707
2706
  PROCESSING: "PROCESSING";
2708
2707
  READY_TO_SIGN: "READY_TO_SIGN";
2708
+ COMPLETED: "COMPLETED";
2709
2709
  FAILED: "FAILED";
2710
2710
  }>, z.ZodArray<z.ZodEnum<{
2711
2711
  AUTHORIZED: "AUTHORIZED";
2712
- COMPLETED: "COMPLETED";
2713
2712
  PROCESSING: "PROCESSING";
2714
2713
  READY_TO_SIGN: "READY_TO_SIGN";
2714
+ COMPLETED: "COMPLETED";
2715
2715
  FAILED: "FAILED";
2716
2716
  }>>]>>;
2717
2717
  }, z.core.$strip>;
@@ -2948,20 +2948,20 @@ declare const getPaymentsInputSchema: z.ZodObject<{
2948
2948
  filterPaymentDateFrom: z.ZodOptional<z.ZodDate>;
2949
2949
  filterPaymentDateTo: z.ZodOptional<z.ZodDate>;
2950
2950
  filterPaymentStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
2951
- BOOKED: "BOOKED";
2952
- REJECTED: "REJECTED";
2953
- PENDING: "PENDING";
2954
2951
  PROCESSING: "PROCESSING";
2952
+ PENDING: "PENDING";
2953
+ BOOKED: "BOOKED";
2955
2954
  CANCELLED: "CANCELLED";
2955
+ REJECTED: "REJECTED";
2956
2956
  SCHEDULED: "SCHEDULED";
2957
2957
  HOLD: "HOLD";
2958
2958
  INFO: "INFO";
2959
2959
  }>, z.ZodArray<z.ZodEnum<{
2960
- BOOKED: "BOOKED";
2961
- REJECTED: "REJECTED";
2962
- PENDING: "PENDING";
2963
2960
  PROCESSING: "PROCESSING";
2961
+ PENDING: "PENDING";
2962
+ BOOKED: "BOOKED";
2964
2963
  CANCELLED: "CANCELLED";
2964
+ REJECTED: "REJECTED";
2965
2965
  SCHEDULED: "SCHEDULED";
2966
2966
  HOLD: "HOLD";
2967
2967
  INFO: "INFO";
@@ -3597,19 +3597,19 @@ declare const getPaymentRequestsInputSchema: z.ZodObject<{
3597
3597
  filterAccountId: z.ZodOptional<z.ZodUnion<readonly [z.ZodUUID, z.ZodArray<z.ZodUUID>]>>;
3598
3598
  filterStatus: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
3599
3599
  AUTHORIZED: "AUTHORIZED";
3600
- OPENED: "OPENED";
3601
3600
  COMPLETED: "COMPLETED";
3602
3601
  BOOKED: "BOOKED";
3603
- SETTLED: "SETTLED";
3604
3602
  REJECTED: "REJECTED";
3603
+ OPENED: "OPENED";
3604
+ SETTLED: "SETTLED";
3605
3605
  CLOSED: "CLOSED";
3606
3606
  }>, z.ZodArray<z.ZodEnum<{
3607
3607
  AUTHORIZED: "AUTHORIZED";
3608
- OPENED: "OPENED";
3609
3608
  COMPLETED: "COMPLETED";
3610
3609
  BOOKED: "BOOKED";
3611
- SETTLED: "SETTLED";
3612
3610
  REJECTED: "REJECTED";
3611
+ OPENED: "OPENED";
3612
+ SETTLED: "SETTLED";
3613
3613
  CLOSED: "CLOSED";
3614
3614
  }>>]>>;
3615
3615
  filterPaymentType: z.ZodOptional<z.ZodUnion<readonly [z.ZodEnum<{
@@ -3886,6 +3886,7 @@ declare class BankServiceBase extends BankServiceBase_base {
3886
3886
  statusChanged: number;
3887
3887
  }>>;
3888
3888
  scheduled(controller: ScheduledController): Promise<void>;
3889
+ private heartbeatSyncWorkflows;
3889
3890
  handleAuthorizationCallback(input: HandleAuthorizationCallbackInput): Promise<IRPCResponse<HandleAuthorizationCallbackOutput>>;
3890
3891
  processBatch(input: ProcessBatchInput): Promise<IRPCResponse<ProcessBatchOutput>>;
3891
3892
  processBatchStatus(input: ProcessBatchStatusInput): Promise<IRPCResponse<ProcessBatchStatusOutput>>;
@@ -1,5 +1,5 @@
1
1
  import { bankAccountMetadataSchema, structuredAddressSchema, uuidv4, first, buildMultiFilterConditions as buildMultiFilterConditions$1, workflowInstanceStatusSchema, develitWorker, createInternalError, action, service } from '@develit-io/backend-sdk';
2
- import { B as tables, g as accountInsertSchema, G as relations, H as initiateConnector, o as isProcessedStatus, p as isTerminalStatus, J as getNonTerminalPaymentRequestsQuery, x as toIncomingPayment, j as assignAccount, u as toBatchedPayment, y as toPaymentRequestInsert, a as FinbricksClient, F as FINBRICKS_ENDPOINTS } from '../shared/bank.BxZARqNE.mjs';
2
+ import { B as tables, g as accountInsertSchema, G as relations, H as initiateConnector, o as isProcessedStatus, p as isTerminalStatus, J as getNonTerminalPaymentRequestsQuery, x as toIncomingPayment, j as assignAccount, u as toBatchedPayment, y as toPaymentRequestInsert, a as FinbricksClient, F as FINBRICKS_ENDPOINTS } from '../shared/bank.CL02VO9K.mjs';
3
3
  import { eq, sql, and, like, asc, desc, inArray, gte, lte, isNull, count } from 'drizzle-orm';
4
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
5
5
  import { drizzle } from 'drizzle-orm/d1';
@@ -9,10 +9,69 @@ import { I as INSTRUCTION_PRIORITIES, C as CHARGE_BEARERS, g as PAYMENT_TYPES, b
9
9
  import { CURRENCY_CODES } from '@develit-io/general-codes';
10
10
  import 'date-fns';
11
11
  import 'node:crypto';
12
- import { h as encrypt, d as createCredentialsResolver, e as updatePaymentRequestStatusCommand, a as getPaymentRequestsByBatchIdQuery, g as getBatchByIdQuery, u as upsertBatchCommand, i as importAesKey, f as createPaymentCommand, b as getAccountByIdQuery } from '../shared/bank.BScD3GXI.mjs';
12
+ import { h as encrypt, d as createCredentialsResolver, e as updatePaymentRequestStatusCommand, a as getPaymentRequestsByBatchIdQuery, g as getBatchByIdQuery, u as upsertBatchCommand, i as importAesKey, f as createPaymentCommand, b as getAccountByIdQuery } from '../shared/bank.qcrBpYcu.mjs';
13
13
  import 'drizzle-orm/zod';
14
14
  import 'drizzle-orm/sqlite-core';
15
15
 
16
+ const SYNC_WORKFLOW_HEARTBEAT_CRON = "0 */8 * * *";
17
+ const DEAD_STATUSES = /* @__PURE__ */ new Set([
18
+ "complete",
19
+ "terminated",
20
+ "errored",
21
+ "unknown"
22
+ ]);
23
+ async function heartbeatSyncWorkflows({
24
+ entities,
25
+ resetAfterIterations,
26
+ getInstance,
27
+ createInstance,
28
+ logger
29
+ }) {
30
+ await Promise.all(
31
+ entities.map(async ({ id, iterationCount }) => {
32
+ try {
33
+ const instance = await getInstance(id);
34
+ const { status } = await instance.status();
35
+ if (DEAD_STATUSES.has(status)) {
36
+ await instance.restart();
37
+ logger.info("sync-workflow.heartbeat.zombie-recovery", {
38
+ id,
39
+ previousStatus: status
40
+ });
41
+ return;
42
+ }
43
+ if (iterationCount == null) {
44
+ logger.warn("sync-workflow.heartbeat.iteration-count-missing", {
45
+ id,
46
+ status
47
+ });
48
+ return;
49
+ }
50
+ if (iterationCount >= resetAfterIterations) {
51
+ await instance.restart();
52
+ logger.info("sync-workflow.heartbeat.scheduled-reset", {
53
+ id,
54
+ iterationCount
55
+ });
56
+ }
57
+ } catch (err) {
58
+ try {
59
+ await createInstance(id);
60
+ logger.info("sync-workflow.heartbeat.created-missing-instance", {
61
+ id
62
+ });
63
+ } catch (createErr) {
64
+ logger.error("sync-workflow.heartbeat.failed", {
65
+ id,
66
+ error: err instanceof Error ? err.message : String(err),
67
+ createError: createErr instanceof Error ? createErr.message : String(createErr)
68
+ });
69
+ }
70
+ }
71
+ })
72
+ );
73
+ }
74
+
16
75
  const sendPaymentInputSchema = z.object({
17
76
  correlationId: z.string().min(1),
18
77
  refId: z.string().optional(),
@@ -872,8 +931,46 @@ let BankServiceBase = class extends develitWorker(WorkerEntrypoint) {
872
931
  if (controller.cron === this.env.CRON_PAYMENT_STATUSES) {
873
932
  console.log("Scheduled CRON payment request statuses");
874
933
  await this.updatePaymentRequestStatuses();
934
+ return;
935
+ }
936
+ if (controller.cron === SYNC_WORKFLOW_HEARTBEAT_CRON) {
937
+ console.log("Scheduled CRON sync workflow heartbeat");
938
+ await this.heartbeatSyncWorkflows();
875
939
  }
876
940
  }
941
+ async heartbeatSyncWorkflows() {
942
+ const accounts = await this.db.select({
943
+ id: tables.account.id,
944
+ lastSyncMetadata: tables.account.lastSyncMetadata
945
+ }).from(tables.account).where(eq(tables.account.status, "AUTHORIZED")).all();
946
+ const resetAfterIterations = Number(
947
+ this.env.SYNC_WORKFLOW_RESET_AFTER_ITERATIONS
948
+ );
949
+ if (!Number.isFinite(resetAfterIterations) || resetAfterIterations <= 0) {
950
+ console.error(
951
+ "Invalid SYNC_WORKFLOW_RESET_AFTER_ITERATIONS, skipping heartbeat",
952
+ { value: this.env.SYNC_WORKFLOW_RESET_AFTER_ITERATIONS }
953
+ );
954
+ return;
955
+ }
956
+ await heartbeatSyncWorkflows({
957
+ entities: accounts.map((a) => ({
958
+ id: a.id,
959
+ iterationCount: a.lastSyncMetadata?.iterationCount
960
+ })),
961
+ resetAfterIterations,
962
+ getInstance: (id) => this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.get(id),
963
+ createInstance: (id) => this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.create({
964
+ id,
965
+ params: { accountId: id }
966
+ }),
967
+ logger: {
968
+ info: (event, data) => console.log(JSON.stringify({ level: "info", event, ...data })),
969
+ warn: (event, data) => console.warn(JSON.stringify({ level: "warn", event, ...data })),
970
+ error: (event, data) => console.error(JSON.stringify({ level: "error", event, ...data }))
971
+ }
972
+ });
973
+ }
877
974
  async handleAuthorizationCallback(input) {
878
975
  return this.handleAction(
879
976
  { data: input, schema: handleAuthorizationCallbackInputSchema },