UNPKG

@accounter/server

Version:

117 lines 5.07 kB
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 getBusinessTripsAccommodationsExpensesByBusinessTripIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_accommodations a LEFT JOIN accounter_schema.extended_business_trip_transactions t USING (id) WHERE t.business_trip_id IN $$businessTripIds;`; const getBusinessTripsAccommodationsExpensesByIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_accommodations a LEFT JOIN accounter_schema.extended_business_trip_transactions t USING (id) WHERE a.id IN $$expenseIds;`; const updateBusinessTripAccommodationsExpense = sql ` UPDATE accounter_schema.business_trips_transactions_accommodations SET country = COALESCE( $country, country ), nights_count = COALESCE( $nightsCount, nights_count ), attendees_stay = COALESCE( $attendeesStay, attendees_stay ) WHERE id = $businessTripExpenseId RETURNING *; `; const insertBusinessTripAccommodationsExpense = sql ` INSERT INTO accounter_schema.business_trips_transactions_accommodations (id, country, nights_count, attendees_stay) VALUES($id, $country, $nightsCount, $attendeesStay) RETURNING *;`; const deleteBusinessTripAccommodationsExpense = sql ` DELETE FROM accounter_schema.business_trips_transactions_accommodations WHERE id = $businessTripExpenseId RETURNING id; `; let BusinessTripAccommodationsExpensesProvider = class BusinessTripAccommodationsExpensesProvider { dbProvider; cache = getCacheInstance({ stdTTL: 60 * 5, }); constructor(dbProvider) { this.dbProvider = dbProvider; } async batchBusinessTripsAccommodationsExpensesByBusinessTripIds(businessTripIds) { const businessTripsAccommodationsExpenses = await getBusinessTripsAccommodationsExpensesByBusinessTripIds.run({ businessTripIds, }, this.dbProvider); return businessTripIds.map(id => businessTripsAccommodationsExpenses.filter(record => record.business_trip_id === id)); } getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsAccommodationsExpensesByBusinessTripIds(ids), { cacheKeyFn: key => `business-trip-accommodation-expense-trip-${key}`, cacheMap: this.cache, }); async batchBusinessTripsAccommodationsExpensesByIds(expenseIds) { const businessTripsAccommodationsExpenses = await getBusinessTripsAccommodationsExpensesByIds.run({ expenseIds, }, this.dbProvider); return expenseIds.map(id => businessTripsAccommodationsExpenses.find(record => record.id === id)); } getBusinessTripsAccommodationsExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsAccommodationsExpensesByIds(ids), { cacheKeyFn: key => `business-trip-accommodation-expense-${key}`, cacheMap: this.cache, }); updateBusinessTripAccommodationsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return updateBusinessTripAccommodationsExpense.run(params, this.dbProvider); } insertBusinessTripAccommodationsExpense(params) { params.attendeesStay ||= []; return insertBusinessTripAccommodationsExpense.run(params, this.dbProvider); } deleteBusinessTripAccommodationsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return deleteBusinessTripAccommodationsExpense.run(params, this.dbProvider); } async invalidateById(expenseId) { const expense = await this.getBusinessTripsAccommodationsExpensesByIdLoader.load(expenseId); if (expense) { this.cache.delete(`business-trip-accommodation-expense-trip-${expense.business_trip_id}`); } this.cache.delete(`business-trip-accommodation-expense-${expenseId}`); } async invalidateByBusinessTripId(businessTripId) { const expenses = await this.getBusinessTripsAccommodationsExpensesByBusinessTripIdLoader.load(businessTripId); for (const expense of expenses ?? []) { this.cache.delete(`business-trip-accommodation-expense-${expense.id}`); } this.cache.delete(`business-trip-accommodation-expense-trip-${businessTripId}`); } clearCache() { this.cache.clear(); } }; BusinessTripAccommodationsExpensesProvider = __decorate([ Injectable({ scope: Scope.Singleton, global: true, }), __metadata("design:paramtypes", [DBProvider]) ], BusinessTripAccommodationsExpensesProvider); export { BusinessTripAccommodationsExpensesProvider }; //# sourceMappingURL=business-trips-expenses-accommodations.provider.js.map