@develit-services/bank 0.2.4 → 0.3.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.
- package/dist/database/schema.cjs +2 -1
- package/dist/database/schema.d.cts +1 -1
- package/dist/database/schema.d.mts +1 -1
- package/dist/database/schema.d.ts +1 -1
- package/dist/database/schema.mjs +2 -1
- package/dist/export/worker.cjs +204 -162
- package/dist/export/worker.d.cts +135 -27
- package/dist/export/worker.d.mts +135 -27
- package/dist/export/worker.d.ts +135 -27
- package/dist/export/worker.mjs +196 -154
- package/dist/export/workflows.cjs +12 -14
- package/dist/export/workflows.d.cts +10 -3
- package/dist/export/workflows.d.mts +10 -3
- package/dist/export/workflows.d.ts +10 -3
- package/dist/export/workflows.mjs +7 -9
- package/dist/export/wrangler.cjs +7 -2
- package/dist/export/wrangler.mjs +7 -2
- package/dist/shared/{bank.CyinOg3r.cjs → bank.BGuS_o9V.cjs} +1 -1
- package/dist/shared/bank.CIJDq7GL.cjs +401 -0
- package/dist/shared/{bank.NdqOkgpd.cjs → bank.CpcK1Dse.cjs} +12 -8
- package/dist/shared/{bank.DKtu6cfY.mjs → bank.CthUj-ff.mjs} +1 -1
- package/dist/shared/{bank.DXn9jD0Q.mjs → bank.lZMrqjVR.mjs} +12 -8
- package/dist/shared/bank.mZtcXaJo.mjs +391 -0
- package/dist/shared/{bank.Cfz44GPA.d.ts → bank.xrXNjWCo.d.cts} +109 -14
- package/dist/shared/{bank.Cfz44GPA.d.cts → bank.xrXNjWCo.d.mts} +109 -14
- package/dist/shared/{bank.Cfz44GPA.d.mts → bank.xrXNjWCo.d.ts} +109 -14
- package/dist/types.cjs +3 -2
- package/dist/types.d.cts +2 -2
- package/dist/types.d.mts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/types.mjs +3 -2
- package/package.json +1 -1
- package/dist/shared/bank.BvUtf1S3.mjs +0 -130
- package/dist/shared/bank.CHS79KTx.cjs +0 -136
package/dist/export/worker.mjs
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
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, M as MockConnector, F as FinbricksConnector, E as ErsteConnector } from '../shared/bank.
|
|
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.lZMrqjVR.mjs';
|
|
5
5
|
import 'jose';
|
|
6
6
|
import { CURRENCY_CODES } from '@develit-io/general-codes';
|
|
7
7
|
import { z } from 'zod';
|
|
8
|
-
import { e as encrypt, i as importAesKey,
|
|
8
|
+
import { e as encrypt, P as PROCESS_BATCH_WORKFLOW_EVENTS, i as importAesKey, a as getCredentialsByAccountId, b as initiateConnector, u as upsertBatchCommand, c as createPaymentCommand, g as getAccountByIdQuery } from '../shared/bank.mZtcXaJo.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 '
|
|
14
|
+
import 'node:crypto';
|
|
15
|
+
import 'cloudflare:workflows';
|
|
16
|
+
import '../shared/bank.CthUj-ff.mjs';
|
|
15
17
|
|
|
16
18
|
const upsertAccountCommand = (db, { account }) => {
|
|
17
19
|
const id = account.id || uuidv4();
|
|
@@ -27,29 +29,6 @@ const upsertAccountCommand = (db, { account }) => {
|
|
|
27
29
|
};
|
|
28
30
|
};
|
|
29
31
|
|
|
30
|
-
const updatePaymentCommand = (db, { payment }) => {
|
|
31
|
-
return {
|
|
32
|
-
command: db.update(tables.payment).set(payment).where(eq(tables.payment.id, payment.id)).returning()
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const upsertBatchCommand = (db, { batch }) => {
|
|
37
|
-
const id = batch.id || uuidv4();
|
|
38
|
-
const command = db.insert(tables.batch).values({
|
|
39
|
-
...batch,
|
|
40
|
-
id
|
|
41
|
-
}).onConflictDoUpdate({
|
|
42
|
-
target: tables.batch.id,
|
|
43
|
-
set: {
|
|
44
|
-
...batch
|
|
45
|
-
}
|
|
46
|
-
}).returning();
|
|
47
|
-
return {
|
|
48
|
-
id,
|
|
49
|
-
command
|
|
50
|
-
};
|
|
51
|
-
};
|
|
52
|
-
|
|
53
32
|
const createOneTimeTokenCommand = (db, {
|
|
54
33
|
ott,
|
|
55
34
|
refId,
|
|
@@ -67,6 +46,12 @@ const createOneTimeTokenCommand = (db, {
|
|
|
67
46
|
return { id, command };
|
|
68
47
|
};
|
|
69
48
|
|
|
49
|
+
const updateAccountCommand = (db, { account }) => {
|
|
50
|
+
return {
|
|
51
|
+
command: db.update(tables.account).set({ ...account }).where(eq(tables.account.id, account.id)).returning()
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
70
55
|
const expireOneTimeTokenCommand = (db, {
|
|
71
56
|
ott
|
|
72
57
|
}) => {
|
|
@@ -98,6 +83,15 @@ const deleteAccountCredentialsCommand = (db, { accountId }) => {
|
|
|
98
83
|
};
|
|
99
84
|
};
|
|
100
85
|
|
|
86
|
+
const deletePaymentsByAccountCommand = (db, { accountId }) => {
|
|
87
|
+
const command = db.update(tables.payment).set({
|
|
88
|
+
deletedAt: /* @__PURE__ */ new Date()
|
|
89
|
+
}).where(eq(tables.payment.accountId, accountId));
|
|
90
|
+
return {
|
|
91
|
+
command
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
101
95
|
const getAccountByIbanQuery = async (db, { iban }) => {
|
|
102
96
|
return await db.select().from(tables.account).where(eq(tables.account.iban, iban)).get();
|
|
103
97
|
};
|
|
@@ -116,13 +110,7 @@ const getAllAccountsQuery = async (db) => {
|
|
|
116
110
|
};
|
|
117
111
|
|
|
118
112
|
const getAllPendingBatchesQuery = (db) => {
|
|
119
|
-
return db.select().from(tables.batch).where(
|
|
120
|
-
inArray(tables.batch.status, [
|
|
121
|
-
"PROCESSING",
|
|
122
|
-
"READY_TO_SIGN",
|
|
123
|
-
"WAITING_FOR_PROCESSING"
|
|
124
|
-
])
|
|
125
|
-
);
|
|
113
|
+
return db.select().from(tables.batch).where(inArray(tables.batch.status, ["READY_TO_SIGN"]));
|
|
126
114
|
};
|
|
127
115
|
|
|
128
116
|
const buildMultiFilterConditions = (column, value) => {
|
|
@@ -152,6 +140,7 @@ const getPaymentsWithPaginationQuery = async (db, {
|
|
|
152
140
|
limit,
|
|
153
141
|
page,
|
|
154
142
|
sort,
|
|
143
|
+
ids,
|
|
155
144
|
filterPaymentAccountId,
|
|
156
145
|
filterPaymentAmount,
|
|
157
146
|
filterPaymentCurrency,
|
|
@@ -176,7 +165,8 @@ const getPaymentsWithPaginationQuery = async (db, {
|
|
|
176
165
|
buildMultiFilterConditions(
|
|
177
166
|
tables.payment.direction,
|
|
178
167
|
filterPaymentyDirection
|
|
179
|
-
)
|
|
168
|
+
),
|
|
169
|
+
buildMultiFilterConditions(tables.payment.id, ids)
|
|
180
170
|
);
|
|
181
171
|
const sortColumn = resolveColumn(tables.payment, sort.column);
|
|
182
172
|
const [{ totalCount }] = await db.select({
|
|
@@ -224,11 +214,6 @@ const getOttQuery = async (db, { ott }) => {
|
|
|
224
214
|
return await db.select().from(tables.ott).where(eq(tables.ott.oneTimeToken, ott)).get();
|
|
225
215
|
};
|
|
226
216
|
|
|
227
|
-
const getPaymentByRefIdQuery = async (db, { refId }) => {
|
|
228
|
-
const payment = await db.select().from(tables.payment).where(eq(tables.payment.refId, refId)).limit(1).get();
|
|
229
|
-
return payment;
|
|
230
|
-
};
|
|
231
|
-
|
|
232
217
|
const seperateSupportedPayments = (mappedPayments, accounts) => {
|
|
233
218
|
const [supportedPayments, unsupportedPayments] = mappedPayments.reduce(
|
|
234
219
|
([valid, invalid], payment) => {
|
|
@@ -305,8 +290,43 @@ const getBatchesInputSchema = z.object({
|
|
|
305
290
|
});
|
|
306
291
|
|
|
307
292
|
const processBatchInputSchema = z.object({
|
|
308
|
-
|
|
309
|
-
|
|
293
|
+
batchId: z.uuid()
|
|
294
|
+
});
|
|
295
|
+
z.object({
|
|
296
|
+
instanceId: z.string(),
|
|
297
|
+
details: workflowInstanceStatusSchema
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const processBatchStatusInputSchema = z.object({
|
|
301
|
+
batchId: z.uuid()
|
|
302
|
+
});
|
|
303
|
+
z.object({
|
|
304
|
+
instanceId: z.string(),
|
|
305
|
+
details: workflowInstanceStatusSchema
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
const processBatchRestartInputSchema = z.object({
|
|
309
|
+
batchId: z.uuid()
|
|
310
|
+
});
|
|
311
|
+
z.object({
|
|
312
|
+
instanceId: z.string(),
|
|
313
|
+
details: workflowInstanceStatusSchema
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const batchAuthorizedEventPayloadSchema = z.object({
|
|
317
|
+
authorized: z.boolean()
|
|
318
|
+
});
|
|
319
|
+
const processBatchEventInputSchema = z.discriminatedUnion("eventType", [
|
|
320
|
+
z.object({
|
|
321
|
+
batchId: z.uuid(),
|
|
322
|
+
eventType: z.literal(PROCESS_BATCH_WORKFLOW_EVENTS.batchAuthorized),
|
|
323
|
+
payload: batchAuthorizedEventPayloadSchema
|
|
324
|
+
})
|
|
325
|
+
// Future event types can be added here as new objects in the array
|
|
326
|
+
]);
|
|
327
|
+
z.object({
|
|
328
|
+
success: z.boolean(),
|
|
329
|
+
message: z.string()
|
|
310
330
|
});
|
|
311
331
|
|
|
312
332
|
const ALLOWED_PAYMENT_FILTERS = {
|
|
@@ -325,6 +345,7 @@ const getPaymentsInputSchema = z.object({
|
|
|
325
345
|
column: z.enum(["createdAt", "updatedAt", "amount"]),
|
|
326
346
|
direction: z.enum(["asc", "desc"])
|
|
327
347
|
}),
|
|
348
|
+
ids: z.array(z.uuid()).optional(),
|
|
328
349
|
[ALLOWED_PAYMENT_FILTERS.ACCOUNT_ID]: z.union([z.uuid(), z.uuid().array()]).optional(),
|
|
329
350
|
[ALLOWED_PAYMENT_FILTERS.AMOUNT]: z.union([z.number(), z.number().array()]).optional(),
|
|
330
351
|
[ALLOWED_PAYMENT_FILTERS.CURRENCY]: z.union([z.enum(CURRENCY_CODES), z.enum(CURRENCY_CODES).array()]).optional(),
|
|
@@ -369,6 +390,10 @@ const updateAccountInputSchema = z.object({
|
|
|
369
390
|
account: accountInsertSchema
|
|
370
391
|
});
|
|
371
392
|
|
|
393
|
+
const disconnectAccountInputSchema = z.object({
|
|
394
|
+
accountId: z.uuid()
|
|
395
|
+
});
|
|
396
|
+
|
|
372
397
|
var __defProp = Object.defineProperty;
|
|
373
398
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
374
399
|
var __decorateClass = (decorators, target, key, kind) => {
|
|
@@ -567,11 +592,27 @@ let BankServiceBase = class extends develitWorker(WorkerEntrypoint) {
|
|
|
567
592
|
});
|
|
568
593
|
return command;
|
|
569
594
|
});
|
|
570
|
-
console.log("Updated batches", finalBatches);
|
|
571
595
|
await this.db.batch([
|
|
572
596
|
updateBatchesCommands[0],
|
|
573
597
|
...updateBatchesCommands.slice(1)
|
|
574
598
|
]);
|
|
599
|
+
for (const batch of finalBatches) {
|
|
600
|
+
try {
|
|
601
|
+
const instance = await this.env.PROCESS_BATCH_WORKFLOW.get(batch.id);
|
|
602
|
+
if (batch.status === "SIGNED" || batch.status === "SIGNATURE_FAILED") {
|
|
603
|
+
instance.sendEvent({
|
|
604
|
+
type: PROCESS_BATCH_WORKFLOW_EVENTS.batchAuthorized,
|
|
605
|
+
payload: {
|
|
606
|
+
authorized: batch.status === "SIGNED"
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
} catch (_) {
|
|
611
|
+
console.log(
|
|
612
|
+
`No workflow instance found for batch ${batch.id}, skipping.`
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
575
616
|
console.log("Batch update completed");
|
|
576
617
|
}
|
|
577
618
|
async addPaymentsToBatch({
|
|
@@ -673,122 +714,62 @@ let BankServiceBase = class extends develitWorker(WorkerEntrypoint) {
|
|
|
673
714
|
async processBatch(input) {
|
|
674
715
|
return this.handleAction(
|
|
675
716
|
{ data: input, schema: processBatchInputSchema },
|
|
676
|
-
{},
|
|
677
|
-
async ({
|
|
678
|
-
await
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
717
|
+
{ successMessage: "Batch processing workflow started" },
|
|
718
|
+
async ({ batchId }) => {
|
|
719
|
+
const instance = await this.env.PROCESS_BATCH_WORKFLOW.create({
|
|
720
|
+
id: batchId,
|
|
721
|
+
params: {
|
|
722
|
+
batchId
|
|
682
723
|
}
|
|
683
|
-
}).command.execute();
|
|
684
|
-
await this._initiateBankConnector({ connectorKey });
|
|
685
|
-
if (!this.bankConnector) {
|
|
686
|
-
await upsertBatchCommand(this.db, {
|
|
687
|
-
batch: {
|
|
688
|
-
...batch,
|
|
689
|
-
status: "FAILED"
|
|
690
|
-
}
|
|
691
|
-
}).command.execute();
|
|
692
|
-
throw createInternalError(null, {
|
|
693
|
-
message: `\u274C Failed to initialize ${connectorKey} bank connector`
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
this.log({
|
|
697
|
-
message: `\u2705 Bank connector initialized successfully for account ${connectorKey}`
|
|
698
724
|
});
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
failedPayments: []
|
|
725
|
+
return {
|
|
726
|
+
instanceId: instance.id,
|
|
727
|
+
details: await instance.status()
|
|
703
728
|
};
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
await
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
const
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
]
|
|
744
|
-
});
|
|
745
|
-
console.log(authorizationUrls, "AUTH URIS FROM BANK");
|
|
746
|
-
if (!authorizationUrls.length) {
|
|
747
|
-
this.logError({ message: "Failed to retrieve signing URI" });
|
|
748
|
-
await upsertBatchCommand(this.db, {
|
|
749
|
-
batch: {
|
|
750
|
-
...batch,
|
|
751
|
-
status: "FAILED"
|
|
752
|
-
}
|
|
753
|
-
}).command.execute();
|
|
754
|
-
return;
|
|
755
|
-
}
|
|
756
|
-
const { command: upsertBatch } = upsertBatchCommand(this.db, {
|
|
757
|
-
batch: {
|
|
758
|
-
...batch,
|
|
759
|
-
payments,
|
|
760
|
-
metadata,
|
|
761
|
-
status: "READY_TO_SIGN",
|
|
762
|
-
authorizationUrls,
|
|
763
|
-
batchPaymentInitiatedAt: /* @__PURE__ */ new Date()
|
|
764
|
-
}
|
|
765
|
-
});
|
|
766
|
-
const updatePayments = payments.map(
|
|
767
|
-
(payment) => updatePaymentCommand(this.db, {
|
|
768
|
-
payment: { ...payment, status: "INITIALIZED" }
|
|
769
|
-
}).command
|
|
770
|
-
);
|
|
771
|
-
await this.db.batch([upsertBatch, ...updatePayments]);
|
|
772
|
-
this.log({
|
|
773
|
-
message: "Authorization Urls for batch to create",
|
|
774
|
-
authorizationUrl: authorizationUrls[0]
|
|
775
|
-
});
|
|
776
|
-
await this.pushToQueue(this.env.NOTIFICATIONS_QUEUE, {
|
|
777
|
-
type: "email",
|
|
778
|
-
payload: {
|
|
779
|
-
email: {
|
|
780
|
-
to: [this.env.BANK_AUTH_RECIPIENT],
|
|
781
|
-
subject: "Payment Authorization",
|
|
782
|
-
text: authorizationUrls[0] || "No Authorization Url"
|
|
783
|
-
}
|
|
784
|
-
},
|
|
785
|
-
metadata: {
|
|
786
|
-
initiator: {
|
|
787
|
-
service: this.name
|
|
788
|
-
}
|
|
789
|
-
}
|
|
729
|
+
}
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
async processBatchStatus(input) {
|
|
733
|
+
return this.handleAction(
|
|
734
|
+
{ data: input, schema: processBatchStatusInputSchema },
|
|
735
|
+
{ successMessage: "Batch processing workflow status retrieved" },
|
|
736
|
+
async ({ batchId }) => {
|
|
737
|
+
const instance = await this.env.PROCESS_BATCH_WORKFLOW.get(batchId);
|
|
738
|
+
return {
|
|
739
|
+
instanceId: instance.id,
|
|
740
|
+
details: await instance.status()
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
);
|
|
744
|
+
}
|
|
745
|
+
async processBatchRestart(input) {
|
|
746
|
+
return this.handleAction(
|
|
747
|
+
{ data: input, schema: processBatchRestartInputSchema },
|
|
748
|
+
{ successMessage: "Batch processing workflow restarted" },
|
|
749
|
+
async ({ batchId }) => {
|
|
750
|
+
const instance = await this.env.PROCESS_BATCH_WORKFLOW.get(batchId);
|
|
751
|
+
await instance.restart();
|
|
752
|
+
return {
|
|
753
|
+
instanceId: instance.id,
|
|
754
|
+
details: await instance.status()
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
async processBatchEvent(input) {
|
|
760
|
+
return this.handleAction(
|
|
761
|
+
{ data: input, schema: processBatchEventInputSchema },
|
|
762
|
+
{ successMessage: "Event sent to batch processing workflow" },
|
|
763
|
+
async ({ batchId, eventType, payload }) => {
|
|
764
|
+
const instance = await this.env.PROCESS_BATCH_WORKFLOW.get(batchId);
|
|
765
|
+
await instance.sendEvent({
|
|
766
|
+
type: eventType,
|
|
767
|
+
payload
|
|
790
768
|
});
|
|
791
|
-
|
|
769
|
+
return {
|
|
770
|
+
success: true,
|
|
771
|
+
message: `Event '${eventType}' sent to workflow instance ${batchId}`
|
|
772
|
+
};
|
|
792
773
|
}
|
|
793
774
|
);
|
|
794
775
|
}
|
|
@@ -1027,6 +1008,64 @@ let BankServiceBase = class extends develitWorker(WorkerEntrypoint) {
|
|
|
1027
1008
|
}
|
|
1028
1009
|
);
|
|
1029
1010
|
}
|
|
1011
|
+
async disconnectAccount(input) {
|
|
1012
|
+
return this.handleAction(
|
|
1013
|
+
{ data: input, schema: disconnectAccountInputSchema },
|
|
1014
|
+
{ successMessage: "Account disconnected successfully" },
|
|
1015
|
+
async ({ accountId }) => {
|
|
1016
|
+
const account = await getAccountByIdQuery(this.db, { accountId });
|
|
1017
|
+
if (!account) {
|
|
1018
|
+
throw createInternalError(null, {
|
|
1019
|
+
message: "Account not found",
|
|
1020
|
+
code: "ACCOUNT_NOT_FOUND",
|
|
1021
|
+
status: 404
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
try {
|
|
1025
|
+
const instance = await this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.get(accountId);
|
|
1026
|
+
await instance.terminate();
|
|
1027
|
+
} catch (error) {
|
|
1028
|
+
this.log({
|
|
1029
|
+
message: "No workflow instance found for account, skipping termination.",
|
|
1030
|
+
error
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
const { command: updateAccount } = updateAccountCommand(this.db, {
|
|
1034
|
+
account: {
|
|
1035
|
+
...account,
|
|
1036
|
+
id: accountId,
|
|
1037
|
+
status: "DISABLED"
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
const { command: deleteCredentials } = deleteAccountCredentialsCommand(
|
|
1041
|
+
this.db,
|
|
1042
|
+
{
|
|
1043
|
+
accountId
|
|
1044
|
+
}
|
|
1045
|
+
);
|
|
1046
|
+
const { command: deletePayments } = deletePaymentsByAccountCommand(
|
|
1047
|
+
this.db,
|
|
1048
|
+
{
|
|
1049
|
+
accountId
|
|
1050
|
+
}
|
|
1051
|
+
);
|
|
1052
|
+
const [updatedAccountResult] = await this.db.batch([
|
|
1053
|
+
updateAccount,
|
|
1054
|
+
deleteCredentials,
|
|
1055
|
+
deletePayments
|
|
1056
|
+
]);
|
|
1057
|
+
const updatedAccount = first(updatedAccountResult);
|
|
1058
|
+
if (!updatedAccount) {
|
|
1059
|
+
throw createInternalError(null, {
|
|
1060
|
+
message: "Account not updated",
|
|
1061
|
+
code: "DB-B-0?",
|
|
1062
|
+
status: 404
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
return updatedAccount;
|
|
1066
|
+
}
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1030
1069
|
async getBatches(input) {
|
|
1031
1070
|
return this.handleAction(
|
|
1032
1071
|
{ data: input, schema: getBatchesInputSchema },
|
|
@@ -1079,6 +1118,9 @@ __decorateClass([
|
|
|
1079
1118
|
__decorateClass([
|
|
1080
1119
|
action("update-account")
|
|
1081
1120
|
], BankServiceBase.prototype, "updateAccount", 1);
|
|
1121
|
+
__decorateClass([
|
|
1122
|
+
action("disconnectAccount")
|
|
1123
|
+
], BankServiceBase.prototype, "disconnectAccount", 1);
|
|
1082
1124
|
__decorateClass([
|
|
1083
1125
|
action("getBatches")
|
|
1084
1126
|
], BankServiceBase.prototype, "getBatches", 1);
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
const cloudflare_workers = require('cloudflare:workers');
|
|
4
4
|
const cloudflare_workflows = require('cloudflare:workflows');
|
|
5
5
|
const d1 = require('drizzle-orm/d1');
|
|
6
|
-
const database_schema = require('../shared/bank.
|
|
6
|
+
const database_schema = require('../shared/bank.CpcK1Dse.cjs');
|
|
7
7
|
const backendSdk = require('@develit-io/backend-sdk');
|
|
8
|
-
const
|
|
8
|
+
const processBatch_workflow = require('../shared/bank.CIJDq7GL.cjs');
|
|
9
9
|
const drizzleOrm = require('drizzle-orm');
|
|
10
10
|
require('drizzle-orm/sqlite-core');
|
|
11
11
|
require('date-fns');
|
|
@@ -13,7 +13,8 @@ require('jose');
|
|
|
13
13
|
require('@develit-io/general-codes');
|
|
14
14
|
require('drizzle-zod');
|
|
15
15
|
require('drizzle-orm/relations');
|
|
16
|
-
require('
|
|
16
|
+
require('node:crypto');
|
|
17
|
+
require('../shared/bank.BGuS_o9V.cjs');
|
|
17
18
|
|
|
18
19
|
const updateAccountLastSyncCommand = (db, {
|
|
19
20
|
lastSyncAt,
|
|
@@ -29,10 +30,6 @@ const updateAccountLastSyncCommand = (db, {
|
|
|
29
30
|
};
|
|
30
31
|
};
|
|
31
32
|
|
|
32
|
-
const getAccountByIdQuery = async (db, { accountId }) => {
|
|
33
|
-
return await db.select().from(database_schema.tables.account).where(drizzleOrm.eq(database_schema.tables.account.id, accountId)).get();
|
|
34
|
-
};
|
|
35
|
-
|
|
36
33
|
function pushToQueue(queue, message) {
|
|
37
34
|
if (!Array.isArray(message)) return queue.send(message, { contentType: "v8" });
|
|
38
35
|
return queue.sendBatch(
|
|
@@ -42,14 +39,14 @@ function pushToQueue(queue, message) {
|
|
|
42
39
|
}))
|
|
43
40
|
);
|
|
44
41
|
}
|
|
45
|
-
class
|
|
42
|
+
class BankSyncAccountPaymentsWorkflow extends cloudflare_workers.WorkflowEntrypoint {
|
|
46
43
|
async run(event, step) {
|
|
47
44
|
const { accountId } = event.payload;
|
|
48
45
|
const db = d1.drizzle(this.env.BANK_D1, { schema: database_schema.tables });
|
|
49
46
|
while (true) {
|
|
50
47
|
const now = /* @__PURE__ */ new Date();
|
|
51
48
|
const account = await step.do("load account", async () => {
|
|
52
|
-
const account2 = await getAccountByIdQuery(db, { accountId });
|
|
49
|
+
const account2 = await processBatch_workflow.getAccountByIdQuery(db, { accountId });
|
|
53
50
|
if (!account2) {
|
|
54
51
|
throw new cloudflare_workflows.NonRetryableError(`Bank account not found: ${accountId}`);
|
|
55
52
|
}
|
|
@@ -66,8 +63,8 @@ class SyncAccountPaymentsWorkflow extends cloudflare_workers.WorkflowEntrypoint
|
|
|
66
63
|
timeout: "30 seconds"
|
|
67
64
|
},
|
|
68
65
|
async () => {
|
|
69
|
-
const encryptionKey = await
|
|
70
|
-
const credentials = await
|
|
66
|
+
const encryptionKey = await processBatch_workflow.importAesKey(this.env.ENCRYPTION_KEY);
|
|
67
|
+
const credentials = await processBatch_workflow.getCredentialsByAccountId(
|
|
71
68
|
db,
|
|
72
69
|
encryptionKey,
|
|
73
70
|
{ accountId }
|
|
@@ -82,7 +79,7 @@ class SyncAccountPaymentsWorkflow extends cloudflare_workers.WorkflowEntrypoint
|
|
|
82
79
|
`Credentials have expired for account: ${accountId}`
|
|
83
80
|
);
|
|
84
81
|
}
|
|
85
|
-
const connector =
|
|
82
|
+
const connector = processBatch_workflow.initiateConnector({
|
|
86
83
|
env: this.env,
|
|
87
84
|
bank: account.connectorKey,
|
|
88
85
|
connectedAccounts: [
|
|
@@ -114,7 +111,7 @@ class SyncAccountPaymentsWorkflow extends cloudflare_workers.WorkflowEntrypoint
|
|
|
114
111
|
async () => {
|
|
115
112
|
const eventsToEmit = [];
|
|
116
113
|
const createCommands = paymentsToInsert.map(
|
|
117
|
-
(p) =>
|
|
114
|
+
(p) => processBatch_workflow.createPaymentCommand(db, { payment: p.parsed }).command
|
|
118
115
|
);
|
|
119
116
|
eventsToEmit.push(
|
|
120
117
|
...paymentsToInsert.map((p) => ({
|
|
@@ -167,4 +164,5 @@ class SyncAccountPaymentsWorkflow extends cloudflare_workers.WorkflowEntrypoint
|
|
|
167
164
|
}
|
|
168
165
|
}
|
|
169
166
|
|
|
170
|
-
exports.
|
|
167
|
+
exports.BankProcessBatchWorkflow = processBatch_workflow.BankProcessBatchWorkflow;
|
|
168
|
+
exports.BankSyncAccountPaymentsWorkflow = BankSyncAccountPaymentsWorkflow;
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
2
|
|
|
3
|
-
type Params = {
|
|
3
|
+
type Params$1 = {
|
|
4
4
|
accountId: string;
|
|
5
5
|
};
|
|
6
|
-
declare class
|
|
6
|
+
declare class BankSyncAccountPaymentsWorkflow extends WorkflowEntrypoint<BankEnv, Params$1> {
|
|
7
|
+
run(event: WorkflowEvent<Params$1>, step: WorkflowStep): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Params = {
|
|
11
|
+
batchId: string;
|
|
12
|
+
};
|
|
13
|
+
declare class BankProcessBatchWorkflow extends WorkflowEntrypoint<BankEnv, Params> {
|
|
7
14
|
run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void>;
|
|
8
15
|
}
|
|
9
16
|
|
|
10
|
-
export {
|
|
17
|
+
export { BankProcessBatchWorkflow, BankSyncAccountPaymentsWorkflow };
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
2
|
|
|
3
|
-
type Params = {
|
|
3
|
+
type Params$1 = {
|
|
4
4
|
accountId: string;
|
|
5
5
|
};
|
|
6
|
-
declare class
|
|
6
|
+
declare class BankSyncAccountPaymentsWorkflow extends WorkflowEntrypoint<BankEnv, Params$1> {
|
|
7
|
+
run(event: WorkflowEvent<Params$1>, step: WorkflowStep): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Params = {
|
|
11
|
+
batchId: string;
|
|
12
|
+
};
|
|
13
|
+
declare class BankProcessBatchWorkflow extends WorkflowEntrypoint<BankEnv, Params> {
|
|
7
14
|
run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void>;
|
|
8
15
|
}
|
|
9
16
|
|
|
10
|
-
export {
|
|
17
|
+
export { BankProcessBatchWorkflow, BankSyncAccountPaymentsWorkflow };
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
2
|
|
|
3
|
-
type Params = {
|
|
3
|
+
type Params$1 = {
|
|
4
4
|
accountId: string;
|
|
5
5
|
};
|
|
6
|
-
declare class
|
|
6
|
+
declare class BankSyncAccountPaymentsWorkflow extends WorkflowEntrypoint<BankEnv, Params$1> {
|
|
7
|
+
run(event: WorkflowEvent<Params$1>, step: WorkflowStep): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
type Params = {
|
|
11
|
+
batchId: string;
|
|
12
|
+
};
|
|
13
|
+
declare class BankProcessBatchWorkflow extends WorkflowEntrypoint<BankEnv, Params> {
|
|
7
14
|
run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void>;
|
|
8
15
|
}
|
|
9
16
|
|
|
10
|
-
export {
|
|
17
|
+
export { BankProcessBatchWorkflow, BankSyncAccountPaymentsWorkflow };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { WorkflowEntrypoint } from 'cloudflare:workers';
|
|
2
2
|
import { NonRetryableError } from 'cloudflare:workflows';
|
|
3
3
|
import { drizzle } from 'drizzle-orm/d1';
|
|
4
|
-
import { t as tables } from '../shared/bank.
|
|
4
|
+
import { t as tables } from '../shared/bank.lZMrqjVR.mjs';
|
|
5
5
|
import { asNonEmpty } from '@develit-io/backend-sdk';
|
|
6
|
-
import { i as importAesKey,
|
|
6
|
+
import { g as getAccountByIdQuery, i as importAesKey, a as getCredentialsByAccountId, b as initiateConnector, c as createPaymentCommand } from '../shared/bank.mZtcXaJo.mjs';
|
|
7
|
+
export { B as BankProcessBatchWorkflow } from '../shared/bank.mZtcXaJo.mjs';
|
|
7
8
|
import { eq } from 'drizzle-orm';
|
|
8
9
|
import 'drizzle-orm/sqlite-core';
|
|
9
10
|
import 'date-fns';
|
|
@@ -11,7 +12,8 @@ import 'jose';
|
|
|
11
12
|
import '@develit-io/general-codes';
|
|
12
13
|
import 'drizzle-zod';
|
|
13
14
|
import 'drizzle-orm/relations';
|
|
14
|
-
import '
|
|
15
|
+
import 'node:crypto';
|
|
16
|
+
import '../shared/bank.CthUj-ff.mjs';
|
|
15
17
|
|
|
16
18
|
const updateAccountLastSyncCommand = (db, {
|
|
17
19
|
lastSyncAt,
|
|
@@ -27,10 +29,6 @@ const updateAccountLastSyncCommand = (db, {
|
|
|
27
29
|
};
|
|
28
30
|
};
|
|
29
31
|
|
|
30
|
-
const getAccountByIdQuery = async (db, { accountId }) => {
|
|
31
|
-
return await db.select().from(tables.account).where(eq(tables.account.id, accountId)).get();
|
|
32
|
-
};
|
|
33
|
-
|
|
34
32
|
function pushToQueue(queue, message) {
|
|
35
33
|
if (!Array.isArray(message)) return queue.send(message, { contentType: "v8" });
|
|
36
34
|
return queue.sendBatch(
|
|
@@ -40,7 +38,7 @@ function pushToQueue(queue, message) {
|
|
|
40
38
|
}))
|
|
41
39
|
);
|
|
42
40
|
}
|
|
43
|
-
class
|
|
41
|
+
class BankSyncAccountPaymentsWorkflow extends WorkflowEntrypoint {
|
|
44
42
|
async run(event, step) {
|
|
45
43
|
const { accountId } = event.payload;
|
|
46
44
|
const db = drizzle(this.env.BANK_D1, { schema: tables });
|
|
@@ -165,4 +163,4 @@ class SyncAccountPaymentsWorkflow extends WorkflowEntrypoint {
|
|
|
165
163
|
}
|
|
166
164
|
}
|
|
167
165
|
|
|
168
|
-
export {
|
|
166
|
+
export { BankSyncAccountPaymentsWorkflow };
|
package/dist/export/wrangler.cjs
CHANGED
|
@@ -37,9 +37,14 @@ function defineBankServiceWrangler(config) {
|
|
|
37
37
|
],
|
|
38
38
|
workflows: [
|
|
39
39
|
{
|
|
40
|
-
name: `${project}-sync-account-payments`,
|
|
40
|
+
name: `${project}-bank-sync-account-payments`,
|
|
41
41
|
binding: "SYNC_ACCOUNT_PAYMENTS_WORKFLOW",
|
|
42
|
-
class_name: "
|
|
42
|
+
class_name: "BankSyncAccountPaymentsWorkflow"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: `${project}-bank-process-batch`,
|
|
46
|
+
binding: "PROCESS_BATCH_WORKFLOW",
|
|
47
|
+
class_name: "BankProcessBatchWorkflow"
|
|
43
48
|
}
|
|
44
49
|
],
|
|
45
50
|
queues: {
|