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 getAllBankDeposits = sql `
  SELECT id, name, currency, account_id, open_date, close_date
  FROM accounter_schema.bank_deposits;`;
const getAllOpenBankDeposits = sql `
  SELECT id, name, currency, account_id, open_date, close_date
  FROM accounter_schema.bank_deposits
  WHERE close_date IS NULL;`;
const getBankDepositsByIds = sql `
  SELECT id, name, currency, account_id, open_date, close_date
  FROM accounter_schema.bank_deposits
  WHERE id IN $$depositIds;`;
const insertBankDeposit = sql `
  INSERT INTO accounter_schema.bank_deposits (name, currency, account_id, open_date, close_date, owner_id)
  VALUES ($name, $currency, $accountId, $openDate, $closeDate, $ownerId)
  RETURNING id, name, currency, account_id, open_date, close_date;`;
const updateBankDeposit = sql `
  UPDATE accounter_schema.bank_deposits
  SET name       = $name,
      currency   = COALESCE($currency, currency),
      open_date  = $openDate,
      close_date = $closeDate,
      account_id = $accountId
  WHERE id = $depositId
  RETURNING id, name, currency, account_id, open_date, close_date;`;
let BankDepositsProvider = class BankDepositsProvider {
    db;
    adminContextProvider;
    constructor(db, adminContextProvider) {
        this.db = db;
        this.adminContextProvider = adminContextProvider;
    }
    allBankDepositsCache = null;
    getAllBankDeposits() {
        if (this.allBankDepositsCache) {
            return this.allBankDepositsCache;
        }
        this.allBankDepositsCache = getAllBankDeposits.run(undefined, this.db).then(deposits => {
            for (const deposit of deposits) {
                this.bankDepositByIdLoader.prime(deposit.id, deposit);
            }
            return deposits;
        });
        return this.allBankDepositsCache;
    }
    allOpenBankDepositsCache = null;
    getAllOpenBankDeposits() {
        if (this.allOpenBankDepositsCache) {
            return this.allOpenBankDepositsCache;
        }
        this.allOpenBankDepositsCache = getAllOpenBankDeposits
            .run(undefined, this.db)
            .then(deposits => {
            for (const deposit of deposits) {
                this.bankDepositByIdLoader.prime(deposit.id, deposit);
            }
            return deposits;
        });
        return this.allOpenBankDepositsCache;
    }
    async batchBankDepositsByIds(depositIds) {
        const deposits = await getBankDepositsByIds.run({ depositIds }, this.db);
        return depositIds.map(id => deposits.find(d => d.id === id) ?? null);
    }
    bankDepositByIdLoader = new DataLoader((keys) => this.batchBankDepositsByIds(keys));
    async insertBankDeposit(params) {
        this.clearCache();
        const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext();
        const [result] = await insertBankDeposit.run(reassureOwnerIdExists(params, ownerId), this.db);
        if (result) {
            this.bankDepositByIdLoader.prime(result.id, result);
        }
        return result;
    }
    async updateBankDeposit(params) {
        const [result] = await updateBankDeposit.run(params, this.db);
        if (result) {
            this.bankDepositByIdLoader.clear(result.id).prime(result.id, result);
            this.allBankDepositsCache = null;
            this.allOpenBankDepositsCache = null;
        }
        return result ?? null;
    }
    clearCache() {
        this.bankDepositByIdLoader.clearAll();
        this.allBankDepositsCache = null;
        this.allOpenBankDepositsCache = null;
    }
};
BankDepositsProvider = __decorate([
    Injectable({
        scope: Scope.Operation,
        global: true,
    }),
    __metadata("design:paramtypes", [TenantAwareDBClient,
        AdminContextProvider])
], BankDepositsProvider);
export { BankDepositsProvider };
//# sourceMappingURL=bank-deposits.provider.js.map