UNPKG

@accounter/server

Version:
91 lines 4.63 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 getBusinessTripsExpenseMatchesByTransactionIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_match WHERE transaction_id in $$transactionIds;`; const getBusinessTripsExpenseMatchesByExpenseIds = sql ` SELECT * FROM accounter_schema.business_trips_transactions_match WHERE business_trip_transaction_id in $$expenseIds;`; const insertBusinessTripExpenseMatch = sql ` INSERT INTO accounter_schema.business_trips_transactions_match (business_trip_transaction_id, transaction_id, amount, owner_id) VALUES($businessTripExpenseId, $transactionId, $amount, $ownerId) RETURNING *;`; const deleteBusinessTripExpenseMatch = sql ` DELETE FROM accounter_schema.business_trips_transactions_match WHERE business_trip_transaction_id = $businessTripExpenseId RETURNING *;`; const deleteSpecificBusinessTripExpenseMatch = sql ` DELETE FROM accounter_schema.business_trips_transactions_match WHERE business_trip_transaction_id = $businessTripExpenseId AND transaction_id = $transactionId RETURNING *;`; let BusinessTripExpensesTransactionsMatchProvider = class BusinessTripExpensesTransactionsMatchProvider { db; context; constructor(db, context) { this.db = db; this.context = context; } async batchBusinessTripsExpenseMatchesByTransactionIds(transactionIds) { const businessTrips = await getBusinessTripsExpenseMatchesByTransactionIds.run({ transactionIds: transactionIds, }, this.db); return transactionIds.map(id => businessTrips.filter(record => record.transaction_id === id)); } getBusinessTripsExpenseMatchesByTransactionIdLoader = new DataLoader((ids) => this.batchBusinessTripsExpenseMatchesByTransactionIds(ids)); async batchBusinessTripsExpenseMatchesByExpenseIds(expenseIds) { const businessTrips = await getBusinessTripsExpenseMatchesByExpenseIds.run({ expenseIds: expenseIds, }, this.db); return expenseIds.map(id => businessTrips.filter(record => record.business_trip_transaction_id === id)); } getBusinessTripsExpenseMatchesByExpenseIdLoader = new DataLoader((ids) => this.batchBusinessTripsExpenseMatchesByExpenseIds(ids)); insertBusinessTripExpenseMatch(params) { if (params.businessTripExpenseId) { this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clear(params.businessTripExpenseId); } if (params.transactionId) { this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clear(params.transactionId); } return insertBusinessTripExpenseMatch.run(reassureOwnerIdExists(params, this.context), this.db); } async deleteBusinessTripExpenseMatch(params) { if (params.businessTripExpenseId) { const expenses = await this.getBusinessTripsExpenseMatchesByExpenseIdLoader.load(params.businessTripExpenseId); expenses.map(expense => { this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clear(expense.transaction_id); }); this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clear(params.businessTripExpenseId); } return deleteBusinessTripExpenseMatch.run(params, this.db); } deleteSpecificBusinessTripExpenseMatch(params) { if (params.businessTripExpenseId) { this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clear(params.businessTripExpenseId); } if (params.transactionId) { this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clear(params.transactionId); } return deleteSpecificBusinessTripExpenseMatch.run(params, this.db); } clearCache() { this.getBusinessTripsExpenseMatchesByTransactionIdLoader.clearAll(); this.getBusinessTripsExpenseMatchesByExpenseIdLoader.clearAll(); } }; BusinessTripExpensesTransactionsMatchProvider = __decorate([ Injectable({ scope: Scope.Operation, global: true, }), __param(1, Inject(CONTEXT)), __metadata("design:paramtypes", [TenantAwareDBClient, Object]) ], BusinessTripExpensesTransactionsMatchProvider); export { BusinessTripExpensesTransactionsMatchProvider }; //# sourceMappingURL=business-trips-expenses-transactions-match.provider.js.map