UNPKG

@accounter/server

Version:

133 lines 4.78 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 getDepreciationRecordsByChargeIds = sql ` SELECT d.*, (d.activation_date + (CONCAT((100 / dc.percentage * 365)::text, ' day'))::interval)::date as expiration_date FROM accounter_schema.depreciation d LEFT JOIN accounter_schema.depreciation_categories dc ON d.category = dc.id WHERE d.charge_id IN $$chargeIds;`; const getDepreciationRecordsByIds = sql ` SELECT d.*, (d.activation_date + (CONCAT((100 / dc.percentage * 365)::text, ' day'))::interval)::date as expiration_date FROM accounter_schema.depreciation d LEFT JOIN accounter_schema.depreciation_categories dc ON d.category = dc.id WHERE d.id IN $$depreciationRecordIds;`; const getDepreciationRecordsByDates = sql ` WITH depreciation AS (SELECT d.*, (activation_date + (CONCAT((100 / dc.percentage * 365)::text, ' day'))::interval)::date AS expiration_date FROM accounter_schema.depreciation d LEFT JOIN accounter_schema.depreciation_categories dc ON d.category = dc.id WHERE activation_date <= $toDate) SELECT * FROM depreciation WHERE expiration_date >= $fromDate;`; const updateDepreciationRecord = sql ` UPDATE accounter_schema.depreciation SET charge_id = COALESCE( $chargeId, charge_id ), amount = COALESCE( $amount, amount ), currency = COALESCE( $currency, currency ), activation_date = COALESCE( $activationDate, activation_date ), type = COALESCE( $type, type ), category = COALESCE( $categoryId, category ) WHERE id = $id RETURNING *;`; const insertDepreciationRecord = sql ` INSERT INTO accounter_schema.depreciation (charge_id, amount, currency, activation_date, type, category) VALUES ($chargeId, $amount, $currency, $activationDate, $type, $categoryId) RETURNING *`; const deleteDepreciationRecord = sql ` DELETE FROM accounter_schema.depreciation WHERE id = $depreciationRecordId RETURNING id; `; const deleteDepreciationRecordByChargeId = sql ` DELETE FROM accounter_schema.depreciation WHERE id = $chargeId RETURNING id; `; let DepreciationProvider = class DepreciationProvider { dbProvider; cache = getCacheInstance({ stdTTL: 60 * 5, }); constructor(dbProvider) { this.dbProvider = dbProvider; } async batchDepreciationRecordsByIds(depreciationRecordIds) { const records = await getDepreciationRecordsByIds.run({ depreciationRecordIds, }, this.dbProvider); return depreciationRecordIds.map(id => records.find(record => record.id === id)); } getDepreciationRecordByIdLoader = new DataLoader((ids) => this.batchDepreciationRecordsByIds(ids), { cacheKeyFn: key => `depreciation-record-${key}`, cacheMap: this.cache, }); async batchDepreciationRecordsByChargeIds(chargeIds) { const records = await getDepreciationRecordsByChargeIds.run({ chargeIds, }, this.dbProvider); return chargeIds.map(id => records.filter(record => record.charge_id === id)); } getDepreciationRecordsByChargeIdLoader = new DataLoader((ids) => this.batchDepreciationRecordsByChargeIds(ids), { cacheKeyFn: key => `depreciation-records-by-charge-${key}`, cacheMap: this.cache, }); getDepreciationRecordsByDates(params) { return getDepreciationRecordsByDates.run(params, this.dbProvider); } updateDepreciationRecord(params) { this.clearCache(); return updateDepreciationRecord.run(params, this.dbProvider); } insertDepreciationRecord(params) { this.clearCache(); return insertDepreciationRecord.run(params, this.dbProvider); } deleteDepreciationRecord(params) { this.clearCache(); return deleteDepreciationRecord.run(params, this.dbProvider); } deleteDepreciationRecordByChargeId(params) { this.clearCache(); return deleteDepreciationRecordByChargeId.run(params, this.dbProvider); } clearCache() { this.cache.clear(); } }; DepreciationProvider = __decorate([ Injectable({ scope: Scope.Singleton, global: true, }), __metadata("design:paramtypes", [DBProvider]) ], DepreciationProvider); export { DepreciationProvider }; //# sourceMappingURL=depreciation.provider.js.map