UNPKG

@accounter/server

Version:
112 lines 4.72 kB
import { __decorate, __metadata, __param } from "tslib"; import DataLoader from 'dataloader'; import { CONTEXT, Inject, Injectable, Scope } from 'graphql-modules'; import { sql } from '@pgtyped/runtime'; import { reassureOwnerIdExists } from '../../../shared/helpers/index.js'; import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.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, owner_id) VALUES($id, $path, $class, $attendeeIds, $ownerId) RETURNING *;`; const deleteBusinessTripFlightsExpense = sql ` DELETE FROM accounter_schema.business_trips_transactions_flights WHERE id = $businessTripExpenseId RETURNING id; `; let BusinessTripFlightsExpensesProvider = class BusinessTripFlightsExpensesProvider { db; context; constructor(db, context) { this.db = db; this.context = context; } async batchBusinessTripsFlightsExpensesByBusinessTripIds(businessTripIds) { const businessTripsFlightsExpenses = await getBusinessTripsFlightsExpensesByBusinessTripIds.run({ businessTripIds, }, this.db); return businessTripIds.map(id => businessTripsFlightsExpenses.filter(record => record.business_trip_id === id)); } getBusinessTripsFlightsExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsFlightsExpensesByBusinessTripIds(ids)); async batchBusinessTripsFlightsExpensesByIds(expenseIds) { const businessTripsFlightsExpenses = await getBusinessTripsFlightsExpensesByIds.run({ expenseIds, }, this.db); return expenseIds.map(id => businessTripsFlightsExpenses.find(record => record.id === id)); } getBusinessTripsFlightsExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsFlightsExpensesByIds(ids)); updateBusinessTripFlightsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return updateBusinessTripFlightsExpense.run(params, this.db); } insertBusinessTripFlightsExpense(params) { params.attendeeIds ||= []; return insertBusinessTripFlightsExpense.run(reassureOwnerIdExists(params, this.context), this.db); } deleteBusinessTripFlightsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return deleteBusinessTripFlightsExpense.run(params, this.db); } async invalidateById(expenseId) { const expense = await this.getBusinessTripsFlightsExpensesByIdLoader.load(expenseId); if (expense?.business_trip_id) { this.getBusinessTripsFlightsExpensesByBusinessTripIdLoader.clear(expense.business_trip_id); } this.getBusinessTripsFlightsExpensesByIdLoader.clear(expenseId); } async invalidateByBusinessTripId(businessTripId) { const expenses = await this.getBusinessTripsFlightsExpensesByBusinessTripIdLoader.load(businessTripId); for (const expense of expenses ?? []) { this.getBusinessTripsFlightsExpensesByIdLoader.clear(expense.id); } this.getBusinessTripsFlightsExpensesByBusinessTripIdLoader.clear(businessTripId); } clearCache() { this.getBusinessTripsFlightsExpensesByBusinessTripIdLoader.clearAll(); this.getBusinessTripsFlightsExpensesByIdLoader.clearAll(); } }; BusinessTripFlightsExpensesProvider = __decorate([ Injectable({ scope: Scope.Operation, global: true, }), __param(1, Inject(CONTEXT)), __metadata("design:paramtypes", [TenantAwareDBClient, Object]) ], BusinessTripFlightsExpensesProvider); export { BusinessTripFlightsExpensesProvider }; //# sourceMappingURL=business-trips-expenses-flights.provider.js.map