Centralized errors and added their translations. Didn't test

This commit is contained in:
2024-10-31 06:33:57 +03:00
parent 47e90764e7
commit 7a2ab7dd5b
26 changed files with 543 additions and 359 deletions

View File

@@ -1,4 +1,6 @@
import db from '../db.js'
import customError from '../response/customError.js';
import responseCodes from '../response/responseCodes.js';
import statuses from '../utils/status.js';
import bcrypt from 'bcrypt';
@@ -12,12 +14,14 @@ class UserService {
async getByUsername(username) {
let user = (await db.query("SELECT * FROM Users WHERE username = $1", [username])).rows;
if (user == undefined) return statuses.not_found;
if (user == undefined) throw new customError(`getByUsername user not found`, responseCodes.responses.usernames.not_found);
return (user[0]);
}
async getAll() {
return (await db.query("SELECT * FROM Users")).rows;
let res = await db.query("SELECT * FROM Users");
if (res.rowCount == 0) throw new customError(`getAll user not found`, responseCodes.responses.usernames.not_found);
return res.rows[0]
}
async isInGroup(userId, groupId) {
@@ -25,7 +29,7 @@ class UserService {
}
async joinGroup(userId, groupId) {
if (await (this.isInGroup(userId, groupId))) return statuses.duplicate;
if (await (this.isInGroup(userId, groupId))) throw new customError(`joinGroup user already in group`, responseCodes.responses.user.already_in_group);
await db.query("UPDATE Users SET groups = array_append(groups, $1::integer) WHERE ID = $2", [groupId, userId]);
return statuses.ok;
}