@develit-services/bank 0.0.41 → 0.0.43

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,16 +1,39 @@
1
1
  import { uuidv4, useResult, createInternalError, bankAccount, base } from '@develit-io/backend-sdk';
2
2
  import { sqliteTable, integer, text, unique, real } from 'drizzle-orm/sqlite-core';
3
+ import { format, parseISO } from 'date-fns';
3
4
  import { and, eq } from 'drizzle-orm';
4
5
  import { COUNTRY_CODES_2, CURRENCY_CODES } from '@develit-io/general-codes';
5
6
  import { z } from 'zod';
6
7
  import { importPKCS8, SignJWT } from 'jose';
7
8
  import { createInsertSchema, createUpdateSchema, createSelectSchema } from 'drizzle-zod';
8
- import { format, parseISO } from 'date-fns';
9
9
  import { relations } from 'drizzle-orm/relations';
10
10
 
11
11
  class IBankConnector {
12
12
  }
13
13
 
14
+ const mapReferencesToPayment = (reference) => {
15
+ const symbols = {
16
+ vs: void 0,
17
+ ss: void 0,
18
+ ks: void 0
19
+ };
20
+ if (!reference) return symbols;
21
+ if (Array.isArray(reference) && reference.length > 0) {
22
+ symbols.vs = reference.find((ref) => ref.includes("VS")) || void 0;
23
+ symbols.ss = reference.find((ref) => ref.includes("SS")) || void 0;
24
+ symbols.ks = reference.find((ref) => ref.includes("KS")) || void 0;
25
+ }
26
+ if (typeof reference === "string") {
27
+ const vsMatch = reference.match(/VS[:\s]*(\d+)/i);
28
+ const ssMatch = reference.match(/SS[:\s]*(\d+)/i);
29
+ const ksMatch = reference.match(/KS[:\s]*(\d+)/i);
30
+ if (vsMatch) symbols.vs = vsMatch[1];
31
+ if (ssMatch) symbols.ss = ssMatch[1];
32
+ if (ksMatch) symbols.ks = ksMatch[1];
33
+ }
34
+ return symbols;
35
+ };
36
+
14
37
  async function signFinbricksJws({
15
38
  privateKeyPem,
16
39
  merchantId,
@@ -111,29 +134,6 @@ const FINBRICKS_ENDPOINTS = {
111
134
  BATCH_STATUS: "/transaction/platform/batchPayment/status"
112
135
  };
113
136
 
114
- const mapReferencesToPayment = (reference) => {
115
- const symbols = {
116
- vs: void 0,
117
- ss: void 0,
118
- ks: void 0
119
- };
120
- if (!reference) return symbols;
121
- if (Array.isArray(reference) && reference.length > 0) {
122
- symbols.vs = reference.find((ref) => ref.includes("VS")) || void 0;
123
- symbols.ss = reference.find((ref) => ref.includes("SS")) || void 0;
124
- symbols.ks = reference.find((ref) => ref.includes("KS")) || void 0;
125
- }
126
- if (typeof reference === "string") {
127
- const vsMatch = reference.match(/VS[:\s]*(\d+)/i);
128
- const ssMatch = reference.match(/SS[:\s]*(\d+)/i);
129
- const ksMatch = reference.match(/KS[:\s]*(\d+)/i);
130
- if (vsMatch) symbols.vs = vsMatch[1];
131
- if (ssMatch) symbols.ss = ssMatch[1];
132
- if (ksMatch) symbols.ks = ksMatch[1];
133
- }
134
- return symbols;
135
- };
136
-
137
137
  const mapFinbricksStatus = (status) => {
138
138
  switch (status) {
139
139
  case "BOOK":
@@ -180,6 +180,7 @@ const mapFinbricksTransactionToPayment = (tx, account) => {
180
180
  const related = tx.entryDetails?.transactionDetails?.relatedParties;
181
181
  const base = {
182
182
  id: uuidv4(),
183
+ correlationId: uuidv4(),
183
184
  connectorKey: account.connectorKey,
184
185
  accountId: account.id,
185
186
  bankRefId: tx.entryReference || tx.fbxReference,
@@ -192,14 +193,30 @@ const mapFinbricksTransactionToPayment = (tx, account) => {
192
193
  ...mapReferencesToPayment(
193
194
  tx.entryDetails.transactionDetails?.remittanceInformation?.structured?.creditorReferenceInformation?.reference || tx.entryDetails.transactionDetails?.references?.endToEndIdentification
194
195
  ),
195
- debtorHolderName: isIncoming ? related?.debtor?.name || "Unknown" : "Unknown",
196
- creditorHolderName: isIncoming ? "Unknown" : related?.creditor?.name || "Unknown",
197
- debtorIban: isIncoming ? related.debtorAccount?.identification?.iban || account.iban : account.iban,
198
- creditorIban: isIncoming ? account.iban : related.creditorAccount?.identification?.iban || account.iban,
196
+ creditor: {
197
+ holderName: isIncoming ? "Unknown" : related?.creditor?.name || "Unknown",
198
+ iban: isIncoming ? account.iban || void 0 : related.creditorAccount?.identification?.iban || account.iban || void 0,
199
+ number: isIncoming ? account.number || void 0 : related.creditorAccount?.identification?.other?.identification ? related.creditorAccount.identification.other.identification.split(
200
+ "/"
201
+ )[0] : account.number || void 0,
202
+ bankCode: isIncoming ? account.bankCode || void 0 : related.creditorAccount?.identification?.other?.identification ? related.creditorAccount.identification.other.identification.split(
203
+ "/"
204
+ )[1] : account.bankCode || void 0
205
+ },
206
+ debtor: {
207
+ holderName: isIncoming ? related?.debtor?.name || "Unknown" : "Unknown",
208
+ iban: isIncoming ? related.debtorAccount?.identification?.iban || account.iban || void 0 : account.iban ?? void 0,
209
+ number: isIncoming ? related.debtorAccount?.identification?.other?.identification ? related.debtorAccount.identification.other.identification.split(
210
+ "/"
211
+ )[0] : account.number || void 0 : account.number || void 0,
212
+ bankCode: isIncoming ? related.debtorAccount?.identification?.other?.identification ? related.debtorAccount.identification.other.identification.split(
213
+ "/"
214
+ )[1] : account.bankCode || void 0 : account.bankCode || void 0
215
+ },
199
216
  direction: "INCOMING"
200
217
  // přepíšeme správným směrem níže
201
218
  };
202
- base.direction = getPaymentDirection(base, account.iban);
219
+ base.direction = isIncoming ? "INCOMING" : "OUTGOING";
203
220
  return base;
204
221
  };
205
222
 
@@ -484,13 +501,13 @@ class FinbricksConnector extends IBankConnector {
484
501
  },
485
502
  amount: payment.amount,
486
503
  debtor: {
487
- debtorAccountIban: payment.debtorIban,
488
- debtorName: payment.debtorHolderName || "Devizov\xE1 burza",
504
+ debtorAccountIban: payment.debtor.iban,
505
+ debtorName: payment.debtor.holderName,
489
506
  paymentProvider: this.PROVIDER
490
507
  },
491
508
  creditor: {
492
- creditorAccountIban: payment.creditorIban,
493
- creditorName: payment.creditorHolderName
509
+ creditorAccountIban: payment.creditor.iban,
510
+ creditorName: payment.creditor.holderName
494
511
  },
495
512
  callbackUrl: "https://www.example.com"
496
513
  }
@@ -524,9 +541,9 @@ class FinbricksConnector extends IBankConnector {
524
541
  merchantId: this.finbricks.MERCHANT_ID,
525
542
  merchantTransactionId: payment.bankRefId,
526
543
  totalPrice: payment.amount,
527
- debtorAccountIban: payment.debtorIban,
528
- creditorAccountIban: payment.creditorIban,
529
- creditorName: payment.creditorHolderName,
544
+ debtorAccountIban: payment.debtor.iban,
545
+ creditorAccountIban: payment.creditor.iban,
546
+ creditorName: payment.creditor.holderName,
530
547
  description: payment.message,
531
548
  variableSymbol: payment.vs,
532
549
  callbackUrl: "https://example.com/callback",
@@ -742,7 +759,7 @@ class ErsteConnector extends IBankConnector {
742
759
  debtorAccount: { identification: { iban: payment.debtorIban } },
743
760
  creditorAccount: { identification: { iban: payment.creditorIban } },
744
761
  creditor: {
745
- name: payment.creditorHolderName
762
+ name: payment.creditor.holderName
746
763
  }
747
764
  };
748
765
  const [data, error] = await useResult(
@@ -884,17 +901,32 @@ class ErsteConnector extends IBankConnector {
884
901
  const symbols = mapReferencesToPayment(reference);
885
902
  const paymentInsert = {
886
903
  id: uuidv4(),
904
+ correlationId: uuidv4(),
887
905
  connectorKey: "ERSTE",
888
906
  accountId: account.id,
889
907
  bankRefId: payment.entryReference,
890
908
  amount: payment.amount.value,
891
909
  currency: payment.amount.currency.code,
892
- debtorHolderName: payment.entryDetails.transactionDetails.relatedParties.debtor?.name,
893
- debtorIban: payment.entryDetails.transactionDetails.relatedParties.debtorAccount?.identification.iban,
894
- debtorAccountNumberWithBankCode: payment.entryDetails.transactionDetails.relatedParties.debtorAccount?.identification.other?.identification,
895
- creditorHolderName: payment.entryDetails.transactionDetails.relatedParties.creditor?.name || "",
896
- creditorIban: payment.entryDetails.transactionDetails.relatedParties.creditorAccount?.identification.iban,
897
- creditorAccountNumberWithBankCode: payment.entryDetails.transactionDetails.relatedParties.creditorAccount?.identification.other?.identification,
910
+ debtor: {
911
+ holderName: payment.entryDetails.transactionDetails.relatedParties.debtor?.name,
912
+ iban: payment.entryDetails.transactionDetails.relatedParties.debtorAccount?.identification.iban,
913
+ number: payment.entryDetails.transactionDetails.relatedParties.debtorAccount?.identification.other?.identification ? payment.entryDetails.transactionDetails.relatedParties.debtorAccount.identification.other.identification.split(
914
+ "/"
915
+ )[0] : void 0,
916
+ bankCode: payment.entryDetails.transactionDetails.relatedParties.debtorAccount?.identification.other?.identification ? payment.entryDetails.transactionDetails.relatedParties.debtorAccount.identification.other.identification.split(
917
+ "/"
918
+ )[1] : void 0
919
+ },
920
+ creditor: {
921
+ holderName: payment.entryDetails.transactionDetails.relatedParties.creditor?.name,
922
+ iban: payment.entryDetails.transactionDetails.relatedParties.creditorAccount?.identification.iban,
923
+ number: payment.entryDetails.transactionDetails.relatedParties.creditorAccount?.identification.other?.identification ? payment.entryDetails.transactionDetails.relatedParties.creditorAccount.identification.other.identification.split(
924
+ "/"
925
+ )[0] : void 0,
926
+ bankCode: payment.entryDetails.transactionDetails.relatedParties.creditorAccount?.identification.other?.identification ? payment.entryDetails.transactionDetails.relatedParties.creditorAccount.identification.other.identification.split(
927
+ "/"
928
+ )[1] : void 0
929
+ },
898
930
  paymentType: "DOMESTIC",
899
931
  direction: "INCOMING",
900
932
  message: payment.entryDetails.transactionDetails.remittanceInformation?.unstructured,
@@ -906,7 +938,7 @@ class ErsteConnector extends IBankConnector {
906
938
  };
907
939
  return {
908
940
  ...paymentInsert,
909
- direction: getPaymentDirection(paymentInsert, account.iban)
941
+ direction: account.iban ? getPaymentDirection(paymentInsert, account.iban) : "INCOMING"
910
942
  };
911
943
  });
912
944
  return payments;
@@ -1010,6 +1042,8 @@ class MockConnector extends IBankConnector {
1010
1042
  }
1011
1043
 
1012
1044
  const PAYMENT_TYPES = ["SEPA", "SWIFT", "IFSC", "DOMESTIC"];
1045
+ const CHARGE_BEARERS = ["SHA", "OUR", "BEN"];
1046
+ const INSTRUCTION_PRIORITIES = ["NORM", "INST"];
1013
1047
  const PAYMENT_STATUSES = [
1014
1048
  "PREPARED",
1015
1049
  "INITIALIZED",
@@ -1079,6 +1113,7 @@ const account = sqliteTable(
1079
1113
  }).$type().notNull(),
1080
1114
  status: text("status", { enum: ACCOUNT_STATUSES }).$type().notNull(),
1081
1115
  bankRefId: text("bank_ref_id").notNull(),
1116
+ batchSizeLimit: integer("batch_size_limit").notNull().default(50),
1082
1117
  sync: integer("sync", { mode: "boolean" }).default(false).notNull(),
1083
1118
  syncPeriod: integer("sync_period"),
1084
1119
  lastSyncedAt: integer("last_synced_at", { mode: "timestamp_ms" })
@@ -1133,6 +1168,7 @@ const batch = sqliteTable("batch", {
1133
1168
 
1134
1169
  const payment = sqliteTable("payment", {
1135
1170
  ...base,
1171
+ correlationId: text("correlation_id").notNull(),
1136
1172
  refId: text("ref_id"),
1137
1173
  bankRefId: text("bank_ref_id"),
1138
1174
  accountId: text("account_id").references(() => account.id).notNull(),
@@ -1154,14 +1190,10 @@ const payment = sqliteTable("payment", {
1154
1190
  processedAt: integer("processed_at", {
1155
1191
  mode: "timestamp_ms"
1156
1192
  }),
1157
- creditorHolderName: text("creditor_holder_name").notNull(),
1158
- creditorAccountNumberWithBankCode: text(
1159
- "creditor_account_number_with_bank_code"
1160
- ),
1193
+ creditor: text("creditor", { mode: "json" }).$type().notNull(),
1161
1194
  creditorIban: text("creditor_iban"),
1162
- debtorIban: text("debtor_iban"),
1163
- debtorHolderName: text("debtor_holder_name"),
1164
- debtorAccountNumberWithBankCode: text("debtor_account_number_with_bank_code")
1195
+ debtor: text("debtor", { mode: "json" }).$type().notNull(),
1196
+ debtorIban: text("debtor_iban")
1165
1197
  });
1166
1198
  const paymentRelations = relations(payment, ({ one }) => ({
1167
1199
  batch: one(batch, { fields: [payment.batchId], references: [batch.id] })
@@ -1179,4 +1211,4 @@ const schema = {
1179
1211
 
1180
1212
  const tables = schema;
1181
1213
 
1182
- export { ACCOUNT_STATUSES as A, BATCH_STATUSES as B, COUNTRY_CODES as C, ErsteConnector as E, FinbricksConnector as F, IBankConnector as I, MockConnector as M, PAYMENT_TYPES as P, TOKEN_TYPES as T, FinbricksClient as a, FINBRICKS_ENDPOINTS as b, PAYMENT_STATUSES as c, PAYMENT_DIRECTIONS as d, CONNECTOR_KEYS as e, CREDENTIALS_TYPES as f, accountInsertSchema as g, accountUpdateSchema as h, accountSelectSchema as i, accountCredentialsInsertSchema as j, accountCredentialsUpdateSchema as k, accountCredentialsSelectSchema as l, ottUpdateSchema as m, ottSelectSchema as n, ottInsertSchema as o, paymentInsertTypeZod as p, getPaymentDirection as q, batch as r, signFinbricksJws as s, tables as t, useFinbricksFetch as u, payment as v, paymentRelations as w, ott as x, account as y, accountCredentials as z };
1214
+ export { ACCOUNT_STATUSES as A, BATCH_STATUSES as B, CHARGE_BEARERS as C, account as D, ErsteConnector as E, FinbricksConnector as F, accountCredentials as G, IBankConnector as I, MockConnector as M, PAYMENT_TYPES as P, TOKEN_TYPES as T, FinbricksClient as a, FINBRICKS_ENDPOINTS as b, INSTRUCTION_PRIORITIES as c, PAYMENT_STATUSES as d, PAYMENT_DIRECTIONS as e, COUNTRY_CODES as f, CONNECTOR_KEYS as g, CREDENTIALS_TYPES as h, accountInsertSchema as i, accountUpdateSchema as j, accountSelectSchema as k, accountCredentialsInsertSchema as l, accountCredentialsUpdateSchema as m, accountCredentialsSelectSchema as n, ottInsertSchema as o, paymentInsertTypeZod as p, ottUpdateSchema as q, ottSelectSchema as r, signFinbricksJws as s, tables as t, useFinbricksFetch as u, getPaymentDirection as v, batch as w, payment as x, paymentRelations as y, ott as z };