From cae267f012360aff1f191836d74d4caf24c2407e Mon Sep 17 00:00:00 2001 From: Sergio De la torre Date: Wed, 1 Jun 2022 12:49:18 +0200 Subject: [PATCH 1/2] Handle status error --- methods/bindShelvingTag.js | 8 ++++---- utilities/filter.js | 11 +++++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/methods/bindShelvingTag.js b/methods/bindShelvingTag.js index 7ff394b..4e4227d 100644 --- a/methods/bindShelvingTag.js +++ b/methods/bindShelvingTag.js @@ -7,19 +7,19 @@ const express = require('express'); const router = express.Router(); // eslint-disable-line // Obtenemos los parametros -router.get('/:smartTagFk&:shelving&:level', async(req, res) => { +router.get('/:smartTagFk&:shelving&:level', async (req, res) => { const smartTagFk = req.params.smartTagFk;// valido = "A0A3B82"+ 4 CARACTERES HEXADECIMALES (0-F) const shelving = req.params.shelving;// valido = 3 CARACTERES LETRAS (A-Z) const level = req.params.level; const shelvingId = shelving + level; - filter.isSmartTag(smartTagFk); - await filter.isShelving(shelving); + filter.isSmartTag(smartTagFk, res); + await filter.isShelving(shelving, res); // Vinculamos la etiqueta con la balda del carry y en la base de datos await insertShelving(shelvingId, smartTagFk); // al carry en ESL CLOUD SYSTEM insertSmartTag(smartTagFk, shelving, level); // en vn.smartTag - res.json({message: 'SUCCESS'}); + res.json({ message: 'SUCCESS' }); }); module.exports = router; diff --git a/utilities/filter.js b/utilities/filter.js index 4998812..d42412d 100644 --- a/utilities/filter.js +++ b/utilities/filter.js @@ -1,15 +1,18 @@ const con = require('../db/connect'); -function isSmartTag(toFilter) { +function isSmartTag(toFilter, res) { const exp = /^A0A3B82[A-F0-9]{5}$/; if (!exp.test(toFilter)) - throw new Error('CODIGO DE ETIQUETA INCORRECTO'); + res.status(400).json({ error: { code: 'invalidPlate', message: 'Matricula incorrecta' } }) } -async function isShelving(shelving) { + + +async function isShelving(shelving, res) { const sql = `SELECT * FROM vn.shelving WHERE code = ?`; const [response] = await con.query(sql, shelving); if (response.length == 0) - throw new Error('CODIGO MATRICULA INCORRECTO'); + res.status(400).json({ error: { code: 'invalidPlate', message: 'Matricula incorrecta' } }) + } exports.isSmartTag = isSmartTag; From b113636e9c7f848e02b1130e836d0bfe5b7e1218 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 2 Jun 2022 13:59:14 +0200 Subject: [PATCH 2/2] fix storeId error and more error handling --- methods/bindShelvingTag.js | 13 +++++++++---- methods/bindTicketShelving.js | 8 +++++++- utilities/filter.js | 7 ++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/methods/bindShelvingTag.js b/methods/bindShelvingTag.js index 4e4227d..78d468f 100644 --- a/methods/bindShelvingTag.js +++ b/methods/bindShelvingTag.js @@ -7,7 +7,7 @@ const express = require('express'); const router = express.Router(); // eslint-disable-line // Obtenemos los parametros -router.get('/:smartTagFk&:shelving&:level', async (req, res) => { +router.get('/:smartTagFk&:shelving&:level', async(req, res) => { const smartTagFk = req.params.smartTagFk;// valido = "A0A3B82"+ 4 CARACTERES HEXADECIMALES (0-F) const shelving = req.params.shelving;// valido = 3 CARACTERES LETRAS (A-Z) const level = req.params.level; @@ -19,7 +19,7 @@ router.get('/:smartTagFk&:shelving&:level', async (req, res) => { // Vinculamos la etiqueta con la balda del carry y en la base de datos await insertShelving(shelvingId, smartTagFk); // al carry en ESL CLOUD SYSTEM insertSmartTag(smartTagFk, shelving, level); // en vn.smartTag - res.json({ message: 'SUCCESS' }); + res.json({message: 'SUCCESS'}); }); module.exports = router; @@ -30,7 +30,7 @@ async function insertShelving(shelvingId, smartTagFk) { const key = info.data.token; const currentUser = info.data.currentUser; - await got.post('http://app.etiquetaselectronicas.com:9999/item/batchImportItem', { // eslint-disable-line + let response = await got.post('http://app.etiquetaselectronicas.com:9999/item/batchImportItem', { // eslint-disable-line json: { agencyId: currentUser.agencyId, merchantId: currentUser.merchantId, @@ -51,7 +51,10 @@ async function insertShelving(shelvingId, smartTagFk) { 'Authorization': key } }); - await got.post('http://app.etiquetaselectronicas.com:9999/bind/batchBind', { // eslint-disable-line + if (!response.body.success) + res.status(400).json({error: {code: 'bindingCreateError', message: response.body.message}}); + + response = await got.post('http://app.etiquetaselectronicas.com:9999/bind/batchBind', { // eslint-disable-line json: { storeId: config.storeId, tagItemBinds: [{ @@ -65,4 +68,6 @@ async function insertShelving(shelvingId, smartTagFk) { 'Authorization': key } }); + if (!response.body.success) + res.status(400).json({error: {code: 'bindingTagError', message: response.body.message}}); } diff --git a/methods/bindTicketShelving.js b/methods/bindTicketShelving.js index 02f8e5a..35b2828 100644 --- a/methods/bindTicketShelving.js +++ b/methods/bindTicketShelving.js @@ -3,12 +3,17 @@ const router = express.Router(); // eslint-disable-line const collectionSmartTag = require('../db/selectCollection'); const dataLogIn = require('../server'); const got = require('got'); +const config = require('../config'); // const insertTicketDB = require('../db/insertTicket'); router.get('/:collectionFk', async(req, res) => { const collectionFk = req.params.collectionFk; const [collection] = await collectionSmartTag(collectionFk); + if (!collection.length) + res.status(400).json({error: {code: 'emptyCollectionSmartTag', message: 'No hay datos'}}); + await insertTicket(collection); + res.json({message: 'SUCCESS'}); }); @@ -18,12 +23,13 @@ async function insertTicket(collection) { const info = await dataLogIn.dataLogIn; const key = info.data.token; const currentUser = info.data.currentUser; + for (let i = 0; i < collection.length; i++) { await got.post('http://app.etiquetaselectronicas.com:9999/item/batchImportItem', { // eslint-disable-line json: { agencyId: currentUser.agencyId, merchantId: currentUser.merchantId, - storeId: currentUser.storeId, + storeId: config.storeId, unitName: currentUser.unitName, itemList: [ { diff --git a/utilities/filter.js b/utilities/filter.js index d42412d..80738b4 100644 --- a/utilities/filter.js +++ b/utilities/filter.js @@ -2,17 +2,14 @@ const con = require('../db/connect'); function isSmartTag(toFilter, res) { const exp = /^A0A3B82[A-F0-9]{5}$/; if (!exp.test(toFilter)) - res.status(400).json({ error: { code: 'invalidPlate', message: 'Matricula incorrecta' } }) + res.status(400).json({error: {code: 'invalidPlate', message: 'Matricula incorrecta'}}); } - - async function isShelving(shelving, res) { const sql = `SELECT * FROM vn.shelving WHERE code = ?`; const [response] = await con.query(sql, shelving); if (response.length == 0) - res.status(400).json({ error: { code: 'invalidPlate', message: 'Matricula incorrecta' } }) - + res.status(400).json({error: {code: 'invalidPlate', message: 'Matricula incorrecta'}}); } exports.isSmartTag = isSmartTag;