tested and fixed

This commit is contained in:
2024-10-31 07:47:27 +03:00
parent 7a2ab7dd5b
commit c9e679ec4c
22 changed files with 91 additions and 66 deletions

View File

@@ -4,14 +4,17 @@ import path from 'path';
import customError from '../response/customError.js';
import responseCodes from '../response/responseCodes.js';
import translate from '../utils/translate.js';
import { createHash } from 'crypto';
const TAG = "/controllers/abstractproduct.js";
class AbstractProductController {
async create(req, res) {
const { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
const { groupId, localId, barcode, name, net_weight, category, unit } = req.body;
const tempPath = req.file.path;
const image_buffer = fs.readFileSync(tempPath);
const image_filename = createHash('md5').update(image_buffer).digest('hex');
const targetPath = path.join(path.resolve(path.dirname('')), `/uploads/${image_filename}.png`);
if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
@@ -27,11 +30,18 @@ class AbstractProductController {
}
async update(req, res) {
let { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
let { groupId, localId, barcode, name, net_weight, category, unit } = req.body;
const tempPath = req.file.path;
const targetPath = path.join(path.resolve(path.dirname('')) + `/uploads/${image_filename}.png`);
var tempPath, image_buffer, image_filename, targetPath;
if (req.file) {
tempPath = req.file.path;
image_buffer = fs.readFileSync(tempPath);
image_filename = createHash('md5').update(image_buffer).digest('hex');
targetPath = path.join(path.resolve(path.dirname('')) + `/uploads/${image_filename}.png`);
fs.copyFileSync(tempPath, targetPath);
fs.rmSync(tempPath);
}
if (barcode) await AbstractProductService.updateBarcode(groupId, localId, barcode);
@@ -39,16 +49,7 @@ class AbstractProductController {
if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);
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 (tempPath) await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
if (category) await AbstractProductService.updateCategory(groupId, localId, category);

View File

@@ -1,5 +1,6 @@
import CategoryService from "../services/category.js";
import translate from "../utils/translate.js";
import responseCodes from "../response/responseCodes.js";
const TAG = "controllers/category.js";

View File

@@ -4,6 +4,7 @@ import jwt from 'jsonwebtoken';
import config from '../../config.json' with {type: "json"};
import log from '../utils/log.js';
import translate from '../utils/translate.js';
import responseCodes from '../response/responseCodes.js';
const TAG = "/controllers/group.js";
@@ -16,7 +17,7 @@ class GroupController {
log.info(`New group with name ${groupName} was just created by user ${user.login.username}`);
UserService.joinGroup(user.login.id, status.id);
await 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) {

View File

@@ -1,5 +1,6 @@
import ProductService from '../services/product.js';
import translate from '../utils/translate.js';
import responseCodes from '../response/responseCodes.js';
const TAG = "/controllers/product.js"

View File

@@ -5,6 +5,7 @@ import genToken from '../utils/jwt.js';
import AbstractProductService from '../services/abstractproduct.js';
import ProductService from '../services/product.js';
import translate from '../utils/translate.js';
import responseCodes from '../response/responseCodes.js';
const TAG = "/controllers/userjs"
@@ -25,7 +26,7 @@ class UserController {
if (!bcrypt.compareSync(password, user.password)) return res.status(401).send("Wrong password");
const token = genToken(user);
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
return res.status(200).send(token);
}
async synchronize(req, res) {
@@ -36,7 +37,7 @@ class UserController {
result.products = await ProductService.getAll(groupId);
result.categories = await CategoryService.getAll(groupId);
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
return res.status(200).send(result);
}
}