Skip to content

Commit

Permalink
chore(persons): sync first report with status history changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 3, 2024
1 parent 8628fe4 commit d3b3942
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/constants/table_encryption_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const TABLE_ENCRYPTION_MAP = {
privileges: 'shared',
enrollments: 'shared',
emergency_contacts: 'shared',
first_report: 'shared',
},
app_settings: {
country_code: 'public',
Expand Down
1 change: 1 addition & 0 deletions src/features/persons/basic_info/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const PersonBasicInfo = () => {
justifyContent: 'space-between',
gap: '16px',
flexWrap: 'wrap',
minHeight: '28px',
}}
>
<Typography className="h2">{t('tr_basicInformation')}</Typography>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -60,6 +65,8 @@ const useBaptizedPublisher = () => {
activeRecord.updatedAt = new Date().toISOString();
}

updateFirstReport(newPerson);

await setPersonCurrentDetails(newPerson);
}

Expand All @@ -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);
};

Expand All @@ -101,6 +112,8 @@ const useBaptizedPublisher = () => {
);
}

updateFirstReport(newPerson);

await setPersonCurrentDetails(newPerson);
};

Expand All @@ -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);
};

Expand Down Expand Up @@ -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,
Expand All @@ -180,6 +201,8 @@ const useBaptizedPublisher = () => {
start_date: startMonth,
updatedAt: new Date().toISOString(),
});

updateFirstReport(newPerson);
}

await setPersonCurrentDetails(newPerson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -12,7 +13,7 @@ const useFirstReport = () => {
: null;
}, [person]);

const handleChangeFirstReport = async (value: Date) => {
const handleChangeFirstReport = (value: Date) => {
const newPerson = structuredClone(person);

let finalValue = '';
Expand Down Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -59,6 +64,8 @@ const useUnbaptizedPublisher = () => {
activeRecord.updatedAt = new Date().toISOString();
}

updateFirstReport(newPerson);

await setPersonCurrentDetails(newPerson);
}

Expand All @@ -78,6 +85,8 @@ const useUnbaptizedPublisher = () => {
end_date: null,
});

updateFirstReport(newPerson);

await setPersonCurrentDetails(newPerson);
};

Expand All @@ -100,6 +109,8 @@ const useUnbaptizedPublisher = () => {
);
}

updateFirstReport(newPerson);

await setPersonCurrentDetails(newPerson);
};

Expand All @@ -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);
};
Expand Down
6 changes: 6 additions & 0 deletions src/services/app/persons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion src/services/dexie/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '' },
Expand Down

0 comments on commit d3b3942

Please sign in to comment.