Skip to content

Commit

Permalink
fix(controllers): allow admin users to get additional backup fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Dec 1, 2024
1 parent 7bf7317 commit 0d85251
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { requestChecker } from './v3/middleware/request_checker.js';
import { updateTracker } from './v3/middleware/update_tracker.js';
import { serverReadyChecker } from './v3/middleware/server_ready_checker.js';

import routesV2 from './v2/routes/index.js';
import routesV3 from './v3/routes/index.js';

import { errorHandler, getRoot, invalidEndpointHandler } from './v3/controllers/app_controller.js';
Expand Down Expand Up @@ -99,10 +98,7 @@ app.use(handle(i18next));

app.get('/', getRoot);

// load v2 routes
app.use('/api/v2', routesV2);

// load v3 routes
// load routes
app.use('/api/v3', routesV3);

// Handling invalid routes
Expand Down
12 changes: 0 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ import 'dotenv/config';

import app from './app.js';

// import v2 files

import { users } from './v2/classes/Users.js';
import { congregations } from './v2/classes/Congregations.js';
import { initializeAPI as initializeDbCPE } from './v2/config/cpe.db-config.js';

// import v3 files

import { logger } from './v3/services/logger/logger.js';
import { ServerTempVariableType } from './v3/definition/server.js';
import { UsersList } from './v3/classes/Users.js';
Expand All @@ -25,7 +17,6 @@ export const API_VAR: ServerTempVariableType = {
REQUEST_TRACKER: [],
};

await initializeDbCPE();
await initializeAPI();

logger('info', JSON.stringify({ details: `API: minimum frontend client version set to ${API_VAR.MINIMUM_APP_VERSION}` }));
Expand All @@ -34,9 +25,6 @@ app.listen(PORT, async () => {
logger('info', JSON.stringify({ details: `server up and running on port ${PORT} (v${APP_VERSION})` }));
logger('info', JSON.stringify({ details: `loading Firebase data ...` }));

await users.loadAll();
await congregations.loadAll();

await UsersList.load();
await CongregationsList.load();

Expand Down
75 changes: 75 additions & 0 deletions src/v3/controllers/admin_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Request, Response } from 'express';
import { UsersList } from '../classes/Users.js';
import { CongregationsList } from '../classes/Congregations.js';

export const validateAdmin = async (req: Request, res: Response) => {
res.locals.type = 'info';
res.locals.message = 'administrator successfully logged in';
res.status(200).json({ message: 'OK' });
};

export const logoutAdmin = async (req: Request, res: Response) => {
// remove all sessions
const { id } = res.locals.currentUser;
const admin = UsersList.findById(id);

if (admin) await admin.adminLogout();

res.locals.type = 'info';
res.locals.message = 'administrator successfully logged out';
res.status(200).json({ message: 'LOGGED_OUT' });
};

export const getAllCongregations = async (req: Request, res: Response) => {
const congsList = CongregationsList.list;

const result = congsList.map((cong) => {
return {
id: cong.id,
country_code: cong.settings.country_code,
cong_name: cong.settings.cong_name,
cong_number: cong.settings.cong_number,
cong_members: cong.members.map((user) => {
return { ...user.profile, id: user.id };
}),
};
});

res.locals.type = 'info';
res.locals.message = 'admin fetched all congregation';
res.status(200).json(result);
};

export const deleteCongregation = async (req: Request, res: Response) => {
const { id } = req.params;

if (!id) {
res.locals.type = 'warn';
res.locals.message = 'the congregation request id params is undefined';
res.status(400).json({ message: 'REQUEST_ID_INVALID' });

return;
}

const cong = CongregationsList.findById(id);

if (!cong) {
res.locals.type = 'warn';
res.locals.message = 'no congregation could not be found with the provided id';
res.status(404).json({ message: 'CONGREGATION_NOT_FOUND' });
return;
}

if (cong.members.length > 0) {
res.locals.type = 'warn';
res.locals.message = 'congregation could not be deleted since there are still users inside';
res.status(405).json({ message: 'CONG_ACTIVE' });
return;
}

await CongregationsList.delete(id);

res.locals.type = 'info';
res.locals.message = 'congregation deleted';
res.status(200).json({ message: 'OK' });
};
6 changes: 3 additions & 3 deletions src/v3/controllers/users_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,10 @@ export const retrieveUserBackup = async (req: Request, res: Response) => {

const secretaryRole = userRole.includes('secretary');
const coordinatorRole = userRole.includes('coordinator');
const elderRole = userRole.includes('elder');

const adminRole = userRole.includes('admin') || secretaryRole || coordinatorRole;

const elderRole = userRole.includes('elder');

const scheduleEditor =
adminRole ||
userRole.some((role) => role === 'midweek_schedule' || role === 'weekend_schedule' || role === 'public_talk_schedule');
Expand Down Expand Up @@ -436,7 +436,7 @@ export const retrieveUserBackup = async (req: Request, res: Response) => {
result.persons = cong.persons;
}

if (elderRole) {
if (adminRole || elderRole) {
result.speakers_congregations = cong.speakers_congregations;
result.visiting_speakers = cong.visiting_speakers;
result.cong_field_service_reports = cong.field_service_reports;
Expand Down
26 changes: 26 additions & 0 deletions src/v3/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from 'express';

import { visitorChecker } from '../middleware/visitor_checker.js';
import { adminAuthChecker } from '../middleware/admin_auth_checker.js';

import { deleteCongregation, getAllCongregations, logoutAdmin, validateAdmin } from '../controllers/admin_controller.js';

const router = express.Router();

// activate middleware
router.use(visitorChecker());
router.use(adminAuthChecker());

// validate user admin => passed middleware
router.get('/', validateAdmin);

// logout admin
router.get('/logout', logoutAdmin);

// get all congregations
router.get('/congregations', getAllCongregations);

// delete congregation
router.delete('/congregations/:id', deleteCongregation);

export default router;
2 changes: 2 additions & 0 deletions src/v3/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import userRoute from './users.js';
import mfaRoute from './mfa.js';
import publicRoute from './public.js';
import pocketRoute from './pockets.js';
import adminRoute from './admin.js';

import { appVersionChecker } from '../middleware/app_version_checker.js';

Expand All @@ -27,5 +28,6 @@ router.use('/users', userRoute);
router.use('/congregations', congregationRoute);
router.use('/congregations/meeting', congregationMeetingEditorRoute);
router.use('/congregations/admin', congregationAdminRoute);
router.use('/admin', adminRoute);

export default router;

0 comments on commit 0d85251

Please sign in to comment.