Skip to content

Commit 5373dbc

Browse files
committed
refactor(domain): extract hasRequiredDeclarationInfo predicate
Move the inline `!userPhone || hasCse === null` check into a pure domain function, as suggested in PR review by maxgfr and Viczei.
1 parent 8d8b50a commit 5373dbc

5 files changed

Lines changed: 40 additions & 3 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { hasRequiredDeclarationInfo } from "../shared/declarationPrerequisites";
4+
5+
describe("hasRequiredDeclarationInfo", () => {
6+
it("returns true when phone and CSE are provided", () => {
7+
expect(hasRequiredDeclarationInfo("0612345678", true)).toBe(true);
8+
expect(hasRequiredDeclarationInfo("0612345678", false)).toBe(true);
9+
});
10+
11+
it("returns false when phone is missing", () => {
12+
expect(hasRequiredDeclarationInfo(null, true)).toBe(false);
13+
});
14+
15+
it("returns false when CSE is null", () => {
16+
expect(hasRequiredDeclarationInfo("0612345678", null)).toBe(false);
17+
});
18+
19+
it("returns false when both are missing", () => {
20+
expect(hasRequiredDeclarationInfo(null, null)).toBe(false);
21+
});
22+
23+
it("returns false when phone is empty string", () => {
24+
expect(hasRequiredDeclarationInfo("", true)).toBe(false);
25+
});
26+
});

packages/app/src/modules/domain/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export {
1919
GAP_ALERT_THRESHOLD,
2020
MAX_CSE_FILES,
2121
} from "./shared/constants";
22+
// Declaration prerequisites
23+
export { hasRequiredDeclarationInfo } from "./shared/declarationPrerequisites";
2224
// Declaration status
2325
export { computeDeclarationStatus } from "./shared/declarationStatus";
2426
// Display formatting (%, €, units)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** Returns true if the user has provided all required info to start a declaration (phone + CSE status). */
2+
export function hasRequiredDeclarationInfo(
3+
userPhone: string | null,
4+
hasCse: boolean | null,
5+
): boolean {
6+
return !!userPhone && hasCse !== null;
7+
}

packages/app/src/modules/my-space/CompanyDeclarationsPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getCurrentYear } from "~/modules/domain";
1+
import { getCurrentYear, hasRequiredDeclarationInfo } from "~/modules/domain";
22

33
import { ArchivesSection } from "./ArchivesSection";
44
import { CompanyEditModal } from "./CompanyEditModal";
@@ -60,7 +60,7 @@ export function CompanyDeclarationsPage({
6060
/>
6161
<ArchivesSection />
6262
<CompanyEditModal company={company} />
63-
{(!userPhone || company.hasCse === null) && (
63+
{!hasRequiredDeclarationInfo(userPhone, company.hasCse) && (
6464
<MissingInfoModal
6565
hasCse={company.hasCse}
6666
siren={company.siren}

packages/app/src/modules/my-space/DeclarationLink.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use client";
22

3+
import { hasRequiredDeclarationInfo } from "~/modules/domain";
4+
35
import { DECLARATION_PROCESS_PANEL_ID } from "./DeclarationProcessPanel";
46
import type { DeclarationType } from "./types";
57

@@ -21,7 +23,7 @@ export function DeclarationLink({
2123
hasCse,
2224
children,
2325
}: Props) {
24-
const hasMissingInfo = !userPhone || hasCse === null;
26+
const hasMissingInfo = !hasRequiredDeclarationInfo(userPhone, hasCse);
2527

2628
// When info is missing, open missing-info modal (for both types)
2729
if (hasMissingInfo) {

0 commit comments

Comments
 (0)