From 2554da6a6fa08a5cc952f8d4e1a680800ead1dc5 Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:23:07 +0300 Subject: [PATCH] fix(persons): error message now shown when saving records --- .../button_actions/useButtonActions.tsx | 3 + src/services/dexie/persons.ts | 139 ++++++++---------- 2 files changed, 68 insertions(+), 74 deletions(-) diff --git a/src/features/persons/button_actions/useButtonActions.tsx b/src/features/persons/button_actions/useButtonActions.tsx index daa8af07e1..41c9dae6ce 100644 --- a/src/features/persons/button_actions/useButtonActions.tsx +++ b/src/features/persons/button_actions/useButtonActions.tsx @@ -83,6 +83,8 @@ const useButtonActions = () => { setIsDisqualify(false); } catch (error) { + setIsDisqualify(false); + await displaySnackNotification({ header: getMessageByCode('error_app_generic-title'), message: error.message, @@ -102,6 +104,7 @@ const useButtonActions = () => { await dbPersonsSave(newPerson); setIsQualify(false); } catch (error) { + setIsQualify(false); await displaySnackNotification({ header: getMessageByCode('error_app_generic-title'), message: error.message, diff --git a/src/services/dexie/persons.ts b/src/services/dexie/persons.ts index 267ae280af..99cd5d2500 100644 --- a/src/services/dexie/persons.ts +++ b/src/services/dexie/persons.ts @@ -3,100 +3,91 @@ import { PersonType } from '@definition/person'; import { getTranslation } from '@services/i18n/translation'; export const dbPersonsSave = async (person: PersonType, isNew?: boolean) => { - try { - const setting = await appDb.app_settings.get(1); + const setting = await appDb.app_settings.get(1); - if (setting.user_settings.account_type === 'vip') { - // CHECK FOR MULTIPLE RECORDS HISTORY FOR ALL SPIRITUAL STATUS - const baptizedActive = - person.person_data.publisher_baptized.history.filter( - (record) => record._deleted === false && record.end_date === null - ).length; + if (setting.user_settings.account_type === 'vip') { + // CHECK FOR MULTIPLE RECORDS HISTORY FOR ALL SPIRITUAL STATUS + const baptizedActive = person.person_data.publisher_baptized.history.filter( + (record) => record._deleted === false && record.end_date === null + ).length; - if (baptizedActive > 1) { - throw new Error(getTranslation({ key: 'tr_baptizedActiveMultiple' })); - } + if (baptizedActive > 1) { + throw new Error(getTranslation({ key: 'tr_baptizedActiveMultiple' })); + } - const unbaptizedActive = - person.person_data.publisher_unbaptized.history.filter( - (record) => record._deleted === false && record.end_date === null - ).length; + const unbaptizedActive = + person.person_data.publisher_unbaptized.history.filter( + (record) => record._deleted === false && record.end_date === null + ).length; - if (unbaptizedActive > 1) { - throw new Error(getTranslation({ key: 'tr_unbaptizedActiveMultiple' })); - } + if (unbaptizedActive > 1) { + throw new Error(getTranslation({ key: 'tr_unbaptizedActiveMultiple' })); + } - const midweekActive = - person.person_data.midweek_meeting_student.history.filter( - (record) => record._deleted === false && record.end_date === null - ).length; + const midweekActive = + person.person_data.midweek_meeting_student.history.filter( + (record) => record._deleted === false && record.end_date === null + ).length; - if (midweekActive > 1) { - throw new Error(getTranslation({ key: 'tr_midweekActiveMultiple' })); - } + if (midweekActive > 1) { + throw new Error(getTranslation({ key: 'tr_midweekActiveMultiple' })); + } - // CHECK FOR ACTIVE RECORDS IN INACTIVE STATUSES - if (!person.person_data.publisher_baptized.active.value) { - if (baptizedActive > 0) { - throw new Error(getTranslation({ key: 'tr_baptizedInvalidRecords' })); - } + // CHECK FOR ACTIVE RECORDS IN INACTIVE STATUSES + if (!person.person_data.publisher_baptized.active.value) { + if (baptizedActive > 0) { + throw new Error(getTranslation({ key: 'tr_baptizedInvalidRecords' })); } + } - if (!person.person_data.publisher_unbaptized.active.value) { - if (unbaptizedActive > 0) { - throw new Error( - getTranslation({ key: 'tr_unbaptizedInvalidRecords' }) - ); - } + if (!person.person_data.publisher_unbaptized.active.value) { + if (unbaptizedActive > 0) { + throw new Error(getTranslation({ key: 'tr_unbaptizedInvalidRecords' })); } + } - if (!person.person_data.midweek_meeting_student.active.value) { - if (midweekActive > 0) { - throw new Error(getTranslation({ key: 'tr_midweekInvalidRecords' })); - } + if (!person.person_data.midweek_meeting_student.active.value) { + if (midweekActive > 0) { + throw new Error(getTranslation({ key: 'tr_midweekInvalidRecords' })); } + } - // CHECK FOR MULTIPLE ACTIVE PRIVILEGES - const privilegesActive = person.person_data.privileges.filter( - (record) => record._deleted === false && record.end_date === null - ).length; + // CHECK FOR MULTIPLE ACTIVE PRIVILEGES + const privilegesActive = person.person_data.privileges.filter( + (record) => record._deleted === false && record.end_date === null + ).length; - if (privilegesActive > 1) { - throw new Error(getTranslation({ key: 'tr_privilegesActiveMultiple' })); - } + if (privilegesActive > 1) { + throw new Error(getTranslation({ key: 'tr_privilegesActiveMultiple' })); + } - // CHECK FOR MULTIPLE ACTIVE ENROLLMENTS - const enrollmentsActive = person.person_data.enrollments.filter( - (record) => record._deleted === false && record.end_date === null - ).length; + // CHECK FOR MULTIPLE ACTIVE ENROLLMENTS + const enrollmentsActive = person.person_data.enrollments.filter( + (record) => record._deleted === false && record.end_date === null + ).length; - if (enrollmentsActive > 1) { - throw new Error( - getTranslation({ key: 'tr_enrollemntsActiveMultiple' }) - ); - } + if (enrollmentsActive > 1) { + throw new Error(getTranslation({ key: 'tr_enrollemntsActiveMultiple' })); + } - // CHECK FOR EXISTING RECORD IF NEW - if (isNew) { - const personsAll = await dbPersonsGetAll(); - const found = personsAll.find( - (record) => - record.person_data.person_firstname.value === - person.person_data.person_firstname.value && - record.person_data.person_lastname.value === - person.person_data.person_lastname.value - ); - - if (found) { - throw new Error(getTranslation({ key: 'tr_personRecordExists' })); - } + // CHECK FOR EXISTING RECORD IF NEW + if (isNew) { + const personsAll = await dbPersonsGetAll(); + const found = personsAll.find( + (record) => + record.person_data.person_firstname.value === + person.person_data.person_firstname.value && + record.person_data.person_lastname.value === + person.person_data.person_lastname.value + ); + + if (found) { + throw new Error(getTranslation({ key: 'tr_personRecordExists' })); } } - - await appDb.persons.put(person); - } catch (err) { - console.error(err); } + + await appDb.persons.put(person); }; export const dbPersonsDelete = async (person_uid: string) => {