Skip to content

Commit

Permalink
fix: staff tool for company infos
Browse files Browse the repository at this point in the history
  • Loading branch information
jonat75 committed Oct 16, 2024
1 parent b812371 commit b8b58ed
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
27 changes: 18 additions & 9 deletions packages/app/src/api/core-domain/useCases/SaveDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,24 @@ export class SaveDeclaration implements UseCase<Input, void> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ 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";

type Props = { displayTitle?: string; déclaration: DeclarationDTO; edit?: boolean };

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,
Expand Down Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ServerActionResponse<undefined, string>> {
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.",
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ResultatGlobalPage = () => {
dispositions de l’article D.1142-5 du code du travail.
<br />
<br />
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.
</p>

Expand Down
5 changes: 2 additions & 3 deletions packages/app/src/design-system/base/RecapCardCompany.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -58,13 +59,11 @@ export const RecapCardCompany = ({ company, full, title, edit, onSubmit }: Props
const {
register,
handleSubmit,
getValues,
formState: { isValid, errors },
} = useForm<CompanyDTO>({
resolver: zodResolver(companySchema),
defaultValues: company,
});
console.log("RecapCardCompany", isValid, errors, getValues());

const handleOnSummit = async (data: CompanyDTO) => {
if (onSubmit) onSubmit(data);
Expand Down Expand Up @@ -185,7 +184,7 @@ export const RecapCardCompany = ({ company, full, title, edit, onSubmit }: Props
<RecapCard
title={fullTitle}
sideButtonProps={{
iconId: "fr-icon-information-fill",
iconId: "fr-icon-edit-line",
title: titleFull,
priority: "tertiary no outline",
style: { alignSelf: "center" },
Expand Down

0 comments on commit b8b58ed

Please sign in to comment.