@accounter/server
Version:
Accounter GraphQL server
129 lines • 5.17 kB
JavaScript
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 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, owner_id)
VALUES($businessTripExpenseId, $chargeId, $date, $valueDate, $amount, $currency, $employeeBusinessId, $ownerId)
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 {
db;
adminContextProvider;
constructor(db, adminContextProvider) {
this.db = db;
this.adminContextProvider = adminContextProvider;
}
async batchBusinessTripEmployeePaymentsByChargeIds(chargeIds) {
const businessTrips = await getBusinessTripEmployeePaymentsByChargeIds.run({
chargeIds,
}, this.db);
return chargeIds.map(id => businessTrips.filter(record => record.charge_id === id));
}
getBusinessTripEmployeePaymentsByChargeIdLoader = new DataLoader((ids) => this.batchBusinessTripEmployeePaymentsByChargeIds(ids));
async updateBusinessTripEmployeePayment(params) {
// clear cache for old chargeId
const [payment] = await getBusinessTripEmployeePaymentById.run(params, this.db);
if (!payment) {
throw new Error('Business trip employee payment not found');
}
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clear(payment.charge_id);
// clear cache for new chargeId
if (params.chargeId) {
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clear(params.chargeId);
}
return updateBusinessTripEmployeePayment.run(params, this.db);
}
async replaceBusinessTripsEmployeePaymentsChargeId(params) {
if (params.assertChargeID) {
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clear(params.assertChargeID);
}
if (params.replaceChargeID) {
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clear(params.replaceChargeID);
}
return replaceBusinessTripsEmployeePaymentsChargeId.run(params, this.db);
}
async insertBusinessTripEmployeePayment(params) {
if (params.chargeId) {
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clear(params.chargeId);
}
const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
return insertBusinessTripEmployeePayment.run(reassureOwnerIdExists(params, ownerId), this.db);
}
async deleteBusinessTripEmployeePayment(params) {
// clear cache for old chargeId
const [payment] = await getBusinessTripEmployeePaymentById.run(params, this.db);
if (!payment) {
throw new Error('Business trip employee payment not found');
}
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clear(payment.charge_id);
return deleteBusinessTripEmployeePayment.run(params, this.db);
}
clearCache() {
this.getBusinessTripEmployeePaymentsByChargeIdLoader.clearAll();
}
};
BusinessTripEmployeePaymentsProvider = __decorate([
Injectable({
scope: Scope.Operation,
global: true,
}),
__metadata("design:paramtypes", [TenantAwareDBClient,
AdminContextProvider])
], BusinessTripEmployeePaymentsProvider);
export { BusinessTripEmployeePaymentsProvider };
//# sourceMappingURL=business-trips-employee-payments.provider.js.map