@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
222 lines • 7.21 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';
import { BusinessesProvider } from '../../financial-entities/providers/businesses.provider.js';
const getAllOpenContracts = sql `
SELECT *
FROM accounter_schema.clients_contracts
WHERE is_active IS TRUE;`;
const getContractsByIds = sql `
SELECT *
FROM accounter_schema.clients_contracts
WHERE id In $$ids;`;
const getContractsByAdminBusinessIds = sql `
SELECT fe.owner_id, c.*
FROM accounter_schema.clients_contracts c
LEFT JOIN accounter_schema.financial_entities fe ON c.client_id = fe.id
WHERE owner_id IN $$adminBusinessIds;`;
const getContractsByClientIds = sql `
SELECT *
FROM accounter_schema.clients_contracts
WHERE client_id IN $$clientIds;`;
const deleteContract = sql `
DELETE FROM accounter_schema.clients_contracts
WHERE id = $id;`;
const updateContract = sql `
UPDATE accounter_schema.clients_contracts
SET
client_id = COALESCE(
$clientId,
client_id
),
purchase_orders = COALESCE(
$purchaseOrders,
purchase_orders
),
start_date = COALESCE(
$startDate,
start_date
),
end_date = COALESCE(
$endDate,
end_date
),
remarks = COALESCE(
$remarks,
remarks
),
document_type = COALESCE(
$documentType,
document_type
),
amount = COALESCE(
$amount,
amount
),
currency = COALESCE(
$currency,
currency
),
billing_cycle = COALESCE(
$billingCycle,
billing_cycle
),
product = COALESCE(
$product,
product
),
plan = COALESCE(
$plan,
plan
),
is_active = COALESCE(
$isActive,
is_active
),
ms_cloud = COALESCE(
$msCloud,
ms_cloud
),
operations_count = COALESCE(
$operationsLimit,
operations_count
)
WHERE
id = $contractId
RETURNING *;
`;
const insertContract = sql `
INSERT INTO accounter_schema.clients_contracts (
client_id,
purchase_orders,
start_date,
end_date,
remarks,
document_type,
amount,
currency,
billing_cycle,
product,
plan,
is_active,
ms_cloud,
operations_count
)
VALUES ($clientId,
$purchaseOrders,
$startDate,
$endDate,
$remarks,
$documentType,
$amount,
$currency,
$billingCycle,
$product,
$plan,
$isActive,
$msCloud,
$operationsLimit)
RETURNING *;`;
let ContractsProvider = class ContractsProvider {
dbProvider;
businessesProvider;
cache = getCacheInstance({
stdTTL: 60 * 60, // 1 hours
});
constructor(dbProvider, businessesProvider) {
this.dbProvider = dbProvider;
this.businessesProvider = businessesProvider;
}
getAllOpenContracts() {
const cached = this.cache.get('all-contracts');
if (cached) {
return Promise.resolve(cached);
}
return getAllOpenContracts.run(undefined, this.dbProvider).then(contracts => {
if (contracts) {
this.cache.set('all-contracts', contracts);
contracts.map(contract => {
this.cache.set(`contract-${contract.id}`, contract);
});
}
return contracts;
});
}
async contractsByIds(ids) {
const contracts = await getContractsByIds.run({ ids }, this.dbProvider);
return ids.map(id => contracts.find(contract => contract.id === id));
}
getContractsByIdLoader = new DataLoader((ids) => this.contractsByIds(ids), {
cacheKeyFn: id => `contract-${id}`,
cacheMap: this.cache,
});
async contractsByAdminBusinessIds(adminBusinessIds) {
const contracts = await getContractsByAdminBusinessIds.run({ adminBusinessIds }, this.dbProvider);
return adminBusinessIds.map(adminBusinessId => contracts.filter(contract => contract.owner_id === adminBusinessId));
}
getContractsByAdminBusinessIdLoader = new DataLoader((adminBusinessIds) => this.contractsByAdminBusinessIds(adminBusinessIds), {
cacheKeyFn: adminBusinessId => `admin-business-${adminBusinessId}-contracts`,
cacheMap: this.cache,
});
async contractsByClients(clientIds) {
const contracts = await getContractsByClientIds.run({ clientIds }, this.dbProvider);
return clientIds.map(clientId => contracts.filter(contract => contract.client_id === clientId));
}
getContractsByClientIdLoader = new DataLoader((ids) => this.contractsByClients(ids), {
cacheKeyFn: id => `client-contracts-${id}`,
cacheMap: this.cache,
});
async createContract(params) {
const [newContract] = await insertContract.run(params, this.dbProvider);
this.cache.set(`contract-${newContract.id}`, newContract);
// Invalidate list caches
this.getContractsByClientIdLoader.clear(newContract.client_id);
const business = await this.businessesProvider.getBusinessByIdLoader.load(newContract.client_id);
if (business?.owner_id) {
this.getContractsByAdminBusinessIdLoader.clear(business.owner_id);
}
return newContract;
}
async updateContract(params) {
const [updatedContract] = await updateContract.run(params, this.dbProvider);
if (params.contractId) {
this.invalidateCacheForContract(params.contractId);
}
else {
this.clearCache();
}
this.cache.set(`contract-${updatedContract.id}`, updatedContract);
return updatedContract;
}
async deleteContract(contractId) {
await deleteContract.run({ id: contractId }, this.dbProvider);
this.invalidateCacheForContract(contractId);
return true;
}
async invalidateCacheForContract(contractId) {
const contract = await this.getContractsByIdLoader.load(contractId);
if (contract) {
this.cache.delete(`client-contracts-${contract.client_id}`);
const business = await this.businessesProvider.getBusinessByIdLoader.load(contract.client_id);
if (business?.owner_id) {
this.cache.delete(`admin-business-${business.owner_id}-contracts`);
}
}
}
clearCache() {
this.cache.clear();
}
};
ContractsProvider = __decorate([
Injectable({
scope: Scope.Operation,
global: true,
}),
__metadata("design:paramtypes", [DBProvider,
BusinessesProvider])
], ContractsProvider);
export { ContractsProvider };
//# sourceMappingURL=contracts.provider.js.map