@accounter/server
Version:
Accounter GraphQL server
73 lines • 2.47 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.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 {
db;
constructor(db) {
this.db = db;
}
allDividendsCache = null;
getAllDividends() {
if (this.allDividendsCache) {
return this.allDividendsCache;
}
this.allDividendsCache = getAllDividends.run(undefined, this.db);
return this.allDividendsCache;
}
async batchDividendsByChargeIds(chargeIds) {
try {
const dividends = await getDividendsByChargeId.run({ chargeIds }, this.db);
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));
async batchDividendsByBusinessIds(businessIds) {
try {
const dividends = await getDividendsByBusinessIds.run({ businessIds }, this.db);
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));
clearCache() {
this.getDividendsByChargeIdLoader.clearAll();
this.getDividendsByBusinessIdLoader.clearAll();
}
};
DividendsProvider = __decorate([
Injectable({
scope: Scope.Operation,
global: true,
}),
__metadata("design:paramtypes", [TenantAwareDBClient])
], DividendsProvider);
export { DividendsProvider };
//# sourceMappingURL=dividends.provider.js.map