UNPKG

@accounter/server

Version:
101 lines (89 loc) 2.95 kB
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'; import type { IGetAllSortCodesQuery, IGetAllSortCodesResult, IGetSortCodesByIdsQuery, IInsertSortCodeParams, IInsertSortCodeQuery, IUpdateSortCodeParams, IUpdateSortCodeQuery, } from '../types.js'; const getAllSortCodes = sql<IGetAllSortCodesQuery>` SELECT * FROM accounter_schema.sort_codes`; const getSortCodesByIds = sql<IGetSortCodesByIdsQuery>` SELECT sc.* FROM accounter_schema.sort_codes sc WHERE ($isSortCodesIds = 0 OR sc.key IN $$sortCodesIds);`; const insertSortCode = sql<IInsertSortCodeQuery>` INSERT INTO accounter_schema.sort_codes (name, key, default_irs_code, owner_id) VALUES ($name, $key, $defaultIrsCode, $ownerId) RETURNING *; `; const updateSortCode = sql<IUpdateSortCodeQuery>` UPDATE accounter_schema.sort_codes SET name = COALESCE( $name, name ), default_irs_code = COALESCE( $defaultIrsCode, default_irs_code ) WHERE key = $key RETURNING *; `; @Injectable({ scope: Scope.Operation, global: true, }) export class SortCodesProvider { constructor( private db: TenantAwareDBClient, private adminContextProvider: AdminContextProvider, ) {} private allSortCodesCache: Promise<IGetAllSortCodesResult[]> | null = null; public getAllSortCodes() { if (this.allSortCodesCache) { return this.allSortCodesCache; } this.allSortCodesCache = getAllSortCodes.run(undefined, this.db).then(data => { data.map(sortCode => { this.getSortCodesByIdLoader.prime(sortCode.key, sortCode); }); return data; }); return this.allSortCodesCache; } private async batchSortCodesByIds(sortCodesIds: readonly number[]) { const ledgerRecords = await getSortCodesByIds.run( { isSortCodesIds: sortCodesIds.length > 0 ? 1 : 0, sortCodesIds, }, this.db, ); return sortCodesIds.map(id => ledgerRecords.find(record => record.key === id)); } public getSortCodesByIdLoader = new DataLoader((keys: readonly number[]) => this.batchSortCodesByIds(keys), ); public async addSortCode(params: IInsertSortCodeParams) { this.clearCache(); const { ownerId } = await this.adminContextProvider.getVerifiedAdminContext(); return insertSortCode.run(reassureOwnerIdExists(params, ownerId), this.db); } public async updateSortCode(params: IUpdateSortCodeParams) { this.clearCache(); return updateSortCode.run(params, this.db); } public clearCache() { this.getSortCodesByIdLoader.clearAll(); this.allSortCodesCache = null; } }