UNPKG

@accounter/server

Version:
114 lines 4.93 kB
import { __decorate, __metadata } from "tslib"; import DataLoader from 'dataloader'; import { Injectable, Scope } from 'graphql-modules'; import { sql } from '@pgtyped/runtime'; import { reassureOwnerIdExists } from '../../../shared/helpers/index.js'; import { AdminContextProvider } from '../../admin-context/providers/admin-context.provider.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; adminContextProvider; constructor(db, adminContextProvider) { this.db = db; this.adminContextProvider = adminContextProvider; } 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)); async updateBusinessTripFlightsExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return updateBusinessTripFlightsExpense.run(params, this.db); } async insertBusinessTripFlightsExpense(params) { params.attendeeIds ||= []; const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext(); return insertBusinessTripFlightsExpense.run(reassureOwnerIdExists(params, ownerId), this.db); } async 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, }), __metadata("design:paramtypes", [TenantAwareDBClient, AdminContextProvider]) ], BusinessTripFlightsExpensesProvider); export { BusinessTripFlightsExpensesProvider }; //# sourceMappingURL=business-trips-expenses-flights.provider.js.map