a lot of bugfixes, completed abstract products and products

This commit is contained in:
2024-10-27 04:45:13 +03:00
parent a27ce5762c
commit 75f9fb6d7d
24 changed files with 462 additions and 284 deletions

View File

@@ -1,13 +1,53 @@
import AbstractProductService from '../services/abstractproduct.js';
import statuses from '../utils/status.js';
import log from '../utils/log.js';
const TAG = "/controllers/abstractproduct.js"
class AbstractProductController {
async create(req, res) {
const { groupId, barcode, name, net_weight, image_filename, category, unit } = req.body;
// console.log(groupId, barcode, name, net_weight, image_filename, category, unit)
await AbstractProductService.create(groupId, barcode, name, net_weight, image_filename, category, unit);
return res.status(200).send("Successfull");
try {
const { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
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)
}
}
}
async update(req, res) {
try {
let { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;
if (barcode) await AbstractProductService.updateBarcode(groupId, localId, barcode);
if (name) await AbstractProductService.updateName(groupId, localId, name);
if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);
if (image_filename) await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
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);
}
}
}
};

View File

@@ -14,7 +14,7 @@ class GroupController {
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}`);
UserService.joinGroup(user.login.id, status.id);

View File

@@ -0,0 +1,48 @@
import ProductService from '../services/product.js';
import statuses from '../utils/status.js';
import log from '../utils/log.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)
}
}
}
async update(req, res) {
try {
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 (amount) await ProductService.updateAmount(groupId, localId, amount)
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);
}
}
return res.status(200).send("Successfull");
}
};
export default new AbstractProductController();

View File

@@ -2,16 +2,18 @@ import UserService from '../services/user.js';
import log from '../utils/log.js';
import bcrypt from 'bcrypt';
import genToken from '../utils/jwt.js';
import AbstractProductService from '../services/abstractproduct.js';
import ProductService from '../services/product.js';
const TAG = "/controllers/userjs"
class UserController {
async register (req, res) {
async register(req, res) {
try {
const {username, password} = req.body;
const { username, password } = req.body;
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}`)); }
@@ -19,7 +21,7 @@ class UserController {
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");
@@ -28,6 +30,19 @@ class UserController {
return res.status(200).send(token);
} catch (e) { res.status(500).send(log.unknownError(`${TAG}/login: ${e}`)); }
}
async synchronize(req, res) {
try {
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);
return res.status(200).json(result);
} catch (e) { res.status(500).send(log.unknownError(`${TAG}/synchronize: ${e}`)); }
}
}
export default new UserController()