Skip to content

Commit

Permalink
feat: staff can edit declaration if older than one year (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturlg authored Mar 8, 2024
1 parent d3ebb40 commit ce4d616
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/app/src/api/core-domain/useCases/SaveDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { type IDeclarationRepo } from "../repo/IDeclarationRepo";

interface Input {
declaration: CreateDeclarationDTO;
override?: boolean;
}

export class SaveDeclaration implements UseCase<Input, void> {
Expand All @@ -31,7 +32,7 @@ export class SaveDeclaration implements UseCase<Input, void> {
private readonly entrepriseService: IEntrepriseService,
) {}

public async execute({ declaration: dto }: Input): Promise<void> {
public async execute({ declaration: dto, override }: Input): Promise<void> {
const now = new Date();

const siren = dto.entreprise?.entrepriseDéclarante?.siren;
Expand Down Expand Up @@ -242,7 +243,7 @@ export class SaveDeclaration implements UseCase<Input, void> {
if (found) {
const olderThanOneYear = isAfter(new Date(), add(found.declaredAt, { years: 1 }));

if (olderThanOneYear) {
if (olderThanOneYear && !override) {
throw new SaveDeclarationOverOneYearError("Déclaration is older than one year.");
}
declaration = Declaration.fromJson({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import { config } from "@common/config";
import { useHasMounted } from "@components/utils/ClientOnly";
import { useDeclarationFormManager } from "@services/apiClient/useDeclarationFormManager";
import { add, isAfter } from "date-fns";
import { useSession } from "next-auth/react";

export const AlertExistingDeclaration = () => {
const { formData } = useDeclarationFormManager();
const hasMounted = useHasMounted();
const session = useSession();

const declarationDate = formData["declaration-existante"].date;

if (!hasMounted || !declarationDate) return null;

const olderThanOneYear = isAfter(new Date(), add(new Date(declarationDate), { years: 1 }));
const olderThanOneYear = session?.data?.staff
? false
: isAfter(new Date(), add(new Date(declarationDate), { years: 1 }));

return (
<Alert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BackNextButtonsGroup } from "@design-system";
import { useDeclarationFormManager } from "@services/apiClient/useDeclarationFormManager";
import { add, isAfter } from "date-fns";
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
import { type PropsWithChildren } from "react";

import { prepareDataWithExistingDeclaration } from "../../commencer/CommencerForm";
Expand All @@ -18,12 +19,15 @@ type Props = {
};

export const EditButton = ({ déclaration, year }: PropsWithChildren<Props>) => {
const session = useSession();
const router = useRouter();
const { setStatus, saveFormData, resetFormData } = useDeclarationFormManager();

const date = déclaration["declaration-existante"].date;

const olderThanOneYear = date === undefined || isAfter(new Date(), add(new Date(date), { years: 1 }));
const olderThanOneYear = session?.data?.staff
? false
: date === undefined || isAfter(new Date(), add(new Date(date), { years: 1 }));
const saveAndGoNext = async (
{ siren, annéeIndicateurs }: { annéeIndicateurs: number; siren: string },
formData: DeclarationDTO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ const RecapPage = async ({ params: { siren, year: strYear } }: NextServerPagePro
const canEdit = canEditSiren(session?.user)(siren);

if (!declarationDate) return <SkeletonForm fields={8} />;
const olderThanOneYear = isAfter(new Date(), add(new Date(declarationDate), { years: 1 }));
const olderThanOneYear = session?.user?.staff
? false
: isAfter(new Date(), add(new Date(declarationDate), { years: 1 }));

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function getDeclaration(siren: string, year: number) {
export async function saveDeclaration(
declaration: CreateDeclarationDTO,
): Promise<ServerActionResponse<undefined, string>> {
await assertServerSession({
const session = await assertServerSession({
owner: {
check: declaration.commencer?.siren || "",
message: "Not authorized to save declaration for this Siren.",
Expand All @@ -48,7 +48,7 @@ export async function saveDeclaration(

try {
const useCase = new SaveDeclaration(declarationRepo, entrepriseService);
await useCase.execute({ declaration });
await useCase.execute({ declaration, override: session?.user?.staff });

const receiptUseCase = new SendDeclarationReceipt(declarationRepo, globalMailerService, jsxPdfService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ const RepEqPage = async ({ params: { siren, year: strYear } }: NextServerPagePro
const session = await getServerSession(authConfig);
const isOwner = !!session?.user.companies.some(company => company.siren === siren) || session?.user.staff;

const olderThanOneYear = isAfter(new Date(), add(new Date(repEq.declaredAt), { years: 1 }));
const olderThanOneYear = session?.user?.staff
? false
: isAfter(new Date(), add(new Date(repEq.declaredAt), { years: 1 }));
const monCompteProHost = monCompteProProvider.issuer;

return (
Expand Down

0 comments on commit ce4d616

Please sign in to comment.