Centralized errors and added their translations. Didn't test
This commit is contained in:
		@@ -1,12 +1,15 @@
 | 
			
		||||
import db from '../db.js';
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import errorHandler from '../utils/pgerrorhandler.js';
 | 
			
		||||
import tryHandler from '../response/errorHandler.js';
 | 
			
		||||
import responses from '../response/responseCodes.js';
 | 
			
		||||
import customError from '../response/customError.js';
 | 
			
		||||
 | 
			
		||||
class AbstractProductService {
 | 
			
		||||
    async create(groupid, localid, barcode, name, net_weight, image_filename, category, unit) {
 | 
			
		||||
        await db.query("INSERT INTO abstract_products (group_id, local_id, barcode, name, net_weight, image_filename, category, unit) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", [groupid, localid, barcode, name, net_weight, image_filename, category, unit])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "abstract product")
 | 
			
		||||
                errorHandler(e, "abstractproduct")
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -20,21 +23,21 @@ class AbstractProductService {
 | 
			
		||||
    async updateName(groupId, localId, name) {
 | 
			
		||||
        await db.query("UPDATE abstract_products SET name = $1 WHERE group_id = $2 AND local_id = $3", [name, groupId, localId])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "name")
 | 
			
		||||
                errorHandler(e, "abstractproductname")
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async updateNetWeight(groupId, localId, net_weight) {
 | 
			
		||||
        await db.query("UPDATE abstract_products SET net_weight = $1 WHERE group_id = $2 AND local_id = $3", [net_weight, groupId, localId]
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "net weight")
 | 
			
		||||
                errorHandler(e, "netweight")
 | 
			
		||||
            }));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async updateImageFilename(groupId, localId, image_filename) {
 | 
			
		||||
        await db.query("UPDATE abstract_products SET image_filename = $1 WHERE group_id = $2 AND local_id = $3", [image_filename, groupId, localId])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "image filename")
 | 
			
		||||
                errorHandler(e, "imagehash")
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -54,7 +57,7 @@ class AbstractProductService {
 | 
			
		||||
 | 
			
		||||
    async getAll(groupId) {
 | 
			
		||||
        let result = (await db.query("SELECT local_id, barcode, name, net_weight, image_filename, category, unit FROM abstract_products WHERE group_id = $1", [groupId])).rows;
 | 
			
		||||
        if (!result) return statuses.not_found;
 | 
			
		||||
        if (!result) throw new customError("getAll abstract products not found", responses.responses.abstractproducts.not_found)
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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';
 | 
			
		||||
 | 
			
		||||
class CategoryService {
 | 
			
		||||
@@ -11,14 +13,14 @@ class CategoryService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getById(groupId, localId) {
 | 
			
		||||
        let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2", [groupId, localId]))
 | 
			
		||||
        if (result.rowCount == 0) return statuses.not_found;
 | 
			
		||||
        let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2", [groupId, localId]));
 | 
			
		||||
        if (result.rowCount == 0) throw new customError(`getById categorirs not found`, responseCodes.responses.category.not_found)
 | 
			
		||||
        return result.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getByName(groupId, localId, name) {
 | 
			
		||||
        let result = (await db.query("SELECT * FROM categories WHERE group_id = $1 AND local_id = $2 AND name = $3", [groupId, localId, name]));
 | 
			
		||||
        if (result.rowCount == 0) return statuses.not_found;
 | 
			
		||||
        if (result.rowCount == 0) throw new customError(`getByName categories not found`, responseCodes.responses.category.not_found)
 | 
			
		||||
        return result.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,17 +1,18 @@
 | 
			
		||||
import db from '../db.js';
 | 
			
		||||
import customError from '../response/customError.js';
 | 
			
		||||
import responseCodes from '../response/responseCodes.js';
 | 
			
		||||
import errorHandler from '../utils/pgerrorhandler.js';
 | 
			
		||||
import status from '../utils/status.js';
 | 
			
		||||
 | 
			
		||||
class GroupService {
 | 
			
		||||
    async create(name, creatorId) {
 | 
			
		||||
        let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch(errorHandler);
 | 
			
		||||
        let res = await db.query("INSERT INTO groups (name, admin_id) VALUES ($1, $2) RETURNING ID", [name, creatorId]).catch((e) => errorHandler(e, "group"));
 | 
			
		||||
 | 
			
		||||
        return res.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getById(id) {
 | 
			
		||||
        let res = (await db.query("SELECT * FROM groups WHERE id = $1", [id]));
 | 
			
		||||
        if (res.rowCount == 0) return status.not_found;
 | 
			
		||||
        if (res.rowCount == 0) throw new customError(`getByUd group not found`, responseCodes.responses.groups.id_not_found);
 | 
			
		||||
        return res.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -28,7 +29,9 @@ class GroupService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getByName(name) {
 | 
			
		||||
        return (await db.query("SELECT * FROM groups WHERE name = $1", [name])).rows[0];
 | 
			
		||||
        let res = (await db.query("SELECT * FROM groups WHERE name = $1", [name]));
 | 
			
		||||
        if (res.rowCount == 0) throw new customError(`getByName group not found`, responseCodes.responses.groups.name_not_found);
 | 
			
		||||
        return res.rows[0];
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +1,20 @@
 | 
			
		||||
import db from '../db.js';
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import errorHandler from '../utils/pgerrorhandler.js';
 | 
			
		||||
import customError from '../response/customError.js';
 | 
			
		||||
import responseCodes from '../response/responseCodes.js';
 | 
			
		||||
 | 
			
		||||
class ProductService {
 | 
			
		||||
    async create(groupid, localid, abstract_product_id, amount, date_of_production, expiry_date) {
 | 
			
		||||
        await db.query("INSERT INTO products (group_id, local_id, abstract_product_id, amount, date_of_production, expiry_date) VALUES ($1, $2, $3, $4, $5, $6)", [groupid, localid, abstract_product_id, amount, date_of_production, expiry_date])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "Abstract Product")
 | 
			
		||||
                errorHandler(e, "product")
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async updateAbstractProductId(groupId, localId, abstract_product_id) {
 | 
			
		||||
        await db.query("UPDATE products SET abstract_product_id = $1 WHERE group_id = $2 AND local_id = $3", [abstract_product_id, groupId, localId])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "abstract product id")
 | 
			
		||||
                errorHandler(e, "abstractproductid")
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
@@ -28,20 +29,20 @@ class ProductService {
 | 
			
		||||
    async updateDateOfProduction(groupId, localId, date_of_production) {
 | 
			
		||||
        await db.query("UPDATE products SET date_of_production = $1 WHERE group_id = $2 AND local_id = $3", [date_of_production, groupId, localId])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "date of production")
 | 
			
		||||
                errorHandler(e, "dateofproduction")
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async updateExpiryDate(groupId, localId, expiry_date) {
 | 
			
		||||
        await db.query("UPDATE products SET expiry_date = $1 WHERE group_id = $2 AND local_id = $3", [expiry_date, groupId, localId])
 | 
			
		||||
            .catch((e) => {
 | 
			
		||||
                errorHandler(e, "expiry date")
 | 
			
		||||
                errorHandler(e, "expirydate")
 | 
			
		||||
            });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async getAll(groupId) {
 | 
			
		||||
        let result = (await db.query("SELECT local_id, abstract_product_id, amount, date_of_production, expiry_date FROM products WHERE group_id = $1", [groupId])).rows;
 | 
			
		||||
        if (!result) return statuses.not_found;
 | 
			
		||||
        if (!result) throw new customError(`getAll product not found`, responseCodes.responses.products.not_found);
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user