UNPKG

@accounter/server

Version:

130 lines 5.01 kB
import { __decorate, __metadata } from "tslib"; import DataLoader from 'dataloader'; import { Injectable, Scope } from 'graphql-modules'; import { DBProvider } from '../../app-providers/db.provider.js'; import { sql } from '@pgtyped/runtime'; import { getCacheInstance } from '../../../shared/helpers/index.js'; const getBusinessTripEmployeePaymentsByChargeIds = sql ` SELECT btep.*, btt.business_trip_id, btt.category FROM accounter_schema.business_trips_employee_payments btep LEFT JOIN accounter_schema.business_trips_transactions btt ON btt.id = btep.id WHERE btep.charge_id IN $$chargeIds;`; const getBusinessTripEmployeePaymentById = sql ` SELECT * FROM accounter_schema.business_trips_employee_payments WHERE id = $businessTripExpenseId;`; const updateBusinessTripEmployeePayment = sql ` UPDATE accounter_schema.business_trips_employee_payments SET charge_id = COALESCE( $chargeId, charge_id ), date = COALESCE( $date, date ), value_date = COALESCE( $valueDate, value_date ), amount = COALESCE( $amount, amount ), currency = COALESCE( $currency, currency ), employee_business_id = COALESCE( $employeeBusinessId, employee_business_id ) WHERE id = $businessTripExpenseId RETURNING *; `; const insertBusinessTripEmployeePayment = sql ` INSERT INTO accounter_schema.business_trips_employee_payments (id, charge_id, date, value_date, amount, currency, employee_business_id) VALUES($businessTripExpenseId, $chargeId, $date, $valueDate, $amount, $currency, $employeeBusinessId) RETURNING *;`; const deleteBusinessTripEmployeePayment = sql ` DELETE FROM accounter_schema.business_trips_employee_payments WHERE id = $businessTripExpenseId RETURNING id; `; const replaceBusinessTripsEmployeePaymentsChargeId = sql ` UPDATE accounter_schema.business_trips_employee_payments SET charge_id = $assertChargeID WHERE charge_id = $replaceChargeID RETURNING *; `; let BusinessTripEmployeePaymentsProvider = class BusinessTripEmployeePaymentsProvider { dbProvider; cache = getCacheInstance({ stdTTL: 60 * 5, }); constructor(dbProvider) { this.dbProvider = dbProvider; } async batchBusinessTripEmployeePaymentsByChargeIds(chargeIds) { const businessTrips = await getBusinessTripEmployeePaymentsByChargeIds.run({ chargeIds, }, this.dbProvider); return chargeIds.map(id => businessTrips.filter(record => record.charge_id === id)); } getBusinessTripEmployeePaymentsByChargeIdLoader = new DataLoader((ids) => this.batchBusinessTripEmployeePaymentsByChargeIds(ids), { cacheKeyFn: key => `business-trip-employee-payment-by-charge-${key}`, cacheMap: this.cache, }); async updateBusinessTripEmployeePayment(params) { // clear cache for old chargeId const [payment] = await getBusinessTripEmployeePaymentById.run(params, this.dbProvider); if (!payment) { throw new Error('Business trip employee payment not found'); } this.cache.delete(`business-trip-employee-payment-by-charge-${payment.charge_id}`); // clear cache for new chargeId if (params.chargeId) { this.cache.delete(`business-trip-employee-payment-by-charge-${params.chargeId}`); } return updateBusinessTripEmployeePayment.run(params, this.dbProvider); } replaceBusinessTripsEmployeePaymentsChargeId(params) { if (params.assertChargeID) { this.cache.delete(`business-trip-employee-payment-by-charge-${params.assertChargeID}`); } if (params.replaceChargeID) { this.cache.delete(`business-trip-employee-payment-by-charge-${params.replaceChargeID}`); } return replaceBusinessTripsEmployeePaymentsChargeId.run(params, this.dbProvider); } insertBusinessTripEmployeePayment(params) { if (params.chargeId) { this.cache.delete(`business-trip-employee-payment-by-charge-${params.chargeId}`); } return insertBusinessTripEmployeePayment.run(params, this.dbProvider); } async deleteBusinessTripEmployeePayment(params) { // clear cache for old chargeId const [payment] = await getBusinessTripEmployeePaymentById.run(params, this.dbProvider); if (!payment) { throw new Error('Business trip employee payment not found'); } this.cache.delete(`business-trip-employee-payment-by-charge-${payment.charge_id}`); return deleteBusinessTripEmployeePayment.run(params, this.dbProvider); } clearCache() { this.cache.clear(); } }; BusinessTripEmployeePaymentsProvider = __decorate([ Injectable({ scope: Scope.Singleton, global: true, }), __metadata("design:paramtypes", [DBProvider]) ], BusinessTripEmployeePaymentsProvider); export { BusinessTripEmployeePaymentsProvider }; //# sourceMappingURL=business-trips-employee-payments.provider.js.map