From 5853023069bcfcc5e59eb7675da581d9b0bd43fd Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 7 Nov 2024 03:30:27 +0300 Subject: [PATCH] now /api/user/synchronize calculates hashes for items --- src/controllers/user.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/controllers/user.js b/src/controllers/user.js index b1a5286..330068a 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -8,6 +8,7 @@ import CategoryService from '../services/category.js'; import translate from '../utils/translate.js'; import responseCodes from '../response/responseCodes.js'; import customError from '../response/customError.js'; +import { createHash } from 'crypto'; const TAG = "/controllers/userjs" @@ -36,8 +37,22 @@ class UserController { let result = {}; result.abstract_products = await AbstractProductService.getAll(groupId); + result.abstract_products.forEach((abstractproduct) => { + let data = `${abstractproduct.local_id}:${abstractproduct.barcode}:${abstractproduct.name}:${abstractproduct.net_weight}:${abstractproduct.image_filename}:${abstractproduct.category}:${abstractproduct.unit}` + abstractproduct.hash = createHash('md5').update(data).digest('hex'); + }); + result.products = await ProductService.getAll(groupId); + result.products.forEach((product) => { + let data = `${product.local_id}:${product.abstract_product_id}:${product.amount}:${new Date(product.date_of_production).valueOf() / 1000}:${new Date(product.expiry_date).valueOf() / 1000}` + product.hash = createHash('md5').update(data).digest('hex'); + }); + result.categories = await CategoryService.getAll(groupId); + result.categories.forEach((category) => { + let data = `${category.local_id}:${category.name}` + category.hash = createHash('md5').update(data).digest('hex'); + }); return res.status(200).send(result); }