@develit-services/bank 0.1.6 → 0.1.8
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 +1 -1
- package/dist/database/schema.mjs +1 -1
- package/dist/export/worker.cjs +14 -15
- package/dist/export/worker.d.cts +0 -1
- package/dist/export/worker.d.mts +0 -1
- package/dist/export/worker.d.ts +0 -1
- package/dist/export/worker.mjs +4 -5
- package/dist/export/workflows.cjs +160 -9
- package/dist/export/workflows.d.cts +10 -2
- package/dist/export/workflows.d.mts +10 -2
- package/dist/export/workflows.d.ts +10 -2
- package/dist/export/workflows.mjs +163 -8
- package/dist/shared/bank.B_nwGY5X.mjs +113 -0
- package/dist/shared/{bank.xIMAnZ4v.mjs → bank.DHj3MnQJ.mjs} +60 -0
- package/dist/shared/{bank.PDmcU0T-.cjs → bank.DrMnskU2.cjs} +60 -0
- package/dist/shared/{bank.D2ZeOkyc.mjs → bank.DulU-rr_.mjs} +1 -1
- package/dist/shared/{bank.BcCfzRPi.cjs → bank.YNS7gdPU.cjs} +1 -1
- package/dist/shared/bank.pT49Hbb4.cjs +120 -0
- package/dist/types.cjs +2 -2
- package/dist/types.d.cts +65 -1
- package/dist/types.d.mts +65 -1
- package/dist/types.d.ts +65 -1
- package/dist/types.mjs +2 -2
- package/package.json +1 -1
- package/dist/shared/bank.B7_g0cO5.mjs +0 -277
- package/dist/shared/bank.ChAGzUFo.d.cts +0 -18
- package/dist/shared/bank.ChAGzUFo.d.mts +0 -18
- package/dist/shared/bank.ChAGzUFo.d.ts +0 -18
- package/dist/shared/bank.CrqX5SOO.cjs +0 -286
package/dist/types.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { A as ACCOUNT_STATUSES, B as BATCH_STATUES, B as BATCH_STATUSES, C as CHARGE_BEARERS, g as CONNECTOR_KEYS, f as COUNTRY_CODES, h as CREDENTIALS_TYPES, E as ErsteConnector, b as FINBRICKS_ENDPOINTS, a as FinbricksClient, F as FinbricksConnector, I as IBankConnector, c as INSTRUCTION_PRIORITIES, M as MockConnector, e as PAYMENT_DIRECTIONS, d as PAYMENT_STATUSES, P as PAYMENT_TYPES, T as TOKEN_TYPES, l as accountCredentialsInsertSchema, n as accountCredentialsSelectSchema, m as accountCredentialsUpdateSchema, i as accountInsertSchema, k as accountSelectSchema, j as accountUpdateSchema, o as ottInsertSchema, r as ottSelectSchema, q as ottUpdateSchema, p as paymentInsertTypeZod, s as signFinbricksJws, u as useFinbricksFetch } from './shared/bank.
|
|
2
|
-
export { M as MockCobsConnector } from './shared/bank.
|
|
1
|
+
export { A as ACCOUNT_STATUSES, B as BATCH_STATUES, B as BATCH_STATUSES, C as CHARGE_BEARERS, g as CONNECTOR_KEYS, f as COUNTRY_CODES, h as CREDENTIALS_TYPES, E as ErsteConnector, b as FINBRICKS_ENDPOINTS, a as FinbricksClient, F as FinbricksConnector, I as IBankConnector, c as INSTRUCTION_PRIORITIES, M as MockConnector, e as PAYMENT_DIRECTIONS, d as PAYMENT_STATUSES, P as PAYMENT_TYPES, T as TOKEN_TYPES, l as accountCredentialsInsertSchema, n as accountCredentialsSelectSchema, m as accountCredentialsUpdateSchema, i as accountInsertSchema, k as accountSelectSchema, j as accountUpdateSchema, o as ottInsertSchema, r as ottSelectSchema, q as ottUpdateSchema, p as paymentInsertTypeZod, s as signFinbricksJws, u as useFinbricksFetch } from './shared/bank.DHj3MnQJ.mjs';
|
|
2
|
+
export { M as MockCobsConnector } from './shared/bank.DulU-rr_.mjs';
|
|
3
3
|
export { BANK_CODES, CURRENCY_CODES } from '@develit-io/general-codes';
|
|
4
4
|
import '@develit-io/backend-sdk';
|
|
5
5
|
import 'drizzle-orm/sqlite-core';
|
package/package.json
CHANGED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import { WorkflowEntrypoint } from 'cloudflare:workers';
|
|
2
|
-
import { NonRetryableError } from 'cloudflare:workflows';
|
|
3
|
-
import { drizzle } from 'drizzle-orm/d1';
|
|
4
|
-
import { t as tables, F as FinbricksConnector, M as MockConnector, E as ErsteConnector, v as getPaymentDirection } from './bank.xIMAnZ4v.mjs';
|
|
5
|
-
import { asNonEmpty } from '@develit-io/backend-sdk';
|
|
6
|
-
import { eq, inArray } from 'drizzle-orm';
|
|
7
|
-
import { M as MockCobsConnector } from './bank.D2ZeOkyc.mjs';
|
|
8
|
-
import 'jose';
|
|
9
|
-
import '@develit-io/general-codes';
|
|
10
|
-
|
|
11
|
-
const createPaymentCommand = (db, { payment }) => {
|
|
12
|
-
return {
|
|
13
|
-
command: db.insert(tables.payment).values({
|
|
14
|
-
...payment,
|
|
15
|
-
creditorIban: payment.creditor.iban,
|
|
16
|
-
debtorIban: payment.debtor.iban
|
|
17
|
-
}).returning()
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const updatePaymentCommand = (db, { payment }) => {
|
|
22
|
-
return {
|
|
23
|
-
command: db.update(tables.payment).set(payment).where(eq(tables.payment.id, payment.id)).returning()
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const updateAccountLastSyncCommand = (db, { lastSyncedAt, accountId }) => {
|
|
28
|
-
const command = db.update(tables.account).set({
|
|
29
|
-
lastSyncedAt
|
|
30
|
-
}).where(eq(tables.account.id, accountId)).returning();
|
|
31
|
-
return {
|
|
32
|
-
command
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const getAccountByIdQuery = async (db, { accountId }) => {
|
|
37
|
-
return await db.select().from(tables.account).where(eq(tables.account.id, accountId)).get();
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
const getCredentialsByAccountId = async (db, { accountId }) => {
|
|
41
|
-
const cred = await db.select().from(tables.accountCredentials).where(eq(tables.accountCredentials.accountId, accountId)).get();
|
|
42
|
-
return cred;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
const getPaymentsByBankRefIdsQuery = async (db, { ids }) => {
|
|
46
|
-
return await db.select().from(tables.payment).where(inArray(tables.payment.bankRefId, ids));
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
class CreditasConnector extends FinbricksConnector {
|
|
50
|
-
constructor(config) {
|
|
51
|
-
super("CREDITAS", config);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
class FioConnector extends FinbricksConnector {
|
|
56
|
-
constructor(config) {
|
|
57
|
-
super("FIO", config);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
class MonetaConnector extends FinbricksConnector {
|
|
62
|
-
constructor(config) {
|
|
63
|
-
super("MONETA", config);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const initiateConnector = ({
|
|
68
|
-
bank,
|
|
69
|
-
env,
|
|
70
|
-
connectedAccounts
|
|
71
|
-
}) => {
|
|
72
|
-
switch (bank) {
|
|
73
|
-
case "ERSTE":
|
|
74
|
-
return new ErsteConnector({
|
|
75
|
-
API_KEY: env.ERSTE_API_KEY,
|
|
76
|
-
CLIENT_ID: env.ERSTE_CLIENT_ID,
|
|
77
|
-
CLIENT_SECRET: env.ERSTE_CLIENT_SECRET,
|
|
78
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
79
|
-
AUTH_URI: env.ERSTE_AUTH_URI,
|
|
80
|
-
PAYMENTS_URI: env.ERSTE_PAYMENTS_URI,
|
|
81
|
-
ACCOUNTS_URI: env.ERSTE_ACCOUNTS_URI,
|
|
82
|
-
connectedAccounts
|
|
83
|
-
});
|
|
84
|
-
case "CREDITAS":
|
|
85
|
-
return new CreditasConnector({
|
|
86
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
87
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
88
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
89
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
90
|
-
connectedAccounts
|
|
91
|
-
});
|
|
92
|
-
case "MOCK_COBS":
|
|
93
|
-
return new MockCobsConnector({
|
|
94
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
95
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
96
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
97
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
98
|
-
connectedAccounts
|
|
99
|
-
});
|
|
100
|
-
case "FIO":
|
|
101
|
-
return new FioConnector({
|
|
102
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
103
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
104
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
105
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
106
|
-
connectedAccounts
|
|
107
|
-
});
|
|
108
|
-
case "MONETA":
|
|
109
|
-
return new MonetaConnector({
|
|
110
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
111
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
112
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
113
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
114
|
-
connectedAccounts
|
|
115
|
-
});
|
|
116
|
-
default:
|
|
117
|
-
return new MockConnector();
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
function pushToQueue(queue, message) {
|
|
122
|
-
if (!Array.isArray(message)) return queue.send(message, { contentType: "v8" });
|
|
123
|
-
return queue.sendBatch(
|
|
124
|
-
message.map((m) => ({
|
|
125
|
-
body: m,
|
|
126
|
-
contentType: "v8"
|
|
127
|
-
}))
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
class SyncAccountPaymentsWorkflow extends WorkflowEntrypoint {
|
|
131
|
-
async run(event, step) {
|
|
132
|
-
const { accountId } = event.payload;
|
|
133
|
-
const db = drizzle(this.env.BANK_D1, { schema: tables });
|
|
134
|
-
while (true) {
|
|
135
|
-
const account = await step.do("load account", async () => {
|
|
136
|
-
const account2 = await getAccountByIdQuery(db, { accountId });
|
|
137
|
-
if (!account2) {
|
|
138
|
-
throw new NonRetryableError(`Bank account not found: ${accountId}`);
|
|
139
|
-
}
|
|
140
|
-
return account2;
|
|
141
|
-
});
|
|
142
|
-
if (!account.lastSyncedAt) {
|
|
143
|
-
throw new NonRetryableError(
|
|
144
|
-
`lastSyncedAt is not set for account: ${accountId}`
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
const credentials = await step.do(
|
|
148
|
-
"load account credentials",
|
|
149
|
-
async () => {
|
|
150
|
-
const credentials2 = await getCredentialsByAccountId(db, { accountId });
|
|
151
|
-
if (!credentials2) {
|
|
152
|
-
throw new NonRetryableError(
|
|
153
|
-
`No credentials found for account: ${accountId}`
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
return credentials2;
|
|
157
|
-
}
|
|
158
|
-
);
|
|
159
|
-
const payments = await step.do(
|
|
160
|
-
"fetch bank payments",
|
|
161
|
-
{
|
|
162
|
-
retries: { limit: 5, delay: 2e3, backoff: "constant" },
|
|
163
|
-
timeout: "30 seconds"
|
|
164
|
-
},
|
|
165
|
-
async () => {
|
|
166
|
-
const connector = initiateConnector({
|
|
167
|
-
env: this.env,
|
|
168
|
-
bank: account.connectorKey,
|
|
169
|
-
connectedAccounts: [
|
|
170
|
-
{
|
|
171
|
-
...account,
|
|
172
|
-
iban: account.iban,
|
|
173
|
-
token: credentials.value
|
|
174
|
-
}
|
|
175
|
-
]
|
|
176
|
-
});
|
|
177
|
-
const fetched = await connector.getAllAccountPayments({
|
|
178
|
-
environment: this.env.ENVIRONMENT,
|
|
179
|
-
db,
|
|
180
|
-
account,
|
|
181
|
-
filter: { dateFrom: account.lastSyncedAt }
|
|
182
|
-
});
|
|
183
|
-
return fetched.map((p) => ({
|
|
184
|
-
...p,
|
|
185
|
-
direction: getPaymentDirection(p, account.iban)
|
|
186
|
-
}));
|
|
187
|
-
}
|
|
188
|
-
);
|
|
189
|
-
if (payments.length) {
|
|
190
|
-
const bankRefIds = payments.map((payment) => payment.bankRefId).filter(Boolean);
|
|
191
|
-
const existing = await step.do("load existing payments", async () => {
|
|
192
|
-
return await getPaymentsByBankRefIdsQuery(db, {
|
|
193
|
-
ids: bankRefIds
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
const paymentsToUpdate = payments.filter(
|
|
197
|
-
(p) => existing.some((e) => e.bankRefId === p.bankRefId)
|
|
198
|
-
);
|
|
199
|
-
const paymentsToCreate = payments.filter(
|
|
200
|
-
(p) => !existing.some((e) => e.bankRefId === p.bankRefId)
|
|
201
|
-
);
|
|
202
|
-
await step.do("process payments and update lastSyncAt", async () => {
|
|
203
|
-
const eventsToEmit = [];
|
|
204
|
-
const updateCommands = paymentsToUpdate.map(
|
|
205
|
-
(p) => updatePaymentCommand(db, { payment: p }).command
|
|
206
|
-
);
|
|
207
|
-
eventsToEmit.push(
|
|
208
|
-
...paymentsToUpdate.map((p) => ({
|
|
209
|
-
eventType: "BANK_PAYMENT",
|
|
210
|
-
eventSignal: "paymentUpdated",
|
|
211
|
-
bankPayment: p,
|
|
212
|
-
metadata: {
|
|
213
|
-
correlationId: p.correlationId,
|
|
214
|
-
entityId: p.id,
|
|
215
|
-
idempotencySuffix: p.status,
|
|
216
|
-
timestamp: (/* @__PURE__ */ new Date()).toDateString()
|
|
217
|
-
}
|
|
218
|
-
}))
|
|
219
|
-
);
|
|
220
|
-
const createCommands = paymentsToCreate.map(
|
|
221
|
-
(p) => createPaymentCommand(db, { payment: p }).command
|
|
222
|
-
);
|
|
223
|
-
eventsToEmit.push(
|
|
224
|
-
...paymentsToCreate.map((p) => ({
|
|
225
|
-
eventType: "BANK_PAYMENT",
|
|
226
|
-
eventSignal: "paymentCreated",
|
|
227
|
-
bankPayment: p,
|
|
228
|
-
metadata: {
|
|
229
|
-
correlationId: p.correlationId,
|
|
230
|
-
entityId: p.id,
|
|
231
|
-
timestamp: (/* @__PURE__ */ new Date()).toDateString()
|
|
232
|
-
}
|
|
233
|
-
}))
|
|
234
|
-
);
|
|
235
|
-
const latest = payments.reduce((current, p) => {
|
|
236
|
-
return new Date(p.processedAt).getTime() > new Date(current.processedAt).getTime() ? p : current;
|
|
237
|
-
}, payments[0]);
|
|
238
|
-
const updateLastSyncCommand = updateAccountLastSyncCommand(db, {
|
|
239
|
-
accountId: account.id,
|
|
240
|
-
lastSyncedAt: latest.processedAt
|
|
241
|
-
}).command;
|
|
242
|
-
if (eventsToEmit.length) {
|
|
243
|
-
await db.batch(
|
|
244
|
-
asNonEmpty([
|
|
245
|
-
updateLastSyncCommand,
|
|
246
|
-
...updateCommands,
|
|
247
|
-
...createCommands
|
|
248
|
-
])
|
|
249
|
-
);
|
|
250
|
-
await pushToQueue(
|
|
251
|
-
this.env.QUEUE_BUS_QUEUE,
|
|
252
|
-
eventsToEmit
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
return {
|
|
256
|
-
payments: payments.length,
|
|
257
|
-
updated: paymentsToUpdate.length,
|
|
258
|
-
created: paymentsToCreate.length,
|
|
259
|
-
eventsEmitted: eventsToEmit.length,
|
|
260
|
-
lastSyncedAt: latest?.createdAt
|
|
261
|
-
};
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
await step.sleep(
|
|
265
|
-
"Sleep for next sync",
|
|
266
|
-
`${account.syncIntervalS} seconds`
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const workflows = {
|
|
273
|
-
__proto__: null,
|
|
274
|
-
SyncAccountPaymentsWorkflow: SyncAccountPaymentsWorkflow
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
export { SyncAccountPaymentsWorkflow as S, updateAccountLastSyncCommand as a, getCredentialsByAccountId as b, createPaymentCommand as c, getPaymentsByBankRefIdsQuery as g, initiateConnector as i, updatePaymentCommand as u, workflows as w };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
|
-
|
|
3
|
-
type Params = {
|
|
4
|
-
accountId: string;
|
|
5
|
-
};
|
|
6
|
-
declare class SyncAccountPaymentsWorkflow extends WorkflowEntrypoint<BankEnv, Params> {
|
|
7
|
-
run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type workflows_SyncAccountPaymentsWorkflow = SyncAccountPaymentsWorkflow;
|
|
11
|
-
declare const workflows_SyncAccountPaymentsWorkflow: typeof SyncAccountPaymentsWorkflow;
|
|
12
|
-
declare namespace workflows {
|
|
13
|
-
export {
|
|
14
|
-
workflows_SyncAccountPaymentsWorkflow as SyncAccountPaymentsWorkflow,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { SyncAccountPaymentsWorkflow as S, workflows as w };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
|
-
|
|
3
|
-
type Params = {
|
|
4
|
-
accountId: string;
|
|
5
|
-
};
|
|
6
|
-
declare class SyncAccountPaymentsWorkflow extends WorkflowEntrypoint<BankEnv, Params> {
|
|
7
|
-
run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type workflows_SyncAccountPaymentsWorkflow = SyncAccountPaymentsWorkflow;
|
|
11
|
-
declare const workflows_SyncAccountPaymentsWorkflow: typeof SyncAccountPaymentsWorkflow;
|
|
12
|
-
declare namespace workflows {
|
|
13
|
-
export {
|
|
14
|
-
workflows_SyncAccountPaymentsWorkflow as SyncAccountPaymentsWorkflow,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { SyncAccountPaymentsWorkflow as S, workflows as w };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
|
|
2
|
-
|
|
3
|
-
type Params = {
|
|
4
|
-
accountId: string;
|
|
5
|
-
};
|
|
6
|
-
declare class SyncAccountPaymentsWorkflow extends WorkflowEntrypoint<BankEnv, Params> {
|
|
7
|
-
run(event: WorkflowEvent<Params>, step: WorkflowStep): Promise<void>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
type workflows_SyncAccountPaymentsWorkflow = SyncAccountPaymentsWorkflow;
|
|
11
|
-
declare const workflows_SyncAccountPaymentsWorkflow: typeof SyncAccountPaymentsWorkflow;
|
|
12
|
-
declare namespace workflows {
|
|
13
|
-
export {
|
|
14
|
-
workflows_SyncAccountPaymentsWorkflow as SyncAccountPaymentsWorkflow,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { SyncAccountPaymentsWorkflow as S, workflows as w };
|
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const cloudflare_workers = require('cloudflare:workers');
|
|
4
|
-
const cloudflare_workflows = require('cloudflare:workflows');
|
|
5
|
-
const d1 = require('drizzle-orm/d1');
|
|
6
|
-
const database_schema = require('./bank.PDmcU0T-.cjs');
|
|
7
|
-
const backendSdk = require('@develit-io/backend-sdk');
|
|
8
|
-
const drizzleOrm = require('drizzle-orm');
|
|
9
|
-
const mockCobs_connector = require('./bank.BcCfzRPi.cjs');
|
|
10
|
-
require('jose');
|
|
11
|
-
require('@develit-io/general-codes');
|
|
12
|
-
|
|
13
|
-
const createPaymentCommand = (db, { payment }) => {
|
|
14
|
-
return {
|
|
15
|
-
command: db.insert(database_schema.tables.payment).values({
|
|
16
|
-
...payment,
|
|
17
|
-
creditorIban: payment.creditor.iban,
|
|
18
|
-
debtorIban: payment.debtor.iban
|
|
19
|
-
}).returning()
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const updatePaymentCommand = (db, { payment }) => {
|
|
24
|
-
return {
|
|
25
|
-
command: db.update(database_schema.tables.payment).set(payment).where(drizzleOrm.eq(database_schema.tables.payment.id, payment.id)).returning()
|
|
26
|
-
};
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const updateAccountLastSyncCommand = (db, { lastSyncedAt, accountId }) => {
|
|
30
|
-
const command = db.update(database_schema.tables.account).set({
|
|
31
|
-
lastSyncedAt
|
|
32
|
-
}).where(drizzleOrm.eq(database_schema.tables.account.id, accountId)).returning();
|
|
33
|
-
return {
|
|
34
|
-
command
|
|
35
|
-
};
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const getAccountByIdQuery = async (db, { accountId }) => {
|
|
39
|
-
return await db.select().from(database_schema.tables.account).where(drizzleOrm.eq(database_schema.tables.account.id, accountId)).get();
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const getCredentialsByAccountId = async (db, { accountId }) => {
|
|
43
|
-
const cred = await db.select().from(database_schema.tables.accountCredentials).where(drizzleOrm.eq(database_schema.tables.accountCredentials.accountId, accountId)).get();
|
|
44
|
-
return cred;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const getPaymentsByBankRefIdsQuery = async (db, { ids }) => {
|
|
48
|
-
return await db.select().from(database_schema.tables.payment).where(drizzleOrm.inArray(database_schema.tables.payment.bankRefId, ids));
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
class CreditasConnector extends database_schema.FinbricksConnector {
|
|
52
|
-
constructor(config) {
|
|
53
|
-
super("CREDITAS", config);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
class FioConnector extends database_schema.FinbricksConnector {
|
|
58
|
-
constructor(config) {
|
|
59
|
-
super("FIO", config);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
class MonetaConnector extends database_schema.FinbricksConnector {
|
|
64
|
-
constructor(config) {
|
|
65
|
-
super("MONETA", config);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const initiateConnector = ({
|
|
70
|
-
bank,
|
|
71
|
-
env,
|
|
72
|
-
connectedAccounts
|
|
73
|
-
}) => {
|
|
74
|
-
switch (bank) {
|
|
75
|
-
case "ERSTE":
|
|
76
|
-
return new database_schema.ErsteConnector({
|
|
77
|
-
API_KEY: env.ERSTE_API_KEY,
|
|
78
|
-
CLIENT_ID: env.ERSTE_CLIENT_ID,
|
|
79
|
-
CLIENT_SECRET: env.ERSTE_CLIENT_SECRET,
|
|
80
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
81
|
-
AUTH_URI: env.ERSTE_AUTH_URI,
|
|
82
|
-
PAYMENTS_URI: env.ERSTE_PAYMENTS_URI,
|
|
83
|
-
ACCOUNTS_URI: env.ERSTE_ACCOUNTS_URI,
|
|
84
|
-
connectedAccounts
|
|
85
|
-
});
|
|
86
|
-
case "CREDITAS":
|
|
87
|
-
return new CreditasConnector({
|
|
88
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
89
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
90
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
91
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
92
|
-
connectedAccounts
|
|
93
|
-
});
|
|
94
|
-
case "MOCK_COBS":
|
|
95
|
-
return new mockCobs_connector.MockCobsConnector({
|
|
96
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
97
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
98
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
99
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
100
|
-
connectedAccounts
|
|
101
|
-
});
|
|
102
|
-
case "FIO":
|
|
103
|
-
return new FioConnector({
|
|
104
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
105
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
106
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
107
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
108
|
-
connectedAccounts
|
|
109
|
-
});
|
|
110
|
-
case "MONETA":
|
|
111
|
-
return new MonetaConnector({
|
|
112
|
-
BASE_URI: env.FINBRICKS_BASE_URI,
|
|
113
|
-
MERCHANT_ID: env.FINBRICKS_MERCHANT_ID,
|
|
114
|
-
PRIVATE_KEY_PEM: env.FINBRICKS_PRIVATE_KEY_PEM,
|
|
115
|
-
REDIRECT_URI: env.REDIRECT_URI,
|
|
116
|
-
connectedAccounts
|
|
117
|
-
});
|
|
118
|
-
default:
|
|
119
|
-
return new database_schema.MockConnector();
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
function pushToQueue(queue, message) {
|
|
124
|
-
if (!Array.isArray(message)) return queue.send(message, { contentType: "v8" });
|
|
125
|
-
return queue.sendBatch(
|
|
126
|
-
message.map((m) => ({
|
|
127
|
-
body: m,
|
|
128
|
-
contentType: "v8"
|
|
129
|
-
}))
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
class SyncAccountPaymentsWorkflow extends cloudflare_workers.WorkflowEntrypoint {
|
|
133
|
-
async run(event, step) {
|
|
134
|
-
const { accountId } = event.payload;
|
|
135
|
-
const db = d1.drizzle(this.env.BANK_D1, { schema: database_schema.tables });
|
|
136
|
-
while (true) {
|
|
137
|
-
const account = await step.do("load account", async () => {
|
|
138
|
-
const account2 = await getAccountByIdQuery(db, { accountId });
|
|
139
|
-
if (!account2) {
|
|
140
|
-
throw new cloudflare_workflows.NonRetryableError(`Bank account not found: ${accountId}`);
|
|
141
|
-
}
|
|
142
|
-
return account2;
|
|
143
|
-
});
|
|
144
|
-
if (!account.lastSyncedAt) {
|
|
145
|
-
throw new cloudflare_workflows.NonRetryableError(
|
|
146
|
-
`lastSyncedAt is not set for account: ${accountId}`
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
const credentials = await step.do(
|
|
150
|
-
"load account credentials",
|
|
151
|
-
async () => {
|
|
152
|
-
const credentials2 = await getCredentialsByAccountId(db, { accountId });
|
|
153
|
-
if (!credentials2) {
|
|
154
|
-
throw new cloudflare_workflows.NonRetryableError(
|
|
155
|
-
`No credentials found for account: ${accountId}`
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
return credentials2;
|
|
159
|
-
}
|
|
160
|
-
);
|
|
161
|
-
const payments = await step.do(
|
|
162
|
-
"fetch bank payments",
|
|
163
|
-
{
|
|
164
|
-
retries: { limit: 5, delay: 2e3, backoff: "constant" },
|
|
165
|
-
timeout: "30 seconds"
|
|
166
|
-
},
|
|
167
|
-
async () => {
|
|
168
|
-
const connector = initiateConnector({
|
|
169
|
-
env: this.env,
|
|
170
|
-
bank: account.connectorKey,
|
|
171
|
-
connectedAccounts: [
|
|
172
|
-
{
|
|
173
|
-
...account,
|
|
174
|
-
iban: account.iban,
|
|
175
|
-
token: credentials.value
|
|
176
|
-
}
|
|
177
|
-
]
|
|
178
|
-
});
|
|
179
|
-
const fetched = await connector.getAllAccountPayments({
|
|
180
|
-
environment: this.env.ENVIRONMENT,
|
|
181
|
-
db,
|
|
182
|
-
account,
|
|
183
|
-
filter: { dateFrom: account.lastSyncedAt }
|
|
184
|
-
});
|
|
185
|
-
return fetched.map((p) => ({
|
|
186
|
-
...p,
|
|
187
|
-
direction: database_schema.getPaymentDirection(p, account.iban)
|
|
188
|
-
}));
|
|
189
|
-
}
|
|
190
|
-
);
|
|
191
|
-
if (payments.length) {
|
|
192
|
-
const bankRefIds = payments.map((payment) => payment.bankRefId).filter(Boolean);
|
|
193
|
-
const existing = await step.do("load existing payments", async () => {
|
|
194
|
-
return await getPaymentsByBankRefIdsQuery(db, {
|
|
195
|
-
ids: bankRefIds
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
const paymentsToUpdate = payments.filter(
|
|
199
|
-
(p) => existing.some((e) => e.bankRefId === p.bankRefId)
|
|
200
|
-
);
|
|
201
|
-
const paymentsToCreate = payments.filter(
|
|
202
|
-
(p) => !existing.some((e) => e.bankRefId === p.bankRefId)
|
|
203
|
-
);
|
|
204
|
-
await step.do("process payments and update lastSyncAt", async () => {
|
|
205
|
-
const eventsToEmit = [];
|
|
206
|
-
const updateCommands = paymentsToUpdate.map(
|
|
207
|
-
(p) => updatePaymentCommand(db, { payment: p }).command
|
|
208
|
-
);
|
|
209
|
-
eventsToEmit.push(
|
|
210
|
-
...paymentsToUpdate.map((p) => ({
|
|
211
|
-
eventType: "BANK_PAYMENT",
|
|
212
|
-
eventSignal: "paymentUpdated",
|
|
213
|
-
bankPayment: p,
|
|
214
|
-
metadata: {
|
|
215
|
-
correlationId: p.correlationId,
|
|
216
|
-
entityId: p.id,
|
|
217
|
-
idempotencySuffix: p.status,
|
|
218
|
-
timestamp: (/* @__PURE__ */ new Date()).toDateString()
|
|
219
|
-
}
|
|
220
|
-
}))
|
|
221
|
-
);
|
|
222
|
-
const createCommands = paymentsToCreate.map(
|
|
223
|
-
(p) => createPaymentCommand(db, { payment: p }).command
|
|
224
|
-
);
|
|
225
|
-
eventsToEmit.push(
|
|
226
|
-
...paymentsToCreate.map((p) => ({
|
|
227
|
-
eventType: "BANK_PAYMENT",
|
|
228
|
-
eventSignal: "paymentCreated",
|
|
229
|
-
bankPayment: p,
|
|
230
|
-
metadata: {
|
|
231
|
-
correlationId: p.correlationId,
|
|
232
|
-
entityId: p.id,
|
|
233
|
-
timestamp: (/* @__PURE__ */ new Date()).toDateString()
|
|
234
|
-
}
|
|
235
|
-
}))
|
|
236
|
-
);
|
|
237
|
-
const latest = payments.reduce((current, p) => {
|
|
238
|
-
return new Date(p.processedAt).getTime() > new Date(current.processedAt).getTime() ? p : current;
|
|
239
|
-
}, payments[0]);
|
|
240
|
-
const updateLastSyncCommand = updateAccountLastSyncCommand(db, {
|
|
241
|
-
accountId: account.id,
|
|
242
|
-
lastSyncedAt: latest.processedAt
|
|
243
|
-
}).command;
|
|
244
|
-
if (eventsToEmit.length) {
|
|
245
|
-
await db.batch(
|
|
246
|
-
backendSdk.asNonEmpty([
|
|
247
|
-
updateLastSyncCommand,
|
|
248
|
-
...updateCommands,
|
|
249
|
-
...createCommands
|
|
250
|
-
])
|
|
251
|
-
);
|
|
252
|
-
await pushToQueue(
|
|
253
|
-
this.env.QUEUE_BUS_QUEUE,
|
|
254
|
-
eventsToEmit
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
return {
|
|
258
|
-
payments: payments.length,
|
|
259
|
-
updated: paymentsToUpdate.length,
|
|
260
|
-
created: paymentsToCreate.length,
|
|
261
|
-
eventsEmitted: eventsToEmit.length,
|
|
262
|
-
lastSyncedAt: latest?.createdAt
|
|
263
|
-
};
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
await step.sleep(
|
|
267
|
-
"Sleep for next sync",
|
|
268
|
-
`${account.syncIntervalS} seconds`
|
|
269
|
-
);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const workflows = {
|
|
275
|
-
__proto__: null,
|
|
276
|
-
SyncAccountPaymentsWorkflow: SyncAccountPaymentsWorkflow
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
exports.SyncAccountPaymentsWorkflow = SyncAccountPaymentsWorkflow;
|
|
280
|
-
exports.createPaymentCommand = createPaymentCommand;
|
|
281
|
-
exports.getCredentialsByAccountId = getCredentialsByAccountId;
|
|
282
|
-
exports.getPaymentsByBankRefIdsQuery = getPaymentsByBankRefIdsQuery;
|
|
283
|
-
exports.initiateConnector = initiateConnector;
|
|
284
|
-
exports.updateAccountLastSyncCommand = updateAccountLastSyncCommand;
|
|
285
|
-
exports.updatePaymentCommand = updatePaymentCommand;
|
|
286
|
-
exports.workflows = workflows;
|