@develit-services/bank 5.4.2 → 5.5.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.
- package/dist/base.cjs +158 -139
- package/dist/base.d.cts +4 -4
- package/dist/base.d.mts +4 -4
- package/dist/base.d.ts +4 -4
- package/dist/base.mjs +23 -4
- 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/export/workflows.cjs +32 -32
- package/dist/export/workflows.mjs +5 -5
- package/dist/shared/bank.B04qoHtD.cjs +360 -0
- package/dist/shared/{bank.o3KC69CF.d.mts → bank.BuwSQL_7.d.mts} +1 -1
- package/dist/shared/{bank.BrBu52ls.d.ts → bank.CdkOsZE8.d.cts} +1 -1
- package/dist/shared/{bank.BrBu52ls.d.cts → bank.CdkOsZE8.d.mts} +1 -1
- package/dist/shared/{bank.BrBu52ls.d.mts → bank.CdkOsZE8.d.ts} +1 -1
- package/dist/shared/{bank.BqLdCHnj.mjs → bank.D0iIfh-X.mjs} +52 -245
- package/dist/shared/{bank.3A2oRRgQ.cjs → bank.DMrkshJa.cjs} +52 -245
- package/dist/shared/{bank.Dzk9ABBs.d.cts → bank.D__RVhfE.d.cts} +1 -1
- package/dist/shared/{bank.Cfz0frnc.d.ts → bank.IAc8kIC-.d.ts} +1 -1
- package/dist/shared/bank._VynZSqn.mjs +348 -0
- package/dist/types.cjs +37 -37
- package/dist/types.d.cts +4 -4
- package/dist/types.d.mts +4 -4
- package/dist/types.d.ts +4 -4
- package/dist/types.mjs +2 -2
- package/package.json +1 -1
- package/dist/shared/bank.C_IudwLH.cjs +0 -160
- package/dist/shared/bank.CtxJ_SOF.mjs +0 -149
package/dist/base.cjs
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const backendSdk = require('@develit-io/backend-sdk');
|
|
4
|
-
const
|
|
4
|
+
const paymentDirection = require('./shared/bank.DMrkshJa.cjs');
|
|
5
5
|
const drizzleOrm = require('drizzle-orm');
|
|
6
6
|
const cloudflare_workers = require('cloudflare:workers');
|
|
7
7
|
const d1 = require('drizzle-orm/d1');
|
|
8
8
|
require('jose');
|
|
9
|
+
const bank = require('./shared/bank.B04qoHtD.cjs');
|
|
9
10
|
const zod = require('zod');
|
|
10
11
|
const database_schema = require('./shared/bank.9Yw4KHyl.cjs');
|
|
11
12
|
const generalCodes = require('@develit-io/general-codes');
|
|
12
13
|
require('date-fns');
|
|
13
14
|
require('node:crypto');
|
|
14
|
-
const credentialsResolver = require('./shared/bank.C_IudwLH.cjs');
|
|
15
15
|
require('drizzle-orm/zod');
|
|
16
16
|
require('drizzle-orm/sqlite-core');
|
|
17
17
|
|
|
@@ -73,11 +73,30 @@ async function heartbeatSyncWorkflows({
|
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
const FINITE_STATUSES = /* @__PURE__ */ new Set([
|
|
77
|
+
"complete",
|
|
78
|
+
"errored",
|
|
79
|
+
"terminated"
|
|
80
|
+
]);
|
|
81
|
+
async function terminateSyncWorkflow(instance) {
|
|
82
|
+
const { status } = await instance.status();
|
|
83
|
+
if (FINITE_STATUSES.has(status)) return;
|
|
84
|
+
try {
|
|
85
|
+
await instance.terminate();
|
|
86
|
+
} catch (err) {
|
|
87
|
+
if (!isAlreadyFiniteError(err)) throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function isAlreadyFiniteError(err) {
|
|
91
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
92
|
+
return /cannot_terminate|finite state/i.test(message);
|
|
93
|
+
}
|
|
94
|
+
|
|
76
95
|
const upsertAccountCommand = (db, { account }) => {
|
|
77
96
|
const id = account.id || backendSdk.uuidv4();
|
|
78
97
|
const { id: _id, ...accountWithoutId } = account;
|
|
79
|
-
const command = db.insert(
|
|
80
|
-
target:
|
|
98
|
+
const command = db.insert(paymentDirection.tables.account).values({ ...account, id }).onConflictDoUpdate({
|
|
99
|
+
target: paymentDirection.tables.account.iban,
|
|
81
100
|
set: accountWithoutId
|
|
82
101
|
// preserve existing id on re-auth
|
|
83
102
|
}).returning();
|
|
@@ -94,7 +113,7 @@ const createOneTimeTokenCommand = (db, {
|
|
|
94
113
|
}) => {
|
|
95
114
|
const id = backendSdk.uuidv4();
|
|
96
115
|
const expiresAt = new Date(Date.now() + 1e3 * 60 * 60) ;
|
|
97
|
-
const command = db.insert(
|
|
116
|
+
const command = db.insert(paymentDirection.tables.ott).values({
|
|
98
117
|
id,
|
|
99
118
|
oneTimeToken: ott,
|
|
100
119
|
tokenType,
|
|
@@ -106,7 +125,7 @@ const createOneTimeTokenCommand = (db, {
|
|
|
106
125
|
|
|
107
126
|
const updateAccountCommand = (db, { account }) => {
|
|
108
127
|
return {
|
|
109
|
-
command: db.update(
|
|
128
|
+
command: db.update(paymentDirection.tables.account).set({ ...account }).where(drizzleOrm.eq(paymentDirection.tables.account.id, account.id)).returning()
|
|
110
129
|
};
|
|
111
130
|
};
|
|
112
131
|
|
|
@@ -114,16 +133,16 @@ const expireOneTimeTokenCommand = (db, {
|
|
|
114
133
|
ott
|
|
115
134
|
}) => {
|
|
116
135
|
return {
|
|
117
|
-
command: db.update(
|
|
136
|
+
command: db.update(paymentDirection.tables.ott).set({
|
|
118
137
|
expiresAt: /* @__PURE__ */ new Date()
|
|
119
|
-
}).where(drizzleOrm.eq(
|
|
138
|
+
}).where(drizzleOrm.eq(paymentDirection.tables.ott.oneTimeToken, ott))
|
|
120
139
|
};
|
|
121
140
|
};
|
|
122
141
|
|
|
123
142
|
const createAccountCredentialsCommand = async (db, encryptionKey, { credentials }) => {
|
|
124
143
|
const id = credentials.id || backendSdk.uuidv4();
|
|
125
|
-
const encryptedValue = await
|
|
126
|
-
const command = db.insert(
|
|
144
|
+
const encryptedValue = await bank.encrypt(credentials.value, encryptionKey);
|
|
145
|
+
const command = db.insert(paymentDirection.tables.accountCredentials).values({
|
|
127
146
|
...credentials,
|
|
128
147
|
id,
|
|
129
148
|
value: encryptedValue
|
|
@@ -135,16 +154,16 @@ const createAccountCredentialsCommand = async (db, encryptionKey, { credentials
|
|
|
135
154
|
};
|
|
136
155
|
|
|
137
156
|
const deleteAccountCredentialsCommand = (db, { accountId }) => {
|
|
138
|
-
const command = db.delete(
|
|
157
|
+
const command = db.delete(paymentDirection.tables.accountCredentials).where(drizzleOrm.eq(paymentDirection.tables.accountCredentials.accountId, accountId));
|
|
139
158
|
return {
|
|
140
159
|
command
|
|
141
160
|
};
|
|
142
161
|
};
|
|
143
162
|
|
|
144
163
|
const deletePaymentsByAccountCommand = (db, { accountId }) => {
|
|
145
|
-
const command = db.update(
|
|
164
|
+
const command = db.update(paymentDirection.tables.payment).set({
|
|
146
165
|
deletedAt: /* @__PURE__ */ new Date()
|
|
147
|
-
}).where(drizzleOrm.eq(
|
|
166
|
+
}).where(drizzleOrm.eq(paymentDirection.tables.payment.accountId, accountId));
|
|
148
167
|
return {
|
|
149
168
|
command
|
|
150
169
|
};
|
|
@@ -152,7 +171,7 @@ const deletePaymentsByAccountCommand = (db, { accountId }) => {
|
|
|
152
171
|
|
|
153
172
|
const createPaymentRequestCommand = (db, { paymentRequest }) => {
|
|
154
173
|
return {
|
|
155
|
-
command: db.insert(
|
|
174
|
+
command: db.insert(paymentDirection.tables.paymentRequest).values({
|
|
156
175
|
...paymentRequest,
|
|
157
176
|
creditorIban: paymentRequest.creditor.iban,
|
|
158
177
|
debtorIban: paymentRequest.debtor.iban
|
|
@@ -163,16 +182,16 @@ const createPaymentRequestCommand = (db, { paymentRequest }) => {
|
|
|
163
182
|
const getAccountBatchCountsQuery = async (db, { accountId }) => {
|
|
164
183
|
const result = await db.select({
|
|
165
184
|
totalCount: drizzleOrm.sql`COUNT(*)`.as("totalCount"),
|
|
166
|
-
processingCount: drizzleOrm.sql`SUM(CASE WHEN ${
|
|
185
|
+
processingCount: drizzleOrm.sql`SUM(CASE WHEN ${paymentDirection.tables.batch.status} = 'PROCESSING' THEN 1 ELSE 0 END)`.as(
|
|
167
186
|
"processingCount"
|
|
168
187
|
),
|
|
169
|
-
readyToSignCount: drizzleOrm.sql`SUM(CASE WHEN ${
|
|
188
|
+
readyToSignCount: drizzleOrm.sql`SUM(CASE WHEN ${paymentDirection.tables.batch.status} = 'READY_TO_SIGN' THEN 1 ELSE 0 END)`.as(
|
|
170
189
|
"readyToSignCount"
|
|
171
190
|
),
|
|
172
|
-
failedCount: drizzleOrm.sql`SUM(CASE WHEN ${
|
|
191
|
+
failedCount: drizzleOrm.sql`SUM(CASE WHEN ${paymentDirection.tables.batch.status} = 'FAILED' THEN 1 ELSE 0 END)`.as(
|
|
173
192
|
"failedCount"
|
|
174
193
|
)
|
|
175
|
-
}).from(
|
|
194
|
+
}).from(paymentDirection.tables.batch).where(drizzleOrm.eq(paymentDirection.tables.batch.accountId, accountId)).then(backendSdk.first);
|
|
176
195
|
return result || {
|
|
177
196
|
totalCount: 0,
|
|
178
197
|
processingCount: 0,
|
|
@@ -182,18 +201,18 @@ const getAccountBatchCountsQuery = async (db, { accountId }) => {
|
|
|
182
201
|
};
|
|
183
202
|
|
|
184
203
|
const getAccountByIbanQuery = async (db, { iban }) => {
|
|
185
|
-
return await db.select().from(
|
|
204
|
+
return await db.select().from(paymentDirection.tables.account).where(drizzleOrm.eq(paymentDirection.tables.account.iban, iban)).get();
|
|
186
205
|
};
|
|
187
206
|
|
|
188
207
|
const getAllAccountsQuery = async (db, filters) => {
|
|
189
208
|
const whereConditions = drizzleOrm.and(
|
|
190
|
-
backendSdk.buildMultiFilterConditions(
|
|
209
|
+
backendSdk.buildMultiFilterConditions(paymentDirection.tables.account.iban, filters?.filterIbans),
|
|
191
210
|
backendSdk.buildMultiFilterConditions(
|
|
192
|
-
|
|
211
|
+
paymentDirection.tables.account.currency,
|
|
193
212
|
filters?.filterCurrencies
|
|
194
213
|
),
|
|
195
214
|
backendSdk.buildMultiFilterConditions(
|
|
196
|
-
|
|
215
|
+
paymentDirection.tables.account.bankRefId,
|
|
197
216
|
filters?.filterBankRefIds
|
|
198
217
|
)
|
|
199
218
|
);
|
|
@@ -201,11 +220,11 @@ const getAllAccountsQuery = async (db, filters) => {
|
|
|
201
220
|
const limit = filters?.limit ?? 20;
|
|
202
221
|
const offset = (page - 1) * limit;
|
|
203
222
|
const [accountsWithExpiration, countResult] = await Promise.all([
|
|
204
|
-
db.select().from(
|
|
205
|
-
|
|
206
|
-
drizzleOrm.eq(
|
|
223
|
+
db.select().from(paymentDirection.tables.account).leftJoin(
|
|
224
|
+
paymentDirection.tables.accountCredentials,
|
|
225
|
+
drizzleOrm.eq(paymentDirection.tables.accountCredentials.accountId, paymentDirection.tables.account.id)
|
|
207
226
|
).where(whereConditions).limit(limit).offset(offset),
|
|
208
|
-
db.select({ count: drizzleOrm.sql`count(*)` }).from(
|
|
227
|
+
db.select({ count: drizzleOrm.sql`count(*)` }).from(paymentDirection.tables.account).where(whereConditions)
|
|
209
228
|
]);
|
|
210
229
|
return {
|
|
211
230
|
accounts: accountsWithExpiration.map(
|
|
@@ -267,42 +286,42 @@ const getPaymentsWithPaginationQuery = async (db, {
|
|
|
267
286
|
}) => {
|
|
268
287
|
const whereConditions = drizzleOrm.and(
|
|
269
288
|
buildMultiFilterConditions(
|
|
270
|
-
|
|
289
|
+
paymentDirection.tables.payment.accountId,
|
|
271
290
|
filterPaymentAccountId
|
|
272
291
|
),
|
|
273
|
-
buildMultiFilterConditions(
|
|
274
|
-
buildMultiFilterConditions(
|
|
292
|
+
buildMultiFilterConditions(paymentDirection.tables.payment.amount, filterPaymentAmount),
|
|
293
|
+
buildMultiFilterConditions(paymentDirection.tables.payment.currency, filterPaymentCurrency),
|
|
275
294
|
buildRangeFilterConditions(
|
|
276
|
-
|
|
295
|
+
paymentDirection.tables.payment.createdAt,
|
|
277
296
|
filterPaymentDateFrom,
|
|
278
297
|
filterPaymentDateTo
|
|
279
298
|
),
|
|
280
|
-
buildMultiFilterConditions(
|
|
299
|
+
buildMultiFilterConditions(paymentDirection.tables.payment.status, filterPaymentStatus),
|
|
281
300
|
buildMultiFilterConditions(
|
|
282
|
-
|
|
301
|
+
paymentDirection.tables.payment.direction,
|
|
283
302
|
filterPaymentDirection
|
|
284
303
|
),
|
|
285
|
-
buildMultiFilterConditions(
|
|
286
|
-
buildMultiFilterConditions(
|
|
304
|
+
buildMultiFilterConditions(paymentDirection.tables.payment.id, ids),
|
|
305
|
+
buildMultiFilterConditions(paymentDirection.tables.payment.paymentType, filterPaymentType),
|
|
287
306
|
buildRangeFilterConditions(
|
|
288
|
-
|
|
307
|
+
paymentDirection.tables.payment.amount,
|
|
289
308
|
filterPaymentMinAmount,
|
|
290
309
|
filterPaymentMaxAmount
|
|
291
310
|
),
|
|
292
|
-
filterPaymentVariableSymbol !== void 0 ? drizzleOrm.eq(
|
|
293
|
-
filterPaymentSpecificSymbol !== void 0 ? drizzleOrm.eq(
|
|
294
|
-
filterPaymentConstantSymbol !== void 0 ? drizzleOrm.eq(
|
|
295
|
-
filterPaymentMessage !== void 0 ? drizzleOrm.like(
|
|
296
|
-
filterPaymentCreditorIban !== void 0 ? drizzleOrm.eq(
|
|
297
|
-
filterPaymentDebtorIban !== void 0 ? drizzleOrm.eq(
|
|
298
|
-
filterPaymentBatchId !== void 0 ? drizzleOrm.eq(
|
|
299
|
-
filterPaymentBankRefId !== void 0 ? drizzleOrm.eq(
|
|
311
|
+
filterPaymentVariableSymbol !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.vs, filterPaymentVariableSymbol) : void 0,
|
|
312
|
+
filterPaymentSpecificSymbol !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.ss, filterPaymentSpecificSymbol) : void 0,
|
|
313
|
+
filterPaymentConstantSymbol !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.ks, filterPaymentConstantSymbol) : void 0,
|
|
314
|
+
filterPaymentMessage !== void 0 ? drizzleOrm.like(paymentDirection.tables.payment.message, `%${filterPaymentMessage}%`) : void 0,
|
|
315
|
+
filterPaymentCreditorIban !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.creditorIban, filterPaymentCreditorIban) : void 0,
|
|
316
|
+
filterPaymentDebtorIban !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.debtorIban, filterPaymentDebtorIban) : void 0,
|
|
317
|
+
filterPaymentBatchId !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.batchId, filterPaymentBatchId) : void 0,
|
|
318
|
+
filterPaymentBankRefId !== void 0 ? drizzleOrm.eq(paymentDirection.tables.payment.bankRefId, filterPaymentBankRefId) : void 0
|
|
300
319
|
);
|
|
301
|
-
const sortColumn = resolveColumn(
|
|
320
|
+
const sortColumn = resolveColumn(paymentDirection.tables.payment, sort.column);
|
|
302
321
|
const [{ totalCount }] = await db.select({
|
|
303
322
|
totalCount: drizzleOrm.sql`count(*)`
|
|
304
|
-
}).from(
|
|
305
|
-
const payments = await db.select().from(
|
|
323
|
+
}).from(paymentDirection.tables.payment).where(whereConditions);
|
|
324
|
+
const payments = await db.select().from(paymentDirection.tables.payment).where(whereConditions).limit(limit).offset((page - 1) * limit).orderBy(sort.direction === "asc" ? drizzleOrm.asc(sortColumn) : drizzleOrm.desc(sortColumn));
|
|
306
325
|
return {
|
|
307
326
|
payments,
|
|
308
327
|
totalCount
|
|
@@ -317,20 +336,20 @@ const getBatchesQuery = async (db, {
|
|
|
317
336
|
filterBatchStatus
|
|
318
337
|
}) => {
|
|
319
338
|
const whereConditions = drizzleOrm.and(
|
|
320
|
-
drizzleOrm.isNull(
|
|
321
|
-
buildMultiFilterConditions(
|
|
322
|
-
buildMultiFilterConditions(
|
|
339
|
+
drizzleOrm.isNull(paymentDirection.tables.batch.deletedAt),
|
|
340
|
+
buildMultiFilterConditions(paymentDirection.tables.batch.accountId, filterBatchAccountId),
|
|
341
|
+
buildMultiFilterConditions(paymentDirection.tables.batch.status, filterBatchStatus)
|
|
323
342
|
);
|
|
324
|
-
const sortColumn = resolveColumn(
|
|
343
|
+
const sortColumn = resolveColumn(paymentDirection.tables.batch, sort.column);
|
|
325
344
|
const [{ totalCount }] = await db.select({
|
|
326
345
|
totalCount: drizzleOrm.sql`count(*)`
|
|
327
|
-
}).from(
|
|
328
|
-
const batches = await db.select().from(
|
|
346
|
+
}).from(paymentDirection.tables.batch).where(whereConditions);
|
|
347
|
+
const batches = await db.select().from(paymentDirection.tables.batch).where(whereConditions).limit(limit).offset((page - 1) * limit).orderBy(sort.direction === "asc" ? drizzleOrm.asc(sortColumn) : drizzleOrm.desc(sortColumn));
|
|
329
348
|
const batchIds = batches.map((b) => b.id);
|
|
330
|
-
const paymentRequests = batchIds.length > 0 ? await db.select().from(
|
|
349
|
+
const paymentRequests = batchIds.length > 0 ? await db.select().from(paymentDirection.tables.paymentRequest).where(
|
|
331
350
|
drizzleOrm.and(
|
|
332
|
-
drizzleOrm.inArray(
|
|
333
|
-
drizzleOrm.isNull(
|
|
351
|
+
drizzleOrm.inArray(paymentDirection.tables.paymentRequest.batchId, batchIds),
|
|
352
|
+
drizzleOrm.isNull(paymentDirection.tables.paymentRequest.deletedAt)
|
|
334
353
|
)
|
|
335
354
|
) : [];
|
|
336
355
|
const paymentRequestsByBatchId = Map.groupBy(
|
|
@@ -348,7 +367,7 @@ const getBatchesQuery = async (db, {
|
|
|
348
367
|
};
|
|
349
368
|
|
|
350
369
|
const getOttQuery = async (db, { ott }) => {
|
|
351
|
-
return await db.select().from(
|
|
370
|
+
return await db.select().from(paymentDirection.tables.ott).where(drizzleOrm.eq(paymentDirection.tables.ott.oneTimeToken, ott)).get();
|
|
352
371
|
};
|
|
353
372
|
|
|
354
373
|
const CHUNK_SIZE = 90;
|
|
@@ -358,15 +377,15 @@ const getPaymentsByPaymentRequestIdsQuery = async (db, { paymentRequestIds }) =>
|
|
|
358
377
|
for (let i = 0; i < paymentRequestIds.length; i += CHUNK_SIZE) {
|
|
359
378
|
const chunkIds = paymentRequestIds.slice(i, i + CHUNK_SIZE);
|
|
360
379
|
const rows = await db.select({
|
|
361
|
-
id:
|
|
362
|
-
correlationId:
|
|
363
|
-
status:
|
|
364
|
-
amount:
|
|
365
|
-
currency:
|
|
366
|
-
creditor:
|
|
367
|
-
createdAt:
|
|
368
|
-
updatedAt:
|
|
369
|
-
}).from(
|
|
380
|
+
id: paymentDirection.tables.paymentRequest.id,
|
|
381
|
+
correlationId: paymentDirection.tables.paymentRequest.correlationId,
|
|
382
|
+
status: paymentDirection.tables.paymentRequest.status,
|
|
383
|
+
amount: paymentDirection.tables.paymentRequest.amount,
|
|
384
|
+
currency: paymentDirection.tables.paymentRequest.currency,
|
|
385
|
+
creditor: paymentDirection.tables.paymentRequest.creditor,
|
|
386
|
+
createdAt: paymentDirection.tables.paymentRequest.createdAt,
|
|
387
|
+
updatedAt: paymentDirection.tables.paymentRequest.updatedAt
|
|
388
|
+
}).from(paymentDirection.tables.paymentRequest).where(drizzleOrm.inArray(paymentDirection.tables.paymentRequest.id, chunkIds));
|
|
370
389
|
paymentRequests.push(...rows);
|
|
371
390
|
}
|
|
372
391
|
return paymentRequests.map((pr) => {
|
|
@@ -388,10 +407,10 @@ const getPaymentsByPaymentRequestIdsQuery = async (db, { paymentRequestIds }) =>
|
|
|
388
407
|
};
|
|
389
408
|
|
|
390
409
|
const getPaymentRequestByIdQuery = async (db, { paymentRequestId }) => {
|
|
391
|
-
const result = await db.select().from(
|
|
410
|
+
const result = await db.select().from(paymentDirection.tables.paymentRequest).where(
|
|
392
411
|
drizzleOrm.and(
|
|
393
|
-
drizzleOrm.eq(
|
|
394
|
-
drizzleOrm.isNull(
|
|
412
|
+
drizzleOrm.eq(paymentDirection.tables.paymentRequest.id, paymentRequestId),
|
|
413
|
+
drizzleOrm.isNull(paymentDirection.tables.paymentRequest.deletedAt)
|
|
395
414
|
)
|
|
396
415
|
).limit(1);
|
|
397
416
|
return backendSdk.first(result);
|
|
@@ -401,30 +420,30 @@ const getPaymentRequestsQuery = async (db, params) => {
|
|
|
401
420
|
const limit = params.limit;
|
|
402
421
|
const offset = (params.page - 1) * params.limit;
|
|
403
422
|
const orderDir = params.sort.direction === "asc" ? drizzleOrm.asc : drizzleOrm.desc;
|
|
404
|
-
const orderCol = params.sort.column === "amount" ?
|
|
423
|
+
const orderCol = params.sort.column === "amount" ? paymentDirection.tables.paymentRequest.amount : params.sort.column === "updatedAt" ? paymentDirection.tables.paymentRequest.updatedAt : paymentDirection.tables.paymentRequest.createdAt;
|
|
405
424
|
const where = drizzleOrm.and(
|
|
406
|
-
drizzleOrm.isNull(
|
|
407
|
-
params.filterAccountId ? Array.isArray(params.filterAccountId) ? drizzleOrm.inArray(
|
|
408
|
-
params.filterStatus ? Array.isArray(params.filterStatus) ? drizzleOrm.inArray(
|
|
409
|
-
params.filterPaymentType ? Array.isArray(params.filterPaymentType) ? drizzleOrm.inArray(
|
|
410
|
-
params.filterCurrency ? Array.isArray(params.filterCurrency) ? drizzleOrm.inArray(
|
|
411
|
-
params.filterBatchId ? drizzleOrm.eq(
|
|
412
|
-
params.filterDateFrom ? drizzleOrm.gte(
|
|
413
|
-
params.filterDateTo ? drizzleOrm.lte(
|
|
414
|
-
params.filterMinAmount !== void 0 ? drizzleOrm.gte(
|
|
415
|
-
params.filterMaxAmount !== void 0 ? drizzleOrm.lte(
|
|
416
|
-
params.filterVariableSymbol ? drizzleOrm.eq(
|
|
417
|
-
params.filterSpecificSymbol ? drizzleOrm.eq(
|
|
418
|
-
params.filterConstantSymbol ? drizzleOrm.eq(
|
|
419
|
-
params.filterMessage ? drizzleOrm.like(
|
|
420
|
-
params.filterBankRefId ? drizzleOrm.eq(
|
|
421
|
-
params.filterCreditorIban ? drizzleOrm.eq(
|
|
422
|
-
params.filterDebtorIban ? drizzleOrm.eq(
|
|
423
|
-
params.ids?.length ? drizzleOrm.inArray(
|
|
425
|
+
drizzleOrm.isNull(paymentDirection.tables.paymentRequest.deletedAt),
|
|
426
|
+
params.filterAccountId ? Array.isArray(params.filterAccountId) ? drizzleOrm.inArray(paymentDirection.tables.paymentRequest.accountId, params.filterAccountId) : drizzleOrm.eq(paymentDirection.tables.paymentRequest.accountId, params.filterAccountId) : void 0,
|
|
427
|
+
params.filterStatus ? Array.isArray(params.filterStatus) ? drizzleOrm.inArray(paymentDirection.tables.paymentRequest.status, params.filterStatus) : drizzleOrm.eq(paymentDirection.tables.paymentRequest.status, params.filterStatus) : void 0,
|
|
428
|
+
params.filterPaymentType ? Array.isArray(params.filterPaymentType) ? drizzleOrm.inArray(paymentDirection.tables.paymentRequest.paymentType, params.filterPaymentType) : drizzleOrm.eq(paymentDirection.tables.paymentRequest.paymentType, params.filterPaymentType) : void 0,
|
|
429
|
+
params.filterCurrency ? Array.isArray(params.filterCurrency) ? drizzleOrm.inArray(paymentDirection.tables.paymentRequest.currency, params.filterCurrency) : drizzleOrm.eq(paymentDirection.tables.paymentRequest.currency, params.filterCurrency) : void 0,
|
|
430
|
+
params.filterBatchId ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.batchId, params.filterBatchId) : void 0,
|
|
431
|
+
params.filterDateFrom ? drizzleOrm.gte(paymentDirection.tables.paymentRequest.createdAt, params.filterDateFrom) : void 0,
|
|
432
|
+
params.filterDateTo ? drizzleOrm.lte(paymentDirection.tables.paymentRequest.createdAt, params.filterDateTo) : void 0,
|
|
433
|
+
params.filterMinAmount !== void 0 ? drizzleOrm.gte(paymentDirection.tables.paymentRequest.amount, params.filterMinAmount) : void 0,
|
|
434
|
+
params.filterMaxAmount !== void 0 ? drizzleOrm.lte(paymentDirection.tables.paymentRequest.amount, params.filterMaxAmount) : void 0,
|
|
435
|
+
params.filterVariableSymbol ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.vs, params.filterVariableSymbol) : void 0,
|
|
436
|
+
params.filterSpecificSymbol ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.ss, params.filterSpecificSymbol) : void 0,
|
|
437
|
+
params.filterConstantSymbol ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.ks, params.filterConstantSymbol) : void 0,
|
|
438
|
+
params.filterMessage ? drizzleOrm.like(paymentDirection.tables.paymentRequest.message, `%${params.filterMessage}%`) : void 0,
|
|
439
|
+
params.filterBankRefId ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.bankRefId, params.filterBankRefId) : void 0,
|
|
440
|
+
params.filterCreditorIban ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.creditorIban, params.filterCreditorIban) : void 0,
|
|
441
|
+
params.filterDebtorIban ? drizzleOrm.eq(paymentDirection.tables.paymentRequest.debtorIban, params.filterDebtorIban) : void 0,
|
|
442
|
+
params.ids?.length ? drizzleOrm.inArray(paymentDirection.tables.paymentRequest.id, params.ids) : void 0
|
|
424
443
|
);
|
|
425
444
|
const [paymentRequests, [{ value: totalCount }]] = await Promise.all([
|
|
426
|
-
db.select().from(
|
|
427
|
-
db.select({ value: drizzleOrm.count() }).from(
|
|
445
|
+
db.select().from(paymentDirection.tables.paymentRequest).where(where).orderBy(orderDir(orderCol)).limit(limit).offset(offset),
|
|
446
|
+
db.select({ value: drizzleOrm.count() }).from(paymentDirection.tables.paymentRequest).where(where)
|
|
428
447
|
]);
|
|
429
448
|
return { paymentRequests, totalCount };
|
|
430
449
|
};
|
|
@@ -659,7 +678,7 @@ zod.z.object({
|
|
|
659
678
|
});
|
|
660
679
|
|
|
661
680
|
const updateAccountInputSchema = zod.z.object({
|
|
662
|
-
account:
|
|
681
|
+
account: paymentDirection.accountInsertSchema
|
|
663
682
|
});
|
|
664
683
|
|
|
665
684
|
const getBankAccountsInputSchema = zod.z.object({
|
|
@@ -735,7 +754,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
735
754
|
// 14 days
|
|
736
755
|
this.COMPLETED_POLLING_WINDOW_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
737
756
|
this.allowedProviders = config.allowedProviders;
|
|
738
|
-
this.db = d1.drizzle(this.env.BANK_D1, { schema:
|
|
757
|
+
this.db = d1.drizzle(this.env.BANK_D1, { schema: paymentDirection.tables, relations: paymentDirection.relations });
|
|
739
758
|
}
|
|
740
759
|
async _getAccounts(filters) {
|
|
741
760
|
return await getAllAccountsQuery(this.db, filters);
|
|
@@ -769,7 +788,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
769
788
|
bankRefId: acc.bankRefId,
|
|
770
789
|
config: acc.connectorConfig
|
|
771
790
|
}));
|
|
772
|
-
resolveCredentials = await
|
|
791
|
+
resolveCredentials = await bank.createCredentialsResolver(this.db, this.env);
|
|
773
792
|
}
|
|
774
793
|
return bank.initiateConnector({
|
|
775
794
|
bank: connectorKey,
|
|
@@ -871,7 +890,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
871
890
|
{ successMessage: "Account sync workflow terminated" },
|
|
872
891
|
async ({ accountId }) => {
|
|
873
892
|
const instance = await this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.get(accountId);
|
|
874
|
-
await instance
|
|
893
|
+
await terminateSyncWorkflow(instance);
|
|
875
894
|
return {
|
|
876
895
|
instanceId: instance.id
|
|
877
896
|
};
|
|
@@ -943,10 +962,10 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
943
962
|
oldStatus: pr.status,
|
|
944
963
|
newStatus
|
|
945
964
|
});
|
|
946
|
-
await
|
|
965
|
+
await bank.updatePaymentRequestStatusCommand(this.db, {
|
|
947
966
|
id: pr.id,
|
|
948
967
|
status: newStatus,
|
|
949
|
-
processedAt:
|
|
968
|
+
processedAt: paymentDirection.isProcessedStatus(newStatus) ? /* @__PURE__ */ new Date() : void 0
|
|
950
969
|
}).command.execute();
|
|
951
970
|
statusChanged++;
|
|
952
971
|
eventsToEmit.push(
|
|
@@ -1024,26 +1043,26 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1024
1043
|
*/
|
|
1025
1044
|
async _deriveBatchStatus(batchId) {
|
|
1026
1045
|
const [allPRs, batch] = await Promise.all([
|
|
1027
|
-
|
|
1028
|
-
|
|
1046
|
+
bank.getPaymentRequestsByBatchIdQuery(this.db, { batchId }),
|
|
1047
|
+
bank.getBatchByIdQuery(this.db, { batchId })
|
|
1029
1048
|
]);
|
|
1030
1049
|
if (!batch || batch.status === "COMPLETED" || batch.status === "FAILED")
|
|
1031
1050
|
return;
|
|
1032
1051
|
if (allPRs.length === 0) return;
|
|
1033
1052
|
const connectorKey = allPRs[0]?.connectorKey;
|
|
1034
1053
|
const allTerminal = allPRs.every(
|
|
1035
|
-
(pr) =>
|
|
1054
|
+
(pr) => paymentDirection.isTerminalStatus(pr.status, connectorKey)
|
|
1036
1055
|
);
|
|
1037
1056
|
const allAuthorizedOrHigher = allPRs.every((pr) => pr.status !== "OPENED");
|
|
1038
1057
|
if (allTerminal) {
|
|
1039
1058
|
const hasFailed = allPRs.some(
|
|
1040
1059
|
(pr) => pr.status === "REJECTED" || pr.status === "CLOSED"
|
|
1041
1060
|
);
|
|
1042
|
-
await
|
|
1061
|
+
await bank.upsertBatchCommand(this.db, {
|
|
1043
1062
|
batch: { ...batch, status: hasFailed ? "FAILED" : "COMPLETED" }
|
|
1044
1063
|
}).command.execute();
|
|
1045
1064
|
} else if (allAuthorizedOrHigher && batch.status !== "AUTHORIZED") {
|
|
1046
|
-
await
|
|
1065
|
+
await bank.upsertBatchCommand(this.db, {
|
|
1047
1066
|
batch: { ...batch, status: "AUTHORIZED" }
|
|
1048
1067
|
}).command.execute();
|
|
1049
1068
|
}
|
|
@@ -1057,7 +1076,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1057
1076
|
console.log("[updatePaymentRequestStatuses] Starting", {
|
|
1058
1077
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
1059
1078
|
});
|
|
1060
|
-
const nonTerminalPRs = await
|
|
1079
|
+
const nonTerminalPRs = await paymentDirection.getNonTerminalPaymentRequestsQuery(this.db);
|
|
1061
1080
|
if (nonTerminalPRs.length === 0) {
|
|
1062
1081
|
console.log(
|
|
1063
1082
|
"[updatePaymentRequestStatuses] No non-terminal PRs found",
|
|
@@ -1080,7 +1099,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1080
1099
|
const pollableIds = [];
|
|
1081
1100
|
for (const pr of nonTerminalPRs) {
|
|
1082
1101
|
const status = pr.status;
|
|
1083
|
-
if (
|
|
1102
|
+
if (paymentDirection.isTerminalStatus(status, pr.connectorKey)) {
|
|
1084
1103
|
continue;
|
|
1085
1104
|
}
|
|
1086
1105
|
if (status === "OPENED" || status === "AUTHORIZED") {
|
|
@@ -1096,7 +1115,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1096
1115
|
age: `${Math.floor((now - pr.createdAt.getTime()) / (24 * 60 * 60 * 1e3))} days`
|
|
1097
1116
|
}
|
|
1098
1117
|
);
|
|
1099
|
-
await
|
|
1118
|
+
await bank.updatePaymentRequestStatusCommand(this.db, {
|
|
1100
1119
|
id: pr.id,
|
|
1101
1120
|
status: "CLOSED",
|
|
1102
1121
|
statusReason: "Polling timeout: no final status received after 14 days",
|
|
@@ -1135,9 +1154,9 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1135
1154
|
}
|
|
1136
1155
|
async heartbeatSyncWorkflows() {
|
|
1137
1156
|
const accounts = await this.db.select({
|
|
1138
|
-
id:
|
|
1139
|
-
lastSyncMetadata:
|
|
1140
|
-
}).from(
|
|
1157
|
+
id: paymentDirection.tables.account.id,
|
|
1158
|
+
lastSyncMetadata: paymentDirection.tables.account.lastSyncMetadata
|
|
1159
|
+
}).from(paymentDirection.tables.account).where(drizzleOrm.eq(paymentDirection.tables.account.status, "AUTHORIZED")).all();
|
|
1141
1160
|
const resetAfterIterations = Number(
|
|
1142
1161
|
this.env.SYNC_WORKFLOW_RESET_AFTER_ITERATIONS
|
|
1143
1162
|
);
|
|
@@ -1182,7 +1201,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1182
1201
|
});
|
|
1183
1202
|
if (pr) connectorKey = pr.connectorKey;
|
|
1184
1203
|
} else if (batchId) {
|
|
1185
|
-
const batchPrs = await
|
|
1204
|
+
const batchPrs = await bank.getPaymentRequestsByBatchIdQuery(this.db, {
|
|
1186
1205
|
batchId
|
|
1187
1206
|
});
|
|
1188
1207
|
if (batchPrs.length > 0) connectorKey = batchPrs[0].connectorKey;
|
|
@@ -1211,7 +1230,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1211
1230
|
prIds.push(result.paymentRequestId);
|
|
1212
1231
|
}
|
|
1213
1232
|
if (result.type === "batch") {
|
|
1214
|
-
const batchPayments = await
|
|
1233
|
+
const batchPayments = await bank.getPaymentRequestsByBatchIdQuery(
|
|
1215
1234
|
this.db,
|
|
1216
1235
|
{ batchId: result.batchId }
|
|
1217
1236
|
);
|
|
@@ -1362,7 +1381,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1362
1381
|
accountId: acc.id
|
|
1363
1382
|
}).command
|
|
1364
1383
|
);
|
|
1365
|
-
const encryptionKey = await
|
|
1384
|
+
const encryptionKey = await bank.importAesKey(
|
|
1366
1385
|
(await this.env.SECRETS_STORE.get({
|
|
1367
1386
|
secretName: "BANK_SERVICE_ENCRYPTION_KEY"
|
|
1368
1387
|
})).data?.secretValue || ""
|
|
@@ -1473,7 +1492,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1473
1492
|
debtor,
|
|
1474
1493
|
debtorIban: debtor.iban
|
|
1475
1494
|
};
|
|
1476
|
-
const { command } =
|
|
1495
|
+
const { command } = bank.createPaymentCommand(this.db, { payment });
|
|
1477
1496
|
const createdPayment = await command.execute().then(backendSdk.first);
|
|
1478
1497
|
this.logQueuePush({ payment, isPaymentExecuted: true });
|
|
1479
1498
|
await this.pushToQueue(this.env.QUEUE_BUS_QUEUE, {
|
|
@@ -1495,7 +1514,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1495
1514
|
{ data: input, schema: sendPaymentInputSchema },
|
|
1496
1515
|
{ successMessage: "Payment initiated successfully" },
|
|
1497
1516
|
async (data) => {
|
|
1498
|
-
const incomingPayment =
|
|
1517
|
+
const incomingPayment = paymentDirection.toIncomingPayment(data);
|
|
1499
1518
|
this._validatePaymentTypeAndCurrency(
|
|
1500
1519
|
data.paymentType,
|
|
1501
1520
|
data.currency,
|
|
@@ -1503,7 +1522,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1503
1522
|
);
|
|
1504
1523
|
if (data.creditor.bankCode?.length === 4 && !data.creditor.iban && data.creditor.number) {
|
|
1505
1524
|
try {
|
|
1506
|
-
data.creditor.iban =
|
|
1525
|
+
data.creditor.iban = paymentDirection.calculateCzechIban(
|
|
1507
1526
|
data.creditor.number,
|
|
1508
1527
|
data.creditor.bankCode
|
|
1509
1528
|
);
|
|
@@ -1545,11 +1564,11 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1545
1564
|
status: 422
|
|
1546
1565
|
});
|
|
1547
1566
|
}
|
|
1548
|
-
const accountAssigned =
|
|
1549
|
-
const batchedPayment =
|
|
1567
|
+
const accountAssigned = paymentDirection.assignAccount(incomingPayment, account);
|
|
1568
|
+
const batchedPayment = paymentDirection.toBatchedPayment(accountAssigned);
|
|
1550
1569
|
const { command: insertPaymentRequest } = createPaymentRequestCommand(
|
|
1551
1570
|
this.db,
|
|
1552
|
-
{ paymentRequest:
|
|
1571
|
+
{ paymentRequest: paymentDirection.toPaymentRequestInsert(accountAssigned, null) }
|
|
1553
1572
|
);
|
|
1554
1573
|
await insertPaymentRequest.execute();
|
|
1555
1574
|
const initiate = () => {
|
|
@@ -1572,19 +1591,19 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1572
1591
|
try {
|
|
1573
1592
|
initiated = await initiate();
|
|
1574
1593
|
} catch (err) {
|
|
1575
|
-
await
|
|
1594
|
+
await bank.updatePaymentRequestStatusCommand(this.db, {
|
|
1576
1595
|
id: incomingPayment.id,
|
|
1577
1596
|
deletedAt: /* @__PURE__ */ new Date()
|
|
1578
1597
|
}).command.execute();
|
|
1579
1598
|
throw err;
|
|
1580
1599
|
}
|
|
1581
|
-
await
|
|
1600
|
+
await bank.updatePaymentRequestStatusCommand(this.db, {
|
|
1582
1601
|
id: incomingPayment.id,
|
|
1583
1602
|
status: initiated.payment.status,
|
|
1584
1603
|
bankRefId: initiated.payment.bankRefId,
|
|
1585
1604
|
initiatedAt: initiated.payment.initiatedAt,
|
|
1586
1605
|
authorizationUrl: initiated.authorizationUrl,
|
|
1587
|
-
processedAt:
|
|
1606
|
+
processedAt: paymentDirection.isProcessedStatus(initiated.payment.status) ? /* @__PURE__ */ new Date() : void 0
|
|
1588
1607
|
}).command.execute();
|
|
1589
1608
|
const paymentRequest = await getPaymentRequestByIdQuery(this.db, {
|
|
1590
1609
|
paymentRequestId: incomingPayment.id
|
|
@@ -1690,13 +1709,13 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1690
1709
|
status: 422
|
|
1691
1710
|
});
|
|
1692
1711
|
}
|
|
1693
|
-
const incomingPayments = paymentInputs.map(
|
|
1712
|
+
const incomingPayments = paymentInputs.map(paymentDirection.toIncomingPayment);
|
|
1694
1713
|
const batchedPayments = incomingPayments.map(
|
|
1695
|
-
(p) =>
|
|
1714
|
+
(p) => paymentDirection.toBatchedPayment(paymentDirection.assignAccount(p, account))
|
|
1696
1715
|
);
|
|
1697
1716
|
const batchId = backendSdk.uuidv4();
|
|
1698
1717
|
const batchMode = connector.supportsBatch(paymentType) ? "NATIVE" : "SINGLE";
|
|
1699
|
-
const batchCmd =
|
|
1718
|
+
const batchCmd = bank.upsertBatchCommand(this.db, {
|
|
1700
1719
|
batch: {
|
|
1701
1720
|
id: batchId,
|
|
1702
1721
|
authorizationUrls: [],
|
|
@@ -1709,8 +1728,8 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1709
1728
|
}).command;
|
|
1710
1729
|
const prCmds = incomingPayments.map(
|
|
1711
1730
|
(p) => createPaymentRequestCommand(this.db, {
|
|
1712
|
-
paymentRequest:
|
|
1713
|
-
|
|
1731
|
+
paymentRequest: paymentDirection.toPaymentRequestInsert(
|
|
1732
|
+
paymentDirection.assignAccount(p, account),
|
|
1714
1733
|
batchId
|
|
1715
1734
|
)
|
|
1716
1735
|
}).command
|
|
@@ -1733,12 +1752,12 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1733
1752
|
result = await initiateBatch();
|
|
1734
1753
|
} catch (err) {
|
|
1735
1754
|
const deletePrCmds = incomingPayments.map(
|
|
1736
|
-
(p) =>
|
|
1755
|
+
(p) => bank.updatePaymentRequestStatusCommand(this.db, {
|
|
1737
1756
|
id: p.id,
|
|
1738
1757
|
deletedAt: /* @__PURE__ */ new Date()
|
|
1739
1758
|
}).command
|
|
1740
1759
|
);
|
|
1741
|
-
const deleteBatchCmd =
|
|
1760
|
+
const deleteBatchCmd = bank.upsertBatchCommand(this.db, {
|
|
1742
1761
|
batch: {
|
|
1743
1762
|
id: batchId,
|
|
1744
1763
|
accountId: account.id,
|
|
@@ -1753,16 +1772,16 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1753
1772
|
const { authorizationUrls, payments: preparedPayments } = result;
|
|
1754
1773
|
const isPerPaymentFallback = authorizationUrls.length === preparedPayments.length;
|
|
1755
1774
|
const updatePrCmds = preparedPayments.map(
|
|
1756
|
-
(pp, i) =>
|
|
1775
|
+
(pp, i) => bank.updatePaymentRequestStatusCommand(this.db, {
|
|
1757
1776
|
id: pp.id,
|
|
1758
1777
|
status: pp.status,
|
|
1759
1778
|
bankRefId: pp.bankRefId,
|
|
1760
1779
|
initiatedAt: pp.initiatedAt,
|
|
1761
1780
|
authorizationUrl: isPerPaymentFallback ? authorizationUrls[i] : authorizationUrls[0],
|
|
1762
|
-
processedAt:
|
|
1781
|
+
processedAt: paymentDirection.isProcessedStatus(pp.status) ? /* @__PURE__ */ new Date() : void 0
|
|
1763
1782
|
}).command
|
|
1764
1783
|
);
|
|
1765
|
-
const updateBatchCmd =
|
|
1784
|
+
const updateBatchCmd = bank.upsertBatchCommand(this.db, {
|
|
1766
1785
|
batch: {
|
|
1767
1786
|
id: batchId,
|
|
1768
1787
|
accountId: account.id,
|
|
@@ -1775,8 +1794,8 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1775
1794
|
}).command;
|
|
1776
1795
|
await this.db.batch([updateBatchCmd, ...updatePrCmds]);
|
|
1777
1796
|
const [updatedBatch, paymentRequests] = await Promise.all([
|
|
1778
|
-
|
|
1779
|
-
|
|
1797
|
+
bank.getBatchByIdQuery(this.db, { batchId }),
|
|
1798
|
+
bank.getPaymentRequestsByBatchIdQuery(this.db, { batchId })
|
|
1780
1799
|
]);
|
|
1781
1800
|
if (!updatedBatch) {
|
|
1782
1801
|
throw backendSdk.createInternalError(null, {
|
|
@@ -1848,11 +1867,11 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1848
1867
|
if (includePendingPaymentRequestCount) {
|
|
1849
1868
|
const [row] = await this.db.select({
|
|
1850
1869
|
count: drizzleOrm.sql`count(*)`
|
|
1851
|
-
}).from(
|
|
1870
|
+
}).from(paymentDirection.tables.paymentRequest).where(
|
|
1852
1871
|
drizzleOrm.and(
|
|
1853
|
-
drizzleOrm.eq(
|
|
1854
|
-
drizzleOrm.eq(
|
|
1855
|
-
drizzleOrm.isNull(
|
|
1872
|
+
drizzleOrm.eq(paymentDirection.tables.paymentRequest.accountId, a.id),
|
|
1873
|
+
drizzleOrm.eq(paymentDirection.tables.paymentRequest.status, "OPENED"),
|
|
1874
|
+
drizzleOrm.isNull(paymentDirection.tables.paymentRequest.deletedAt)
|
|
1856
1875
|
)
|
|
1857
1876
|
);
|
|
1858
1877
|
result.pendingPaymentRequestCount = row?.count ?? 0;
|
|
@@ -1872,7 +1891,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1872
1891
|
{ data: input, schema: getAccountBalanceInputSchema },
|
|
1873
1892
|
{ successMessage: "Account balance retrieved successfully" },
|
|
1874
1893
|
async (data) => {
|
|
1875
|
-
const account = await
|
|
1894
|
+
const account = await bank.getAccountByIdQuery(this.db, {
|
|
1876
1895
|
accountId: data.accountId
|
|
1877
1896
|
});
|
|
1878
1897
|
if (!account) {
|
|
@@ -1924,7 +1943,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1924
1943
|
{ data: input, schema: disconnectAccountInputSchema },
|
|
1925
1944
|
{ successMessage: "Account disconnected successfully" },
|
|
1926
1945
|
async ({ accountId }) => {
|
|
1927
|
-
const account = await
|
|
1946
|
+
const account = await bank.getAccountByIdQuery(this.db, { accountId });
|
|
1928
1947
|
if (!account) {
|
|
1929
1948
|
throw backendSdk.createInternalError(null, {
|
|
1930
1949
|
message: "Account not found",
|
|
@@ -1934,7 +1953,7 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
1934
1953
|
}
|
|
1935
1954
|
try {
|
|
1936
1955
|
const instance = await this.env.SYNC_ACCOUNT_PAYMENTS_WORKFLOW.get(accountId);
|
|
1937
|
-
await instance
|
|
1956
|
+
await terminateSyncWorkflow(instance);
|
|
1938
1957
|
} catch (error) {
|
|
1939
1958
|
this.log({
|
|
1940
1959
|
message: "No workflow instance found for account, skipping termination.",
|
|
@@ -2043,14 +2062,14 @@ exports.BankServiceBase = class BankServiceBase extends backendSdk.develitWorker
|
|
|
2043
2062
|
const privateKeyPem = (await this.env.SECRETS_STORE.get({
|
|
2044
2063
|
secretName: "BANK_SERVICE_FINBRICKS_PRIVATE_KEY_PEM"
|
|
2045
2064
|
})).data?.secretValue || "";
|
|
2046
|
-
const client = new
|
|
2065
|
+
const client = new paymentDirection.FinbricksClient(
|
|
2047
2066
|
this.env.FINBRICKS_BASE_URI,
|
|
2048
2067
|
this.env.FINBRICKS_MERCHANT_ID,
|
|
2049
2068
|
privateKeyPem,
|
|
2050
2069
|
this.env.REDIRECT_URI
|
|
2051
2070
|
);
|
|
2052
2071
|
return client.request({
|
|
2053
|
-
endpoint:
|
|
2072
|
+
endpoint: paymentDirection.FINBRICKS_ENDPOINTS.BANK_INFO,
|
|
2054
2073
|
method: "GET",
|
|
2055
2074
|
query: {
|
|
2056
2075
|
merchantId: this.env.FINBRICKS_MERCHANT_ID
|