From df79ef60eb5ec67e31fe46b9f9300a8fac99895f Mon Sep 17 00:00:00 2001 From: devthejo Date: Tue, 28 Jan 2025 16:20:03 +0100 Subject: [PATCH] fix: #2407 --- packages/app/src/api/core-domain/repo/IReferentRepo.ts | 2 ++ .../api/core-domain/repo/impl/PostgresReferentRepo.ts | 9 +++++++++ .../api/core-domain/useCases/SendDeclarationReceipt.ts | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/app/src/api/core-domain/repo/IReferentRepo.ts b/packages/app/src/api/core-domain/repo/IReferentRepo.ts index 4964ab5c5..c77139185 100644 --- a/packages/app/src/api/core-domain/repo/IReferentRepo.ts +++ b/packages/app/src/api/core-domain/repo/IReferentRepo.ts @@ -1,8 +1,10 @@ import { type Referent } from "@common/core-domain/domain/Referent"; +import { type County } from "@common/core-domain/domain/valueObjects/County"; import { type Region } from "@common/core-domain/domain/valueObjects/Region"; import { type BulkRepo } from "@common/shared-domain"; export interface IReferentRepo extends BulkRepo { + getOneByCounty(county?: County): Promise; getOneByRegion(region?: Region): Promise; truncate(): Promise; } diff --git a/packages/app/src/api/core-domain/repo/impl/PostgresReferentRepo.ts b/packages/app/src/api/core-domain/repo/impl/PostgresReferentRepo.ts index 99c4c1a10..323b19815 100644 --- a/packages/app/src/api/core-domain/repo/impl/PostgresReferentRepo.ts +++ b/packages/app/src/api/core-domain/repo/impl/PostgresReferentRepo.ts @@ -1,6 +1,7 @@ import { type ReferentRaw } from "@api/core-domain/infra/db/raw"; import { sql } from "@api/shared-domain/infra/db/postgres"; import { type Referent } from "@common/core-domain/domain/Referent"; +import { type County } from "@common/core-domain/domain/valueObjects/County"; import { type Region } from "@common/core-domain/domain/valueObjects/Region"; import { referentMap } from "@common/core-domain/mappers/referentMap"; import { UnexpectedRepositoryError } from "@common/shared-domain"; @@ -55,6 +56,14 @@ export class PostgresReferentRepo implements IReferentRepo { return referentMap.toDomain(raw); } + public async getOneByCounty(county?: County): Promise { + if (!county) return null; + const [raw] = await this.sql`select * from ${this.table} where county = ${county.getValue()} limit 1`; + + if (!raw) return null; + return referentMap.toDomain(raw); + } + public async save(item: Referent): Promise { const raw = referentMap.toPersistence(item); diff --git a/packages/app/src/api/core-domain/useCases/SendDeclarationReceipt.ts b/packages/app/src/api/core-domain/useCases/SendDeclarationReceipt.ts index 3673ab9aa..b63286014 100644 --- a/packages/app/src/api/core-domain/useCases/SendDeclarationReceipt.ts +++ b/packages/app/src/api/core-domain/useCases/SendDeclarationReceipt.ts @@ -34,7 +34,7 @@ export class SendDeclarationReceipt implements UseCase { throw new SendDeclarationReceiptNotFoundError(`No declaration found with siren ${siren} and year ${year}`); } - const referent = await this.referentRepo.getOneByRegion(declaration.declaration?.company?.region); + const referent = await this.referentRepo.getOneByCounty(declaration.declaration?.company?.county); const buffer = await this.jsxPdfService.buffer(DeclarationReceipt(declaration)); const url = `${config.host}/index-egapro/declaration/${siren}/${year}`;