Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Name",
"account_details_field_corporation_name": "Firmenname",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "USt - IdNr.",
"account_details_field_country": "Land des Wohnsitzes",
"account_details_field_address": "Adresse",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Name",
"account_details_field_corporation_name": "Name of company",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "VAT number",
"account_details_field_country": "Country of residence",
"account_details_field_address": "Address ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Nombre",
"account_details_field_corporation_name": "Razón social",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "Número de IVA",
"account_details_field_country": "País de residencia",
"account_details_field_address": "Dirección",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Nom",
"account_details_field_corporation_name": "Raison sociale",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "Numéro de TVA",
"account_details_field_country": "Pays de résidence",
"account_details_field_address": "Adresse",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"account_details_field_name": "Nom",
"account_details_field_corporation_name": "Raison sociale",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "Numéro de TVA",
"account_details_field_country": "Pays de résidence",
"account_details_field_address": "Adresse",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Nome",
"account_details_field_corporation_name": "Ragione sociale",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "Numero di P. IVA",
"account_details_field_country": "Paese di residenza",
"account_details_field_address": "Indirizzo",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Nazwa",
"account_details_field_corporation_name": "Nazwa firmy",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "Numer NIP",
"account_details_field_country": "Kraj",
"account_details_field_address": "Adres siedziby/zamieszkania i pobytu",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"account_details_field_name": "Nome",
"account_details_field_corporation_name": "Razão social",
"account_details_field_siret": "SIRET",
"account_details_field_siren": "SIREN",
"account_details_field_vat": "Número de IVA",
"account_details_field_country": "País de residência",
"account_details_field_address": "Morada",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, it, expect } from 'vitest';
import {
shouldAccessOrganizationSearch,
shouldEnableSIRENDisplay,
getSirenFromSiret,
} from './flowHelper';

describe('shouldAccessOrganizationSearch', () => {
it('returns true for a French organization that is not individual', () => {
expect(shouldAccessOrganizationSearch('FR', 'corporation')).toBe(true);
expect(shouldAccessOrganizationSearch('FR', 'association')).toBe(true);
});

it('returns false for an individual', () => {
expect(shouldAccessOrganizationSearch('FR', 'individual')).toBe(false);
});

it('returns false for a non-French country', () => {
expect(shouldAccessOrganizationSearch('US', 'corporation')).toBe(false);
});

it('returns false if country or legalForm is missing', () => {
expect(shouldAccessOrganizationSearch(undefined, 'corporation')).toBe(
false,
);
expect(shouldAccessOrganizationSearch('FR')).toBe(false);
});
});

describe('shouldEnableSIRENDisplay', () => {
it('returns true for a French corporation', () => {
expect(shouldEnableSIRENDisplay('FR', 'corporation')).toBe(true);
});

it('returns false for other legal forms', () => {
expect(shouldEnableSIRENDisplay('FR', 'association')).toBe(false);
expect(shouldEnableSIRENDisplay('FR', 'individual')).toBe(false);
});

it('returns false for a non-French country', () => {
expect(shouldEnableSIRENDisplay('US', 'corporation')).toBe(false);
});

it('returns false if country or legalForm is missing', () => {
expect(shouldEnableSIRENDisplay(undefined, 'corporation')).toBe(false);
expect(shouldEnableSIRENDisplay('FR')).toBe(false);
});
});

