Skip to content

Commit

Permalink
fix(backup): user field service reports and bible studies not included
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 5, 2024
1 parent 0b63512 commit 16075d2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
48 changes: 36 additions & 12 deletions src/v3/classes/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { UserCongregationAssignParams, UserProfile, UserSession, UserSettings }
import {
getUserAuthDetails,
getUserDetails,
setUserBibleStudies,
setUserEmail,
setUserFieldServiceReports,
setUserProfile,
setUserSessions,
setUserSettings,
Expand All @@ -12,6 +14,7 @@ import { decryptData, encryptData } from '../services/encryption/encryption.js';
import { generateUserSecret } from '../utils/user_utils.js';
import { CongregationsList } from './Congregations.js';
import { saveCongPersons, saveIncomingReports } from '../services/firebase/congregations.js';
import { BackupData } from '../definition/congregation.js';

export class User {
id: string;
Expand Down Expand Up @@ -281,22 +284,43 @@ export class User {
}
}

async saveBackup(backup: object) {
const data = backup as Record<string, object | string>;
async saveBackup(cong_backup: BackupData, userRole: AppRoleType[]) {
const userSettings = cong_backup.app_settings.user_settings;

const profile = structuredClone(this.profile);
profile.firstname = data['firstname'] as UserProfile['firstname'];
profile.lastname = data['lastname'] as UserProfile['lastname'];
if (userSettings) {
const data = userSettings as Record<string, object | string>;

const profile = structuredClone(this.profile);
profile.firstname = data['firstname'] as UserProfile['firstname'];
profile.lastname = data['lastname'] as UserProfile['lastname'];

await this.updateProfile(profile);

const settings = structuredClone(this.settings);
settings.backup_automatic = data['backup_automatic'] as string;
settings.data_view = data['data_view'] as string;
settings.hour_credits_enabled = data['hour_credits_enabled'] as string;
settings.theme_follow_os_enabled = data['theme_follow_os_enabled'] as string;

await this.updateSettings(settings);
}

const isPublisher = userRole.includes('publisher');

const userFieldServiceReports = cong_backup.user_field_service_reports;
const userBibleStudies = cong_backup.user_bible_studies;

await this.updateProfile(profile);
if (isPublisher && userBibleStudies) {
await setUserBibleStudies(this.id, userBibleStudies);

const settings = structuredClone(this.settings);
settings.backup_automatic = data['backup_automatic'] as string;
settings.data_view = data['data_view'] as string;
settings.hour_credits_enabled = data['hour_credits_enabled'] as string;
settings.theme_follow_os_enabled = data['theme_follow_os_enabled'] as string;
this.bible_studies = userBibleStudies;
}

if (isPublisher && userFieldServiceReports) {
await setUserFieldServiceReports(this.id, userFieldServiceReports);

await this.updateSettings(settings);
this.field_service_reports = userFieldServiceReports;
}
}

getApplications() {
Expand Down
9 changes: 4 additions & 5 deletions src/v3/controllers/pockets_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,19 +393,18 @@ export const saveUserBackup = async (req: Request, res: Response) => {
return;
}

const userRole = user.profile.congregation!.cong_role;

const cong_backup = req.body.cong_backup as BackupData;
const userSettings = cong_backup.app_settings.user_settings;
const userPerson = cong_backup.persons?.at(0);

if (userSettings) {
user.saveBackup(userSettings);
}

if (userPerson) {
const personData = userPerson.person_data as StandardRecord;
user.updatePersonData(personData.timeAway as string, personData.emergency_contacts as string);
}

user.saveBackup(cong_backup, userRole);

res.locals.type = 'info';
res.locals.message = 'user send backup successfully';
res.status(200).json({ message: 'BACKUP_SENT' });
Expand Down
6 changes: 1 addition & 5 deletions src/v3/controllers/users_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,7 @@ export const saveUserBackup = async (req: Request, res: Response) => {
user.updatePersonData(personData.timeAway as string, personData.emergency_contacts as string);
}

const userSettings = cong_backup.app_settings.user_settings;

if (userSettings) {
user.saveBackup(userSettings);
}
user.saveBackup(cong_backup, userRole);

res.locals.type = 'info';
res.locals.message = 'user send backup for congregation successfully';
Expand Down
12 changes: 12 additions & 0 deletions src/v3/services/firebase/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export const getUserBibleStudies = async (id: string) => {
return [];
};

export const setUserBibleStudies = async (id: string, reports: StandardRecord[]) => {
const data = JSON.stringify(reports);
const path = `${id}/bible_studies.txt`;
await uploadFileToStorage(data, { type: 'user', path });
};

export const getUserFieldServiceReports = async (id: string) => {
const path = `${id}/field_service_reports.txt`;
const data = await getFileFromStorage({ type: 'user', path });
Expand All @@ -89,6 +95,12 @@ export const getUserFieldServiceReports = async (id: string) => {
return [];
};

export const setUserFieldServiceReports = async (id: string, reports: StandardRecord[]) => {
const data = JSON.stringify(reports);
const path = `${id}/field_service_reports.txt`;
await uploadFileToStorage(data, { type: 'user', path });
};

export const getUserDetails = async (id: string) => {
return {
settings: await getUserSettings(id),
Expand Down

0 comments on commit 16075d2

Please sign in to comment.