Skip to content

Commit 8b1f32d

Browse files
authored
Merge pull request #1709 from Cihatata/i18n-support-backend
I18n support for server
2 parents 209bbf6 + 5371f53 commit 8b1f32d

27 files changed

+879
-371
lines changed

Server/controllers/authController.js

+21-19
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ import {
88
newPasswordValidation,
99
} from "../validation/joi.js";
1010
import logger from "../utils/logger.js";
11-
import { errorMessages, successMessages } from "../utils/messages.js";
1211
import jwt from "jsonwebtoken";
1312
import { getTokenFromHeaders, tokenType } from "../utils/utils.js";
1413
import crypto from "crypto";
1514
import { handleValidationError, handleError } from "./controllerUtils.js";
1615
const SERVICE_NAME = "authController";
1716

1817
class AuthController {
19-
constructor(db, settingsService, emailService, jobQueue) {
18+
constructor(db, settingsService, emailService, jobQueue, stringService) {
2019
this.db = db;
2120
this.settingsService = settingsService;
2221
this.emailService = emailService;
2322
this.jobQueue = jobQueue;
23+
this.stringService = stringService;
2424
}
2525

2626
/**
@@ -85,7 +85,7 @@ class AuthController {
8585

8686
const newUser = await this.db.insertUser({ ...req.body }, req.file);
8787
logger.info({
88-
message: successMessages.AUTH_CREATE_USER,
88+
message: this.stringService.authCreateUser,
8989
service: SERVICE_NAME,
9090
details: newUser._id,
9191
});
@@ -116,7 +116,7 @@ class AuthController {
116116
});
117117

118118
res.success({
119-
msg: successMessages.AUTH_CREATE_USER,
119+
msg: this.stringService.authCreateUser,
120120
data: { user: newUser, token: token, refreshToken: refreshToken },
121121
});
122122
} catch (error) {
@@ -153,7 +153,7 @@ class AuthController {
153153
// Compare password
154154
const match = await user.comparePassword(password);
155155
if (match !== true) {
156-
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
156+
const error = new Error(this.stringService.authIncorrectPassword);
157157
error.status = 401;
158158
next(error);
159159
return;
@@ -176,7 +176,7 @@ class AuthController {
176176
userWithoutPassword.avatarImage = user.avatarImage;
177177

178178
return res.success({
179-
msg: successMessages.AUTH_LOGIN_USER,
179+
msg: this.stringService.authLoginUser,
180180
data: {
181181
user: userWithoutPassword,
182182
token: token,
@@ -200,13 +200,14 @@ class AuthController {
200200
* @throws {Error} If there is an error during the process such as any of the token is not received
201201
*/
202202
refreshAuthToken = async (req, res, next) => {
203+
203204
try {
204205
// check for refreshToken
205206
const refreshToken = req.headers["x-refresh-token"];
206207

207208
if (!refreshToken) {
208209
// No refresh token provided
209-
const error = new Error(errorMessages.NO_REFRESH_TOKEN);
210+
const error = new Error(this.stringService.noRefreshToken);
210211
error.status = 401;
211212
error.service = SERVICE_NAME;
212213
error.method = "refreshAuthToken";
@@ -221,8 +222,8 @@ class AuthController {
221222
// Invalid or expired refresh token, trigger logout
222223
const errorMessage =
223224
refreshErr.name === "TokenExpiredError"
224-
? errorMessages.EXPIRED_REFRESH_TOKEN
225-
: errorMessages.INVALID_REFRESH_TOKEN;
225+
? this.stringService.expiredAuthToken
226+
: this.stringService.invalidAuthToken;
226227
const error = new Error(errorMessage);
227228
error.status = 401;
228229
error.service = SERVICE_NAME;
@@ -243,7 +244,7 @@ class AuthController {
243244
);
244245

245246
return res.success({
246-
msg: successMessages.AUTH_TOKEN_REFRESHED,
247+
msg: this.stringService.authTokenRefreshed,
247248
data: { user: payloadData, token: newAuthToken, refreshToken: refreshToken },
248249
});
249250
} catch (error) {
@@ -265,6 +266,7 @@ class AuthController {
265266
* @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).
266267
*/
267268
editUser = async (req, res, next) => {
269+
268270
try {
269271
await editUserParamValidation.validateAsync(req.params);
270272
await editUserBodyValidation.validateAsync(req.body);
@@ -276,7 +278,7 @@ class AuthController {
276278

277279
// TODO is this neccessary any longer? Verify ownership middleware should handle this
278280
if (req.params.userId !== req.user._id.toString()) {
279-
const error = new Error(errorMessages.AUTH_UNAUTHORIZED);
281+
const error = new Error(this.stringService.unauthorized);
280282
error.status = 401;
281283
error.service = SERVICE_NAME;
282284
next(error);
@@ -300,7 +302,7 @@ class AuthController {
300302
// If not a match, throw a 403
301303
// 403 instead of 401 to avoid triggering axios interceptor
302304
if (!match) {
303-
const error = new Error(errorMessages.AUTH_INCORRECT_PASSWORD);
305+
const error = new Error(this.stringService.authIncorrectPassword);
304306
error.status = 403;
305307
next(error);
306308
return;
@@ -311,7 +313,7 @@ class AuthController {
311313

312314
const updatedUser = await this.db.updateUser(req, res);
313315
res.success({
314-
msg: successMessages.AUTH_UPDATE_USER,
316+
msg: this.stringService.authUpdateUser,
315317
data: updatedUser,
316318
});
317319
} catch (error) {
@@ -333,7 +335,7 @@ class AuthController {
333335
const superAdminExists = await this.db.checkSuperadmin(req, res);
334336

335337
return res.success({
336-
msg: successMessages.AUTH_ADMIN_EXISTS,
338+
msg: this.stringService.authAdminExists,
337339
data: superAdminExists,
338340
});
339341
} catch (error) {
@@ -379,7 +381,7 @@ class AuthController {
379381
);
380382

381383
return res.success({
382-
msg: successMessages.AUTH_CREATE_RECOVERY_TOKEN,
384+
msg: this.stringService.authCreateRecoveryToken,
383385
data: msgId,
384386
});
385387
} catch (error) {
@@ -410,7 +412,7 @@ class AuthController {
410412
await this.db.validateRecoveryToken(req, res);
411413

412414
return res.success({
413-
msg: successMessages.AUTH_VERIFY_RECOVERY_TOKEN,
415+
msg: this.stringService.authVerifyRecoveryToken,
414416
});
415417
} catch (error) {
416418
next(handleError(error, SERVICE_NAME, "validateRecoveryTokenController"));
@@ -443,7 +445,7 @@ class AuthController {
443445
const token = this.issueToken(user._doc, tokenType.ACCESS_TOKEN, appSettings);
444446

445447
return res.success({
446-
msg: successMessages.AUTH_RESET_PASSWORD,
448+
msg: this.stringService.authResetPassword,
447449
data: { user, token },
448450
});
449451
} catch (error) {
@@ -497,7 +499,7 @@ class AuthController {
497499
await this.db.deleteUser(user._id);
498500

499501
return res.success({
500-
msg: successMessages.AUTH_DELETE_USER,
502+
msg: this.stringService.authDeleteUser,
501503
});
502504
} catch (error) {
503505
next(handleError(error, SERVICE_NAME, "deleteUserController"));
@@ -509,7 +511,7 @@ class AuthController {
509511
const allUsers = await this.db.getAllUsers(req, res);
510512

511513
return res.success({
512-
msg: successMessages.AUTH_GET_ALL_USERS,
514+
msg: this.stringService.authGetAllUsers,
513515
data: allUsers,
514516
});
515517
} catch (error) {

Server/controllers/checkController.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import {
99
deleteChecksByTeamIdParamValidation,
1010
updateChecksTTLBodyValidation,
1111
} from "../validation/joi.js";
12-
import { successMessages } from "../utils/messages.js";
1312
import jwt from "jsonwebtoken";
1413
import { getTokenFromHeaders } from "../utils/utils.js";
1514
import { handleValidationError, handleError } from "./controllerUtils.js";
1615

1716
const SERVICE_NAME = "checkController";
1817

1918
class CheckController {
20-
constructor(db, settingsService) {
19+
constructor(db, settingsService, stringService) {
2120
this.db = db;
2221
this.settingsService = settingsService;
22+
this.stringService = stringService;
2323
}
2424

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

3838
return res.success({
39-
msg: successMessages.CHECK_CREATE,
39+
msg: this.stringService.checkCreate,
4040
data: check,
4141
});
4242
} catch (error) {
@@ -57,7 +57,7 @@ class CheckController {
5757
const result = await this.db.getChecksByMonitor(req);
5858

5959
return res.success({
60-
msg: successMessages.CHECK_GET,
60+
msg: this.stringService.checkGet,
6161
data: result,
6262
});
6363
} catch (error) {
@@ -77,7 +77,7 @@ class CheckController {
7777
const checkData = await this.db.getChecksByTeam(req);
7878

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

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

119119
return res.success({
120-
msg: successMessages.CHECK_DELETE,
120+
msg: this.stringService.checkDelete,
121121
data: { deletedCount },
122122
});
123123
} catch (error) {
@@ -144,7 +144,7 @@ class CheckController {
144144
await this.db.updateChecksTTL(teamId, ttl);
145145

146146
return res.success({
147-
msg: successMessages.CHECK_UPDATE_TTL,
147+
msg: this.stringService.checkUpdateTTL,
148148
});
149149
} catch (error) {
150150
next(handleError(error, SERVICE_NAME, "updateTTL"));

Server/controllers/inviteController.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import logger from "../utils/logger.js";
77
import jwt from "jsonwebtoken";
88
import { handleError, handleValidationError } from "./controllerUtils.js";
99
import { getTokenFromHeaders } from "../utils/utils.js";
10-
import { successMessages } from "../utils/messages.js";
10+
1111
const SERVICE_NAME = "inviteController";
1212

1313
class InviteController {
14-
constructor(db, settingsService, emailService) {
14+
constructor(db, settingsService, emailService, stringService) {
1515
this.db = db;
1616
this.settingsService = settingsService;
1717
this.emailService = emailService;
18+
this.stringService = stringService;
1819
}
1920

2021
/**
@@ -66,7 +67,7 @@ class InviteController {
6667
});
6768

6869
return res.success({
69-
msg: successMessages.INVITE_ISSUED,
70+
msg: this.stringService.inviteIssued,
7071
data: inviteToken,
7172
});
7273
} catch (error) {
@@ -86,7 +87,7 @@ class InviteController {
8687
const invite = await this.db.getInviteToken(req.body.token);
8788

8889
return res.success({
89-
msg: successMessages.INVITE_VERIFIED,
90+
msg: this.stringService.inviteVerified,
9091
data: invite,
9192
});
9293
} catch (error) {

Server/controllers/maintenanceWindowController.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
} from "../validation/joi.js";
1010
import jwt from "jsonwebtoken";
1111
import { getTokenFromHeaders } from "../utils/utils.js";
12-
import { successMessages } from "../utils/messages.js";
1312
import { handleValidationError, handleError } from "./controllerUtils.js";
13+
1414
const SERVICE_NAME = "maintenanceWindowController";
1515

1616
class MaintenanceWindowController {
17-
constructor(db, settingsService) {
17+
constructor(db, settingsService, stringService) {
1818
this.db = db;
1919
this.settingsService = settingsService;
20+
this.stringService = stringService;
2021
}
2122

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

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

6566
return res.success({
66-
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_ID,
67+
msg: this.stringService.maintenanceWindowGetById,
6768
data: maintenanceWindow,
6869
});
6970
} catch (error) {
@@ -89,7 +90,7 @@ class MaintenanceWindowController {
8990
);
9091

9192
return res.success({
92-
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_TEAM,
93+
msg: this.stringService.maintenanceWindowGetByTeam,
9394
data: maintenanceWindows,
9495
});
9596
} catch (error) {
@@ -111,7 +112,7 @@ class MaintenanceWindowController {
111112
);
112113

113114
return res.success({
114-
msg: successMessages.MAINTENANCE_WINDOW_GET_BY_USER,
115+
msg: this.stringService.maintenanceWindowGetByUser,
115116
data: maintenanceWindows,
116117
});
117118
} catch (error) {
@@ -129,7 +130,7 @@ class MaintenanceWindowController {
129130
try {
130131
await this.db.deleteMaintenanceWindowById(req.params.id);
131132
return res.success({
132-
msg: successMessages.MAINTENANCE_WINDOW_DELETE,
133+
msg: this.stringService.maintenanceWindowDelete,
133134
});
134135
} catch (error) {
135136
next(handleError(error, SERVICE_NAME, "deleteMaintenanceWindow"));
@@ -150,7 +151,7 @@ class MaintenanceWindowController {
150151
req.body
151152
);
152153
return res.success({
153-
msg: successMessages.MAINTENANCE_WINDOW_EDIT,
154+
msg: this.stringService.maintenanceWindowEdit,
154155
data: editedMaintenanceWindow,
155156
});
156157
} catch (error) {

0 commit comments

Comments
 (0)