diff --git a/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.test.ts b/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.test.ts index 9434f3f52..a94613e35 100644 --- a/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.test.ts +++ b/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.test.ts @@ -1,6 +1,7 @@ -import { AgentTypeEnum } from "dto"; +import { AgentTypeEnum, RegistrationSrcTypeEnum } from "dto"; import type { MockInstance } from "vitest"; import { beforeEach } from "vitest"; +import type { SpyInstance } from "vitest"; import StructureFormStepController from "./StructureFormStep.controller"; import Dispatch from "$lib/core/Dispatch"; import { isPhoneNumber } from "$lib/helpers/stringHelper"; @@ -208,4 +209,70 @@ describe("StructureFormStepController", () => { expect(values.centralSomething).toBeUndefined(); }); }); + + describe("onUpdateRegistrationSrc", () => { + let mockUpdate: SpyInstance; + beforeEach(() => { + mockUpdate = vi.spyOn(StructureFormStepController.prototype, "onUpdate").mockImplementation(vi.fn()); + }); + + afterAll(() => { + mockUpdate.mockRestore(); + }); + + it("registrationSrcEmail and registrationSrcDetails are set to empty strings", () => { + const values = { + registrationSrc: [RegistrationSrcTypeEnum.DEMO], + registrationSrcEmail: "test@email.com", + registrationSrcDetails: "Other", + }; + const expected = { + registrationSrc: [RegistrationSrcTypeEnum.DEMO], + registrationSrcEmail: "", + registrationSrcDetails: "", + }; + ctrl.onUpdateRegistrationSrc(values); + expect(values).toStrictEqual(expected); + }); + it("registrationSrcEmail and registrationSrcDetails are not set to empty strings", () => { + const values = { + registrationSrc: [RegistrationSrcTypeEnum.COLLEAGUES_HIERARCHY, RegistrationSrcTypeEnum.OTHER], + registrationSrcEmail: "test@email.com", + registrationSrcDetails: "Other", + }; + const expected = { ...values }; + ctrl.onUpdateRegistrationSrc(values); + expect(values).toStrictEqual(expected); + }); + }); + + describe("isRegistrationSrcEmailVisible", () => { + it("is visible", () => { + const registerSrcValue = [RegistrationSrcTypeEnum.SOCIALS, RegistrationSrcTypeEnum.COLLEAGUES_HIERARCHY]; + const expected = true; + const isVisible = ctrl.isRegistrationSrcEmailVisible(registerSrcValue); + expect(isVisible).toStrictEqual(expected); + }); + it("is not visible", () => { + const registerSrcValue = [RegistrationSrcTypeEnum.SOCIALS, RegistrationSrcTypeEnum.OTHER]; + const expected = false; + const isVisible = ctrl.isRegistrationSrcEmailVisible(registerSrcValue); + expect(isVisible).toStrictEqual(expected); + }); + }); + + describe("isRegistrationSrcDetailsVisible", () => { + it("is visible", () => { + const registerSrcValue = [RegistrationSrcTypeEnum.SOCIALS, RegistrationSrcTypeEnum.OTHER]; + const expected = true; + const isVisible = ctrl.isRegistrationSrcDetailsVisible(registerSrcValue); + expect(isVisible).toStrictEqual(expected); + }); + it("is not visible", () => { + const registerSrcValue = [RegistrationSrcTypeEnum.SOCIALS, RegistrationSrcTypeEnum.COLLEAGUES_HIERARCHY]; + const expected = false; + const isVisible = ctrl.isRegistrationSrcDetailsVisible(registerSrcValue); + expect(isVisible).toStrictEqual(expected); + }); + }); }); diff --git a/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.ts b/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.ts index 18bf10b96..f733cf372 100644 --- a/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.ts +++ b/packages/front/src/lib/components/StructureFormStep/StructureFormStep.controller.ts @@ -1,4 +1,4 @@ -import { AgentTypeEnum, AgentJobTypeEnum } from "dto"; +import { AgentTypeEnum, AgentJobTypeEnum, RegistrationSrcTypeEnum } from "dto"; import type { ComponentType, SvelteComponent } from "svelte"; import OperatorSubStep from "./OperatorSubStep/OperatorSubStep.svelte"; import CentralSubStep from "./CentralSubStep/CentralSubStep.svelte"; @@ -48,6 +48,16 @@ export default class StructureFormStepController { { value: AgentJobTypeEnum.CONTROLLER, label: "Contrôleur / Inspecteur" }, { value: AgentJobTypeEnum.OTHER, label: "Autre" }, ]; + public readonly registrationSrcOptions: Option[] = [ + { value: RegistrationSrcTypeEnum.DEMO, label: "Lors d’une présentation avec une personne de Data.Subvention" }, + { value: RegistrationSrcTypeEnum.SEARCH_ENGINE, label: "Via des recherches sur internet" }, + { + value: RegistrationSrcTypeEnum.COLLEAGUES_HIERARCHY, + label: "Un de mes collègues ou de ma hiérarchie m’en a parlé", + }, + { value: RegistrationSrcTypeEnum.SOCIALS, label: "Via une publication sur les réseaux sociaux" }, + { value: RegistrationSrcTypeEnum.OTHER, label: "Autre" }, + ]; private static subStepByAgentType: Record = { [AgentTypeEnum.CENTRAL_ADMIN]: CentralSubStep, @@ -102,6 +112,27 @@ export default class StructureFormStepController { this.dispatch(shouldBlockStep ? "error" : "valid"); } + onUpdateRegistrationSrc(values: Record) { + //Set registrationSrcEmail & registrationSrcDetails to empty, to avoid saving unused data + this.onUpdate(values, "registrationSrc"); + if (values["registrationSrc"] && Array.isArray(values["registrationSrc"])) { + if ( + !values["registrationSrc"].includes(RegistrationSrcTypeEnum.COLLEAGUES_HIERARCHY) && + values["registrationSrcEmail"] !== "" + ) { + values["registrationSrcEmail"] = ""; + this.onUpdate(values, "registrationSrcEmail"); + } + if ( + !values["registrationSrc"].includes(RegistrationSrcTypeEnum.OTHER) && + values["registrationSrcDetails"] !== "" + ) { + values["registrationSrcDetails"] = ""; + this.onUpdate(values, "registrationSrcDetails"); + } + } + } + cleanSubStepValues(values: Record, contextAgentType: AgentTypeEnum) { const prefixes: string[] = []; for (const [agentType, prefix] of Object.entries(StructureFormStepController.subFieldsPrefixByAgentType)) { @@ -130,4 +161,12 @@ export default class StructureFormStepController { : undefined, ); } + + isRegistrationSrcEmailVisible(registerSrcValue: RegistrationSrcTypeEnum[]) { + return registerSrcValue && registerSrcValue.includes(RegistrationSrcTypeEnum.COLLEAGUES_HIERARCHY); + } + + isRegistrationSrcDetailsVisible(registerSrcValue: RegistrationSrcTypeEnum[]) { + return registerSrcValue && registerSrcValue.includes(RegistrationSrcTypeEnum.OTHER); + } } diff --git a/packages/front/src/lib/components/StructureFormStep/StructureFormStep.svelte b/packages/front/src/lib/components/StructureFormStep/StructureFormStep.svelte index 4b621f7f4..9896ad355 100644 --- a/packages/front/src/lib/components/StructureFormStep/StructureFormStep.svelte +++ b/packages/front/src/lib/components/StructureFormStep/StructureFormStep.svelte @@ -1,4 +1,5 @@