UNPKG

@accounter/server

Version:
111 lines 4.99 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 getBusinessTripsOtherExpensesByBusinessTripIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_other a LEFT JOIN accounter_schema.extended_business_trip_transactions t USING (id) WHERE ($isBusinessTripIds = 0 OR t.business_trip_id IN $$businessTripIds);`; const getBusinessTripsOtherExpensesByIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_other a LEFT JOIN accounter_schema.extended_business_trip_transactions t USING (id) WHERE ($isIds = 0 OR t.id IN $$expenseIds);`; const updateBusinessTripOtherExpense = sql ` UPDATE accounter_schema.business_trips_transactions_other SET deductible_expense = COALESCE( $deductibleExpense, deductible_expense ), description = COALESCE( $description, description ) WHERE id = $businessTripExpenseId RETURNING *; `; const insertBusinessTripOtherExpense = sql ` INSERT INTO accounter_schema.business_trips_transactions_other (id, deductible_expense, description, owner_id) VALUES($id, $deductibleExpense, $description, $ownerId) RETURNING *;`; const deleteBusinessTripOtherExpense = sql ` DELETE FROM accounter_schema.business_trips_transactions_other WHERE id = $businessTripExpenseId RETURNING id; `; let BusinessTripOtherExpensesProvider = class BusinessTripOtherExpensesProvider { db; adminContextProvider; constructor(db, adminContextProvider) { this.db = db; this.adminContextProvider = adminContextProvider; } async batchBusinessTripsOtherExpensesByBusinessTripIds(businessTripIds) { const businessTripsOtherExpenses = await getBusinessTripsOtherExpensesByBusinessTripIds.run({ isBusinessTripIds: businessTripIds.length > 0 ? 1 : 0, businessTripIds, }, this.db); return businessTripIds.map(id => businessTripsOtherExpenses.filter(record => record.business_trip_id === id)); } getBusinessTripsOtherExpensesByBusinessTripIdLoader = new DataLoader((ids) => this.batchBusinessTripsOtherExpensesByBusinessTripIds(ids)); async batchBusinessTripsOtherExpensesByIds(expenseIds) { const businessTripsOtherExpenses = await getBusinessTripsOtherExpensesByIds.run({ isIds: expenseIds.length > 0 ? 1 : 0, expenseIds, }, this.db); return expenseIds.map(id => businessTripsOtherExpenses.find(record => record.id === id)); } getBusinessTripsOtherExpensesByIdLoader = new DataLoader((ids) => this.batchBusinessTripsOtherExpensesByIds(ids)); async updateBusinessTripOtherExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return updateBusinessTripOtherExpense.run(params, this.db); } async insertBusinessTripOtherExpense(params) { const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext(); return insertBusinessTripOtherExpense.run(reassureOwnerIdExists(params, ownerId), this.db); } async deleteBusinessTripOtherExpense(params) { if (params.businessTripExpenseId) { this.invalidateById(params.businessTripExpenseId); } return deleteBusinessTripOtherExpense.run(params, this.db); } async invalidateById(expenseId) { const expense = await this.getBusinessTripsOtherExpensesByIdLoader.load(expenseId); if (expense?.business_trip_id) { this.getBusinessTripsOtherExpensesByBusinessTripIdLoader.clear(expense.business_trip_id); } this.getBusinessTripsOtherExpensesByIdLoader.clear(expenseId); } async invalidateByBusinessTripId(businessTripId) { const expenses = await this.getBusinessTripsOtherExpensesByBusinessTripIdLoader.load(businessTripId); for (const expense of expenses ?? []) { this.getBusinessTripsOtherExpensesByIdLoader.clear(expense.id); } this.getBusinessTripsOtherExpensesByBusinessTripIdLoader.clear(businessTripId); } clearCache() { this.getBusinessTripsOtherExpensesByBusinessTripIdLoader.clearAll(); this.getBusinessTripsOtherExpensesByIdLoader.clearAll(); } }; BusinessTripOtherExpensesProvider = __decorate([ Injectable({ scope: Scope.Operation, global: true, }), __metadata("design:paramtypes", [TenantAwareDBClient, AdminContextProvider]) ], BusinessTripOtherExpensesProvider); export { BusinessTripOtherExpensesProvider }; //# sourceMappingURL=business-trips-expenses-other.provider.js.map