UNPKG

@accounter/server

Version:

131 lines 3.94 kB
import { __decorate, __metadata } from "tslib"; import DataLoader from 'dataloader'; import { Injectable, Scope } from 'graphql-modules'; import { DBProvider } from '../../app-providers/db.provider.js'; import { sql } from '@pgtyped/runtime'; import { getCacheInstance } from '../../../shared/helpers/index.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 * FROM accounter_schema.clients WHERE green_invoice_id IN $$greenInvoiceBusinessIds; `; const updateClient = sql ` UPDATE accounter_schema.clients SET green_invoice_id = COALESCE( $greenInvoiceId, green_invoice_id, NULL ), hive_id = COALESCE( $hiveId, hive_id, NULL ), emails = COALESCE( $emails, emails, NULL ), business_id = COALESCE( $newBusinessId, business_id, 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 (green_invoice_id, hive_id) VALUES ($greenInvoiceId, $hiveId) 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); this.cache.set(`client-green-invoice-id-${client.green_invoice_id}`, client); }); 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_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