Skip to content

Commit

Permalink
fix: declaration list (#2344)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonat75 authored Aug 14, 2024
1 parent 70fbb0f commit 76c1420
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ jobs:
yarn setup-python
- name: Up for tests API
run: docker-compose up --build -d api db test_db
run: docker compose up --build -d api db test_db

- name: Check types
run: yarn check-all

- name: Down for tests API
run: docker-compose down
run: docker compose down

# TODO reenable. Next13 is not ready yet for proper testing
# + using a mix between pageDir and appDir make tests harder to dev
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/api/core-domain/repo/IDeclarationRepo.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type DeclarationRaw } from "@api/core-domain/infra/db/DeclarationRaw";
import { type Declaration, type DeclarationPK } from "@common/core-domain/domain/Declaration";
import { type DeclarationOpmc } from "@common/core-domain/domain/DeclarationOpmc";
import { type Siren } from "@common/core-domain/domain/valueObjects/Siren";
Expand All @@ -6,6 +7,7 @@ import { type Repo } from "@common/shared-domain";
export interface IDeclarationRepo extends Repo<Declaration> {
count(): Promise<number>;
getAllBySiren(siren: Siren): Promise<Declaration[]>;
getAllSirenAndYear(): Promise<Array<Partial<DeclarationRaw>>>;
getOneDeclarationOpmc(id: DeclarationPK): Promise<DeclarationOpmc | null>;
limit(limit?: number): this;
/** @deprecated - use saveWithIndex */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export class PostgresDeclarationRepo implements IDeclarationRepo {

return raws.map(declarationMap.toDomain);
}

public async getAllSirenAndYear(): Promise<Array<Partial<DeclarationRaw>>> {
const raws = await this.sql`select * from ${this.table} where data notnull ${this.postgresLimit}`;

return raws; //.map(declarationMap.toDomain);
}
public async getOne([siren, year]: DeclarationPK): Promise<Declaration | null> {
try {
const [raw] = await this.sql`select * from ${
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type DeclarationRaw } from "@api/core-domain/infra/db/DeclarationRaw";
import { type IDeclarationRepo } from "@api/core-domain/repo/IDeclarationRepo";
import { AppError, type UseCase } from "@common/shared-domain";

export class GetAllDeclarations implements UseCase<never, Array<Partial<DeclarationRaw>>> {
constructor(private readonly declarationRepo: IDeclarationRepo) {}

public async execute(): Promise<Array<Partial<DeclarationRaw>>> {
try {
return await this.declarationRepo.getAllSirenAndYear();

// return declarations.map(declarationMap.toDTO).filter(d => d) as DeclarationDTO[];
} catch (error: unknown) {
throw new GetAllDeclarationsError("Cannot get all declarations", error as Error);
}
}
}

export class GetAllDeclarationsError extends AppError {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const AccessibilityStatement = () => {
)
</p>
<h2>Etat de conformité</h2>
<p>Egapro est non conforme avec le RGAA. Le site n’a encore pas été audité.</p>
<p>Egapro est partiellement conforme.</p>
<p>
Un audit{" "}
<Link
Expand Down
18 changes: 6 additions & 12 deletions packages/app/src/app/(default)/login/MonCompteProLogin.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
"use client";

import MonCompteProButton from "@codegouvfr/react-dsfr/MonCompteProButton";
import { Link } from "@design-system";
import { signIn } from "next-auth/react";

export interface MonCompteProLoginProps {
callbackUrl: string;
}
export const MonCompteProLogin = ({ callbackUrl }: MonCompteProLoginProps) => (
<>
<MonCompteProButton
onClick={e => {
e.preventDefault();
signIn("moncomptepro", { redirect: false });
}}
/>
<Link href="/aide-moncomptepro" target="_blank">
Consulter l'aide MonComptePro
</Link>
</>
<MonCompteProButton
onClick={e => {
e.preventDefault();
signIn("moncomptepro", { redirect: false });
}}
/>
);
16 changes: 14 additions & 2 deletions packages/app/src/app/(default)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,21 @@ const LoginPage = async ({ searchParams }: NextServerPageProps<never, "callbackU
</p>
<br />
<p className={"text-dsfr-error"}>
Si vous rencontrez des difficultés pour vous connecter, vous devez contacter le support{" "}
Si vous utilisez une protection contre les spams (ex. MailInBlack), vous devez contacter votre
service informatique pour qu'il autorise les mails en provenance de MonComptePro.
</p>
<br />
<p className={"text-dsfr-error"}>
Pour tout problème lié à MonComptePro, vous devez contacter le support dédié à cette adresse email{" "}
<Link href={"mailto:[email protected]"} target={"_blank"}>
MonComptePro
[email protected]
</Link>
</p>
<br />
<p>
Pour consulter l'aide MonComptePro,{" "}
<Link href={"/aide-moncomptepro"} target={"_blank"}>
cliquez ici
</Link>
</p>
</>
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/app/(default)/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export const MANDATORY_FAVORABLE_POPULATION = "La population envers laquelle l'
export const NOT_HIGHER_THAN_N_RESULT = (n: number) => `Le résultat ne peut pas être supérieur à ${n}`;
export const NOT_ALL_EMPTY_CATEGORIES = "Au moins une catégorie doit avoir un écart renseigné";
export const MANDATORY_SIREN = "Le Siren est requis";
export const INVALID_SIREN = "Le Siren est composé de 9 chiffres sans espace";
export const INVALID_SIREN = "Le Siren n'est pas valide";
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ const formatTableData = (declarations: DeclarationDTO[], declarationOpmcList: De
: capitalize(declaration.entreprise?.type),
declaration.entreprise?.tranche ? CompanyWorkforceRange.Label[declaration.entreprise.tranche] : undefined,
formatIsoToFr(declaration["declaration-existante"].date || ""),
declaration["resultat-global"]?.index || <span title="Non calculable">NC</span>,
typeof declaration["resultat-global"]?.index === "number" ? (
declaration["resultat-global"]?.index
) : (
<span title="Non calculable">NC</span>
),
formatDeclarationOpmcStatus(
getDeclarationOpmcStatus(
declarationOpmcList.find(declarationOpmc => declarationOpmc.commencer?.annéeIndicateurs === rowYear),
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/app/(default)/script/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { declarationRepo } from "@api/core-domain/repo";
import { GetAllDeclarations } from "@api/core-domain/useCases/referent/GetAllDeclarations";

export async function getAllDeclarations() {
// handle default errors
const useCase = new GetAllDeclarations(declarationRepo);
return await useCase.execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { getAllDeclarations } from "../actions";

function validateSIREN(siren: string): boolean {
// Vérifie que la chaîne est composée de 9 chiffres
if (!/^\d{9}$/.test(siren)) {
return false;
}

// Applique l'algorithme de Luhn
let sum = 0;
for (let i = 0; i < 9; i++) {
let num = parseInt(siren.charAt(i), 10);

// Multiplie les chiffres en position paire par 2
if (i % 2 === 1) {
num *= 2;
// Si le résultat est supérieur à 9, soustrait 9
if (num > 9) {
num -= 9;
}
}

sum += num;
}

// Le numéro est valide si la somme est un multiple de 10
return sum % 10 === 0;
}

const CleanInvalidsSirenPage = async () => {
try {
const declarations = await getAllDeclarations();
const invalidDeclarations = [];
for (const declaration of declarations) {
if (!declaration.data?.entreprise.siren) {
//console.log("No siren", declaration.siren);
}
if (!validateSIREN(declaration.data?.entreprise.siren || "")) {
console.log("Invalid siren", declaration.siren);
invalidDeclarations.push({ ...declaration, invalidSiren: declaration.siren });
break;
}
if (!declaration.data?.entreprise.ues?.entreprises) {
//console.log("No ues", declaration.siren);
}
for (const entreprise of declaration.data?.entreprise.ues?.entreprises || []) {
if (!validateSIREN(entreprise.siren)) {
invalidDeclarations.push({ ...declaration, invalidSiren: entreprise.siren });
console.log("Invalid ues siren", entreprise.siren);
break;
}
}
}
// const invalids = declarations
// .filter(declaration => new Siren(declaration.data?.entreprise.siren || ""))
// .filter(declaration => {
// if (!declaration.data?.entreprise.ues?.entreprises) {
// return true;
// }
//
// return declaration.data?.entreprise.ues?.entreprises.reduce((hasValidSirens, entreprise) => {
// if (!hasValidSirens) return false;
// try {
// new Siren(entreprise.siren);
// return true;
// } catch (e) {
// console.log("Invalid siren", entreprise.siren, e);
// return false;
// }
// }, true);
// });

return (
<div>
<p>nombre de sirens invalides: {invalidDeclarations.length}</p>
<ul>
{invalidDeclarations.map((declaration, index) => (
<li key={`invalid-${index}`}>
declaration siren:{declaration.siren}, declaration année: {declaration.year} invalid ues siren:{" "}
{declaration.invalidSiren}
</li>
))}
</ul>
</div>
);
} catch (e) {
console.log(e);
return <p>Error</p>;
}
};

export default CleanInvalidsSirenPage;
4 changes: 2 additions & 2 deletions packages/app/src/app/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const githubLink = {
} satisfies DsfrFooterProps.LinkList.Link;

const helpLink = {
text: "Consulter l'aide pour le calcul de l'index",
text: "Consulter l'aide concernant l'index",
linkProps: {
href: "/aide-simulation",
href: "/aide-index",
target: "_blank",
},
} satisfies DsfrFooterProps.LinkList.Link;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const errorDetailLabel: Record<(typeof errorDetailCodes)[number], string>
EMAIL_DELIVERY_KO: "L'envoi du mail a échoué",
ALREADY_PROCESSED: "La demande a déjà été traitée",
INVALID_EMAIL: "L'email est invalide",
INVALID_SIREN: "Le Siren est composé de 9 chiffres sans espace",
INVALID_SIREN: "Le Siren n'est pas valide",
NOT_FOUND_SIREN: "Le SIREN n'existe pas",
};

Expand Down

0 comments on commit 76c1420

Please sign in to comment.