bsfe_server/src/controllers/abstractproduct.js

105 lines
4.2 KiB
JavaScript

import AbstractProductService from '../services/abstractproduct.js';
import ProductService from '../services/product.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';
import { createHash } from 'crypto';
import notify from '../utils/notify.js';
const TAG = "/controllers/abstractproduct.js";
class AbstractProductController {
async create(req, res) {
const { groupId, localId, barcode, name, net_weight, category, unit } = req.body;
if (!req.file) throw new customError(`user hasn't supplied a file for abstract product creation`, responseCodes.responses.image_filename)
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") {
fs.rmSync(tempPath);
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);
notify(req.headers.authorization.split(' ')[1], groupId, 'create', 'abstractproduct', {
local_id: 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) {
let { groupId, localId, barcode, name, net_weight, category, unit } = req.body;
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);
if (name) await AbstractProductService.updateName(groupId, localId, name);
if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);
if (tempPath) await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
if (category) await AbstractProductService.updateCategory(groupId, localId, category);
if (unit) await AbstractProductService.updateUnit(groupId, localId, unit);
let data = await AbstractProductService.getByLocalId(groupId, localId)
notify(req.headers.authorization.split(' ')[1], groupId, 'update', 'abstractproduct', data);
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok));
}
async getById(req, res) {
let { localId, groupId } = req.params
let result = await AbstractProductService.getByLocalId(groupId, localId)
return res.status(200).send(result)
}
async getImage(req, res) {
let { localId, groupId } = req.params;
let imageFilename = (await AbstractProductService.getByLocalId(groupId, localId)).image_filename
let imagePath = path.join(path.resolve(path.dirname('')), `/uploads/${imageFilename}.png`);
let image = fs.readFileSync(imagePath)
return res.status(200).send(image)
}
async delete(req, res) {
let { localId, groupId } = req.params;
await AbstractProductService.delete(groupId, localId)
notify(req.headers.authorization.split(' ')[1], groupId, 'delete', 'abstractproduct', { local_id: localId });
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok))
}
};
export default new AbstractProductController();