Skip to content

Commit

Permalink
fix(persons): error message now shown when saving records
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 20, 2024
1 parent f5efc9b commit 2554da6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 74 deletions.
3 changes: 3 additions & 0 deletions src/features/persons/button_actions/useButtonActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ const useButtonActions = () => {

setIsDisqualify(false);
} catch (error) {
setIsDisqualify(false);

await displaySnackNotification({
header: getMessageByCode('error_app_generic-title'),
message: error.message,
Expand All @@ -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,
Expand Down
139 changes: 65 additions & 74 deletions src/services/dexie/persons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 2554da6

Please sign in to comment.