Skip to content

Commit

Permalink
feat(persons): use person uid as primary key in indexedDb
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Feb 26, 2023
1 parent 7e8fd31 commit 7e0eb53
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 141 deletions.
1 change: 1 addition & 0 deletions src/features/backupRestore/RestoreDbDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/features/persons/PersonAssignments.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' })}`}
/>
<FormControlLabel
control={
Expand All @@ -265,7 +265,7 @@ const PersonAssignments = ({ student, assignments, setAssignments }) => {
onChange={(e) => handleAssignmentsChange(116, e.target.checked)}
/>
}
label={t('cbsReader', { ns: 'source' })}
label={`${t('cbs', { ns: 'source' })} - ${t('cbsReader', { ns: 'source' })}`}
/>
</FormGroup>
</Box>
Expand Down
2 changes: 1 addition & 1 deletion src/indexedDb/dbAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
89 changes: 33 additions & 56 deletions src/indexedDb/dbPersons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};

Expand All @@ -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',
Expand All @@ -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;
Expand Down Expand Up @@ -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,
});
};
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 3 additions & 1 deletion src/indexedDb/dbUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,16 @@ 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);
}
}

// handle new person record
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);
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/indexedDb/mainDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
28 changes: 6 additions & 22 deletions src/locales/en/ui.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions src/states/congregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down
Loading

0 comments on commit 7e0eb53

Please sign in to comment.