Centralized errors and added their translations. Didn't test
This commit is contained in:
		@@ -1,79 +1,60 @@
 | 
			
		||||
import AbstractProductService from '../services/abstractproduct.js';
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import log from '../utils/log.js';
 | 
			
		||||
import fs from 'fs';
 | 
			
		||||
import path from 'path';
 | 
			
		||||
import customError from '../response/customError.js';
 | 
			
		||||
import responseCodes from '../response/responseCodes.js';
 | 
			
		||||
import translate from '../utils/translate.js';
 | 
			
		||||
 | 
			
		||||
const TAG = "/controllers/abstractproduct.js";
 | 
			
		||||
 | 
			
		||||
class AbstractProductController {
 | 
			
		||||
    async create(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
 | 
			
		||||
        const { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
 | 
			
		||||
 | 
			
		||||
            const tempPath = req.file.path;
 | 
			
		||||
            const targetPath = path.join(path.resolve(path.dirname('')), `/uploads/${image_filename}.png`);
 | 
			
		||||
        const tempPath = req.file.path;
 | 
			
		||||
        const targetPath = path.join(path.resolve(path.dirname('')), `/uploads/${image_filename}.png`);
 | 
			
		||||
 | 
			
		||||
            if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
 | 
			
		||||
                return res.status(400).send("Only .png files are allowed")
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            //fs.renameSync(tempPath, targetPath);
 | 
			
		||||
	    fs.copyFileSync(tempPath, targetPath);
 | 
			
		||||
        if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
 | 
			
		||||
            fs.rmSync(tempPath);
 | 
			
		||||
            await AbstractProductService.create(groupId, localId, barcode, name, net_weight, image_filename, category, unit);
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            switch (e.status) {
 | 
			
		||||
                case statuses.duplicate:
 | 
			
		||||
                    return res.status(400).send(e.message);
 | 
			
		||||
                default:
 | 
			
		||||
                    log.error(e.original);
 | 
			
		||||
                    return res.status(500).send(e.message);
 | 
			
		||||
            }
 | 
			
		||||
            throw new customError(`create abstract product only png allowed`, responseCodes.responses.general.png_only);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        fs.copyFileSync(tempPath, targetPath);
 | 
			
		||||
        fs.rmSync(tempPath);
 | 
			
		||||
 | 
			
		||||
        await AbstractProductService.create(groupId, localId, barcode, name, net_weight, image_filename, category, unit);
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async update(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            let { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
 | 
			
		||||
        let { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
 | 
			
		||||
 | 
			
		||||
            const tempPath = req.file.path;
 | 
			
		||||
            const targetPath = path.join(path.resolve(path.dirname('')) + `/uploads/${image_filename}.png`);
 | 
			
		||||
        const tempPath = req.file.path;
 | 
			
		||||
        const targetPath = path.join(path.resolve(path.dirname('')) + `/uploads/${image_filename}.png`);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            if (barcode) await AbstractProductService.updateBarcode(groupId, localId, barcode);
 | 
			
		||||
        if (barcode) await AbstractProductService.updateBarcode(groupId, localId, barcode);
 | 
			
		||||
 | 
			
		||||
            if (name) await AbstractProductService.updateName(groupId, localId, name);
 | 
			
		||||
        if (name) await AbstractProductService.updateName(groupId, localId, name);
 | 
			
		||||
 | 
			
		||||
            if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);
 | 
			
		||||
        if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);
 | 
			
		||||
 | 
			
		||||
            if (image_filename && tempPath) {
 | 
			
		||||
                //fs.renameSync(tempPath, targetPath);
 | 
			
		||||
	        fs.copyFileSync(tempPath, targetPath);
 | 
			
		||||
                fs.rmSync(tempPath);
 | 
			
		||||
                await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
 | 
			
		||||
            } else if (image_filename && !tempPath) {
 | 
			
		||||
                return res.status(400).send("You must supply image file along with its hash");
 | 
			
		||||
            } else if (!image_file && tempPath) {
 | 
			
		||||
                return res.status(400).send("You must supply image file hash along with file");
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (category) await AbstractProductService.updateCategory(groupId, localId, category);
 | 
			
		||||
 | 
			
		||||
            if (unit) await AbstractProductService.updateUnit(groupId, localId, unit);
 | 
			
		||||
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            switch (e.status) {
 | 
			
		||||
                case statuses.invalid_syntax:
 | 
			
		||||
                    log.error(e.original);
 | 
			
		||||
                    return res.status(400).send(e.message);
 | 
			
		||||
                default:
 | 
			
		||||
                    log.error(e.original);
 | 
			
		||||
                    return res.status(500).send(e.message);
 | 
			
		||||
            }
 | 
			
		||||
        if (image_filename && tempPath) {
 | 
			
		||||
            fs.copyFileSync(tempPath, targetPath);
 | 
			
		||||
            fs.rmSync(tempPath);
 | 
			
		||||
            await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
 | 
			
		||||
        } else if (image_filename && !tempPath) {
 | 
			
		||||
            throw new customError(`Abstract product update image hash without file`, responseCodes.responses.abstractproducts.hash_without_file);
 | 
			
		||||
        } else if (!image_file && tempPath) {
 | 
			
		||||
            fs.rmSync(tempPath);
 | 
			
		||||
            throw new customError(`Abstract product update file without image hash`, responseCodes.responses.abstractproducts.file_without_hash);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (category) await AbstractProductService.updateCategory(groupId, localId, category);
 | 
			
		||||
 | 
			
		||||
        if (unit) await AbstractProductService.updateUnit(groupId, localId, unit);
 | 
			
		||||
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,23 @@
 | 
			
		||||
import CategoryService from "../services/category.js";
 | 
			
		||||
import log from "../utils/log.js";
 | 
			
		||||
import translate from "../utils/translate.js";
 | 
			
		||||
 | 
			
		||||
const TAG = "controllers/category.js";
 | 
			
		||||
 | 
			
		||||
class CategoryController {
 | 
			
		||||
    async create(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { localId, categoryName, groupId } = req.body;
 | 
			
		||||
        const { localId, categoryName, groupId } = req.body;
 | 
			
		||||
 | 
			
		||||
            await CategoryService.create(groupId, localId, categoryName);
 | 
			
		||||
            return res.status(200).send("Success");
 | 
			
		||||
        await CategoryService.create(groupId, localId, categoryName);
 | 
			
		||||
 | 
			
		||||
        } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/create: ${e}`)); }
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async update(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { localId, groupId, name } = req.body;
 | 
			
		||||
        const { localId, groupId, name } = req.body;
 | 
			
		||||
 | 
			
		||||
            await CategoryService.update(groupId, localId, name);
 | 
			
		||||
            return res.status(200).send("Success");
 | 
			
		||||
        } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/update: ${e}`)); }
 | 
			
		||||
        await CategoryService.update(groupId, localId, name);
 | 
			
		||||
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -2,49 +2,41 @@ import GroupService from '../services/group.js';
 | 
			
		||||
import UserService from '../services/user.js';
 | 
			
		||||
import jwt from 'jsonwebtoken';
 | 
			
		||||
import config from '../../config.json' with {type: "json"};
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import log from '../utils/log.js';
 | 
			
		||||
import translate from '../utils/translate.js';
 | 
			
		||||
 | 
			
		||||
const TAG = "/controllers/group.js";
 | 
			
		||||
 | 
			
		||||
class GroupController {
 | 
			
		||||
    async create(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            let { groupName } = req.params;
 | 
			
		||||
        let { groupName } = req.params;
 | 
			
		||||
 | 
			
		||||
            let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
 | 
			
		||||
            let status = await GroupService.create(groupName, user.login.id);
 | 
			
		||||
        let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
 | 
			
		||||
        let status = await GroupService.create(groupName, user.login.id);
 | 
			
		||||
 | 
			
		||||
            log.info(`New group with name ${groupName} was just created by user ${user.login.username}`);
 | 
			
		||||
        log.info(`New group with name ${groupName} was just created by user ${user.login.username}`);
 | 
			
		||||
 | 
			
		||||
            UserService.joinGroup(user.login.id, status.id);
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/create: ${e}`)); }
 | 
			
		||||
        UserService.joinGroup(user.login.id, status.id);
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
    async join(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            let { groupId } = req.params;
 | 
			
		||||
        let { groupId } = req.params;
 | 
			
		||||
 | 
			
		||||
            let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
 | 
			
		||||
            let status = await UserService.joinGroup(user.login.id, groupId);
 | 
			
		||||
        let user = jwt.decode(req.headers.authorization.split(' ')[1], config.secret);
 | 
			
		||||
        await UserService.joinGroup(user.login.id, groupId);
 | 
			
		||||
 | 
			
		||||
            if (status == statuses.duplicate) return res.status(400).send("Already in group");
 | 
			
		||||
 | 
			
		||||
            log.info(`User ${user.login.username} has just joined group with ID ${groupId}`);
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/join: ${e}`)); }
 | 
			
		||||
        log.info(`User ${user.login.username} has just joined group with ID ${groupId}`);
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async updatePassword(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            let { groupId } = req.params;
 | 
			
		||||
            let { password } = req.body;
 | 
			
		||||
        let { groupId } = req.params;
 | 
			
		||||
        let { password } = req.body;
 | 
			
		||||
 | 
			
		||||
            await GroupService.updatePassword(groupId, password);
 | 
			
		||||
            log.info(`Password for group with ID ${groupId} was updated`);
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        await GroupService.updatePassword(groupId, password);
 | 
			
		||||
        log.info(`Password for group with ID ${groupId} was updated`);
 | 
			
		||||
 | 
			
		||||
        } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/updatePassword ${e}`)); }
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,47 +1,30 @@
 | 
			
		||||
import ProductService from '../services/product.js';
 | 
			
		||||
import statuses from '../utils/status.js';
 | 
			
		||||
import log from '../utils/log.js';
 | 
			
		||||
import translate from '../utils/translate.js';
 | 
			
		||||
 | 
			
		||||
const TAG = "/controllers/product.js"
 | 
			
		||||
 | 
			
		||||
class AbstractProductController {
 | 
			
		||||
    async create(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { groupId, localId, abstract_product_id, amount, date_of_production, expiry_date } = req.body;
 | 
			
		||||
            await ProductService.create(groupId, localId, abstract_product_id, amount, date_of_production, expiry_date);
 | 
			
		||||
            return res.status(200).send("Successfull");
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            switch (e.status) {
 | 
			
		||||
                case statuses.duplicate:
 | 
			
		||||
                    return res.status(400).send(e.message);
 | 
			
		||||
                default:
 | 
			
		||||
                    log.error(e.original)
 | 
			
		||||
                    return res.status(500).send(e.message)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        const { groupId, localId, abstract_product_id, amount, date_of_production, expiry_date } = req.body;
 | 
			
		||||
 | 
			
		||||
        await ProductService.create(groupId, localId, abstract_product_id, amount, date_of_production, expiry_date);
 | 
			
		||||
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    async update(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            let { groupId, localId, abstract_product_id, amount, date_of_production, expiry_date } = req.body;
 | 
			
		||||
        let { groupId, localId, abstract_product_id, amount, date_of_production, expiry_date } = req.body;
 | 
			
		||||
 | 
			
		||||
            if (abstract_product_id) await ProductService.updateAbstractProductId(groupId, localId, abstract_product_id);
 | 
			
		||||
        if (abstract_product_id) await ProductService.updateAbstractProductId(groupId, localId, abstract_product_id);
 | 
			
		||||
 | 
			
		||||
            if (amount) await ProductService.updateAmount(groupId, localId, amount);
 | 
			
		||||
        if (amount) await ProductService.updateAmount(groupId, localId, amount);
 | 
			
		||||
 | 
			
		||||
            if (date_of_production) await ProductService.updateDateOfProduction(groupId, localId, date_of_production);
 | 
			
		||||
        if (date_of_production) await ProductService.updateDateOfProduction(groupId, localId, date_of_production);
 | 
			
		||||
 | 
			
		||||
            if (expiry_date) await ProductService.updateExpiryDate(groupId, localId, expiry_date);
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
            switch (e.status) {
 | 
			
		||||
                case statuses.invalid_syntax:
 | 
			
		||||
                    log.error(e.original);
 | 
			
		||||
                    return res.status(400).send(e.message);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (expiry_date) await ProductService.updateExpiryDate(groupId, localId, expiry_date);
 | 
			
		||||
 | 
			
		||||
        return res.status(200).send("Successfull");
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -4,44 +4,39 @@ import bcrypt from 'bcrypt';
 | 
			
		||||
import genToken from '../utils/jwt.js';
 | 
			
		||||
import AbstractProductService from '../services/abstractproduct.js';
 | 
			
		||||
import ProductService from '../services/product.js';
 | 
			
		||||
import translate from '../utils/translate.js';
 | 
			
		||||
 | 
			
		||||
const TAG = "/controllers/userjs"
 | 
			
		||||
 | 
			
		||||
class UserController {
 | 
			
		||||
    async register(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { username, password } = req.body;
 | 
			
		||||
        const { username, password } = req.body;
 | 
			
		||||
 | 
			
		||||
            await UserService.create(username, password);
 | 
			
		||||
        await UserService.create(username, password);
 | 
			
		||||
 | 
			
		||||
            log.info(`New user with name ${username} has just registered`);
 | 
			
		||||
            return res.status(200).send("Successfull register");
 | 
			
		||||
        } catch (e) { res.status(500).send(log.unknownError(`${TAG}/register: ${e}`)); }
 | 
			
		||||
        log.info(`New user with name ${username} has just registered`);
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async login(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { username, password } = req.body;
 | 
			
		||||
        const { username, password } = req.body;
 | 
			
		||||
 | 
			
		||||
            const user = await UserService.getByUsername(username);
 | 
			
		||||
            if (!bcrypt.compareSync(password, user.password)) return res.status(401).send("Wrong password");
 | 
			
		||||
        const user = await UserService.getByUsername(username);
 | 
			
		||||
        if (!bcrypt.compareSync(password, user.password)) return res.status(401).send("Wrong password");
 | 
			
		||||
 | 
			
		||||
            const token = genToken(user);
 | 
			
		||||
            return res.status(200).send(token);
 | 
			
		||||
        } catch (e) { res.status(500).send(log.unknownError(`${TAG}/login: ${e}`)); }
 | 
			
		||||
        const token = genToken(user);
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    async synchronize(req, res) {
 | 
			
		||||
        try {
 | 
			
		||||
            const { groupId } = req.params;
 | 
			
		||||
        const { groupId } = req.params;
 | 
			
		||||
 | 
			
		||||
            let result = {};
 | 
			
		||||
            result.abstract_products = await AbstractProductService.getAll(groupId);
 | 
			
		||||
            result.products = await ProductService.getAll(groupId);
 | 
			
		||||
            result.categories = await CategoryService.getAll(groupId);
 | 
			
		||||
        let result = {};
 | 
			
		||||
        result.abstract_products = await AbstractProductService.getAll(groupId);
 | 
			
		||||
        result.products = await ProductService.getAll(groupId);
 | 
			
		||||
        result.categories = await CategoryService.getAll(groupId);
 | 
			
		||||
 | 
			
		||||
            return res.status(200).json(result);
 | 
			
		||||
        } catch (e) { res.status(500).send(log.unknownError(`${TAG}/synchronize: ${e}`)); }
 | 
			
		||||
        return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user