@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
133 lines • 4.28 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 { validateClientIntegrations } from '../helpers/clients.helper.js';
const getAllClients = sql `
SELECT *
FROM accounter_schema.clients;
`;
const getClientsByIds = sql `
SELECT *
FROM accounter_schema.clients
WHERE business_id IN $$businessIds;
`;
const getClientsByGreenInvoiceIds = sql `
SELECT *, (integrations->>'greenInvoiceId')::uuid as green_invoice_business_id
FROM accounter_schema.clients
WHERE (integrations->>'greenInvoiceId')::uuid in $$greenInvoiceBusinessIds;
`;
const updateClient = sql `
UPDATE accounter_schema.clients
SET
emails = COALESCE(
$emails,
emails,
NULL
),
business_id = COALESCE(
$newBusinessId,
business_id,
NULL
),
integrations = COALESCE(
$integrations,
integrations,
NULL
)
WHERE
business_id = $businessId
RETURNING *;
`;
const deleteClient = sql `
DELETE FROM accounter_schema.clients
WHERE business_id = $businessId
RETURNING business_id;
`;
const insertClient = sql `
INSERT INTO accounter_schema.clients (business_id, emails, integrations)
VALUES ($businessId, $emails, $integrations)
RETURNING *;`;
let ClientsProvider = class ClientsProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 60 * 24, // 24 hours
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async getAllClients() {
const cache = this.cache.get('all-clients');
if (cache) {
return Promise.resolve(cache);
}
return getAllClients.run(undefined, this.dbProvider).then(data => {
this.cache.set('all-clients', data);
data.map(client => {
this.cache.set(`client-id-${client.business_id}`, client);
try {
const { greenInvoiceId } = validateClientIntegrations(client.integrations ?? {});
this.cache.set(`client-green-invoice-id-${greenInvoiceId}`, client);
}
catch {
// swallow errors
}
});
return data;
});
}
async batchClientsByIds(ids) {
try {
const matches = await getClientsByIds.run({ businessIds: ids }, this.dbProvider);
return ids.map(id => matches.find(match => match.business_id === id));
}
catch (e) {
console.error(e);
return ids.map(() => undefined);
}
}
getClientByIdLoader = new DataLoader((keys) => this.batchClientsByIds(keys), {
cacheKeyFn: key => `client-id-${key}`,
cacheMap: this.cache,
});
async batchClientsByGreenInvoiceIds(greenInvoiceIds) {
try {
const matches = await getClientsByGreenInvoiceIds.run({ greenInvoiceBusinessIds: greenInvoiceIds }, this.dbProvider);
return greenInvoiceIds.map(id => matches.find(match => match.green_invoice_business_id === id));
}
catch (e) {
console.error(e);
return greenInvoiceIds.map(() => undefined);
}
}
getClientByGreenInvoiceIdLoader = new DataLoader((keys) => this.batchClientsByGreenInvoiceIds(keys), {
cacheKeyFn: key => `client-green-invoice-id-${key}`,
cacheMap: this.cache,
});
async updateClient(params) {
this.clearCache();
return updateClient.run(params, this.dbProvider);
}
async deleteClient(businessId) {
this.clearCache();
return deleteClient.run({ businessId }, this.dbProvider);
}
async insertClient(params) {
this.clearCache();
return insertClient.run(params, this.dbProvider);
}
clearCache() {
this.cache.clear();
}
};
ClientsProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], ClientsProvider);
export { ClientsProvider };
//# sourceMappingURL=clients.provider.js.map