import { __decorate, __metadata } from "tslib";
import DataLoader from 'dataloader';
import { Injectable, Scope } from 'graphql-modules';
import { sql } from '@pgtyped/runtime';
import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.js';
const getEmployeesByEmployer = sql `
    SELECT *
    FROM accounter_schema.employees
    WHERE employer in $$employerIDs;`;
const getEmployeesById = sql `
    SELECT *
    FROM accounter_schema.employees
    WHERE business_id in $$employeeIDs;`;
let EmployeesProvider = class EmployeesProvider {
    db;
    constructor(db) {
        this.db = db;
    }
    async batchEmployeesByEmployerIDs(employerIDs) {
        if (employerIDs.length) {
            return getEmployeesByEmployer
                .run({ employerIDs }, this.db)
                .then(res => employerIDs.map(employerId => res.filter(employee => employee.employer === employerId)));
        }
        return [];
    }
    getEmployeesByEmployerLoader = new DataLoader((employerIDs) => this.batchEmployeesByEmployerIDs(employerIDs));
    async batchEmployeesByIDs(employeeIDs) {
        if (employeeIDs.length) {
            return getEmployeesById
                .run({ employeeIDs }, this.db)
                .then(res => employeeIDs.map(employeeId => res.find(employee => employee.business_id === employeeId)));
        }
        return [];
    }
    getEmployeesByIdLoader = new DataLoader((employeeIDs) => this.batchEmployeesByIDs(employeeIDs));
    clearCache() {
        this.getEmployeesByEmployerLoader.clearAll();
        this.getEmployeesByIdLoader.clearAll();
    }
};
EmployeesProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient])
], EmployeesProvider);
export { EmployeesProvider };
//# sourceMappingURL=employees.provider.js.map