describe('getSirenFromSiret', () => {
const pattern = '^[0-9]{14}$';

it('returns the first 9 digits of a valid SIRET', () => {
expect(getSirenFromSiret('12345678912345', pattern)).toBe('123456789');
});

it('returns null if the SIRET does not match the pattern', () => {
expect(getSirenFromSiret('ABC123', pattern)).toBeNull();
});

it('returns null if the SIRET or pattern is missing', () => {
expect(getSirenFromSiret(undefined, pattern)).toBeNull();
expect(getSirenFromSiret('12345678912345', null)).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { Country, LegalForm } from '@ovh-ux/manager-config';

export const shouldAccessCompanySearch = (
export const shouldAccessOrganizationSearch = (
country?: Country,
legalForm?: LegalForm,
) => country === 'FR' && legalForm && legalForm !== 'individual';
) => country === 'FR' && !!legalForm && legalForm !== 'individual';

export const shouldEnableSIRENDisplay = (
country?: Country,
legalForm?: LegalForm,
) => country === 'FR' && legalForm === 'corporation';

export const getSirenFromSiret = (
siret?: string,
pattern?: string | null,
): string | null => {
if (!siret || !pattern) return null;

return new RegExp(pattern).test(siret) ? siret.substring(0, 9) : null;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Controller, SubmitHandler, useForm } from 'react-hook-form';
import { Controller, SubmitHandler, useForm, useWatch } from 'react-hook-form';
import { useMutation } from '@tanstack/react-query';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
Expand Down Expand Up @@ -44,7 +44,11 @@ import {
} from '@/hooks/zod/useZod';
import { putMe } from '@/data/api/me';
import { urls } from '@/routes/routes.constant';
import { shouldAccessCompanySearch } from '@/helpers/flowHelper';
import {
getSirenFromSiret,
shouldAccessOrganizationSearch,
shouldEnableSIRENDisplay,
} from '@/helpers/flowHelper';

type AccountDetailsFormProps = {
rules: Record<RuleField, Rule>;
Expand Down Expand Up @@ -113,6 +117,28 @@ function AccountDetailsForm({
resolver: zodResolver(zodSchema),
});

const shouldDisplaySIREN = useMemo(
() => shouldEnableSIRENDisplay(currentUser.country, legalForm),
[currentUser.country, legalForm],
);

const corporationIdValue = useWatch({
control,
name: 'companyNationalIdentificationNumber',
});

const sirenValue = useMemo(
() =>
getSirenFromSiret(
corporationIdValue,
rules?.companyNationalIdentificationNumber?.regularExpression,
),
[
corporationIdValue,
rules?.companyNationalIdentificationNumber?.regularExpression,
],
);

const phoneCountry = watch('phoneCountry');

useEffect(() => {
Expand Down Expand Up @@ -291,41 +317,63 @@ function AccountDetailsForm({
control={control}
name="companyNationalIdentificationNumber"
render={({ field: { name, value, onChange, onBlur } }) => (
<OdsFormField>
<label
htmlFor={name}
slot="label"
aria-label={t('account_details_field_siret')}
>
<OdsText preset="caption">
{t('account_details_field_siret')}
</OdsText>
</label>
<OdsInput
isReadonly={Boolean(companyNationalIdentificationNumber)}
name="companyNationalIdentificationNumber"
value={value}
maxlength={
rules?.companyNationalIdentificationNumber.maxLength ||
undefined
}
hasError={!!errors[name]}
onOdsChange={onChange}
onOdsBlur={onBlur}
/>
{errors[name] &&
rules?.companyNationalIdentificationNumber && (
<OdsText
className="text-critical leading-[0.8]"
preset="caption"
>
{renderTranslatedZodError(
errors[name].message,
rules?.companyNationalIdentificationNumber,
)}
<>
<OdsFormField>
<label
htmlFor={name}
slot="label"
aria-label={t('account_details_field_siret')}
>
<OdsText preset="caption">
{t('account_details_field_siret')}
</OdsText>
)}
</OdsFormField>
</label>
<OdsInput
isReadonly={Boolean(
companyNationalIdentificationNumber,
)}
name="companyNationalIdentificationNumber"
value={value}
maxlength={
rules?.companyNationalIdentificationNumber
.maxLength || undefined
}
hasError={!!errors[name]}
onOdsChange={onChange}
onOdsBlur={onBlur}
/>
{errors[name] &&
rules?.companyNationalIdentificationNumber && (
<OdsText
className="text-critical leading-[0.8]"
preset="caption"
>
{renderTranslatedZodError(
errors[name].message,
rules?.companyNationalIdentificationNumber,
)}
</OdsText>
)}
</OdsFormField>
{shouldDisplaySIREN && (
<OdsFormField>
<label
htmlFor="companyNationalRegistrationNumber"
slot="label"
aria-label={t('account_details_field_siren')}
>
<OdsText preset="caption">
{t('account_details_field_siren')}
</OdsText>
</label>
<OdsInput
isReadonly
name="companyNationalRegistrationNumber"
value={sirenValue}
/>
</OdsFormField>
)}
</>
)}
/>
)}
Expand Down Expand Up @@ -695,7 +743,7 @@ export default function AccountDetailsPage() {
const { t: tAction } = useTranslation(NAMESPACES.ACTIONS);
const { legalForm, organisation } = useUserContext();
const { data: currentUser } = useMe();
const wentThroughCompanySearch = shouldAccessCompanySearch(
const wentThroughOrganizationSearch = shouldAccessOrganizationSearch(
currentUser?.country,
legalForm,
);
Expand Down Expand Up @@ -740,11 +788,13 @@ export default function AccountDetailsPage() {
<OdsLink
icon={ODS_ICON_NAME.arrowLeft}
iconAlignment={ODS_LINK_ICON_ALIGNMENT.left}
href={`#${wentThroughCompanySearch ? urls.company : urls.accountType}`}
href={`#${
wentThroughOrganizationSearch ? urls.company : urls.accountType
}`}
label={tAction('back')}
className="flex mb-6"
/>
{wentThroughCompanySearch && (
{wentThroughOrganizationSearch && (
<OdsText preset={ODS_TEXT_PRESET.caption}>
{tCommon('step', { current: 2, total: 2 })}
</OdsText>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useLegalFormRules } from '@/data/hooks/useRules';
import { AVERAGE_NUMBER_OF_LEGAL_FORMS } from './accountType.constants';
import AccountTypeTooltipContent from './tooltip-content/TooltipContent.component';
import { urls } from '@/routes/routes.constant';
import { shouldAccessCompanySearch } from '@/helpers/flowHelper';
import { shouldAccessOrganizationSearch } from '@/helpers/flowHelper';

export default function AccountType() {
const { t } = useTranslation('account-type');
Expand All @@ -34,7 +34,7 @@ export default function AccountType() {
const validateStep = useCallback(() => {
if (!legalForm) {
setLegalFormError(true);
} else if (shouldAccessCompanySearch(country, legalForm)) {
} else if (shouldAccessOrganizationSearch(country, legalForm)) {
navigate(urls.company);
} else {
navigate(urls.accountDetails);
Expand Down
Loading