@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
86 lines • 2.85 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';
const getAllDividends = sql `
SELECT *
FROM accounter_schema.dividends
ORDER BY date DESC;
`;
const getDividendsByChargeId = sql `
SELECT d.*, t.charge_id
FROM accounter_schema.dividends d
LEFT JOIN accounter_schema.transactions t
ON d.transaction_id = t.id
WHERE t.charge_id in $$chargeIds
ORDER BY date DESC;
`;
const getDividendsByBusinessIds = sql `
SELECT *
FROM accounter_schema.dividends
WHERE business_id IN $$businessIds
ORDER BY date DESC;
`;
let DividendsProvider = class DividendsProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 5,
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async getAllDividends(params) {
const cached = this.cache.get('all-dividends');
if (cached) {
return Promise.resolve(cached);
}
return getAllDividends.run(params, this.dbProvider).then(res => {
if (res) {
this.cache.set('all-dividends', res);
}
return res;
});
}
async batchDividendsByChargeIds(chargeIds) {
try {
const dividends = await getDividendsByChargeId.run({ chargeIds }, this.dbProvider);
return chargeIds.map(id => dividends.filter(dividend => dividend.charge_id === id));
}
catch (e) {
console.error(e);
return chargeIds.map(() => []);
}
}
getDividendsByChargeIdLoader = new DataLoader((keys) => this.batchDividendsByChargeIds(keys), {
cacheKeyFn: key => `dividends-by-charge-${key}`,
cacheMap: this.cache,
});
async batchDividendsByBusinessIds(businessIds) {
try {
const dividends = await getDividendsByBusinessIds.run({ businessIds }, this.dbProvider);
return businessIds.map(id => dividends.filter(dividend => dividend.business_id === id));
}
catch (e) {
console.error(e);
return businessIds.map(() => []);
}
}
getDividendsByBusinessIdLoader = new DataLoader((keys) => this.batchDividendsByBusinessIds(keys), {
cacheKeyFn: key => `dividends-by-business-${key}`,
cacheMap: this.cache,
});
clearCache() {
this.cache.clear();
}
};
DividendsProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], DividendsProvider);
export { DividendsProvider };
//# sourceMappingURL=dividends.provider.js.map