@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
114 lines • 4.9 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 getBusinessTripsCarRentalExpensesByBusinessTripIds = sql `
SELECT *
FROM accounter_schema.business_trips_transactions_car_rental a
LEFT JOIN accounter_schema.extended_business_trip_transactions t
USING (id)
WHERE ($isBusinessTripIds = 0 OR t.business_trip_id IN $$businessTripIds);`;
const getBusinessTripsCarRentalExpensesByIds = sql `
SELECT *
FROM accounter_schema.business_trips_transactions_car_rental a
LEFT JOIN accounter_schema.extended_business_trip_transactions t
USING (id)
WHERE ($isIds = 0 OR t.id IN $$expenseIds);`;
const updateBusinessTripCarRentalExpense = sql `
UPDATE accounter_schema.business_trips_transactions_car_rental
SET
days = COALESCE(
$days,
days
),
is_fuel_expense = COALESCE(
$isFuelExpense,
is_fuel_expense
)
WHERE
id = $businessTripExpenseId
RETURNING *;
`;
const insertBusinessTripCarRentalExpense = sql `
INSERT INTO accounter_schema.business_trips_transactions_car_rental (id, days, is_fuel_expense)
VALUES($id, $days, $isFuelExpense)
RETURNING *;`;
const deleteBusinessTripCarRentalExpense = sql `
DELETE FROM accounter_schema.business_trips_transactions_car_rental
WHERE id = $businessTripExpenseId
RETURNING id;
`;
let BusinessTripCarRentalExpensesProvider = class BusinessTripCarRentalExpensesProvider {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 5,
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async batchBusinessTripsCarRentalExpensesByBusinessTripIds(businessTripIds) {
const businessTripsCarRentalExpenses = await getBusinessTripsCarRentalExpensesByBusinessTripIds.run({
isBusinessTripIds: businessTripIds.length > 0 ? 1 : 0,
businessTripIds,
}, this.dbProvider);
return businessTripIds.map(id => businessTripsCarRentalExpenses.filter(record => record.business_trip_id === id));
}
getBusinessTripsCarRentalExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsCarRentalExpensesByBusinessTripIds(ids), {
cacheKeyFn: key => `business-trip-car-rental-expenses-trip-${key}`,
cacheMap: this.cache,
});
async batchBusinessTripsCarRentalExpensesByIds(expenseIds) {
const businessTripsCarRentalExpenses = await getBusinessTripsCarRentalExpensesByIds.run({
isIds: expenseIds.length > 0 ? 1 : 0,
expenseIds,
}, this.dbProvider);
return expenseIds.map(id => businessTripsCarRentalExpenses.find(record => record.id === id));
}
getBusinessTripsCarRentalExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsCarRentalExpensesByIds(ids), {
cacheKeyFn: key => `business-trip-car-rental-expense-${key}`,
cacheMap: this.cache,
});
updateBusinessTripCarRentalExpense(params) {
if (params.businessTripExpenseId) {
this.invalidateById(params.businessTripExpenseId);
}
return updateBusinessTripCarRentalExpense.run(params, this.dbProvider);
}
insertBusinessTripCarRentalExpense(params) {
return insertBusinessTripCarRentalExpense.run(params, this.dbProvider);
}
deleteBusinessTripCarRentalExpense(params) {
if (params.businessTripExpenseId) {
this.invalidateById(params.businessTripExpenseId);
}
return deleteBusinessTripCarRentalExpense.run(params, this.dbProvider);
}
async invalidateById(expenseId) {
const expense = await this.getBusinessTripsCarRentalExpensesByIdLoader.load(expenseId);
if (expense) {
this.cache.delete(`business-trip-car-rental-expenses-trip-${expense.business_trip_id}`);
}
this.cache.delete(`business-trip-car-rental-expense-${expenseId}`);
}
async invalidateByBusinessTripId(businessTripId) {
const expenses = await this.getBusinessTripsCarRentalExpensesByBusinessTripIdLoader.load(businessTripId);
for (const expense of expenses ?? []) {
this.cache.delete(`business-trip-car-rental-expense-${expense.id}`);
}
this.cache.delete(`business-trip-car-rental-expenses-trip-${businessTripId}`);
}
clearCache() {
this.cache.clear();
}
};
BusinessTripCarRentalExpensesProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], BusinessTripCarRentalExpensesProvider);
export { BusinessTripCarRentalExpensesProvider };
//# sourceMappingURL=business-trips-expenses-car-rental.provider.js.map