UNPKG

@accounter/server

Version:

The test suite is split into three Vitest projects for efficiency and isolation:

117 lines 4.72 kB
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 getBusinessTripsFlightsExpensesByBusinessTripIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_flights f LEFT JOIN accounter_schema.extended_business_trip_transactions t USING (id) WHERE t.business_trip_id IN $$businessTripIds;`; const getBusinessTripsFlightsExpensesByIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_flights f LEFT JOIN accounter_schema.extended_business_trip_transactions t USING (id) WHERE t.id IN $$expenseIds;`; const updateBusinessTripFlightsExpense = sql ` UPDATE accounter_schema.business_trips_transactions_flights SET path = COALESCE( $path, path ), class = COALESCE( $class, class ), attendees = COALESCE( $attendeeIds, attendees ) WHERE id = $businessTripExpenseId RETURNING *; `; const insertBusinessTripFlightsExpense = sql ` INSERT INTO accounter_schema.business_trips_transactions_flights (id, path, class, attendees) VALUES($id, $path, $class, $attendeeIds) RETURNING *;`; const deleteBusinessTripFlightsExpense = sql ` DELETE FROM accounter_schema.business_trips_transactions_flights WHERE id = $businessTripExpenseId RETURNING id; `; let BusinessTripFlightsExpensesProvider = class BusinessTripFlightsExpensesProvider { dbProvider; cache = getCacheInstance({ stdTTL: 60 * 5, }); constructor(dbProvider) { this.dbProvider = dbProvider; } async batchBusinessTripsFlightsExpensesByBusinessTripIds(businessTripIds) { const businessTripsFlightsExpenses = await getBusinessTripsFlightsExpensesByBusinessTripIds.run({ businessTripIds, }, this.dbProvider); return businessTripIds.map(id => businessTripsFlightsExpenses.filter(record => record.business_trip_id === id)); } getBusinessTripsFlightsExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsFlightsExpensesByBusinessTripIds(ids), { cacheKeyFn: key => `business-trip-flights-expense-trip-${key}`, cacheMap: this.cache, }); async batchBusinessTripsFlightsExpensesByIds(expenseIds) { const businessTripsFlightsExpenses = await getBusinessTripsFlightsExpensesByIds.run({ expenseIds, }, this.dbProvider); return expenseIds.map(id => businessTripsFlightsExpenses.find(record => record.id === id)); } getBusinessTripsFlightsExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsFlightsExpensesByIds(ids), { cacheKeyFn: key => `business-trip-flights-expense-${key}`, cacheMap: this.cache, }); updateBusinessTripFlightsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return updateBusinessTripFlightsExpense.run(params, this.dbProvider); } insertBusinessTripFlightsExpense(params) { params.attendeeIds ||= []; return insertBusinessTripFlightsExpense.run(params, this.dbProvider); } deleteBusinessTripFlightsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return deleteBusinessTripFlightsExpense.run(params, this.dbProvider); } async invalidateById(expenseId) { const expense = await this.getBusinessTripsFlightsExpensesByIdLoader.load(expenseId); if (expense) { this.cache.delete(`business-trip-flights-expense-trip-${expense.business_trip_id}`); } this.cache.delete(`business-trip-flights-expense-${expenseId}`); } async invalidateByBusinessTripId(businessTripId) { const expenses = await this.getBusinessTripsFlightsExpensesByBusinessTripIdLoader.load(businessTripId); for (const expense of expenses ?? []) { this.cache.delete(`business-trip-flights-expense-${expense.id}`); } this.cache.delete(`business-trip-flights-expense-trip-${businessTripId}`); } clearCache() { this.cache.clear(); } }; BusinessTripFlightsExpensesProvider = __decorate([ Injectable({ scope: Scope.Singleton, global: true, }), __metadata("design:paramtypes", [DBProvider]) ], BusinessTripFlightsExpensesProvider); export { BusinessTripFlightsExpensesProvider }; //# sourceMappingURL=business-trips-expenses-flights.provider.js.map