From b8b58ed0613cf0cb6417cab02f0286d180451ff0 Mon Sep 17 00:00:00 2001
From: Jonathan Perrault
Date: Wed, 16 Oct 2024 16:18:05 +0200
Subject: [PATCH] fix: staff tool for company infos
---
.../core-domain/useCases/SaveDeclaration.ts | 27 +++++++----
.../[siren]/[year]/RecapDeclaration.tsx | 20 +++++---
.../declaration/[siren]/[year]/actions.ts | 46 +++++++++++++++++++
.../validation-transmission/page.tsx | 2 +-
.../design-system/base/RecapCardCompany.tsx | 5 +-
5 files changed, 81 insertions(+), 19 deletions(-)
create mode 100644 packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/actions.ts
diff --git a/packages/app/src/api/core-domain/useCases/SaveDeclaration.ts b/packages/app/src/api/core-domain/useCases/SaveDeclaration.ts
index ed64bb4d9..818574d83 100644
--- a/packages/app/src/api/core-domain/useCases/SaveDeclaration.ts
+++ b/packages/app/src/api/core-domain/useCases/SaveDeclaration.ts
@@ -58,15 +58,24 @@ export class SaveDeclaration implements UseCase {
phone: dto.declarant?.téléphone,
},
company: {
- address: company.address,
- city: company.city,
- countryCode: company.countryCode?.getValue(),
- county: company.county?.getValue(),
- nafCode: company.nafCode?.getValue(),
- name: company.name,
- postalCode: company.postalCode?.getValue(),
- region: company.region?.getValue(),
- siren: company.siren?.getValue(),
+ address: override ? dto.entreprise?.entrepriseDéclarante?.adresse : company.address,
+ city: override ? dto.entreprise?.entrepriseDéclarante?.commune : company.city,
+ countryCode: override ? dto.entreprise?.entrepriseDéclarante?.codePays : company.countryCode?.getValue(),
+ county: override ? dto.entreprise?.entrepriseDéclarante?.département : company.county?.getValue(),
+ nafCode:
+ override && dto.entreprise?.entrepriseDéclarante?.codeNaf
+ ? dto.entreprise?.entrepriseDéclarante?.codeNaf
+ : company.nafCode?.getValue(),
+ name:
+ override && dto?.entreprise?.entrepriseDéclarante?.raisonSociale
+ ? dto.entreprise.entrepriseDéclarante.raisonSociale
+ : company.name,
+ postalCode: override ? dto.entreprise?.entrepriseDéclarante?.codePostal : company.postalCode?.getValue(),
+ region: override ? dto.entreprise?.entrepriseDéclarante?.région : company.region?.getValue(),
+ siren:
+ override && dto.entreprise?.entrepriseDéclarante?.siren
+ ? dto.entreprise?.entrepriseDéclarante?.siren
+ : company.siren?.getValue(),
range: dto.entreprise?.tranche,
total:
dto["periode-reference"]?.périodeSuffisante === "oui" ? dto["periode-reference"].effectifTotal : undefined,
diff --git a/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/RecapDeclaration.tsx b/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/RecapDeclaration.tsx
index 946a3a6e7..df887abe8 100644
--- a/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/RecapDeclaration.tsx
+++ b/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/RecapDeclaration.tsx
@@ -10,10 +10,10 @@ import { type CompanyDTO } from "@common/core-domain/dtos/CompanyDTO";
import { type DeclarationDTO } from "@common/core-domain/dtos/DeclarationDTO";
import { formatIsoToFr } from "@common/utils/date";
import { BigNote, Box, RecapCard, RecapCardCompany } from "@design-system";
-import { useDeclarationFormManager } from "@services/apiClient/useDeclarationFormManager";
+import { useRouter } from "next/navigation";
-import { saveDeclaration } from "../../actions";
import { funnelStaticConfig } from "../../declarationFunnelConfiguration";
+import { updateCompanyInfos } from "./actions";
import { RecapCardIndicator } from "./RecapCardIndicator";
import { RecapCardPublication } from "./RecapCardPublication";
@@ -21,8 +21,7 @@ type Props = { displayTitle?: string; déclaration: DeclarationDTO; edit?: boole
export const RecapDeclaration = ({ déclaration, edit, displayTitle }: Props) => {
const entreprise = déclaration.entreprise?.entrepriseDéclarante;
-
- const { saveFormData } = useDeclarationFormManager();
+ const router = useRouter();
const company: CompanyDTO = {
name: entreprise?.raisonSociale || "",
address: entreprise?.adresse,
@@ -62,8 +61,17 @@ export const RecapDeclaration = ({ déclaration, edit, displayTitle }: Props) =>
},
},
};
- await saveDeclaration(newFormData);
- //saveFormData(newFormData);
+ const isEditingSiren = data.siren != déclaration.entreprise?.entrepriseDéclarante?.siren;
+
+ await updateCompanyInfos(
+ newFormData,
+ isEditingSiren ? déclaration.entreprise?.entrepriseDéclarante?.siren : void 0,
+ );
+
+ if (isEditingSiren) {
+ router.push(`/index-egapro/declaration/${data.siren}/${déclaration.commencer?.annéeIndicateurs}`);
+ }
+ router.refresh();
};
return (
diff --git a/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/actions.ts b/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/actions.ts
new file mode 100644
index 000000000..1dd42bbc5
--- /dev/null
+++ b/packages/app/src/app/(default)/index-egapro/declaration/[siren]/[year]/actions.ts
@@ -0,0 +1,46 @@
+"use server";
+import { entrepriseService } from "@api/core-domain/infra/services";
+import { declarationRepo } from "@api/core-domain/repo";
+import { SaveDeclaration } from "@api/core-domain/useCases/SaveDeclaration";
+import { assertServerSession } from "@api/utils/auth";
+import { DeclarationSpecificationError } from "@common/core-domain/domain/specification/DeclarationSpecification";
+import { Siren } from "@common/core-domain/domain/valueObjects/Siren";
+import { type CreateDeclarationDTO } from "@common/core-domain/dtos/DeclarationDTO";
+import { ValidationError } from "@common/shared-domain";
+import { PositiveNumber } from "@common/shared-domain/domain/valueObjects";
+import { type ServerActionResponse } from "@common/utils/next";
+
+export async function updateCompanyInfos(
+ declaration: CreateDeclarationDTO,
+ oldSiren: string | undefined,
+): Promise> {
+ const session = await assertServerSession({
+ owner: {
+ check: declaration.commencer?.siren || "",
+ message: "Not authorized to save declaration for this Siren.",
+ },
+ staff: true,
+ });
+
+ try {
+ const useCase = new SaveDeclaration(declarationRepo, entrepriseService);
+ await useCase.execute({ declaration, override: session?.user?.staff });
+ if (oldSiren && declaration.commencer?.annéeIndicateurs)
+ await declarationRepo.delete([new Siren(oldSiren), new PositiveNumber(declaration.commencer?.annéeIndicateurs)]);
+
+ return {
+ ok: true,
+ };
+ } catch (error: unknown) {
+ if (error instanceof DeclarationSpecificationError || error instanceof ValidationError) {
+ return {
+ ok: false,
+ error: error.message ?? error.previousError,
+ };
+ }
+ return {
+ ok: false,
+ error: "Une erreur est survenue, veuillez réessayer.",
+ };
+ }
+}
diff --git a/packages/app/src/app/(default)/index-egapro/declaration/validation-transmission/page.tsx b/packages/app/src/app/(default)/index-egapro/declaration/validation-transmission/page.tsx
index 81d90bd20..b8f82e893 100644
--- a/packages/app/src/app/(default)/index-egapro/declaration/validation-transmission/page.tsx
+++ b/packages/app/src/app/(default)/index-egapro/declaration/validation-transmission/page.tsx
@@ -17,7 +17,7 @@ const ResultatGlobalPage = () => {
dispositions de l’article D.1142-5 du code du travail.
- Pour terminer la procédure, cliquez sur “Valider et transmettre les résultats” ci-dessous. Vous recevrez un
+ Pour terminer la procédure, cliquez sur “Valider et transmettre les résultats” ci-dessous. Vous aprecevrez un
accusé de réception.
diff --git a/packages/app/src/design-system/base/RecapCardCompany.tsx b/packages/app/src/design-system/base/RecapCardCompany.tsx
index 54fa466cc..0e3ebb837 100644
--- a/packages/app/src/design-system/base/RecapCardCompany.tsx
+++ b/packages/app/src/design-system/base/RecapCardCompany.tsx
@@ -1,3 +1,4 @@
+"use client";
import { Button } from "@codegouvfr/react-dsfr/Button";
import Input from "@codegouvfr/react-dsfr/Input";
import { createModal } from "@codegouvfr/react-dsfr/Modal";
@@ -58,13 +59,11 @@ export const RecapCardCompany = ({ company, full, title, edit, onSubmit }: Props
const {
register,
handleSubmit,
- getValues,
formState: { isValid, errors },
} = useForm({
resolver: zodResolver(companySchema),
defaultValues: company,
});
- console.log("RecapCardCompany", isValid, errors, getValues());
const handleOnSummit = async (data: CompanyDTO) => {
if (onSubmit) onSubmit(data);
@@ -185,7 +184,7 @@ export const RecapCardCompany = ({ company, full, title, edit, onSubmit }: Props