From 15eee56c11d7f310cbebeb5b035641a82d10cb5e Mon Sep 17 00:00:00 2001 From: Sampo Tawast Date: Wed, 2 Aug 2023 15:02:57 +0300 Subject: [PATCH] fix: allow two decimals for working hours There might be a scenario where a person works for say, 20 hours and 15 minutes. Previously it was impossible to input such value --- .../0036_alter_employee_working_hours.py | 21 +++++++++++++++++++ backend/benefit/applications/models.py | 4 ++-- .../handler/public/locales/en/common.json | 3 ++- .../handler/public/locales/fi/common.json | 3 ++- .../handler/public/locales/sv/common.json | 3 ++- .../newApplication/utils/validation.ts | 6 ++++++ frontend/benefit/shared/src/constants.ts | 1 + 7 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 backend/benefit/applications/migrations/0036_alter_employee_working_hours.py diff --git a/backend/benefit/applications/migrations/0036_alter_employee_working_hours.py b/backend/benefit/applications/migrations/0036_alter_employee_working_hours.py new file mode 100644 index 0000000000..4d16ce4848 --- /dev/null +++ b/backend/benefit/applications/migrations/0036_alter_employee_working_hours.py @@ -0,0 +1,21 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("applications", "0035_alter_applicationbatch_handler"), + ] + + operations = [ + migrations.AlterField( + model_name="employee", + name="working_hours", + field=models.DecimalField( + blank=True, + decimal_places=2, + max_digits=5, + null=True, + verbose_name="working hour", + ), + ), + ] diff --git a/backend/benefit/applications/models.py b/backend/benefit/applications/models.py index 6aa2f676c2..0430168a06 100755 --- a/backend/benefit/applications/models.py +++ b/backend/benefit/applications/models.py @@ -753,8 +753,8 @@ class Employee(UUIDModel, TimeStampedModel): ) working_hours = models.DecimalField( verbose_name=_("working hour"), - decimal_places=1, - max_digits=4, + decimal_places=2, + max_digits=5, blank=True, null=True, ) diff --git a/frontend/benefit/handler/public/locales/en/common.json b/frontend/benefit/handler/public/locales/en/common.json index b47a8cec5b..d43a85eddb 100644 --- a/frontend/benefit/handler/public/locales/en/common.json +++ b/frontend/benefit/handler/public/locales/en/common.json @@ -822,7 +822,8 @@ "number": { "invalid": "Virheellinen arvo, ilmoita vain numeroita", "max": "Arvon tulee olla enintään {{max}}", - "min": "Arvon tulee olla vähintään {{min}}" + "min": "Arvon tulee olla vähintään {{min}}", + "twoDecimals": "Arvon tulee sisältää enintään kaksi desimaalia" }, "string": { "max": "Tämä kenttä voi olla korkeintaan {{max}} merkkiä pitkä", diff --git a/frontend/benefit/handler/public/locales/fi/common.json b/frontend/benefit/handler/public/locales/fi/common.json index bd6c33c15d..39d39319a2 100644 --- a/frontend/benefit/handler/public/locales/fi/common.json +++ b/frontend/benefit/handler/public/locales/fi/common.json @@ -822,7 +822,8 @@ "number": { "invalid": "Virheellinen arvo, ilmoita vain numeroita", "max": "Arvon tulee olla enintään {{max}}", - "min": "Arvon tulee olla vähintään {{min}}" + "min": "Arvon tulee olla vähintään {{min}}", + "twoDecimals": "Arvon tulee sisältää enintään kaksi desimaalia" }, "string": { "max": "Tämä kenttä voi olla korkeintaan {{max}} merkkiä pitkä", diff --git a/frontend/benefit/handler/public/locales/sv/common.json b/frontend/benefit/handler/public/locales/sv/common.json index e9b9b86d4c..5544491483 100644 --- a/frontend/benefit/handler/public/locales/sv/common.json +++ b/frontend/benefit/handler/public/locales/sv/common.json @@ -828,7 +828,8 @@ "number": { "invalid": "Virheellinen arvo, ilmoita vain numeroita", "max": "Arvon tulee olla enintään {{max}}", - "min": "Arvon tulee olla vähintään {{min}}" + "min": "Arvon tulee olla vähintään {{min}}", + "twoDecimals": "Arvon tulee sisältää enintään kaksi desimaalia" }, "string": { "max": "Tämä kenttä voi olla korkeintaan {{max}} merkkiä pitkä", diff --git a/frontend/benefit/handler/src/components/newApplication/utils/validation.ts b/frontend/benefit/handler/src/components/newApplication/utils/validation.ts index 829cfc6ab3..afff0b3cca 100644 --- a/frontend/benefit/handler/src/components/newApplication/utils/validation.ts +++ b/frontend/benefit/handler/src/components/newApplication/utils/validation.ts @@ -297,6 +297,12 @@ export const getValidationSchema = ( .nullable() .required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)), [EMPLOYEE_KEYS.WORKING_HOURS]: Yup.number() + .test( + 'is-decimal', + t(VALIDATION_MESSAGE_KEYS.NUMBER_TWO_DECIMALS), + (value: number): boolean => + value ? /^\d+.?\d{1,2}$/.test(String(value)) : false + ) .transform((_value, originalValue) => Number(getNumberValue(originalValue)) ) diff --git a/frontend/benefit/shared/src/constants.ts b/frontend/benefit/shared/src/constants.ts index 9bd6c70c2b..5f418bb468 100644 --- a/frontend/benefit/shared/src/constants.ts +++ b/frontend/benefit/shared/src/constants.ts @@ -17,6 +17,7 @@ export enum VALIDATION_MESSAGE_KEYS { NUMBER_INVALID = 'common:form.validation.number.invalid', NUMBER_MIN = 'common:form.validation.number.min', NUMBER_MAX = 'common:form.validation.number.max', + NUMBER_TWO_DECIMALS = 'common:form.validation.number.twoDecimals', PHONE_INVALID = 'common:form.validation.phone.invalid', STRING_POSITIVENUMBER = 'common:form.validation.string.positiveNumber', STRING_MIN = 'common:form.validation.string.min',