@accounter/server
Version:
Accounter GraphQL server
54 lines • 1.77 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { dateToTimelessDateString, getCacheInstance } from '../../../shared/helpers/index.js';
import { DBProvider } from '../../app-providers/db.provider.js';
const getAllVatValues = sql `
SELECT *
FROM accounter_schema.vat_value
ORDER BY date DESC;
`;
let VatProvider = class VatProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 60 * 24, // 24 hours
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
getAllVatValues() {
const data = this.cache.get('all-vat-values');
if (data) {
return data;
}
return getAllVatValues.run(undefined, this.dbProvider).then(data => {
this.cache.set('all-vat-values', data);
return data;
});
}
async batchVatValuesByDates(dates) {
const vatValues = await this.getAllVatValues();
return dates.map(date => {
const record = vatValues.find(value => dateToTimelessDateString(value.date) <= date);
const value = record ? Number(record.percentage) : null;
return value;
});
}
getVatValueByDateLoader = new DataLoader((dates) => this.batchVatValuesByDates(dates), {
cacheKeyFn: key => `vat-value-date-${key}`,
cacheMap: this.cache,
});
clearCache() {
this.cache.clear();
}
};
VatProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], VatProvider);
export { VatProvider };
//# sourceMappingURL=vat.provider.js.map