@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
158 lines • 6.15 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { getCacheInstance } from '../../../shared/helpers/index.js';
import { DBProvider } from '../../app-providers/db.provider.js';
const getFinancialAccountsByOwnerIds = sql `
SELECT id, account_number, account_name, private_business, owner, type
FROM accounter_schema.financial_accounts
WHERE owner IN $$ownerIds;`;
const getFinancialAccountsByAccountNumbers = sql `
SELECT id, account_number, account_name, private_business, owner, type
FROM accounter_schema.financial_accounts
WHERE account_number IN $$accountNumbers;`;
const getFinancialAccountsByAccountIDs = sql `
SELECT id, account_number, account_name, private_business, owner, type
FROM accounter_schema.financial_accounts
WHERE id IN $$accountIDs;`;
const getAllFinancialAccounts = sql `
SELECT id, account_number, account_name, private_business, owner, type
FROM accounter_schema.financial_accounts;`;
const updateFinancialAccount = sql `
UPDATE accounter_schema.financial_accounts
SET
account_number = COALESCE(
$accountNumber,
account_number
),
account_name = COALESCE(
$name,
account_name
),
private_business = COALESCE(
$privateBusiness,
private_business
),
owner = COALESCE(
$ownerId,
owner
),
type = COALESCE(
$type,
type
)
WHERE
id = $financialAccountId
RETURNING *;
`;
const insertFinancialAccounts = sql `
INSERT INTO accounter_schema.financial_accounts (
account_number, account_name, private_business, owner, type
)
VALUES $$bankAccounts(
accountNumber, name, privateBusiness, ownerId, type
)
RETURNING *;`;
const deleteFinancialAccount = sql `
DELETE FROM accounter_schema.financial_accounts
WHERE id = $financialAccountId
RETURNING id;
`;
let FinancialAccountsProvider = class FinancialAccountsProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 5,
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async batchFinancialAccountsByOwnerIds(ownerIds) {
const accounts = await getFinancialAccountsByOwnerIds.run({
ownerIds,
}, this.dbProvider);
return ownerIds.map(ownerId => accounts.filter(charge => charge.owner === ownerId));
}
getFinancialAccountsByOwnerIdLoader = new DataLoader((keys) => this.batchFinancialAccountsByOwnerIds(keys), {
cacheKeyFn: key => `account-by-owner-id-${key}`,
cacheMap: this.cache,
});
async batchFinancialAccountsByAccountNumbers(accountNumbers) {
const accounts = await getFinancialAccountsByAccountNumbers.run({
accountNumbers,
}, this.dbProvider);
return accountNumbers.map(accountNumber => accounts.find(charge => charge.account_number === accountNumber));
}
getFinancialAccountByAccountNumberLoader = new DataLoader((keys) => this.batchFinancialAccountsByAccountNumbers(keys), {
cacheKeyFn: key => `account-number-${key}`,
cacheMap: this.cache,
});
async batchFinancialAccountsByAccountIDs(accountIDs) {
const accounts = await getFinancialAccountsByAccountIDs.run({
accountIDs,
}, this.dbProvider);
return accountIDs.map(id => accounts.find(account => account.id === id));
}
getFinancialAccountByAccountIDLoader = new DataLoader((keys) => this.batchFinancialAccountsByAccountIDs(keys), {
cacheKeyFn: key => `account-id-${key}`,
cacheMap: this.cache,
});
getAllFinancialAccounts() {
const cached = this.cache.get('all-accounts');
if (cached) {
return Promise.resolve(cached);
}
return getAllFinancialAccounts.run(undefined, this.dbProvider).then(res => {
this.cache.set('all-accounts', res);
res.map(account => {
this.getFinancialAccountByAccountNumberLoader.prime(account.account_number, account);
this.getFinancialAccountByAccountIDLoader.prime(account.id, account);
if (account.owner)
this.getFinancialAccountsByOwnerIdLoader.clear(account.owner);
});
return res;
});
}
async updateFinancialAccount(params) {
const updatedAccounts = await updateFinancialAccount.run(params, this.dbProvider);
const updatedAccount = updatedAccounts[0];
if (updatedAccount) {
this.invalidateById(updatedAccount.id);
this.getFinancialAccountByAccountIDLoader.prime(updatedAccount.id, updatedAccount);
}
return updatedAccount;
}
async deleteFinancialAccount(params) {
if (params.financialAccountId) {
await this.invalidateById(params.financialAccountId);
}
return deleteFinancialAccount.run(params, this.dbProvider);
}
async insertFinancialAccounts(params) {
this.cache.delete('all-accounts');
return insertFinancialAccounts.run(params, this.dbProvider);
}
async invalidateById(financialAccountId) {
const account = await this.getFinancialAccountByAccountIDLoader.load(financialAccountId);
if (account) {
if (account.owner) {
this.getFinancialAccountsByOwnerIdLoader.clear(account.owner);
}
this.getFinancialAccountByAccountNumberLoader.clear(account.account_number);
}
this.cache.delete('all-accounts');
this.getFinancialAccountByAccountIDLoader.clear(financialAccountId);
}
clearCache() {
this.cache.clear();
}
};
FinancialAccountsProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], FinancialAccountsProvider);
export { FinancialAccountsProvider };
//# sourceMappingURL=financial-accounts.provider.js.map