diff --git a/frontend/benefit/handler/public/locales/en/common.json b/frontend/benefit/handler/public/locales/en/common.json index e5237beb53..251faf3513 100644 --- a/frontend/benefit/handler/public/locales/en/common.json +++ b/frontend/benefit/handler/public/locales/en/common.json @@ -1295,7 +1295,7 @@ "phoneNumber": { "label": "Puhelin" }, - "coOperationNegotiations":{ + "coOperationNegotiations": { "label": "Muutosneuvottelut" }, "coOperationNegotiationsDescription": { @@ -1321,6 +1321,12 @@ }, "endDate": { "label": "Työsuhde päättyy" + }, + "paySubsidyGranted": { + "label": "Palkkatuki" + }, + "apprenticeshipProgram": { + "label": "Oppisopimus" } } } diff --git a/frontend/benefit/handler/public/locales/fi/common.json b/frontend/benefit/handler/public/locales/fi/common.json index 305295f4df..ee8a62e0d7 100644 --- a/frontend/benefit/handler/public/locales/fi/common.json +++ b/frontend/benefit/handler/public/locales/fi/common.json @@ -1304,7 +1304,7 @@ "alternativeCompanyStreetAddress": { "label": "Osoite" }, - "coOperationNegotiations":{ + "coOperationNegotiations": { "label": "Muutosneuvottelut" }, "coOperationNegotiationsDescription": { @@ -1321,6 +1321,12 @@ }, "endDate": { "label": "Työsuhde päättyy" + }, + "paySubsidyGranted": { + "label": "Palkkatuki" + }, + "apprenticeshipProgram": { + "label": "Oppisopimus" } } } diff --git a/frontend/benefit/handler/public/locales/sv/common.json b/frontend/benefit/handler/public/locales/sv/common.json index e5237beb53..251faf3513 100644 --- a/frontend/benefit/handler/public/locales/sv/common.json +++ b/frontend/benefit/handler/public/locales/sv/common.json @@ -1295,7 +1295,7 @@ "phoneNumber": { "label": "Puhelin" }, - "coOperationNegotiations":{ + "coOperationNegotiations": { "label": "Muutosneuvottelut" }, "coOperationNegotiationsDescription": { @@ -1321,6 +1321,12 @@ }, "endDate": { "label": "Työsuhde päättyy" + }, + "paySubsidyGranted": { + "label": "Palkkatuki" + }, + "apprenticeshipProgram": { + "label": "Oppisopimus" } } } diff --git a/frontend/benefit/handler/src/components/applicationForm/companySearch/useCompanySearch.ts b/frontend/benefit/handler/src/components/applicationForm/companySearch/useCompanySearch.ts index a63f2d1c7d..3784fc75a6 100644 --- a/frontend/benefit/handler/src/components/applicationForm/companySearch/useCompanySearch.ts +++ b/frontend/benefit/handler/src/components/applicationForm/companySearch/useCompanySearch.ts @@ -38,7 +38,7 @@ const useCompanySearch = (): ExtendedComponentProps => { const [companies, setCompanies] = useState([]); const [selectedCompany, setSelectedCompany] = useState(null); const [isLoading, setIsLoading] = useState(false); - const { onCompanySelected } = useFormActions({}); + const { onCompanySelected } = useFormActions({}, {}); const onCompanyChange = (businessId: string): void => { setSelectedCompany(businessId); diff --git a/frontend/benefit/handler/src/components/applicationForm/useApplicationForm.ts b/frontend/benefit/handler/src/components/applicationForm/useApplicationForm.ts index 8719fe4501..4e18b7491a 100644 --- a/frontend/benefit/handler/src/components/applicationForm/useApplicationForm.ts +++ b/frontend/benefit/handler/src/components/applicationForm/useApplicationForm.ts @@ -100,8 +100,10 @@ export const useApplicationForm = (): ExtendedComponentProps => { const [initialApplication, setInitialApplication] = React.useState(null); - const { onSave, onQuietSave, onSubmit, onNext, onDelete } = - useFormActions(application); + const { onSave, onQuietSave, onSubmit, onNext, onDelete } = useFormActions( + application, + initialApplication + ); React.useEffect(() => { if (id) { diff --git a/frontend/benefit/handler/src/constants.ts b/frontend/benefit/handler/src/constants.ts index a655686b83..f81ee543ee 100644 --- a/frontend/benefit/handler/src/constants.ts +++ b/frontend/benefit/handler/src/constants.ts @@ -4,6 +4,7 @@ import { BENEFIT_TYPES, CALCULATION_ROW_TYPES, EMPLOYEE_KEYS, + PAY_SUBSIDY_OPTIONS, } from 'benefit-shared/constants'; export enum ROUTES { @@ -204,3 +205,12 @@ export enum LOCAL_STORAGE_KEYS { export enum APPLICATION_ACTIONS { HANDLER_ALLOW_APPLICATION_EDIT = 'HANDLER_ALLOW_APPLICATION_EDIT', } + +export const PAY_SUBSIDIES_OVERRIDE = { + startDate: null, + endDate: null, + paySubsidyPercent: PAY_SUBSIDY_OPTIONS[0], + workTimePercent: 100, + disabilityOrIllness: false, + durationInMonthsRounded: '', +}; diff --git a/frontend/benefit/handler/src/hooks/useFormActions.tsx b/frontend/benefit/handler/src/hooks/useFormActions.tsx index 20b04ce1df..3c62de7bb1 100644 --- a/frontend/benefit/handler/src/hooks/useFormActions.tsx +++ b/frontend/benefit/handler/src/hooks/useFormActions.tsx @@ -1,6 +1,7 @@ import { APPLICATION_ACTIONS, APPLICATION_FIELD_KEYS, + PAY_SUBSIDIES_OVERRIDE, ROUTES, } from 'benefit/handler/constants'; import DeMinimisContext from 'benefit/handler/context/DeMinimisContext'; @@ -12,7 +13,12 @@ import { PAY_SUBSIDY_GRANTED, PAY_SUBSIDY_OPTIONS, } from 'benefit-shared/constants'; -import { ApplicationData, Employee } from 'benefit-shared/types/application'; +import { + ApplicationData, + Calculation, + Employee, + PaySubsidy, +} from 'benefit-shared/types/application'; import { prettyPrintObject } from 'benefit-shared/utils/errors'; import camelcaseKeys from 'camelcase-keys'; import { useRouter } from 'next/router'; @@ -94,7 +100,10 @@ const getErrorContent = ( } }; -const useFormActions = (application: Partial): FormActions => { +const useFormActions = ( + application: Partial, + initialApplication: Partial +): FormActions => { const router = useRouter(); const { mutateAsync: createApplication, error: createApplicationError } = @@ -150,6 +159,54 @@ const useFormActions = (application: Partial): FormActions => { const { deMinimisAids } = React.useContext(DeMinimisContext); + const getCalculationValuesOnPaySubsidyChange = ( + values: Partial + ): Calculation => ({ + ...values.calculation, + monthlyPay: stringToFloatValue(values.employee.monthlyPay), + vacationMoney: stringToFloatValue(values.employee.vacationMoney), + otherExpenses: stringToFloatValue(values.employee.otherExpenses), + startDate: null, + endDate: null, + stateAidMaxPercentage: null, + grantedAsDeMinimisAid: false, + targetGroupCheck: false, + overrideMonthlyBenefitAmount: null, + overrideMonthlyBenefitAmountComment: '', + }); + + const getPayloadForCalculation = ( + values: Partial + ): Calculation => { + // Return the calculation values as they are + if (values.paySubsidyGranted === initialApplication?.paySubsidyGranted) { + return { + ...values.calculation, + monthlyPay: stringToFloatValue(values.employee.monthlyPay), + otherExpenses: stringToFloatValue(values.employee.otherExpenses), + vacationMoney: stringToFloatValue(values.employee.vacationMoney), + overrideMonthlyBenefitAmount: stringToFloatValue( + values.calculation.overrideMonthlyBenefitAmount + ), + }; + } + return getCalculationValuesOnPaySubsidyChange(values); + }; + + const getPayloadForPaySubsidies = ( + values: Partial + ): PaySubsidy[] => { + if (values.paySubsidyGranted === initialApplication?.paySubsidyGranted) { + return [...values.paySubsidies]; + } + return [ + PAY_SUBSIDY_GRANTED.GRANTED, + PAY_SUBSIDY_GRANTED.GRANTED_AGED, + ].includes(values.paySubsidyGranted) + ? [PAY_SUBSIDIES_OVERRIDE] + : []; + }; + // eslint-disable-next-line sonarjs/cognitive-complexity const getNormalizedValues = (currentValues: Application): Application => { const employee: Employee | undefined = currentValues?.employee ?? undefined; @@ -161,6 +218,8 @@ const useFormActions = (application: Partial): FormActions => { apprenticeshipProgram, paperApplicationDate, changeReason, + calculation, + paySubsidies, } = currentValues; const paySubsidyPercent = @@ -212,34 +271,20 @@ const useFormActions = (application: Partial): FormActions => { changeReason, deMinimisAidSet: deMinimisAids, action: APPLICATION_ACTIONS.HANDLER_ALLOW_APPLICATION_EDIT, + benefitType: BENEFIT_TYPES.SALARY, + calculation: calculation + ? getPayloadForCalculation(currentValues) + : undefined, + paySubsidies: paySubsidies + ? getPayloadForPaySubsidies(currentValues) + : undefined, }; }; const prepareDataForSubmission = ( values: Partial ): ApplicationData => - snakecaseKeys( - { - ...values, - benefit_type: BENEFIT_TYPES.SALARY, - calculation: values.calculation - ? { - ...values.calculation, - monthlyPay: stringToFloatValue(values.calculation.monthlyPay), - otherExpenses: stringToFloatValue( - values.calculation.otherExpenses - ), - vacationMoney: stringToFloatValue( - values.calculation.vacationMoney - ), - overrideMonthlyBenefitAmount: stringToFloatValue( - values.calculation.overrideMonthlyBenefitAmount - ), - } - : undefined, - }, - { deep: true } - ) as ApplicationData; + snakecaseKeys(values, { deep: true }) as ApplicationData; const onSubmit = async ( currentValues: Application, @@ -290,7 +335,6 @@ const useFormActions = (application: Partial): FormActions => { applicationId: string | undefined ): Promise => { const data = prepareDataForSubmission(getNormalizedValues(currentValues)); - try { const result = applicationId ? await updateApplication(data) @@ -339,7 +383,6 @@ const useFormActions = (application: Partial): FormActions => { applicationId: string | undefined ): Promise => { const data = prepareDataForSubmission(getNormalizedValues(currentValues)); - try { const result = applicationId ? await updateApplication(data)