From bfc6001c0cabe03581e46c30459306826dce3c26 Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Sun, 1 Dec 2024 07:42:24 +0300 Subject: [PATCH] chore(app): check for invalid data in backup --- src/services/app/index.ts | 2 ++ src/services/dexie/schedules.ts | 52 +++++++++++++++++++++++++++ src/services/worker/backupUtils.ts | 57 ++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/src/services/app/index.ts b/src/services/app/index.ts index 3f0f01a42b..0783721731 100644 --- a/src/services/app/index.ts +++ b/src/services/app/index.ts @@ -19,6 +19,7 @@ import { songsBuildList } from '@services/i18n/songs'; import { setSongs } from '@services/recoil/songs'; import { schedulesBuildHistoryList } from './schedules'; import { setAssignmentsHistory } from '@services/recoil/schedules'; +import { dbSchedAuxClassUpdate } from '@services/dexie/schedules'; export const loadApp = async () => { const appLang = await promiseGetRecoil(appLangState); @@ -40,6 +41,7 @@ export const loadApp = async () => { export const runUpdater = async () => { await dbWeekTypeUpdate(); await dbAssignmentUpdate(); + await dbSchedAuxClassUpdate(); }; export const userLogoutSuccess = async () => { diff --git a/src/services/dexie/schedules.ts b/src/services/dexie/schedules.ts index 50d3ec4fcd..a40717f941 100644 --- a/src/services/dexie/schedules.ts +++ b/src/services/dexie/schedules.ts @@ -33,3 +33,55 @@ export const dbSchedBulkUpdate = async (weeks: SchedWeekType[]) => { await appDb.sched.bulkUpdate(data); }; + +export const dbSchedAuxClassUpdate = async () => { + const schedules = await appDb.sched.toArray(); + + const schedulesUpdate: SchedWeekType[] = []; + + for (const schedule of schedules) { + const obj = structuredClone(schedule); + + const midweek = obj.midweek_meeting; + + if (Array.isArray(midweek.chairman.aux_class_1)) { + midweek.chairman.aux_class_1 = midweek.chairman.aux_class_1[0]; + } + + if (Array.isArray(midweek.tgw_bible_reading.aux_class_1)) { + midweek.tgw_bible_reading.aux_class_1 = + midweek.tgw_bible_reading.aux_class_1[0]; + } + + if (Array.isArray(midweek.tgw_bible_reading.aux_class_2)) { + midweek.tgw_bible_reading.aux_class_2 = + midweek.tgw_bible_reading.aux_class_2[0]; + } + + if (Array.isArray(midweek.ayf_part1.aux_class_1)) { + midweek.ayf_part1.aux_class_1 = midweek.ayf_part1.aux_class_1[0]; + } + + if (Array.isArray(midweek.ayf_part1.aux_class_2)) { + midweek.ayf_part1.aux_class_2 = midweek.ayf_part1.aux_class_2[0]; + } + + if (Array.isArray(midweek.ayf_part2.aux_class_1)) { + midweek.ayf_part2.aux_class_1 = midweek.ayf_part2.aux_class_1[0]; + } + + if (Array.isArray(midweek.ayf_part2.aux_class_2)) { + midweek.ayf_part2.aux_class_2 = midweek.ayf_part2.aux_class_2[0]; + } + + if (Array.isArray(midweek.ayf_part3.aux_class_1)) { + midweek.ayf_part3.aux_class_1 = midweek.ayf_part3.aux_class_1[0]; + } + + if (Array.isArray(midweek.ayf_part3.aux_class_2)) { + midweek.ayf_part3.aux_class_2 = midweek.ayf_part3.aux_class_2[0]; + } + + schedulesUpdate.push(obj); + } +}; diff --git a/src/services/worker/backupUtils.ts b/src/services/worker/backupUtils.ts index eeca8eeaa4..04c419af93 100644 --- a/src/services/worker/backupUtils.ts +++ b/src/services/worker/backupUtils.ts @@ -893,6 +893,63 @@ const dbRestoreSchedules = async ( const newItem = structuredClone(localItem); syncFromRemote(newItem, remoteItem); + const midweek = newItem.midweek_meeting; + + if (Array.isArray(midweek.chairman.aux_class_1)) { + midweek.chairman.aux_class_1 = + localItem.midweek_meeting.chairman.aux_class_1; + } + + if (Array.isArray(midweek.tgw_bible_reading.aux_class_1)) { + midweek.tgw_bible_reading.aux_class_1 = + localItem.midweek_meeting.tgw_bible_reading.aux_class_1; + } + + if (Array.isArray(midweek.tgw_bible_reading.aux_class_2)) { + midweek.tgw_bible_reading.aux_class_2 = + localItem.midweek_meeting.tgw_bible_reading.aux_class_2; + } + + if (Array.isArray(midweek.ayf_part1.aux_class_1)) { + midweek.ayf_part1.aux_class_1 = + localItem.midweek_meeting.ayf_part1.aux_class_1; + } + + if (Array.isArray(midweek.ayf_part1.aux_class_2)) { + midweek.ayf_part1.aux_class_2 = + localItem.midweek_meeting.ayf_part1.aux_class_2; + } + + if (Array.isArray(midweek.ayf_part2.aux_class_1)) { + midweek.ayf_part2.aux_class_1 = + localItem.midweek_meeting.ayf_part2.aux_class_1; + } + + if (Array.isArray(midweek.ayf_part2.aux_class_2)) { + midweek.ayf_part2.aux_class_2 = + localItem.midweek_meeting.ayf_part2.aux_class_2; + } + + if (Array.isArray(midweek.ayf_part3.aux_class_1)) { + midweek.ayf_part3.aux_class_1 = + localItem.midweek_meeting.ayf_part3.aux_class_1; + } + + if (Array.isArray(midweek.ayf_part3.aux_class_2)) { + midweek.ayf_part3.aux_class_2 = + localItem.midweek_meeting.ayf_part3.aux_class_2; + } + + if (Array.isArray(midweek.ayf_part4.aux_class_1)) { + midweek.ayf_part4.aux_class_1 = + localItem.midweek_meeting.ayf_part4.aux_class_1; + } + + if (Array.isArray(midweek.ayf_part4.aux_class_2)) { + midweek.ayf_part4.aux_class_2 = + localItem.midweek_meeting.ayf_part4.aux_class_2; + } + dataToUpdate.push(newItem); } }