@develit-services/bank 2.2.3 → 2.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.
@@ -1,363 +0,0 @@
1
- 'use strict';
2
-
3
- const backendSdk = require('@develit-io/backend-sdk');
4
- const sqliteCore = require('drizzle-orm/sqlite-core');
5
- require('date-fns');
6
- require('jose');
7
- const generalCodes = require('@develit-io/general-codes');
8
- const relations = require('drizzle-orm/relations');
9
- const drizzleOrm = require('drizzle-orm');
10
- require('node:crypto');
11
- const drizzleZod = require('drizzle-zod');
12
-
13
- const PAYMENT_TYPES = ["SEPA", "SWIFT", "DOMESTIC", "UNKNOWN"];
14
- const CHARGE_BEARERS = ["SHA", "OUR", "BEN"];
15
- const INSTRUCTION_PRIORITIES = ["NORM", "HIGH", "INST"];
16
- const PAYMENT_REQUEST_STATUSES = [
17
- "OPENED",
18
- "AUTHORIZED",
19
- "COMPLETED",
20
- "BOOKED",
21
- "SETTLED",
22
- "REJECTED",
23
- "CLOSED"
24
- ];
25
- const PAYMENT_STATUSES = [
26
- "PENDING",
27
- "PROCESSING",
28
- "BOOKED",
29
- "CANCELLED",
30
- "REJECTED",
31
- "SCHEDULED",
32
- "HOLD",
33
- "INFO"
34
- ];
35
- const PAYMENT_DIRECTIONS = ["INCOMING", "OUTGOING"];
36
- const BATCH_STATUSES = [
37
- "PROCESSING",
38
- "READY_TO_SIGN",
39
- "AUTHORIZED",
40
- "COMPLETED",
41
- "FAILED"
42
- ];
43
- const BATCH_MODES = ["NATIVE", "SINGLE"];
44
- const ACCOUNT_STATUSES = ["AUTHORIZED", "DISABLED", "EXPIRED"];
45
- const COUNTRY_CODES = generalCodes.COUNTRY_CODES_2;
46
-
47
- const TERMINAL_STATUSES$1 = /* @__PURE__ */ new Set([
48
- "SETTLED",
49
- "REJECTED",
50
- "CLOSED"
51
- ]);
52
- const PENDING_STATUSES = /* @__PURE__ */ new Set([
53
- "OPENED",
54
- "AUTHORIZED"
55
- ]);
56
- function isTerminalStatus(status) {
57
- return TERMINAL_STATUSES$1.has(status);
58
- }
59
- function isPendingStatus(status) {
60
- return PENDING_STATUSES.has(status);
61
- }
62
- function isProcessedStatus(status) {
63
- return !isPendingStatus(status);
64
- }
65
- function hasPaymentAccountAssigned(payment) {
66
- return "accountId" in payment && "connectorKey" in payment && payment.accountId !== void 0 && payment.connectorKey !== void 0;
67
- }
68
- function isPaymentCompleted(payment) {
69
- return (payment.status === "COMPLETED" || payment.status === "BOOKED" || payment.status === "SETTLED" || payment.status === "REJECTED" || payment.status === "CLOSED") && "bankRefId" in payment && typeof payment.bankRefId === "string";
70
- }
71
-
72
- const TERMINAL_STATUSES = PAYMENT_REQUEST_STATUSES.filter(isTerminalStatus);
73
- const getNonTerminalPaymentRequestsQuery = (db) => db.select().from(tables.paymentRequest).where(
74
- drizzleOrm.and(
75
- drizzleOrm.not(drizzleOrm.inArray(tables.paymentRequest.status, TERMINAL_STATUSES)),
76
- drizzleOrm.isNull(tables.paymentRequest.deletedAt)
77
- )
78
- );
79
-
80
- const CONNECTOR_KEYS = [
81
- "ERSTE",
82
- "FINBRICKS",
83
- "MOCK",
84
- "CREDITAS",
85
- "MOCK_COBS",
86
- "FIO",
87
- "MONETA",
88
- "DBU",
89
- "CSAS",
90
- "AIRBANK",
91
- "KB",
92
- "CSOB"
93
- ];
94
- const CREDENTIALS_TYPES = [
95
- "AUTH_TOKEN",
96
- "REFRESH_TOKEN",
97
- "CLIENT_ID",
98
- "API_KEY"
99
- ];
100
- const TOKEN_TYPES = ["ACCOUNT_AUTHORIZATION"];
101
-
102
- const account = sqliteCore.sqliteTable(
103
- "account",
104
- {
105
- ...backendSdk.base,
106
- ...backendSdk.bankAccount,
107
- // countryCode is temporary until bankAccount is update to include US country code
108
- countryCode: sqliteCore.text("country_code", { enum: COUNTRY_CODES }).$type().notNull(),
109
- number: sqliteCore.text("number").notNull(),
110
- name: sqliteCore.text("name"),
111
- iban: sqliteCore.text("iban").notNull(),
112
- bankCode: sqliteCore.text("bank_code", { enum: generalCodes.BANK_CODES }).notNull(),
113
- connectorKey: sqliteCore.text("connector_key", {
114
- enum: CONNECTOR_KEYS
115
- }).$type().notNull(),
116
- status: sqliteCore.text("status", { enum: ACCOUNT_STATUSES }).$type().notNull(),
117
- bankRefId: sqliteCore.text("bank_ref_id").notNull(),
118
- batchSizeLimit: sqliteCore.integer("batch_size_limit").notNull().default(50),
119
- syncIntervalS: sqliteCore.integer("sync_interval_s").notNull().default(600),
120
- lastSyncAt: sqliteCore.integer("last_sync_at", { mode: "timestamp_ms" }),
121
- lastSyncMetadata: sqliteCore.text("last_sync_metadata", {
122
- mode: "json"
123
- }).$type()
124
- },
125
- (t) => [sqliteCore.unique().on(t.iban)]
126
- );
127
-
128
- const accountInsertSchema = drizzleZod.createInsertSchema(account, {
129
- address: () => backendSdk.structuredAddressSchema.optional()
130
- });
131
- const accountUpdateSchema = drizzleZod.createUpdateSchema(account, {
132
- address: () => backendSdk.structuredAddressSchema.optional()
133
- });
134
- const accountSelectSchema = drizzleZod.createSelectSchema(account, {
135
- address: () => backendSdk.structuredAddressSchema.nullable()
136
- });
137
-
138
- const accountCredentials = sqliteCore.sqliteTable("account_credentials", {
139
- ...backendSdk.base,
140
- accountId: sqliteCore.text("account_id").references(() => account.id, { onDelete: "restrict", onUpdate: "cascade" }).notNull(),
141
- connectorKey: sqliteCore.text("connector_key", {
142
- enum: CONNECTOR_KEYS
143
- }).$type().notNull(),
144
- type: sqliteCore.text("type", {
145
- enum: CREDENTIALS_TYPES
146
- }).$type().notNull(),
147
- value: sqliteCore.text("value").notNull(),
148
- expiresAt: sqliteCore.integer("expires_at", { mode: "timestamp_ms" }).notNull()
149
- });
150
-
151
- const accountCredentialsInsertSchema = drizzleZod.createInsertSchema(accountCredentials);
152
- const accountCredentialsUpdateSchema = drizzleZod.createUpdateSchema(accountCredentials);
153
- const accountCredentialsSelectSchema = drizzleZod.createSelectSchema(accountCredentials);
154
-
155
- const ott = sqliteCore.sqliteTable("ott", {
156
- ...backendSdk.base,
157
- oneTimeToken: sqliteCore.text("one_time_token").notNull(),
158
- refId: sqliteCore.text("ref_id").notNull(),
159
- tokenType: sqliteCore.text("token_type", { enum: TOKEN_TYPES }).$type().notNull(),
160
- expiresAt: sqliteCore.integer("expires_at", { mode: "timestamp_ms" }).notNull()
161
- });
162
-
163
- const ottInsertSchema = drizzleZod.createInsertSchema(ott);
164
- const ottUpdateSchema = drizzleZod.createUpdateSchema(ott);
165
- const ottSelectSchema = drizzleZod.createSelectSchema(ott);
166
-
167
- const batch = sqliteCore.sqliteTable("batch", {
168
- ...backendSdk.base,
169
- batchPaymentInitiatedAt: sqliteCore.integer("batch_payment_initiated_at", {
170
- mode: "timestamp_ms"
171
- }),
172
- authorizationUrls: sqliteCore.text("authorization_urls", { mode: "json" }).$type(),
173
- accountId: sqliteCore.text("account_id").references(() => account.id, {
174
- onDelete: "restrict",
175
- onUpdate: "cascade"
176
- }),
177
- status: sqliteCore.text("status", { enum: BATCH_STATUSES }).$type(),
178
- statusReason: sqliteCore.text("status_reason"),
179
- statusResponse: sqliteCore.text("status_response", { mode: "json" }).$type(),
180
- metadata: sqliteCore.text("metadata", { mode: "json" }).$type(),
181
- paymentType: sqliteCore.text("payment_type", {
182
- enum: PAYMENT_TYPES
183
- }).$type(),
184
- paymentsChecksum: sqliteCore.text("payments_checksum"),
185
- batchMode: sqliteCore.text("batch_mode", { enum: BATCH_MODES }).$type()
186
- });
187
-
188
- const payment = sqliteCore.sqliteTable(
189
- "payment",
190
- {
191
- ...backendSdk.base,
192
- correlationId: sqliteCore.text("correlation_id").notNull(),
193
- refId: sqliteCore.text("ref_id"),
194
- bankRefId: sqliteCore.text("bank_ref_id").notNull(),
195
- accountId: sqliteCore.text("account_id").references(() => account.id, {
196
- onDelete: "restrict",
197
- onUpdate: "cascade"
198
- }).notNull(),
199
- connectorKey: sqliteCore.text("connector_key", { enum: CONNECTOR_KEYS }).$type().notNull(),
200
- amount: sqliteCore.real("amount").notNull(),
201
- direction: sqliteCore.text("direction").$type().notNull(),
202
- paymentType: sqliteCore.text("payment_type").$type().notNull(),
203
- currency: sqliteCore.text("currency").$type().notNull(),
204
- status: sqliteCore.text("status", { enum: PAYMENT_STATUSES }).$type().notNull(),
205
- statusReason: sqliteCore.text("status_reason"),
206
- batchId: sqliteCore.text("bank_execution_batch_id"),
207
- initiatedAt: sqliteCore.integer("initiated_at", {
208
- mode: "timestamp_ms"
209
- }),
210
- vs: sqliteCore.text("vs"),
211
- ss: sqliteCore.text("ss"),
212
- ks: sqliteCore.text("ks"),
213
- message: sqliteCore.text("message"),
214
- chargeBearer: sqliteCore.text("charge_bearer", {
215
- enum: CHARGE_BEARERS
216
- }).$type(),
217
- instructionPriority: sqliteCore.text("instruction_priority", {
218
- enum: INSTRUCTION_PRIORITIES
219
- }).$type(),
220
- processedAt: sqliteCore.integer("processed_at", {
221
- mode: "timestamp_ms"
222
- }),
223
- creditor: sqliteCore.text("creditor", { mode: "json" }).$type().notNull(),
224
- creditorIban: sqliteCore.text("creditor_iban"),
225
- debtor: sqliteCore.text("debtor", { mode: "json" }).$type().notNull(),
226
- debtorIban: sqliteCore.text("debtor_iban")
227
- },
228
- (t) => [
229
- sqliteCore.unique().on(t.connectorKey, t.accountId, t.bankRefId),
230
- sqliteCore.index("payment_account_id_idx").on(t.accountId),
231
- sqliteCore.index("payment_account_id_status_idx").on(t.accountId, t.status),
232
- sqliteCore.index("payment_account_id_created_at_idx").on(t.accountId, t.createdAt),
233
- sqliteCore.index("payment_created_at_idx").on(t.createdAt),
234
- sqliteCore.index("payment_direction_idx").on(t.direction),
235
- sqliteCore.index("payment_batch_id_idx").on(t.batchId),
236
- sqliteCore.index("payment_creditor_iban_idx").on(t.creditorIban),
237
- sqliteCore.index("payment_debtor_iban_idx").on(t.debtorIban)
238
- ]
239
- );
240
- const paymentRelations = relations.relations(payment, ({ one }) => ({
241
- batch: one(batch, { fields: [payment.batchId], references: [batch.id] })
242
- }));
243
-
244
- const paymentRequest = sqliteCore.sqliteTable(
245
- "payment_request",
246
- {
247
- ...backendSdk.base,
248
- batchId: sqliteCore.text("batch_id").references(() => batch.id, {
249
- onDelete: "restrict",
250
- onUpdate: "cascade"
251
- }),
252
- accountId: sqliteCore.text("account_id").references(() => account.id, {
253
- onDelete: "restrict",
254
- onUpdate: "cascade"
255
- }).notNull(),
256
- correlationId: sqliteCore.text("correlation_id").notNull(),
257
- refId: sqliteCore.text("ref_id"),
258
- bankRefId: sqliteCore.text("bank_ref_id"),
259
- connectorKey: sqliteCore.text("connector_key", { enum: CONNECTOR_KEYS }).$type().notNull(),
260
- amount: sqliteCore.real("amount").notNull(),
261
- paymentType: sqliteCore.text("payment_type", { enum: PAYMENT_TYPES }).$type().notNull(),
262
- currency: sqliteCore.text("currency").$type().notNull(),
263
- status: sqliteCore.text("status", { enum: PAYMENT_REQUEST_STATUSES }).$type().notNull(),
264
- statusReason: sqliteCore.text("status_reason"),
265
- authorizationUrl: sqliteCore.text("authorization_url"),
266
- initiatedAt: sqliteCore.integer("initiated_at", { mode: "timestamp_ms" }),
267
- processedAt: sqliteCore.integer("processed_at", { mode: "timestamp_ms" }),
268
- vs: sqliteCore.text("vs"),
269
- ss: sqliteCore.text("ss"),
270
- ks: sqliteCore.text("ks"),
271
- message: sqliteCore.text("message"),
272
- chargeBearer: sqliteCore.text("charge_bearer", {
273
- enum: CHARGE_BEARERS
274
- }).$type(),
275
- instructionPriority: sqliteCore.text("instruction_priority", {
276
- enum: INSTRUCTION_PRIORITIES
277
- }).$type(),
278
- creditor: sqliteCore.text("creditor", { mode: "json" }).$type().notNull(),
279
- creditorIban: sqliteCore.text("creditor_iban"),
280
- debtor: sqliteCore.text("debtor", { mode: "json" }).$type().notNull(),
281
- debtorIban: sqliteCore.text("debtor_iban"),
282
- sendAsSinglePayment: sqliteCore.integer("send_as_single_payment", {
283
- mode: "boolean"
284
- })
285
- },
286
- (t) => [
287
- sqliteCore.index("payment_request_batch_id_idx").on(t.batchId),
288
- sqliteCore.index("payment_request_account_id_idx").on(t.accountId),
289
- sqliteCore.index("payment_request_creditor_iban_idx").on(t.creditorIban),
290
- sqliteCore.index("payment_request_debtor_iban_idx").on(t.debtorIban),
291
- sqliteCore.index("payment_request_status_idx").on(t.status),
292
- sqliteCore.index("payment_request_created_at_idx").on(t.createdAt),
293
- sqliteCore.index("payment_request_account_status_idx").on(t.accountId, t.status),
294
- sqliteCore.index("payment_request_status_created_at_idx").on(t.status, t.createdAt),
295
- sqliteCore.index("payment_request_account_status_created_at_idx").on(
296
- t.accountId,
297
- t.status,
298
- t.createdAt
299
- )
300
- ]
301
- );
302
- const paymentRequestRelations = relations.relations(paymentRequest, ({ one }) => ({
303
- batch: one(batch, {
304
- fields: [paymentRequest.batchId],
305
- references: [batch.id]
306
- }),
307
- account: one(account, {
308
- fields: [paymentRequest.accountId],
309
- references: [account.id]
310
- })
311
- }));
312
-
313
- const schema = {
314
- __proto__: null,
315
- account: account,
316
- accountCredentials: accountCredentials,
317
- batch: batch,
318
- ott: ott,
319
- payment: payment,
320
- paymentRelations: paymentRelations,
321
- paymentRequest: paymentRequest,
322
- paymentRequestRelations: paymentRequestRelations
323
- };
324
-
325
- const tables = schema;
326
-
327
- exports.ACCOUNT_STATUSES = ACCOUNT_STATUSES;
328
- exports.BATCH_MODES = BATCH_MODES;
329
- exports.BATCH_STATUSES = BATCH_STATUSES;
330
- exports.CHARGE_BEARERS = CHARGE_BEARERS;
331
- exports.CONNECTOR_KEYS = CONNECTOR_KEYS;
332
- exports.COUNTRY_CODES = COUNTRY_CODES;
333
- exports.CREDENTIALS_TYPES = CREDENTIALS_TYPES;
334
- exports.INSTRUCTION_PRIORITIES = INSTRUCTION_PRIORITIES;
335
- exports.PAYMENT_DIRECTIONS = PAYMENT_DIRECTIONS;
336
- exports.PAYMENT_REQUEST_STATUSES = PAYMENT_REQUEST_STATUSES;
337
- exports.PAYMENT_STATUSES = PAYMENT_STATUSES;
338
- exports.PAYMENT_TYPES = PAYMENT_TYPES;
339
- exports.TOKEN_TYPES = TOKEN_TYPES;
340
- exports.account = account;
341
- exports.accountCredentials = accountCredentials;
342
- exports.accountCredentialsInsertSchema = accountCredentialsInsertSchema;
343
- exports.accountCredentialsSelectSchema = accountCredentialsSelectSchema;
344
- exports.accountCredentialsUpdateSchema = accountCredentialsUpdateSchema;
345
- exports.accountInsertSchema = accountInsertSchema;
346
- exports.accountSelectSchema = accountSelectSchema;
347
- exports.accountUpdateSchema = accountUpdateSchema;
348
- exports.batch = batch;
349
- exports.getNonTerminalPaymentRequestsQuery = getNonTerminalPaymentRequestsQuery;
350
- exports.hasPaymentAccountAssigned = hasPaymentAccountAssigned;
351
- exports.isPaymentCompleted = isPaymentCompleted;
352
- exports.isPendingStatus = isPendingStatus;
353
- exports.isProcessedStatus = isProcessedStatus;
354
- exports.isTerminalStatus = isTerminalStatus;
355
- exports.ott = ott;
356
- exports.ottInsertSchema = ottInsertSchema;
357
- exports.ottSelectSchema = ottSelectSchema;
358
- exports.ottUpdateSchema = ottUpdateSchema;
359
- exports.payment = payment;
360
- exports.paymentRelations = paymentRelations;
361
- exports.paymentRequest = paymentRequest;
362
- exports.paymentRequestRelations = paymentRequestRelations;
363
- exports.tables = tables;