@accounter/server
Version:
The test suite is split into three Vitest projects for efficiency and isolation:
96 lines • 4.63 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { getCacheInstance } from '../../../shared/helpers/index.js';
import { DBProvider } from '../../app-providers/db.provider.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)
VALUES($businessTripExpenseId, $transactionId, $amount)
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 {
dbProvider;
cache = getCacheInstance({
stdTTL: 60 * 5,
});
constructor(dbProvider) {
this.dbProvider = dbProvider;
}
async batchBusinessTripsExpenseMatchesByTransactionIds(transactionIds) {
const businessTrips = await getBusinessTripsExpenseMatchesByTransactionIds.run({
transactionIds: transactionIds,
}, this.dbProvider);
return transactionIds.map(id => businessTrips.filter(record => record.transaction_id === id));
}
getBusinessTripsExpenseMatchesByTransactionIdLoader = new DataLoader((ids) => this.batchBusinessTripsExpenseMatchesByTransactionIds(ids), {
cacheKeyFn: key => `business-trip-expense-match-transaction-${key}`,
cacheMap: this.cache,
});
async batchBusinessTripsExpenseMatchesByExpenseIds(expenseIds) {
const businessTrips = await getBusinessTripsExpenseMatchesByExpenseIds.run({
expenseIds: expenseIds,
}, this.dbProvider);
return expenseIds.map(id => businessTrips.filter(record => record.business_trip_transaction_id === id));
}
getBusinessTripsExpenseMatchesByExpenseIdLoader = new DataLoader((ids) => this.batchBusinessTripsExpenseMatchesByExpenseIds(ids), {
cacheKeyFn: key => `business-trip-expense-${key}-match`,
cacheMap: this.cache,
});
insertBusinessTripExpenseMatch(params) {
if (params.businessTripExpenseId) {
this.cache.delete(`business-trip-expense-${params.businessTripExpenseId}-match`);
}
if (params.transactionId) {
this.cache.delete(`business-trip-expense-match-transaction-${params.transactionId}`);
}
return insertBusinessTripExpenseMatch.run(params, this.dbProvider);
}
async deleteBusinessTripExpenseMatch(params) {
if (params.businessTripExpenseId) {
const expenses = await this.getBusinessTripsExpenseMatchesByExpenseIdLoader.load(params.businessTripExpenseId);
expenses.map(expense => {
this.cache.delete(`business-trip-expense-match-transaction-${expense.transaction_id}`);
});
this.cache.delete(`business-trip-expense-${params.businessTripExpenseId}-match`);
}
return deleteBusinessTripExpenseMatch.run(params, this.dbProvider);
}
deleteSpecificBusinessTripExpenseMatch(params) {
if (params.businessTripExpenseId) {
this.cache.delete(`business-trip-expense-${params.businessTripExpenseId}-match`);
}
if (params.transactionId) {
this.cache.delete(`business-trip-expense-match-transaction-${params.transactionId}`);
}
return deleteSpecificBusinessTripExpenseMatch.run(params, this.dbProvider);
}
clearCache() {
this.cache.clear();
}
};
BusinessTripExpensesTransactionsMatchProvider = __decorate([
Injectable({
scope: Scope.Singleton,
global: true,
}),
__metadata("design:paramtypes", [DBProvider])
], BusinessTripExpensesTransactionsMatchProvider);
export { BusinessTripExpensesTransactionsMatchProvider };
//# sourceMappingURL=business-trips-expenses-transactions-match.provider.js.map