Skip to content

Commit

Permalink
chore(app): check for invalid data in backup
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 1, 2024
1 parent 35baf21 commit bfc6001
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/services/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -40,6 +41,7 @@ export const loadApp = async () => {
export const runUpdater = async () => {
await dbWeekTypeUpdate();
await dbAssignmentUpdate();
await dbSchedAuxClassUpdate();
};

export const userLogoutSuccess = async () => {
Expand Down
52 changes: 52 additions & 0 deletions src/services/dexie/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
57 changes: 57 additions & 0 deletions src/services/worker/backupUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit bfc6001

Please sign in to comment.