refactor(db, bindShelving): refactor async
gitea/smart-tag/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-03-31 11:44:35 +02:00
parent 124771cb2d
commit 544730eba4
10 changed files with 2455 additions and 97 deletions

View File

@ -1,7 +1,7 @@
const mysql = require('mysql');
const mysql = require('mysql2/promise');
const config = require('../config');
module.exports = mysql.createConnection({
module.exports = mysql.createPool({
user: config.db.user,
password: config.db.password,
host: config.db.host, // 'test-db.verdnatura.es',

View File

@ -1,10 +1,10 @@
const con = require('./connect');
function maxWagon(collectionFk, callback) {
let consSql = `SELECT MAX(tc.wagon) AS MaxWagons
let sql = `SELECT MAX(tc.wagon) AS MaxWagons
FROM ticketCollection tc
WHERE tc.collectionFk = ${collectionFk}`;
con.query(consSql, function(err, result, fields) {
con.query(sql, function(err, result, fields) {
callback(null, result);
});
}

View File

@ -1,8 +1,8 @@
const con = require('./connect');
function selectCollectionSmartTag(collectionFk, callback) {
const consSql = `SELECT * FROM vn.collectionSmartTag WHERE collectionFk = ?;`;
con.query(consSql, collectionFk, function(err, result, fields) {
const sql = `SELECT * FROM vn.collectionSmartTag WHERE collectionFk = ?;`;
con.query(sql, collectionFk, function(err, result, fields) {
if (err) throw err;
callback(null, result);
});

View File

@ -1,11 +0,0 @@
const con = require('./connect');
function shelvingExist(shelving, callback) {
const consSql = `SELECT * FROM vn.shelving WHERE code = ?`;
con.query(consSql, shelving, function(err, result, fields) {
callback(null, result);
});
}
exports.shelvingExist = shelvingExist;

View File

@ -1,8 +1,8 @@
const con = require('./connect');
function select(ticket, callback) {
let consSql = `SELECT * FROM vn.collectionSmartTag WHERE ticketFk = ?`;
con.query(consSql, ticket, function(err, result, fields) {
let sql = `SELECT * FROM vn.collectionSmartTag WHERE ticketFk = ?`;
con.query(sql, ticket, function(err, result) {
if (err) throw err;
callback(null, result);
});

View File

@ -11,7 +11,7 @@ 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 result = bindShelvingTag(smartTagFk, shelving, level);
const result = await bindShelvingTag(smartTagFk, shelving, level);
if (result == 'INVALID_PLATE')
throw new Error('CODIGO MATRICULA INCORRECTO');
@ -19,17 +19,15 @@ router.get('/:smartTagFk&:shelving&:level', async(req, res) => {
throw new Error('CODIGO DE ETIQUETA INCORRECTO');
else
res.json({message: 'SUCCESS'});
return;
});
module.exports = router;
// Vinculamos la etiqueta con la balda del carry y en la base de datos
function bindShelvingTag(smartTagFk, shelving, level) {
async function bindShelvingTag(smartTagFk, shelving, level) {
let shelvingId = shelving + level;
if (filter.isSmartTag(smartTagFk)) {
if (filter.isShelving(shelving)) {
if (await filter.isShelving(shelving)) {
insertShelving(shelvingId, smartTagFk); // al carry en ESL CLOUD SYSTEM
insertSmartTag.insertSmartTag(smartTagFk, shelving, level); // en vn.smartTag
} else
@ -40,58 +38,42 @@ function bindShelvingTag(smartTagFk, shelving, level) {
// Creamos un "articulo", que sera el que luego modificaremos para añadirle un ticket
async function insertShelving(shelvingId, smartTagFk) {
(async() => {
const info = await dataLogIn.dataLogIn;
let key = info.data.token;
let currentUser = info.data.currentUser;
(async() => {
const {body} = await got.post('http://app.etiquetaselectronicas.com:9999/item/batchImportItem', { // eslint-disable-line
json: {
agencyId: currentUser.agencyId,
merchantId: currentUser.merchantId,
storeId: config.storeId,
unitName: currentUser.unitName,
itemList: [
{
attrCategory: 'verdnatura', // Plantilla
attrName: 'withOutTicket', // Tipo de Plantilla
barCode: shelvingId, // Matricula + nivel --> Cod.Barras artículo
itemTitle: 'tagWithOutTicket',
qrCode: shelvingId
}
]
},
responseType: 'json',
headers: {
'Authorization': key
const info = await dataLogIn.dataLogIn;
const key = info.data.token;
const currentUser = info.data.currentUser;
await got.post('http://app.etiquetaselectronicas.com:9999/item/batchImportItem', { // eslint-disable-line
json: {
agencyId: currentUser.agencyId,
merchantId: currentUser.merchantId,
storeId: config.storeId,
unitName: currentUser.unitName,
itemList: [
{
attrCategory: 'verdnatura', // Plantilla
attrName: 'withOutTicket', // Tipo de Plantilla
barCode: shelvingId, // Matricula + nivel --> Cod.Barras artículo
itemTitle: 'tagWithOutTicket',
qrCode: shelvingId
}
});
await bindShelving(smartTagFk, shelvingId);
})();
})();
]
},
responseType: 'json',
headers: {
'Authorization': key
}
});
await got.post('http://app.etiquetaselectronicas.com:9999/bind/batchBind', { // eslint-disable-line
json: {
storeId: config.storeId,
tagItemBinds: [{
eslBarcode: smartTagFk,
itemBarcode: shelvingId
}
]
},
responseType: 'json',
headers: {
'Authorization': key
}
});
}
// Creamos la etiqueta con su vinculada con el ShelvingId al articulo
async function bindShelving(smartTagFk, shelvingId) {
(async() => {
const info = await dataLogIn.dataLogIn;
let key = info.data.token;
(async() => {
const {body} = await got.post('http://app.etiquetaselectronicas.com:9999/bind/batchBind', { // eslint-disable-line
json: {
storeId: config.storeId,
tagItemBinds: [{
eslBarcode: smartTagFk,
itemBarcode: shelvingId
}
]
},
responseType: 'json',
headers: {
'Authorization': key
}
});
})();
})();
}
exports.bindShelving = bindShelving;

2408
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,6 +15,8 @@
"express": "^4.17.1",
"got": "^11.8.2",
"ip": "^1.1.5",
"mysql": "^2.18.1"
"mysql": "^2.18.1",
"mysql2": "^2.3.3",
"mysql2-promise": "^0.1.4"
}
}

View File

@ -1,6 +1,5 @@
const express = require('express');
const logIn = require('./methods/logIn');
const con = require('./db/connect');
const config = require('./config');
const app = express();
@ -8,7 +7,7 @@ const dataLogIn = logIn;
exports.dataLogIn = dataLogIn;
con.connect(function(err) {});
// con.connect(function(err) {});
// app.use('/getNumShelving', require('./methods/setCollection__'));
app.use('/bindShelving', require('./methods/bindShelvingTag')); // smartTagFk, shelving, level

View File

@ -1,14 +1,14 @@
const select = require('../db/selectShelving');
const con = require('../db/connect');
function isSmartTag(toFilter) {
const exp = /^A0A3B82[A-F0-9]{5}$/;
return exp.test(toFilter);
}
function isShelving(toFilter) {
console.log(select.shelvingExist(toFilter, function(err, data) {
console.log(data);
}));
return select.shelvingExist(toFilter);
async function isShelving(shelving) {
const sql = `SELECT * FROM vn.shelving WHERE code = ?`;
const [response] = await con.query(sql, shelving);
return response.length > 0;
}
exports.isSmartTag = isSmartTag;
exports.isShelving = isShelving;