@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.
@@ -1,160 +0,0 @@
1
- 'use strict';
2
-
3
- const drizzleOrm = require('drizzle-orm');
4
- const bank = require('./bank.3A2oRRgQ.cjs');
5
- const backendSdk = require('@develit-io/backend-sdk');
6
- require('./bank.9Yw4KHyl.cjs');
7
- require('date-fns');
8
- require('jose');
9
- require('@develit-io/general-codes');
10
- const node_crypto = require('node:crypto');
11
-
12
- const createPaymentCommand = (db, { payment }) => {
13
- return {
14
- command: db.insert(bank.tables.payment).values({
15
- ...payment,
16
- creditorIban: payment.creditor.iban,
17
- debtorIban: payment.debtor.iban
18
- }).onConflictDoUpdate({
19
- // Unique index: (connector_key, account_id, bank_ref_id)
20
- target: [
21
- bank.tables.payment.connectorKey,
22
- bank.tables.payment.accountId,
23
- bank.tables.payment.bankRefId
24
- ],
25
- set: {
26
- status: drizzleOrm.sql`excluded.status`,
27
- statusReason: drizzleOrm.sql`excluded.status_reason`,
28
- processedAt: drizzleOrm.sql`excluded.processed_at`,
29
- updatedAt: drizzleOrm.sql`excluded.updated_at`,
30
- // Keep existing refId if already set, otherwise use enriched value
31
- refId: drizzleOrm.sql`coalesce(payment.ref_id, excluded.ref_id)`,
32
- // Keep existing batchId if already set, otherwise use enriched value
33
- batchId: drizzleOrm.sql`coalesce(payment.bank_execution_batch_id, excluded.bank_execution_batch_id)`
34
- }
35
- }).returning()
36
- };
37
- };
38
-
39
- const upsertBatchCommand = (db, { batch }) => {
40
- const id = batch.id || backendSdk.uuidv4();
41
- const command = db.insert(bank.tables.batch).values({
42
- ...batch,
43
- id
44
- }).onConflictDoUpdate({
45
- target: bank.tables.batch.id,
46
- set: {
47
- ...batch
48
- }
49
- }).returning();
50
- return {
51
- id,
52
- command
53
- };
54
- };
55
-
56
- const updatePaymentRequestStatusCommand = (db, values) => {
57
- const { id, ...set } = values;
58
- return {
59
- command: db.update(bank.tables.paymentRequest).set(set).where(
60
- drizzleOrm.and(
61
- drizzleOrm.eq(bank.tables.paymentRequest.id, id),
62
- drizzleOrm.isNull(bank.tables.paymentRequest.deletedAt)
63
- )
64
- ).returning()
65
- };
66
- };
67
-
68
- const getAccountByIdQuery = async (db, { accountId }) => {
69
- return await db.select().from(bank.tables.account).where(drizzleOrm.eq(bank.tables.account.id, accountId)).get();
70
- };
71
-
72
- const getBatchByIdQuery = async (db, { batchId }) => {
73
- return await db.select().from(bank.tables.batch).where(drizzleOrm.eq(bank.tables.batch.id, batchId)).get();
74
- };
75
-
76
- const getCredentialsByAccountId = async (db, encryptionKey, { accountId }) => {
77
- const cred = await db.select().from(bank.tables.accountCredentials).where(drizzleOrm.eq(bank.tables.accountCredentials.accountId, accountId)).get();
78
- return cred ? {
79
- ...cred,
80
- value: await decrypt(cred.value, encryptionKey)
81
- } : void 0;
82
- };
83
-
84
- const getPaymentRequestsByBatchIdQuery = async (db, { batchId }) => {
85
- return await db.select().from(bank.tables.paymentRequest).where(
86
- drizzleOrm.and(
87
- drizzleOrm.eq(bank.tables.paymentRequest.batchId, batchId),
88
- drizzleOrm.isNull(bank.tables.paymentRequest.deletedAt)
89
- )
90
- );
91
- };
92
-
93
- async function importAesKey(base64Key) {
94
- const raw = Uint8Array.from(atob(base64Key), (c) => c.charCodeAt(0));
95
- return await crypto.subtle.importKey("raw", raw, { name: "AES-GCM" }, false, [
96
- "encrypt",
97
- "decrypt"
98
- ]);
99
- }
100
- async function encrypt(value, key) {
101
- const encoder = new TextEncoder();
102
- const iv = crypto.getRandomValues(new Uint8Array(12));
103
- const ciphertext = await crypto.subtle.encrypt(
104
- { name: "AES-GCM", iv },
105
- key,
106
- encoder.encode(value)
107
- );
108
- const combined = new Uint8Array(iv.length + ciphertext.byteLength);
109
- combined.set(iv, 0);
110
- combined.set(new Uint8Array(ciphertext), iv.length);
111
- return btoa(String.fromCharCode(...combined));
112
- }
113
- async function decrypt(base64, key) {
114
- const raw = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
115
- const iv = raw.slice(0, 12);
116
- const ciphertext = raw.slice(12);
117
- const decrypted = await crypto.subtle.decrypt(
118
- { name: "AES-GCM", iv },
119
- key,
120
- ciphertext
121
- );
122
- return new TextDecoder().decode(decrypted);
123
- }
124
- function canonicalize(value) {
125
- if (value === null || typeof value !== "object") return value;
126
- if (Array.isArray(value)) {
127
- return value.map(canonicalize);
128
- }
129
- const obj = value;
130
- const sorted = Object.keys(obj).sort().reduce((acc, key) => {
131
- acc[key] = canonicalize(obj[key]);
132
- return acc;
133
- }, {});
134
- return sorted;
135
- }
136
- function checksum(input) {
137
- const canonical = canonicalize(input);
138
- const json = JSON.stringify(canonical);
139
- return node_crypto.createHash("sha256").update(json).digest("hex");
140
- }
141
-
142
- const createCredentialsResolver = async (db, env) => {
143
- const encryptionKey = await importAesKey(
144
- (await env.SECRETS_STORE.get({
145
- secretName: "BANK_SERVICE_ENCRYPTION_KEY"
146
- })).data?.secretValue || ""
147
- );
148
- return (accountId) => getCredentialsByAccountId(db, encryptionKey, { accountId });
149
- };
150
-
151
- exports.checksum = checksum;
152
- exports.createCredentialsResolver = createCredentialsResolver;
153
- exports.createPaymentCommand = createPaymentCommand;
154
- exports.encrypt = encrypt;
155
- exports.getAccountByIdQuery = getAccountByIdQuery;
156
- exports.getBatchByIdQuery = getBatchByIdQuery;
157
- exports.getPaymentRequestsByBatchIdQuery = getPaymentRequestsByBatchIdQuery;
158
- exports.importAesKey = importAesKey;
159
- exports.updatePaymentRequestStatusCommand = updatePaymentRequestStatusCommand;
160
- exports.upsertBatchCommand = upsertBatchCommand;
@@ -1,149 +0,0 @@
1
- import { sql, and, eq, isNull } from 'drizzle-orm';
2
- import { G as tables } from './bank.BqLdCHnj.mjs';
3
- import { uuidv4 } from '@develit-io/backend-sdk';
4
- import './bank.BzDNLxB_.mjs';
5
- import 'date-fns';
6
- import 'jose';
7
- import '@develit-io/general-codes';
8
- import { createHash } from 'node:crypto';
9
-
10
- const createPaymentCommand = (db, { payment }) => {
11
- return {
12
- command: db.insert(tables.payment).values({
13
- ...payment,
14
- creditorIban: payment.creditor.iban,
15
- debtorIban: payment.debtor.iban
16
- }).onConflictDoUpdate({
17
- // Unique index: (connector_key, account_id, bank_ref_id)
18
- target: [
19
- tables.payment.connectorKey,
20
- tables.payment.accountId,
21
- tables.payment.bankRefId
22
- ],
23
- set: {
24
- status: sql`excluded.status`,
25
- statusReason: sql`excluded.status_reason`,
26
- processedAt: sql`excluded.processed_at`,
27
- updatedAt: sql`excluded.updated_at`,
28
- // Keep existing refId if already set, otherwise use enriched value
29
- refId: sql`coalesce(payment.ref_id, excluded.ref_id)`,
30
- // Keep existing batchId if already set, otherwise use enriched value
31
- batchId: sql`coalesce(payment.bank_execution_batch_id, excluded.bank_execution_batch_id)`
32
- }
33
- }).returning()
34
- };
35
- };
36
-
37
- const upsertBatchCommand = (db, { batch }) => {
38
- const id = batch.id || uuidv4();
39
- const command = db.insert(tables.batch).values({
40
- ...batch,
41
- id
42
- }).onConflictDoUpdate({
43
- target: tables.batch.id,
44
- set: {
45
- ...batch
46
- }
47
- }).returning();
48
- return {
49
- id,
50
- command
51
- };
52
- };
53
-
54
- const updatePaymentRequestStatusCommand = (db, values) => {
55
- const { id, ...set } = values;
56
- return {
57
- command: db.update(tables.paymentRequest).set(set).where(
58
- and(
59
- eq(tables.paymentRequest.id, id),
60
- isNull(tables.paymentRequest.deletedAt)
61
- )
62
- ).returning()
63
- };
64
- };
65
-
66
- const getAccountByIdQuery = async (db, { accountId }) => {
67
- return await db.select().from(tables.account).where(eq(tables.account.id, accountId)).get();
68
- };
69
-
70
- const getBatchByIdQuery = async (db, { batchId }) => {
71
- return await db.select().from(tables.batch).where(eq(tables.batch.id, batchId)).get();
72
- };
73
-
74
- const getCredentialsByAccountId = async (db, encryptionKey, { accountId }) => {
75
- const cred = await db.select().from(tables.accountCredentials).where(eq(tables.accountCredentials.accountId, accountId)).get();
76
- return cred ? {
77
- ...cred,
78
- value: await decrypt(cred.value, encryptionKey)
79
- } : void 0;
80
- };
81
-
82
- const getPaymentRequestsByBatchIdQuery = async (db, { batchId }) => {
83
- return await db.select().from(tables.paymentRequest).where(
84
- and(
85
- eq(tables.paymentRequest.batchId, batchId),
86
- isNull(tables.paymentRequest.deletedAt)
87
- )
88
- );
89
- };
90
-
91
- async function importAesKey(base64Key) {
92
- const raw = Uint8Array.from(atob(base64Key), (c) => c.charCodeAt(0));
93
- return await crypto.subtle.importKey("raw", raw, { name: "AES-GCM" }, false, [
94
- "encrypt",
95
- "decrypt"
96
- ]);
97
- }
98
- async function encrypt(value, key) {
99
- const encoder = new TextEncoder();
100
- const iv = crypto.getRandomValues(new Uint8Array(12));
101
- const ciphertext = await crypto.subtle.encrypt(
102
- { name: "AES-GCM", iv },
103
- key,
104
- encoder.encode(value)
105
- );
106
- const combined = new Uint8Array(iv.length + ciphertext.byteLength);
107
- combined.set(iv, 0);
108
- combined.set(new Uint8Array(ciphertext), iv.length);
109
- return btoa(String.fromCharCode(...combined));
110
- }
111
- async function decrypt(base64, key) {
112
- const raw = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
113
- const iv = raw.slice(0, 12);
114
- const ciphertext = raw.slice(12);
115
- const decrypted = await crypto.subtle.decrypt(
116
- { name: "AES-GCM", iv },
117
- key,
118
- ciphertext
119
- );
120
- return new TextDecoder().decode(decrypted);
121
- }
122
- function canonicalize(value) {
123
- if (value === null || typeof value !== "object") return value;
124
- if (Array.isArray(value)) {
125
- return value.map(canonicalize);
126
- }
127
- const obj = value;
128
- const sorted = Object.keys(obj).sort().reduce((acc, key) => {
129
- acc[key] = canonicalize(obj[key]);
130
- return acc;
131
- }, {});
132
- return sorted;
133
- }
134
- function checksum(input) {
135
- const canonical = canonicalize(input);
136
- const json = JSON.stringify(canonical);
137
- return createHash("sha256").update(json).digest("hex");
138
- }
139
-
140
- const createCredentialsResolver = async (db, env) => {
141
- const encryptionKey = await importAesKey(
142
- (await env.SECRETS_STORE.get({
143
- secretName: "BANK_SERVICE_ENCRYPTION_KEY"
144
- })).data?.secretValue || ""
145
- );
146
- return (accountId) => getCredentialsByAccountId(db, encryptionKey, { accountId });
147
- };
148
-
149
- export { getPaymentRequestsByBatchIdQuery as a, getAccountByIdQuery as b, checksum as c, createCredentialsResolver as d, updatePaymentRequestStatusCommand as e, createPaymentCommand as f, getBatchByIdQuery as g, encrypt as h, importAesKey as i, upsertBatchCommand as u };