From 7e0eb530ad6cf7f752ab071129750ef8761e7ffc Mon Sep 17 00:00:00 2001 From: rhahao <26148770+rhahao@users.noreply.github.com> Date: Sun, 26 Feb 2023 17:02:07 +0300 Subject: [PATCH] feat(persons): use person uid as primary key in indexedDb --- .../backupRestore/RestoreDbDialog.jsx | 1 + src/features/persons/PersonAssignments.jsx | 4 +- src/indexedDb/dbAssignment.js | 2 +- src/indexedDb/dbPersons.js | 89 +++++++------------ src/indexedDb/dbUtility.js | 4 +- src/indexedDb/mainDb.js | 35 ++++++++ src/locales/en/ui.json | 28 ++---- src/states/congregation.js | 5 -- src/utils/updater.js | 56 +----------- 9 files changed, 83 insertions(+), 141 deletions(-) diff --git a/src/features/backupRestore/RestoreDbDialog.jsx b/src/features/backupRestore/RestoreDbDialog.jsx index dc1ba93cfc..920600b382 100644 --- a/src/features/backupRestore/RestoreDbDialog.jsx +++ b/src/features/backupRestore/RestoreDbDialog.jsx @@ -57,6 +57,7 @@ const RestoreDbDialog = () => { if (res.status === 200) { const { cong_persons, cong_schedule, cong_sourceMaterial, cong_swsPocket, cong_settings } = data; + console.log(cong_persons); await dbRestoreCongregationBackup( cong_persons, diff --git a/src/features/persons/PersonAssignments.jsx b/src/features/persons/PersonAssignments.jsx index c92683be6a..91ce13da42 100644 --- a/src/features/persons/PersonAssignments.jsx +++ b/src/features/persons/PersonAssignments.jsx @@ -255,7 +255,7 @@ const PersonAssignments = ({ student, assignments, setAssignments }) => { onChange={(e) => handleAssignmentsChange(115, e.target.checked)} /> } - label={t('cbsConductor', { ns: 'source' })} + label={`${t('cbs', { ns: 'source' })} - ${t('cbsConductor', { ns: 'source' })}`} /> { onChange={(e) => handleAssignmentsChange(116, e.target.checked)} /> } - label={t('cbsReader', { ns: 'source' })} + label={`${t('cbs', { ns: 'source' })} - ${t('cbsReader', { ns: 'source' })}`} /> diff --git a/src/indexedDb/dbAssignment.js b/src/indexedDb/dbAssignment.js index 4dbd1eeb74..96c0bc58bd 100644 --- a/src/indexedDb/dbAssignment.js +++ b/src/indexedDb/dbAssignment.js @@ -546,7 +546,7 @@ export const dbRefreshStudentHistory = async (varPrev, varNew) => { const findIndex = obj.changes.findIndex((item) => item.field === 'lastAssignment'); if (findIndex !== -1) obj.changes.splice(findIndex, 1); obj.changes.push({ date: new Date().toISOString(), field: 'lastAssignment', value: stuAssignment }); - await appDb.table('persons').update(student.id, { ...obj }); + await appDb.table('persons').update(student.person_uid, { ...obj }); const students = await dbGetStudentsMini(); await promiseSetRecoil(allStudentsState, students); diff --git a/src/indexedDb/dbPersons.js b/src/indexedDb/dbPersons.js index f26b6f4789..f794441c1c 100644 --- a/src/indexedDb/dbPersons.js +++ b/src/indexedDb/dbPersons.js @@ -16,30 +16,19 @@ export const dbGetStudents = async () => { const appData = data.filter((student) => student.isMoved === undefined || student.isMoved === false); - for (let i = 0; i < appData.length; i++) { - let person = {}; - person.id = appData[i].id; - person.person_uid = appData[i].person_uid; - person.person_name = appData[i].person_name; - person.person_displayName = appData[i].person_displayName; - person.isMale = appData[i].isMale; - person.isFemale = appData[i].isFemale; - person.isUnavailable = appData[i].isUnavailable; - person.forLivePart = appData[i].forLivePart; - person.viewOnlineSchedule = appData[i].viewOnlineSchedule || false; - person.student_PIN = appData[i].student_PIN || ''; - person.viewStudent_Part = appData[i].viewStudent_Part || []; - person.lastBRead = appData[i].lastBRead || ''; - person.lastInitialCall = appData[i].lastInitialCall || ''; - person.lastReturnVisit = appData[i].lastReturnVisit || ''; - person.lastBibleStudy = appData[i].lastBibleStudy || ''; - person.lastTalk = appData[i].lastTalk || ''; - person.isMoved = appData[i].isMoved || false; - person.isDisqualified = appData[i].isDisqualified || false; - let assignments = appData[i].assignments || []; - person.assignments = sortHistoricalDateDesc(assignments); - - let timeAway = appData[i].timeAway || []; + for (const item of appData) { + const person = {}; + person.person_uid = item.person_uid; + person.person_name = item.person_name; + person.person_displayName = item.person_displayName; + person.isMale = item.isMale; + person.isFemale = item.isFemale; + person.isUnavailable = item.isUnavailable; + person.isMoved = item.isMoved || false; + person.isDisqualified = item.isDisqualified || false; + person.assignments = item.assignments || []; + + let timeAway = item.timeAway || []; person.timeAway = sortHistoricalDateDesc(timeAway); allStudents.push(person); @@ -54,23 +43,23 @@ export const dbGetStudentsMini = async () => { const appData = data.filter((student) => student.isMoved === false); - for (let i = 0; i < appData.length; i++) { - let person = {}; - person.id = appData[i].id; - person.person_uid = appData[i].person_uid; - person.person_name = appData[i].person_name; - person.person_displayName = appData[i].person_displayName; - person.isMale = appData[i].isMale; - person.isFemale = appData[i].isFemale; - person.isDisqualified = appData[i].isDisqualified || false; - person.lastAssignment = appData[i].lastAssignment; - person.assignments = appData[i].assignments; - - let timeAway = appData[i].timeAway || []; + for (const item of appData) { + const person = {}; + person.person_uid = item.person_uid; + person.person_name = item.person_name; + person.person_displayName = item.person_displayName; + person.isMale = item.isMale; + person.isFemale = item.isFemale; + person.isDisqualified = item.isDisqualified || false; + person.lastAssignment = item.lastAssignment; + person.assignments = item.assignments; + + let timeAway = item.timeAway || []; person.timeAway = sortHistoricalDateDesc(timeAway); allStudents.push(person); } + return allStudents; }; @@ -84,24 +73,18 @@ export const dbIsStudentExist = async (varName) => { }; export const dbSavePersonData = async (personData) => { - const person = await dbGetStudentDetails(personData.person_uid); - - await appDb.table('persons').update(person.id, { + await appDb.table('persons').update(personData.person_uid, { person_name: personData.person_name, person_displayName: personData.person_displayName, isMale: personData.isMale, isFemale: personData.isFemale, isUnavailable: personData.isUnavailable, - viewOnlineSchedule: personData.viewOnlineSchedule, - student_PIN: personData.student_PIN, - viewStudent_Part: personData.viewStudent_Part, assignments: personData.assignments, }); }; export const dbDeleteStudent = async (uid) => { - const appData = await appDb.table('persons').get({ person_uid: uid }); - await appDb.persons.delete(appData.id); + await appDb.persons.delete(uid); const data = { table: 'persons', @@ -123,11 +106,6 @@ export const dbAddPersonData = async (personData) => { }); }; -export const dbGetStudentUidById = async (id) => { - const appData = await appDb.table('persons').get({ id: id }); - return appData.person_uid; -}; - export const dbGetStudentByUid = async (uid) => { const appData = await appDb.table('persons').get({ person_uid: uid }); return appData; @@ -288,14 +266,13 @@ export const dbHistoryAssistant = async (mainStuID) => { }; export const dbSavePerson = async (uid, data) => { - const person = await dbGetStudentDetails(uid); - await appDb.table('persons').update(person.id, { + await appDb.table('persons').update(uid, { ...data, }); }; export const dbSavePersonMigration = async (data) => { - await appDb.table('persons').update(data.id, { + await appDb.table('persons').update(data.person_uid, { ...data, }); }; @@ -328,13 +305,13 @@ export const dbBuildPocketUsers = async () => { }; export const dbSavePersonExp = async (data) => { - const { id, person_name, person_displayName } = data; + const { person_uid, person_name, person_displayName } = data; if (person_name && person_displayName) { - if (id) { + if (person_uid) { if (data.historyAssignments) delete data.historyAssignments; const person = await dbGetStudentByUid(data.person_uid); data.changes = comparePerson(person, data); - await appDb.table('persons').update(id, data); + await appDb.table('persons').update(person_uid, data); } else { let obj = { person_uid: window.crypto.randomUUID(), diff --git a/src/indexedDb/dbUtility.js b/src/indexedDb/dbUtility.js index 6d08c431b7..61528a7615 100644 --- a/src/indexedDb/dbUtility.js +++ b/src/indexedDb/dbUtility.js @@ -298,7 +298,8 @@ export const dbRestoreCongregationBackup = async ( }); } - await appDb.table('persons').update(oldPerson.id, oldPerson); + if (oldPerson.id) delete oldPerson.id; + await appDb.table('persons').update(oldPerson.person_uid, oldPerson); } } @@ -306,6 +307,7 @@ export const dbRestoreCongregationBackup = async ( for await (const newPerson of cong_persons) { const oldPerson = oldPersons.find((person) => person.person_uid === newPerson.person_uid); if (!oldPerson) { + if (newPerson.id) delete newPerson.id; await appDb.persons.add(newPerson); } } diff --git a/src/indexedDb/mainDb.js b/src/indexedDb/mainDb.js index b2e65e5ebe..08970ea845 100644 --- a/src/indexedDb/mainDb.js +++ b/src/indexedDb/mainDb.js @@ -19,6 +19,41 @@ appDb.version(2).stores({ sched_MM: '&weekOf, bRead_stu_A, bRead_stu_A_name, bRead_stu_A_dispName, bRead_stu_B, bRead_stu_B_name, bRead_stu_B_dispName, ass1_stu_A, ass1_stu_A_name, ass1_stu_A_dispName, ass1_ass_A, ass1_ass_A_name, ass1_ass_A_dispName, ass1_stu_B, ass1_stu_B_name, ass1_stu_B_dispName, ass1_ass_B, ass1_ass_B_name, ass1_ass_B_dispName, ass2_stu_A, ass2_stu_A_name, ass2_stu_A_dispName, ass2_ass_A, ass2_ass_A_name, ass2_ass_A_dispName, ass2_stu_B, ass2_stu_B_name, ass2_stu_B_dispName, ass2_ass_B, ass2_ass_B_name, ass2_ass_B_dispName, ass3_stu_A, ass3_stu_A_name, ass3_stu_A_dispName, ass3_ass_A, ass3_ass_A_name, ass3_ass_A_dispName, ass3_stu_B, ass3_stu_B_name, ass3_stu_B_dispName, ass3_ass_B, ass3_ass_B_name, ass3_ass_B_dispName, ass4_stu_A, ass4_stu_A_name, ass4_stu_A_dispName, ass4_ass_A, ass4_ass_A_name, ass4_ass_A_dispName, ass4_stu_B, ass4_stu_B_name, ass4_stu_B_dispName, ass4_ass_B, ass4_ass_B_name, ass4_ass_B_dispName, week_type, noMeeting, isReleased, chairmanMM_A, chairmanMM_A_name, chairmanMM_A_dispName, chairmanMM_B, chairmanMM_B_name, chairmanMM_B_dispName, opening_prayer, opening_prayer_name, opening_prayer_dispName, tgw_talk, tgw_talk_name, tgw_talk_dispName, tgw_gems, tgw_gems_name, tgw_gems_dispName, lc_part1, lc_part1_name, lc_part1_dispName, lc_part2, lc_part2_name, lc_part2_dispName, cbs_conductor, cbs_conductor_name, cbs_conductor_dispName, cbs_reader, cbs_reader_name, cbs_reader_dispName, closing_prayer, closing_prayer_name, closing_prayer_dispName, changes', }); +appDb + .version(6) + .stores({ + personsTmp: + '&person_uid, person_name, person_displayName, isMale, isFemale, isUnavailable, lastAssignment, assignments, timeAway, isMoved, isDisqualified, isChairmanMM, isPrayerMM, isTGWTalk, isTGWGems, isLCPart, isCBSConductor, isCBSReader, changes', + }) + .upgrade(async (trans) => { + const oldPersons = await trans.persons.toArray(); + for await (const person of oldPersons) { + delete person.id; + await trans.personsTmp.put(person, person.person_uid); + } + }); +appDb.version(7).stores({ + persons: null, +}); +appDb + .version(8) + .stores({ + persons: + '&person_uid, person_name, person_displayName, isMale, isFemale, isUnavailable, lastAssignment, assignments, timeAway, isMoved, isDisqualified, isChairmanMM, isPrayerMM, isTGWTalk, isTGWGems, isLCPart, isCBSConductor, isCBSReader, changes', + }) + .upgrade(async (trans) => { + const oldPersons = await trans.personsTmp.toArray(); + for await (const person of oldPersons) { + await trans.persons.put(person, person.person_uid); + } + }); +appDb.version(9).stores({ + personsTmp: null, +}); +appDb.version(10).stores({ + persons: + '&person_uid, person_name, person_displayName, isMale, isFemale, isUnavailable, lastAssignment, assignments, timeAway, isMoved, isDisqualified, changes', +}); appDb.on('populate', function () { appDb.app_settings.add({ diff --git a/src/locales/en/ui.json b/src/locales/en/ui.json index 65c6ab8760..a02d6b4b62 100644 --- a/src/locales/en/ui.json +++ b/src/locales/en/ui.json @@ -232,19 +232,12 @@ "female": "Female", "allAssignments": "All assignments", "searchError": "Your search query is invalid!", - "sendListOnline": "Export list for online use", - "exportListLocal": "Export list to xlsx", - "addNew": "Add new student", - "deleteSuccess": "Student deleted successfully", + "addNew": "Add new person", "list": "List", - "addedSuccess": "Student added successfully", - "modifiedSuccess": "Student details updated successfully", "other": "Other details", - "livePart": "Available for live meeting", "deleteTitle": "Delete {{ currentStudent }}", - "deleteConfirmation": "Are you sure to delete this student from the record?", - "existAlready": "This student already exists in your record", - "deleteSucess": "Student delete successfully", + "deleteConfirmation": "Are you sure to delete this person from the record?", + "deleteSucess": "Person deleted successfully", "basicInfo": "Basic information", "assignments": "Assignments", "assignmentsHistory": "Assignments History", @@ -255,14 +248,9 @@ "enablePerson": "Enable", "markDisqualified": "Mark Disqualified", "markTransfer": "Mark Moved", - "missingInfo": "Some student’s information are missing", + "missingInfo": "Some information are missing", "disqualifiedLabel": "Disqualified", - "accessPocketApp": "Online schedule", - "setupPocketAccess": "Setup", - "addPocketDevice": "Add device", - "setupPocketToken": "Please give the following code to the student in order to open the SWS Pocket application. This code can only be used once", "viewOnBehalf": "View members schedule", - "pocketSessions": "This is a list of devices that the student is using for SWS Pocket application. If needed, revoke any session that no longer needed.", "searchResult": "Search results", "recentStudents": "Viewed recently", "clearRecents": "Clear recents", @@ -290,10 +278,7 @@ "epubInProgress": "EPUB import in progress. Please wait ...", "downloadInProgress": "Please wait while downloading source materials from JW.ORG ...", "autofill": "Autofill", - "send": "Send", - "sendToMSC": "To MSC", - "sendToStudents": "To students", - "assignStudent": "Assign student", + "assignStudent": "Assign person", "s89": "S-89 Assignment Form", "availableStudents": "Available for assignment", "studentHistory": "{{ currentStudent }} (History)", @@ -329,7 +314,6 @@ "coTalk": "Talk given by Circuit Overseer", "aboutCongregation": "ABOUT CONGREGATION", "blankRequired": "Please provide input", - "liveClass": "Students for live meeting only", "saved": "Congregation settings updated successfully", "dataStorage": "DATA STORAGE", "createLocalBackup": "Create", @@ -365,7 +349,7 @@ "myAccount": "MY ACCOUNT", "emailLocked": "This email address can no longer be changed. Please contact us if you want to change it.", "twoFactor": "Two-factor authentication", - "twoFactorDesc": "Two-factor authentication adds an additional layer of security to your account by requiring more than just a password to sign in.", + "twoFactorDesc": "Two-factor authentication adds an additional layer of security to your account.", "twoFactorAddDevice": "Add device", "twoFactorTitle": "Two-factor authentication details", "twoFactorApp": "Use Microsoft Authenticator or Google Authenticator app in your new device to scan the code QR below", diff --git a/src/states/congregation.js b/src/states/congregation.js index 7b3d663994..1b4c3d0bdb 100644 --- a/src/states/congregation.js +++ b/src/states/congregation.js @@ -45,11 +45,6 @@ export const meetingTimeState = atom({ default: new Date(Date.now()), }); -export const liveClassState = atom({ - key: 'liveClass', - default: false, -}); - export const usernameState = atom({ key: 'username', default: '', diff --git a/src/utils/updater.js b/src/utils/updater.js index 0a75b3abf5..40fda2df41 100644 --- a/src/utils/updater.js +++ b/src/utils/updater.js @@ -4,8 +4,7 @@ import dateFormat from 'dateformat'; import appDb from '../indexedDb/mainDb'; import { LANGUAGE_LIST } from '../locales/langList.js'; import { dbGetAppSettings, dbUpdateAppSettings } from '../indexedDb/dbAppSettings'; -import { dbGetStudents, dbGetStudentUidById, dbSavePersonMigration } from '../indexedDb/dbPersons'; -import { dbSaveScheduleByAss } from '../indexedDb/dbSchedule'; +import { dbGetStudents, dbSavePersonMigration } from '../indexedDb/dbPersons'; import { dbFirstBibleStudy, dbFirstBRead, @@ -22,7 +21,6 @@ export const runUpdater = async () => { await removeInvalidWeeks(); await updateWeekType(); await updateAssignmentType(); - await updateScheduleToId(); await removeOutdatedSettings(); await builtHistoricalAssignment(); await updatePersonAssignments(); @@ -30,56 +28,6 @@ export const runUpdater = async () => { await loadApp(); }; -const updateScheduleToId = async () => { - let appSettings = await dbGetAppSettings(); - if (!appSettings.isScheduleConverted) { - const scheduleData = await appDb.table('sched_MM').toArray(); - - for (let c = 0; c < scheduleData.length; c++) { - const schedule = scheduleData[c]; - if (schedule.bRead_stu_A !== undefined) { - const uid = await dbGetStudentUidById(schedule.bRead_stu_A); - - await dbSaveScheduleByAss('bRead_stu_A', uid, schedule.weekOf); - } - if (schedule.bRead_stu_B !== undefined) { - const uid = await dbGetStudentUidById(schedule.bRead_stu_B); - await dbSaveScheduleByAss('bRead_stu_B', uid, schedule.weekOf); - } - for (let d = 1; d <= 4; d++) { - const fldNameA = `ass${d}_stu_A`; - if (schedule[fldNameA] !== undefined) { - const uid = await dbGetStudentUidById(schedule[fldNameA]); - await dbSaveScheduleByAss(fldNameA, uid, schedule.weekOf); - } - - const fldNameAssA = `ass${d}_ass_A`; - if (schedule[fldNameAssA] !== undefined) { - const uid = await dbGetStudentUidById(schedule[fldNameAssA]); - await dbSaveScheduleByAss(fldNameAssA, uid, schedule.weekOf); - } - - const fldNameB = `ass${d}_stu_B`; - if (schedule[fldNameB] !== undefined) { - const uid = await dbGetStudentUidById(schedule[fldNameB]); - await dbSaveScheduleByAss(fldNameB, uid, schedule.weekOf); - } - - const fldNameAssB = `ass${d}_ass_B`; - if (schedule[fldNameAssB] !== undefined) { - const uid = await dbGetStudentUidById(schedule[fldNameAssB]); - await dbSaveScheduleByAss(fldNameAssB, uid, schedule.weekOf); - } - } - } - - // save settings - let obj = {}; - obj.isScheduleConverted = true; - await dbUpdateAppSettings(obj); - } -}; - const removeOutdatedSettings = async () => { let appSettings = await dbGetAppSettings(); @@ -643,7 +591,7 @@ const updatePersonAssignments = async () => { } }); - await appDb.persons.update(person.id, { + await appDb.persons.update(person.person_uid, { assignments: newAssignments, }); }