From d3b3942df881bd9b3f7e62ba27c75e52249350e1 Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Tue, 3 Dec 2024 20:33:54 +0300 Subject: [PATCH] chore(persons): sync first report with status history changes --- src/constants/table_encryption_map.ts | 1 + src/features/persons/basic_info/index.tsx | 1 + .../useBaptizedPublisher.tsx | 33 ++++++++++++--- .../first_report/useFirstReport.tsx | 40 ++++++++++++++++++- .../useUnbaptizedPublisher.tsx | 23 ++++++++++- src/services/app/persons.ts | 6 +++ src/services/dexie/schema.ts | 2 +- 7 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/constants/table_encryption_map.ts b/src/constants/table_encryption_map.ts index 9d4a165531..c479bc95fc 100644 --- a/src/constants/table_encryption_map.ts +++ b/src/constants/table_encryption_map.ts @@ -21,6 +21,7 @@ export const TABLE_ENCRYPTION_MAP = { privileges: 'shared', enrollments: 'shared', emergency_contacts: 'shared', + first_report: 'shared', }, app_settings: { country_code: 'public', diff --git a/src/features/persons/basic_info/index.tsx b/src/features/persons/basic_info/index.tsx index 82e22bc469..dc87ac1b64 100644 --- a/src/features/persons/basic_info/index.tsx +++ b/src/features/persons/basic_info/index.tsx @@ -57,6 +57,7 @@ const PersonBasicInfo = () => { justifyContent: 'space-between', gap: '16px', flexWrap: 'wrap', + minHeight: '28px', }} > {t('tr_basicInformation')} diff --git a/src/features/persons/spiritual_status/baptized_publisher/useBaptizedPublisher.tsx b/src/features/persons/spiritual_status/baptized_publisher/useBaptizedPublisher.tsx index 1980c13fe9..ac26956af1 100644 --- a/src/features/persons/spiritual_status/baptized_publisher/useBaptizedPublisher.tsx +++ b/src/features/persons/spiritual_status/baptized_publisher/useBaptizedPublisher.tsx @@ -6,11 +6,15 @@ import { setPersonCurrentDetails } from '@services/recoil/persons'; import { computeYearsDiff, dateFirstDayMonth } from '@utils/date'; import { formatDate } from '@services/dateformat'; import { PersonType } from '@definition/person'; +import useFirstReport from '../first_report/useFirstReport'; const useBaptizedPublisher = () => { const { id } = useParams(); + const isAddPerson = id === undefined; + const { updateFirstReport } = useFirstReport(); + const person = useRecoilValue(personCurrentDetailsState); const [age, setAge] = useState('0'); @@ -39,6 +43,7 @@ const useBaptizedPublisher = () => { new Date(activeRecord.start_date), 'yyyy/MM/dd' ); + const nowDate = formatDate(new Date(), 'yyyy/MM/dd'); if (start_date === nowDate) { @@ -60,6 +65,8 @@ const useBaptizedPublisher = () => { activeRecord.updatedAt = new Date().toISOString(); } + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); } @@ -71,14 +78,18 @@ const useBaptizedPublisher = () => { const handleAddHistory = async () => { const newPerson = structuredClone(person); + const startMonth = dateFirstDayMonth().toISOString(); + newPerson.person_data.publisher_baptized.history.push({ id: crypto.randomUUID(), _deleted: false, updatedAt: new Date().toISOString(), - start_date: dateFirstDayMonth().toISOString(), + start_date: startMonth, end_date: null, }); + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); }; @@ -101,6 +112,8 @@ const useBaptizedPublisher = () => { ); } + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); }; @@ -111,9 +124,19 @@ const useBaptizedPublisher = () => { (history) => history.id === id ); - current.start_date = value.toISOString(); + if (value === null) { + current.start_date = null; + } + + if (value !== null) { + const startMonth = dateFirstDayMonth(value).toISOString(); + current.start_date = startMonth; + } + current.updatedAt = new Date().toISOString(); + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); }; @@ -169,9 +192,7 @@ const useBaptizedPublisher = () => { ); if (histories.length === 0 && value) { - const startMonth = new Date( - formatDate(value, 'yyyy/MM/01') - ).toISOString(); + const startMonth = dateFirstDayMonth(value).toISOString(); newPerson.person_data.publisher_baptized.history.push({ _deleted: false, @@ -180,6 +201,8 @@ const useBaptizedPublisher = () => { start_date: startMonth, updatedAt: new Date().toISOString(), }); + + updateFirstReport(newPerson); } await setPersonCurrentDetails(newPerson); diff --git a/src/features/persons/spiritual_status/first_report/useFirstReport.tsx b/src/features/persons/spiritual_status/first_report/useFirstReport.tsx index 972f1c36a4..2cbc2e1c0f 100644 --- a/src/features/persons/spiritual_status/first_report/useFirstReport.tsx +++ b/src/features/persons/spiritual_status/first_report/useFirstReport.tsx @@ -2,6 +2,7 @@ import { useRecoilState } from 'recoil'; import { personCurrentDetailsState } from '@states/persons'; import { useMemo } from 'react'; import { formatDate } from '@services/dateformat'; +import { PersonType } from '@definition/person'; const useFirstReport = () => { const [person, setPerson] = useRecoilState(personCurrentDetailsState); @@ -12,7 +13,7 @@ const useFirstReport = () => { : null; }, [person]); - const handleChangeFirstReport = async (value: Date) => { + const handleChangeFirstReport = (value: Date) => { const newPerson = structuredClone(person); let finalValue = ''; @@ -96,7 +97,42 @@ const useFirstReport = () => { setPerson(newPerson); }; - return { handleChangeFirstReport, value }; + const updateFirstReport = (newPerson: PersonType) => { + const baptizedHistory = + newPerson.person_data.publisher_baptized.history.filter( + (record) => !record._deleted && record.start_date !== null + ); + + const unbaptizedHistory = + newPerson.person_data.publisher_unbaptized.history.filter( + (record) => !record._deleted && record.start_date !== null + ); + + const history = baptizedHistory.concat(unbaptizedHistory); + + if (history.length === 0) return; + + const minDate = history + .sort((a, b) => a.start_date.localeCompare(b.start_date)) + .at(0).start_date; + + const minDateFormatted = formatDate(new Date(minDate), 'yyyy/MM/dd'); + + const firstReport = newPerson.person_data.first_report.value; + + const currentFirstReport = firstReport + ? formatDate(new Date(firstReport), 'yyyy/MM/dd') + : null; + + if (minDateFormatted !== currentFirstReport) { + newPerson.person_data.first_report = { + value: minDate, + updatedAt: new Date().toISOString(), + }; + } + }; + + return { handleChangeFirstReport, value, updateFirstReport }; }; export default useFirstReport; diff --git a/src/features/persons/spiritual_status/unbaptized_publisher/useUnbaptizedPublisher.tsx b/src/features/persons/spiritual_status/unbaptized_publisher/useUnbaptizedPublisher.tsx index 64f0dc48ee..5ac61334a8 100644 --- a/src/features/persons/spiritual_status/unbaptized_publisher/useUnbaptizedPublisher.tsx +++ b/src/features/persons/spiritual_status/unbaptized_publisher/useUnbaptizedPublisher.tsx @@ -6,11 +6,15 @@ import { setPersonCurrentDetails } from '@services/recoil/persons'; import { PersonType } from '@definition/person'; import { formatDate } from '@services/dateformat'; import { dateFirstDayMonth } from '@utils/date'; +import useFirstReport from '../first_report/useFirstReport'; const useUnbaptizedPublisher = () => { const { id } = useParams(); + const isAddPerson = id === undefined; + const { updateFirstReport } = useFirstReport(); + const person = useRecoilValue(personCurrentDetailsState); const [isExpanded, setIsExpanded] = useState(false); @@ -38,6 +42,7 @@ const useUnbaptizedPublisher = () => { new Date(activeRecord.start_date), 'yyyy/MM/dd' ); + const nowDate = formatDate(new Date(), 'yyyy/MM/dd'); if (start_date === nowDate) { @@ -59,6 +64,8 @@ const useUnbaptizedPublisher = () => { activeRecord.updatedAt = new Date().toISOString(); } + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); } @@ -78,6 +85,8 @@ const useUnbaptizedPublisher = () => { end_date: null, }); + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); }; @@ -100,6 +109,8 @@ const useUnbaptizedPublisher = () => { ); } + updateFirstReport(newPerson); + await setPersonCurrentDetails(newPerson); }; @@ -110,8 +121,16 @@ const useUnbaptizedPublisher = () => { (history) => history.id === id ); - current.start_date = value.toISOString(); - current.updatedAt = new Date().toISOString(); + if (value === null) { + current.start_date = null; + } + + if (value !== null) { + const startMonth = dateFirstDayMonth(value).toISOString(); + current.start_date = startMonth; + } + + updateFirstReport(newPerson); await setPersonCurrentDetails(newPerson); }; diff --git a/src/services/app/persons.ts b/src/services/app/persons.ts index 06954f996d..6729288694 100644 --- a/src/services/app/persons.ts +++ b/src/services/app/persons.ts @@ -65,6 +65,8 @@ const personArchiveMidweekMeeting = ( (record) => record._deleted === false && record.end_date === null ); + if (!current) return; + const start_date = formatDate(new Date(current.start_date), 'yyyy/MM/dd'); const nowDate = formatDate(new Date(), 'yyyy/MM/dd'); @@ -99,6 +101,8 @@ const personArchiveUnbaptizedPublisher = ( (record) => record._deleted === false && record.end_date === null ); + if (!current) return; + const start_date = formatDate(new Date(current.start_date), 'yyyy/MM/dd'); const nowDate = formatDate(new Date(), 'yyyy/MM/dd'); @@ -132,6 +136,8 @@ const personArchiveBaptizedPublisher = ( (record) => record._deleted === false && record.end_date === null ); + if (!current) return; + const start_date = formatDate(new Date(current.start_date), 'yyyy/MM/dd'); const nowDate = formatDate(new Date(), 'yyyy/MM/dd'); diff --git a/src/services/dexie/schema.ts b/src/services/dexie/schema.ts index b85919484b..feb08cd1f8 100644 --- a/src/services/dexie/schema.ts +++ b/src/services/dexie/schema.ts @@ -310,7 +310,7 @@ export const settingSchema: SettingsType = { account_type: '', backup_automatic: { enabled: { value: true, updatedAt: '' }, - interval: { value: 5, updatedAt: '' }, + interval: { value: 15, updatedAt: '' }, }, hour_credits_enabled: { value: false, updatedAt: '' }, firstname: { value: '', updatedAt: '' },