UNPKG

@accounter/server

Version:
96 lines 4.13 kB
import { __decorate, __metadata } from "tslib"; import { Injectable, Scope } from 'graphql-modules'; import { sql } from '@pgtyped/runtime'; import { UUID_REGEX } from '../../../shared/constants.js'; import { makeUUID } from '../../../shared/helpers/deterministic-uuid.js'; import { TenantAwareDBClient } from '../../app-providers/tenant-db-client.js'; const VALID_ENTITY_TYPES = ['business', 'tax_category', 'tag']; const ensureFinancialEntity = sql ` INSERT INTO accounter_schema.financial_entities (id, name, type, owner_id) VALUES ($id, $name, $type, $ownerId) ON CONFLICT (id) DO NOTHING; `; const ensureBusiness = sql ` INSERT INTO accounter_schema.businesses ( id, hebrew_name, address, city, zip_code, email, website, phone_number, vat_number, exempt_dealer, optional_vat, country, can_settle_with_receipt, no_invoices_required, owner_id ) VALUES ( $id, $hebrewName, $address, $city, $zipCode, $email, $website, $phoneNumber, $governmentId, $exemptDealer, $optionalVat, $country, $isReceiptEnough, $isDocumentsOptional, $ownerId ) ON CONFLICT (id) DO NOTHING; `; const ensureTaxCategory = sql ` INSERT INTO accounter_schema.tax_categories (id, hashavshevet_name, tax_excluded, owner_id) VALUES ($id, $hashavshevetName, $taxExcluded, $ownerId) ON CONFLICT (id) DO NOTHING; `; const ensureCountry = sql ` INSERT INTO accounter_schema.countries (code, name) VALUES ($code, $name) ON CONFLICT (code) DO NOTHING; `; let EntityEnsureProvider = class EntityEnsureProvider { db; constructor(db) { this.db = db; } async ensureFinancialEntity(params, client) { const { name, type, ownerId, id: originId } = params; if (!name || name.trim() === '') { throw new Error('ensureFinancialEntity: name is required'); } if (!VALID_ENTITY_TYPES.includes(type)) { throw new Error(`ensureFinancialEntity: invalid type "${type}"`); } const compositeKey = ownerId ? `${name}:owner=${ownerId}` : name; const id = originId ?? makeUUID(type, compositeKey); await ensureFinancialEntity.run({ id, name, type, ownerId: ownerId ?? null }, client ?? this.db); return { id }; } async ensureBusinessForEntity(entityId, options, client) { if (!UUID_REGEX.test(entityId)) { throw new Error(`ensureBusinessForEntity: invalid entityId "${entityId}"`); } await ensureBusiness.run({ id: entityId, hebrewName: options?.hebrewName ?? null, address: options?.address ?? null, city: options?.city ?? null, zipCode: options?.zipCode ?? null, email: options?.email ?? null, website: options?.website ?? null, phoneNumber: options?.phoneNumber ?? null, governmentId: options?.governmentId ?? null, exemptDealer: options?.exemptDealer ?? false, optionalVat: options?.optionalVat ?? false, country: options?.country ?? 'ISR', isReceiptEnough: options?.isReceiptEnough ?? false, isDocumentsOptional: options?.isDocumentsOptional ?? false, ownerId: options?.ownerId ?? null, }, client ?? this.db); } async ensureTaxCategoryForEntity(entityId, options, client) { if (!UUID_REGEX.test(entityId)) { throw new Error(`ensureTaxCategoryForEntity: invalid entityId "${entityId}"`); } await ensureTaxCategory.run({ id: entityId, hashavshevetName: options?.hashavshevetName ?? null, taxExcluded: options?.taxExcluded ?? false, ownerId: options?.ownerId ?? null, }, client ?? this.db); } async ensureCountry(code, name, client) { await ensureCountry.run({ code, name }, client ?? this.db); } }; EntityEnsureProvider = __decorate([ Injectable({ scope: Scope.Operation, global: true }), __metadata("design:paramtypes", [TenantAwareDBClient]) ], EntityEnsureProvider); export { EntityEnsureProvider }; //# sourceMappingURL=entity-ensure.provider.js.map