Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit ffcefc7

Browse files
committed
fix(users): simplify error handling in user operations and improve credit transfer logic
1 parent 31edd5e commit ffcefc7

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

eslint.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ export default defineConfig([
1313
files: ["**/*.{js,mjs,cjs,ts}"],
1414
languageOptions: { globals: globals.browser },
1515
},
16+
{
17+
rules: {
18+
"@typescript-eslint/no-explicit-any": "ignore",
19+
},
20+
},
1621
tseslint.configs.recommended,
1722
]);

src/controllers/UserController.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import bcrypt from "bcryptjs";
42
import crypto from "crypto";
53
import { Request, Response } from "express";
@@ -421,7 +419,7 @@ export class Users {
421419
await this.userService.updateUser(userId, username.trim());
422420
await this.createLog(req, "changeUsername", "users", 200, userId);
423421
res.status(200).send({ message: "Username updated" });
424-
} catch (error) {
422+
} catch {
425423
await this.createLog(req, "changeUsername", "users", 500, userId);
426424
this.sendError(res, 500, "Error updating username");
427425
}
@@ -475,7 +473,7 @@ export class Users {
475473
await this.userService.updateUserPassword(userId, hashedPassword);
476474
await this.createLog(req, "changePassword", "users", 200, userId);
477475
res.status(200).send({ message: "Password changed successfully" });
478-
} catch (error) {
476+
} catch {
479477
await this.createLog(req, "changePassword", "users", 500, userId);
480478
this.sendError(res, 500, "Error changing password");
481479
}
@@ -529,7 +527,7 @@ export class Users {
529527
res
530528
.status(200)
531529
.send({ message: "Password reset successfully", token: jwtToken });
532-
} catch (error) {
530+
} catch {
533531
await this.createLog(req, "resetPassword", "users", 500, user.user_id);
534532
this.sendError(res, 500, "Error resetting password");
535533
}
@@ -656,7 +654,7 @@ export class Users {
656654
});
657655
await this.createLog(req, "searchUsers", "users", 200);
658656
res.send(users.map((user) => this.mapUserSearch(user)));
659-
} catch (error) {
657+
} catch {
660658
await this.createLog(req, "searchUsers", "users", 500);
661659
this.sendError(res, 500, "Error searching users");
662660
}
@@ -688,7 +686,7 @@ export class Users {
688686
public async getUser(req: Request, res: Response) {
689687
try {
690688
await userIdParamValidator.validate(req.params);
691-
} catch (err) {
689+
} catch {
692690
await this.createLog(req, "getUser", "users", 400);
693691
return this.sendError(res, 400, "Invalid userId");
694692
}
@@ -758,7 +756,7 @@ export class Users {
758756
req.user.user_id,
759757
);
760758
res.send(users.map((user) => this.mapUserSearch(user)));
761-
} catch (error) {
759+
} catch {
762760
await this.createLog(
763761
req,
764762
"adminSearchUsers",
@@ -838,7 +836,7 @@ export class Users {
838836
}
839837
try {
840838
await userIdParamValidator.validate(req.params);
841-
} catch (err) {
839+
} catch {
842840
await this.createLog(
843841
req,
844842
"adminGetUser",
@@ -888,7 +886,8 @@ export class Users {
888886
@httpPost("/transfer-credits", LoggedCheck.middleware)
889887
public async transferCredits(req: AuthenticatedRequest, res: Response) {
890888
const { targetUserId, amount } = req.body;
891-
if (!targetUserId || isNaN(amount) || amount <= 0) {
889+
const transferAmount = Number(amount);
890+
if (!targetUserId || isNaN(transferAmount) || transferAmount <= 0) {
892891
await this.createLog(
893892
req,
894893
"transferCredits",
@@ -925,7 +924,7 @@ export class Users {
925924
);
926925
return this.sendError(res, 404, "Recipient not found");
927926
}
928-
if (sender.balance < amount) {
927+
if (sender.balance < transferAmount) {
929928
await this.createLog(
930929
req,
931930
"transferCredits",
@@ -937,11 +936,11 @@ export class Users {
937936
}
938937
await this.userService.updateUserBalance(
939938
sender.user_id,
940-
sender.balance - Number(amount),
939+
sender.balance - transferAmount,
941940
);
942941
await this.userService.updateUserBalance(
943942
recipient.user_id,
944-
recipient.balance + Number(amount),
943+
recipient.balance + transferAmount,
945944
);
946945
await this.createLog(
947946
req,
@@ -951,7 +950,7 @@ export class Users {
951950
sender.user_id,
952951
);
953952
res.status(200).send({ message: "Credits transferred" });
954-
} catch (error) {
953+
} catch {
955954
await this.createLog(
956955
req,
957956
"transferCredits",
@@ -1030,7 +1029,7 @@ export class Users {
10301029
});
10311030
await this.createLog(req, "changeRole", "users", 200, userId);
10321031
return res.status(200).send({ message: "Role updated successfully" });
1033-
} catch (error) {
1032+
} catch {
10341033
await this.createLog(req, "changeRole", "users", 500, userId);
10351034
this.sendError(res, 500, "Error setting role cookie");
10361035
}
@@ -1055,7 +1054,7 @@ export class Users {
10551054
email: userData.email,
10561055
username: userData.username,
10571056
};
1058-
} catch (error) {
1057+
} catch {
10591058
throw new Error("Failed to verify Discord token");
10601059
}
10611060
}
@@ -1076,7 +1075,7 @@ export class Users {
10761075
email: userData.email,
10771076
username: userData.name,
10781077
};
1079-
} catch (error) {
1078+
} catch {
10801079
throw new Error("Failed to verify Google token");
10811080
}
10821081
}

src/lib/webauthnService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
1+
2+
23
import { Crypto } from '@peculiar/webcrypto';
34
import {
45
generateAuthenticationOptions,

0 commit comments

Comments
 (0)