Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n support for server #1709

Merged
merged 17 commits into from
Feb 11, 2025
40 changes: 21 additions & 19 deletions Server/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import {
newPasswordValidation,
} from "../validation/joi.js";
import logger from "../utils/logger.js";
import { errorMessages, successMessages } from "../utils/messages.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders, tokenType } from "../utils/utils.js";
import crypto from "crypto";
import { handleValidationError, handleError } from "./controllerUtils.js";
const SERVICE_NAME = "authController";

class AuthController {
constructor(db, settingsService, emailService, jobQueue) {
constructor(db, settingsService, emailService, jobQueue, stringService) {
this.db = db;
this.settingsService = settingsService;
this.emailService = emailService;
this.jobQueue = jobQueue;
this.stringService = stringService;
}

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ class AuthController {

const newUser = await this.db.insertUser({ ...req.body }, req.file);
logger.info({
message: successMessages.AUTH_CREATE_USER,
message: this.stringService.authCreateUser,
service: SERVICE_NAME,
details: newUser._id,
});
Expand Down Expand Up @@ -116,7 +116,7 @@ class AuthController {
});

res.success({
msg: successMessages.AUTH_CREATE_USER,
msg: this.stringService.authCreateUser,
data: { user: newUser, token: token, refreshToken: refreshToken },
});
} catch (error) {
Expand Down Expand Up @@ -153,7 +153,7 @@ class AuthController {
// Compare password
const match = await user.comparePassword(password);
if (match !== true) {
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
const error = new Error(this.stringService.authIncorrectPassword);
error.status = 401;
next(error);
return;
Expand All @@ -176,7 +176,7 @@ class AuthController {
userWithoutPassword.avatarImage = user.avatarImage;

return res.success({
msg: successMessages.AUTH_LOGIN_USER,
msg: this.stringService.authLoginUser,
data: {
user: userWithoutPassword,
token: token,
Expand All @@ -200,13 +200,14 @@ class AuthController {
* @throws {Error} If there is an error during the process such as any of the token is not received
*/
refreshAuthToken = async (req, res, next) => {

try {
// check for refreshToken
const refreshToken = req.headers["x-refresh-token"];

if (!refreshToken) {
// No refresh token provided
const error = new Error(errorMessages.NO_REFRESH_TOKEN);
const error = new Error(this.stringService.noRefreshToken);
error.status = 401;
error.service = SERVICE_NAME;
error.method = "refreshAuthToken";
Expand All @@ -221,8 +222,8 @@ class AuthController {
// Invalid or expired refresh token, trigger logout
const errorMessage =
refreshErr.name === "TokenExpiredError"
? errorMessages.EXPIRED_REFRESH_TOKEN
: errorMessages.INVALID_REFRESH_TOKEN;
? this.stringService.expiredAuthToken
: this.stringService.invalidAuthToken;
const error = new Error(errorMessage);
error.status = 401;
error.service = SERVICE_NAME;
Expand All @@ -243,7 +244,7 @@ class AuthController {
);

return res.success({
msg: successMessages.AUTH_TOKEN_REFRESHED,
msg: this.stringService.authTokenRefreshed,
data: { user: payloadData, token: newAuthToken, refreshToken: refreshToken },
});
} catch (error) {
Expand All @@ -265,6 +266,7 @@ class AuthController {
* @throws {Error} If there is an error during the process, especially if there is a validation error (422), the user is unauthorized (401), or the password is incorrect (403).
*/
editUser = async (req, res, next) => {

try {
await editUserParamValidation.validateAsync(req.params);
await editUserBodyValidation.validateAsync(req.body);
Expand All @@ -276,7 +278,7 @@ class AuthController {

// TODO is this neccessary any longer? Verify ownership middleware should handle this
if (req.params.userId !== req.user._id.toString()) {
const error = new Error(errorMessages.AUTH_UNAUTHORIZED);
const error = new Error(this.stringService.unauthorized);
error.status = 401;
error.service = SERVICE_NAME;
next(error);
Expand All @@ -300,7 +302,7 @@ class AuthController {
// If not a match, throw a 403
// 403 instead of 401 to avoid triggering axios interceptor
if (!match) {
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
const error = new Error(this.stringService.authIncorrectPassword);
error.status = 403;
next(error);
return;
Expand All @@ -311,7 +313,7 @@ class AuthController {

const updatedUser = await this.db.updateUser(req, res);
res.success({
msg: successMessages.AUTH_UPDATE_USER,
msg: this.stringService.authUpdateUser,
data: updatedUser,
});
} catch (error) {
Expand All @@ -333,7 +335,7 @@ class AuthController {
const superAdminExists = await this.db.checkSuperadmin(req, res);

return res.success({
msg: successMessages.AUTH_ADMIN_EXISTS,
msg: this.stringService.authAdminExists,
data: superAdminExists,
});
} catch (error) {
Expand Down Expand Up @@ -379,7 +381,7 @@ class AuthController {
);

return res.success({
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
msg: this.stringService.authCreateRecoveryToken,
data: msgId,
});
} catch (error) {
Expand Down Expand Up @@ -410,7 +412,7 @@ class AuthController {
await this.db.validateRecoveryToken(req, res);

return res.success({
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN,
msg: this.stringService.authVerifyRecoveryToken,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController"));
Expand Down Expand Up @@ -443,7 +445,7 @@ class AuthController {
const token = this.issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);

return res.success({
msg: successMessages.AUTH_RESET_PASSWORD,
msg: this.stringService.authResetPassword,
data: { user, token },
});
} catch (error) {
Expand Down Expand Up @@ -497,7 +499,7 @@ class AuthController {
await this.db.deleteUser(user._id);

return res.success({
msg: successMessages.AUTH_DELETE_USER,
msg: this.stringService.authDeleteUser,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteUserController"));
Expand All @@ -509,7 +511,7 @@ class AuthController {
const allUsers = await this.db.getAllUsers(req, res);

return res.success({
msg: successMessages.AUTH_GET_ALL_USERS,
msg: this.stringService.authGetAllUsers,
data: allUsers,
});
} catch (error) {
Expand Down
16 changes: 8 additions & 8 deletions Server/controllers/checkController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import {
deleteChecksByTeamIdParamValidation,
updateChecksTTLBodyValidation,
} from "../validation/joi.js";
import { successMessages } from "../utils/messages.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
import { handleValidationError, handleError } from "./controllerUtils.js";

const SERVICE_NAME = "checkController";

class CheckController {
constructor(db, settingsService) {
constructor(db, settingsService, stringService) {
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
}

createCheck = async (req, res, next) => {
Expand All @@ -36,7 +36,7 @@ class CheckController {
const check = await this.db.createCheck(checkData);

return res.success({
msg: successMessages.CHECK_CREATE,
msg: this.stringService.checkCreate,
data: check,
});
} catch (error) {
Expand All @@ -57,7 +57,7 @@ class CheckController {
const result = await this.db.getChecksByMonitor(req);

return res.success({
msg: successMessages.CHECK_GET,
msg: this.stringService.checkGet,
data: result,
});
} catch (error) {
Expand All @@ -77,7 +77,7 @@ class CheckController {
const checkData = await this.db.getChecksByTeam(req);

return res.success({
msg: successMessages.CHECK_GET,
msg: this.stringService.checkGet,
data: checkData,
});
} catch (error) {
Expand All @@ -97,7 +97,7 @@ class CheckController {
const deletedCount = await this.db.deleteChecks(req.params.monitorId);

return res.success({
msg: successMessages.CHECK_DELETE,
msg: this.stringService.checkDelete,
data: { deletedCount },
});
} catch (error) {
Expand All @@ -117,7 +117,7 @@ class CheckController {
const deletedCount = await this.db.deleteChecksByTeamId(req.params.teamId);

return res.success({
msg: successMessages.CHECK_DELETE,
msg: this.stringService.checkDelete,
data: { deletedCount },
});
} catch (error) {
Expand All @@ -144,7 +144,7 @@ class CheckController {
await this.db.updateChecksTTL(teamId, ttl);

return res.success({
msg: successMessages.CHECK_UPDATE_TTL,
msg: this.stringService.checkUpdateTTL,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "updateTTL"));
Expand Down
9 changes: 5 additions & 4 deletions Server/controllers/inviteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import logger from "../utils/logger.js";
import jwt from "jsonwebtoken";
import { handleError, handleValidationError } from "./controllerUtils.js";
import { getTokenFromHeaders } from "../utils/utils.js";
import { successMessages } from "../utils/messages.js";

const SERVICE_NAME = "inviteController";

class InviteController {
constructor(db, settingsService, emailService) {
constructor(db, settingsService, emailService, stringService) {
this.db = db;
this.settingsService = settingsService;
this.emailService = emailService;
this.stringService = stringService;
}

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ class InviteController {
});

return res.success({
msg: successMessages.INVITE_ISSUED,
msg: this.stringService.inviteIssued,
data: inviteToken,
});
} catch (error) {
Expand All @@ -86,7 +87,7 @@ class InviteController {
const invite = await this.db.getInviteToken(req.body.token);

return res.success({
msg: successMessages.INVITE_VERIFIED,
msg: this.stringService.inviteVerified,
data: invite,
});
} catch (error) {
Expand Down
17 changes: 9 additions & 8 deletions Server/controllers/maintenanceWindowController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import {
} from "../validation/joi.js";
import jwt from "jsonwebtoken";
import { getTokenFromHeaders } from "../utils/utils.js";
import { successMessages } from "../utils/messages.js";
import { handleValidationError, handleError } from "./controllerUtils.js";

const SERVICE_NAME = "maintenanceWindowController";

class MaintenanceWindowController {
constructor(db, settingsService) {
constructor(db, settingsService, stringService) {
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
}

createMaintenanceWindows = async (req, res, next) => {
Expand Down Expand Up @@ -45,7 +46,7 @@ class MaintenanceWindowController {
await Promise.all(dbTransactions);

return res.success({
msg: successMessages.MAINTENANCE_WINDOW_CREATE,
msg: this.stringService.maintenanceWindowCreate,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "createMaintenanceWindow"));
Expand All @@ -63,7 +64,7 @@ class MaintenanceWindowController {
const maintenanceWindow = await this.db.getMaintenanceWindowById(req.params.id);

return res.success({
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
msg: this.stringService.maintenanceWindowGetById,
data: maintenanceWindow,
});
} catch (error) {
Expand All @@ -89,7 +90,7 @@ class MaintenanceWindowController {
);

return res.success({
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
msg: this.stringService.maintenanceWindowGetByTeam,
data: maintenanceWindows,
});
} catch (error) {
Expand All @@ -111,7 +112,7 @@ class MaintenanceWindowController {
);

return res.success({
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER,
msg: this.stringService.maintenanceWindowGetByUser,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Yo, we got a naming inconsistency that's making me nervous! 😰

The message key maintenanceWindowGetByUser seems out of place since the method is getMaintenanceWindowsByMonitorId. Consider renaming to maintenanceWindowGetByMonitor for consistency.

data: maintenanceWindows,
});
} catch (error) {
Expand All @@ -129,7 +130,7 @@ class MaintenanceWindowController {
try {
await this.db.deleteMaintenanceWindowById(req.params.id);
return res.success({
msg: successMessages.MAINTENANCE_WINDOW_DELETE,
msg: this.stringService.maintenanceWindowDelete,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "deleteMaintenanceWindow"));
Expand All @@ -150,7 +151,7 @@ class MaintenanceWindowController {
req.body
);
return res.success({
msg: successMessages.MAINTENANCE_WINDOW_EDIT,
msg: this.stringService.maintenanceWindowEdit,
data: editedMaintenanceWindow,
});
} catch (error) {
Expand Down
Loading