@accounter/server
Version:
114 lines • 4.77 kB
JavaScript
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 getBusinessTripsOtherExpensesByBusinessTripIds = sql `
SELECT *
FROM accounter_schema.business_trips_transactions_other a
LEFT JOIN accounter_schema.extended_business_trip_transactions t
USING (id)
WHERE ($isBusinessTripIds = 0 OR t.business_trip_id IN $$businessTripIds);`;
const getBusinessTripsOtherExpensesByIds = sql `
SELECT *
FROM accounter_schema.business_trips_transactions_other a
LEFT JOIN accounter_schema.extended_business_trip_transactions t
USING (id)
WHERE ($isIds = 0 OR t.id IN $$expenseIds);`;
const updateBusinessTripOtherExpense = sql `
UPDATE accounter_schema.business_trips_transactions_other
SET
deductible_expense = COALESCE(
$deductibleExpense,
deductible_expense
),
description = COALESCE(
$description,
description
)
WHERE
id = $businessTripExpenseId
RETURNING *;
`;
const insertBusinessTripOtherExpense = sql `
INSERT INTO accounter_schema.business_trips_transactions_other (id, deductible_expense, description)
VALUES($id, $deductibleExpense, $description)
RETURNING *;`;
const deleteBusinessTripOtherExpense = sql `
DELETE FROM accounter_schema.business_trips_transactions_other
WHERE id = $businessTripExpenseId
RETURNING id;
`;
let BusinessTripOtherExpensesProvider = class BusinessTripOtherExpensesProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 5,
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async batchBusinessTripsOtherExpensesByBusinessTripIds(businessTripIds) {
const businessTripsOtherExpenses = await getBusinessTripsOtherExpensesByBusinessTripIds.run({
isBusinessTripIds: businessTripIds.length > 0 ? 1 : 0,
businessTripIds,
}, this.dbProvider);
return businessTripIds.map(id => businessTripsOtherExpenses.filter(record => record.business_trip_id === id));
}
getBusinessTripsOtherExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsOtherExpensesByBusinessTripIds(ids), {
cacheKeyFn: key => `business-trip-other-expense-trip-${key}`,
cacheMap: this.cache,
});
async batchBusinessTripsOtherExpensesByIds(expenseIds) {
const businessTripsOtherExpenses = await getBusinessTripsOtherExpensesByIds.run({
isIds: expenseIds.length > 0 ? 1 : 0,
expenseIds,
}, this.dbProvider);
return expenseIds.map(id => businessTripsOtherExpenses.find(record => record.id === id));
}
getBusinessTripsOtherExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsOtherExpensesByIds(ids), {
cacheKeyFn: key => `business-trip-other-expense-${key}`,
cacheMap: this.cache,
});
updateBusinessTripOtherExpense(params) {
if (params.businessTripExpenseId) {
this.invalidateById(params.businessTripExpenseId);
}
return updateBusinessTripOtherExpense.run(params, this.dbProvider);
}
insertBusinessTripOtherExpense(params) {
return insertBusinessTripOtherExpense.run(params, this.dbProvider);
}
deleteBusinessTripOtherExpense(params) {
if (params.businessTripExpenseId) {
this.invalidateById(params.businessTripExpenseId);
}
return deleteBusinessTripOtherExpense.run(params, this.dbProvider);
}
async invalidateById(expenseId) {
const expense = await this.getBusinessTripsOtherExpensesByIdLoader.load(expenseId);
if (expense) {
this.cache.delete(`business-trip-other-expense-trip-${expense.business_trip_id}`);
}
this.cache.delete(`business-trip-other-expense-${expenseId}`);
}
async invalidateByBusinessTripId(businessTripId) {
const expenses = await this.getBusinessTripsOtherExpensesByBusinessTripIdLoader.load(businessTripId);
for (const expense of expenses ?? []) {
this.cache.delete(`business-trip-other-expense-${expense.id}`);
}
this.cache.delete(`business-trip-other-expense-trip-${businessTripId}`);
}
clearCache() {
this.cache.clear();
}
};
BusinessTripOtherExpensesProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], BusinessTripOtherExpensesProvider);
export { BusinessTripOtherExpensesProvider };
//# sourceMappingURL=business-trips-expenses-other.provider.js.map