Skip to content

Commit

Permalink
fix(services): update users response for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao committed Jan 30, 2025
1 parent 2f1d03e commit 2128357
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
59 changes: 54 additions & 5 deletions src/v3/controllers/admin_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const congregationPersonsGet = async (req: Request, res: Response) => {
};

export const usersGetAll = async (req: Request, res: Response) => {
const result = await adminUsersGet();
const result = await adminUsersGet(req.signedCookies.visitorid);

res.locals.type = 'info';
res.locals.message = 'admin fetched all users';
Expand Down Expand Up @@ -153,7 +153,7 @@ export const userDelete = async (req: Request, res: Response) => {
}
}

const result = await adminUsersGet();
const result = await adminUsersGet(req.signedCookies.visitorid);

res.locals.type = 'info';
res.locals.message = 'admin deleted an user';
Expand Down Expand Up @@ -182,7 +182,7 @@ export const userDisable2FA = async (req: Request, res: Response) => {

await user.disableMFA();

const result = await adminUsersGet();
const result = await adminUsersGet(req.signedCookies.visitorid);

res.locals.type = 'info';
res.locals.message = 'admin disabled user 2fa';
Expand Down Expand Up @@ -211,7 +211,7 @@ export const userRevokeToken = async (req: Request, res: Response) => {

await user.revokeToken();

const result = await adminUsersGet();
const result = await adminUsersGet(req.signedCookies.visitorid);

res.locals.type = 'info';
res.locals.message = 'admin revoked user token';
Expand Down Expand Up @@ -280,9 +280,58 @@ export const userUpdate = async (req: Request, res: Response) => {
}
}

const result = await adminUsersGet();
const result = await adminUsersGet(req.signedCookies.visitorid);

res.locals.type = 'info';
res.locals.message = 'admin updated user details';
res.status(200).json(result);
};

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

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

return;
}

const user = UsersList.findById(id);

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

const identifiers = req.body.identifiers as string | [];

const session = identifiers.length === 0 ? [] : identifiers.at(0);

if (typeof session === 'string') {
await user.revokeSession(session);
}

if (typeof session === 'object') {
await user.updateSessions([]);
}

const userCong = user.profile.congregation?.id;

if (userCong) {
const cong = CongregationsList.findById(userCong);

if (cong) {
cong.reloadMembers();
}
}

const result = await adminUsersGet(req.signedCookies.visitorid);

res.locals.type = 'info';
res.locals.message = 'admin revoked an user session';
res.status(200).json(result);
};
4 changes: 4 additions & 0 deletions src/v3/routes/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
userDelete,
userDisable2FA,
userRevokeToken,
userSessionDelete,
usersGetAll,
userUpdate,
validateAdmin,
Expand Down Expand Up @@ -50,6 +51,9 @@ router.get('/users/:id/revoke-token', userRevokeToken);
// update an user
router.patch('/users/:id', body('lastname').isString(), body('firstname').isString(), body('email').isString(), userUpdate);

// delete user session
router.delete('/users/:id/sessions', body('identifiers').isArray(), userSessionDelete);

// delete an user
router.delete('/users/:id', userDelete);

Expand Down
18 changes: 16 additions & 2 deletions src/v3/services/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CongregationsList } from '../../classes/Congregations.js';
import { UsersList } from '../../classes/Users.js';

export const adminUsersGet = async () => {
export const adminUsersGet = async (visitorid: string) => {
const users = UsersList.list;

const result = users.map((user) => {
Expand All @@ -10,7 +10,21 @@ export const adminUsersGet = async () => {

return {
id: user.id,
sessions: user.sessions,
sessions:
user.sessions?.map((session) => {
return {
identifier: session.identifier,
isSelf: session.visitorid === visitorid,
ip: session.visitor_details.ip,
country_name: session.visitor_details.ipLocation.country_name,
device: {
browserName: session.visitor_details.browser,
os: session.visitor_details.os,
isMobile: session.visitor_details.isMobile,
},
last_seen: session.last_seen,
};
}) || [],
profile: {
...user.profile,
email: user.email,
Expand Down

0 comments on commit 2128357

Please sign in to comment.