Skip to content

Commit

Permalink
feat(congregation): check retention when sending backup
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao authored Feb 28, 2023
1 parent e140264 commit 8a96142
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 102 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"i18next": "^22.4.10",
"is-online": "^10.0.0",
"jsdom": "^21.1.0",
"jw-epub-parser": "^1.33.0",
"jw-epub-parser": "^1.34.0",
"node-2fa": "^2.0.3",
"node-fetch": "^3.3.0",
"nodemailer": "^6.9.1",
Expand Down
142 changes: 84 additions & 58 deletions src/classes/Congregation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import randomstring from 'randomstring';
import { FieldValue, getFirestore } from 'firebase-admin/firestore';
import { decryptData, encryptData } from '../utils/encryption-utils.js';
import { users } from './Users.js';
import { getOldestWeekDate, getWeekDate } from '../utils/date.js';

const db = getFirestore(); //get default database

Expand Down Expand Up @@ -105,6 +106,8 @@ Congregation.prototype.saveBackup = async function (
cong_settings,
uid
) {
const oldestWeekDate = getOldestWeekDate();

let finalPersons = [];

// new backup persons
Expand Down Expand Up @@ -248,107 +251,130 @@ Congregation.prototype.saveBackup = async function (

// new backup schedule
if (this.cong_schedule_draft.length === 0) {
finalSchedule = cong_schedule;
cong_schedule.forEach((schedule) => {
const weekOfDate = getWeekDate(schedule.weekOf);

if (weekOfDate >= oldestWeekDate) {
finalSchedule.push(schedule);
}
});
}

// updated schedule
if (this.cong_schedule_draft.length > 0) {
// handle modified schedule
this.cong_schedule_draft.forEach((oldSchedule) => {
const newSchedule = cong_schedule.find((schedule) => schedule.weekOf === oldSchedule.weekOf);
if (newSchedule) {
const oldChanges = oldSchedule.changes;
const newChanges = newSchedule.changes;
const weekOfDate = getWeekDate(oldSchedule.weekOf);

if (newChanges) {
newChanges.forEach((change) => {
let isChanged = false;
if (weekOfDate >= oldestWeekDate) {
const newSchedule = cong_schedule.find((schedule) => schedule.weekOf === oldSchedule.weekOf);
if (newSchedule) {
const oldChanges = oldSchedule.changes;
const newChanges = newSchedule.changes;

const oldChange = oldChanges?.find((old) => old.field === change.field);
const originalDate = oldChange?.date || undefined;

if (!oldChange) {
isChanged = true;
}
if (newChanges) {
newChanges.forEach((change) => {
let isChanged = false;

if (originalDate) {
const dateA = new Date(originalDate);
const dateB = new Date(change.date);
const oldChange = oldChanges?.find((old) => old.field === change.field);
const originalDate = oldChange?.date || undefined;

if (dateB > dateA) {
if (!oldChange) {
isChanged = true;
}
}

if (isChanged) {
oldSchedule[change.field] = change.value || null;
if (originalDate) {
const dateA = new Date(originalDate);
const dateB = new Date(change.date);

if (oldSchedule.changes) {
const findIndex = oldSchedule.changes.findIndex((item) => item.field === change.field) || -1;
if (findIndex !== -1) oldSchedule.changes.splice(findIndex, 1);
if (dateB > dateA) {
isChanged = true;
}
}

if (!oldSchedule.changes) {
oldSchedule.changes = [];
}
if (isChanged) {
oldSchedule[change.field] = change.value || null;

oldSchedule.changes.push(change);
}
});
if (oldSchedule.changes) {
const findIndex = oldSchedule.changes.findIndex((item) => item.field === change.field) || -1;
if (findIndex !== -1) oldSchedule.changes.splice(findIndex, 1);
}

if (!oldSchedule.changes) {
oldSchedule.changes = [];
}

oldSchedule.changes.push(change);
}
});
}
}
}

finalSchedule.push(oldSchedule);
finalSchedule.push(oldSchedule);
}
});

// handle new schedule record
cong_schedule.forEach((newSchedule) => {
const oldSchedule = this.cong_schedule_draft.find((schedule) => schedule.weekOf === newSchedule.weekOf);
if (!oldSchedule) {
finalSchedule.push(newSchedule);
const weekOfDate = getWeekDate(newSchedule.weekOf);
const oldestWeekDate = getOldestWeekDate();

if (weekOfDate >= oldestWeekDate) {
const oldSchedule = this.cong_schedule_draft.find((schedule) => schedule.weekOf === newSchedule.weekOf);
if (!oldSchedule) {
finalSchedule.push(newSchedule);
}
}
});
}

cong_sourceMaterial.forEach((newSource) => {
const oldSource = this.cong_sourceMaterial_draft.find((source) => source.weekOf === newSource.weekOf);
const oldSourceIndex = this.cong_sourceMaterial_draft.findIndex((source) => source.weekOf === newSource.weekOf);

if (!oldSource) {
this.cong_sourceMaterial_draft.push(newSource);
}
const weekOfDate = getWeekDate(newSource.weekOf);

if (oldSource) {
// restore keepOverride if qualified
const newKeepOverride = newSource.keepOverride || undefined;
const oldKeepOverride = oldSource ? oldSource.keepOverride : undefined;
let isRestore = false;
if (weekOfDate >= oldestWeekDate) {
const oldSource = this.cong_sourceMaterial_draft.find((source) => source.weekOf === newSource.weekOf);
const oldSourceIndex = this.cong_sourceMaterial_draft.findIndex((source) => source.weekOf === newSource.weekOf);

if (!newKeepOverride) {
isRestore = true;
if (!oldSource) {
this.cong_sourceMaterial_draft.push(newSource);
}

if (newKeepOverride && oldKeepOverride) {
const oldDate = new Date(oldKeepOverride);
const newDate = new Date(newKeepOverride);
if (oldSource) {
// restore keepOverride if qualified
const newKeepOverride = newSource.keepOverride || undefined;
const oldKeepOverride = oldSource ? oldSource.keepOverride : undefined;
let isRestore = false;

if (oldDate > newDate) {
if (!newKeepOverride) {
isRestore = true;
}
}

if (isRestore) {
for (const [key, value] of Object.entries(oldSource)) {
if (key.indexOf('_override') !== -1) {
if (value) newSource[key] = value;
if (newKeepOverride && oldKeepOverride) {
const oldDate = new Date(oldKeepOverride);
const newDate = new Date(newKeepOverride);

if (oldDate > newDate) {
isRestore = true;
}
}

this.cong_sourceMaterial_draft.splice(oldSourceIndex, 1, newSource);
if (isRestore) {
for (const [key, value] of Object.entries(oldSource)) {
if (key.indexOf('_override') !== -1) {
if (value) newSource[key] = value;
}
}

this.cong_sourceMaterial_draft.splice(oldSourceIndex, 1, newSource);
}
}
}
});

this.cong_sourceMaterial_draft = this.cong_sourceMaterial_draft.filter(
(source) => getWeekDate(source.weekOf) >= oldestWeekDate
);

const userInfo = users.findUserByAuthUid(uid);

const buffer = Buffer.from(encryptedPersons, 'utf-8');
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/public-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { extractScheduleDocsId, fetchData } from '../utils/public-utils.js';
export const getSchedules = async (req, res, next) => {
try {
let { language } = req.params;
const issue = req.headers.issuedate || '';

language = language.toUpperCase();

const mergedSources = await fetchData(language);
const mergedSources = await fetchData(language, issue);

if (mergedSources.length > 0) {
res.locals.type = 'info';
Expand Down
16 changes: 16 additions & 0 deletions src/utils/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const getWeekDate = (weekOf) => {
const month = +weekOf.split('/')[0] - 1;
const day = +weekOf.split('/')[1];
const year = +weekOf.split('/')[2];

return new Date(year, month, day);
};

export const getOldestWeekDate = () => {
const today = new Date();
const day = today.getDay();
const diff = today.getDate() - day + (day === 0 ? -6 : 1);
const weekDate = new Date(today.setDate(diff));
const validDate = weekDate.setMonth(weekDate.getMonth() - 12);
return new Date(validDate);
};
Loading

0 comments on commit 8a96142

Please sign in to comment.