From 4e65fb42d04e2ed7a7deaaff90dd0e02df4c9e90 Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 31 Mar 2022 12:27:30 +0200 Subject: [PATCH] refactor(insertTicket): bindTicketShelving and selectCollection --- db/selectCollection.js | 11 ++--- methods/bindTicketShelving.js | 82 +++++++++++++++-------------------- server.js | 3 -- utilities/encrypted.js | 1 - 4 files changed, 39 insertions(+), 58 deletions(-) diff --git a/db/selectCollection.js b/db/selectCollection.js index d141aeb..8c59913 100644 --- a/db/selectCollection.js +++ b/db/selectCollection.js @@ -1,12 +1,7 @@ const con = require('./connect'); -function selectCollectionSmartTag(collectionFk, callback) { +module.exports = function selectCollectionSmartTag(collectionFk) { const sql = `SELECT * FROM vn.collectionSmartTag WHERE collectionFk = ?;`; - con.query(sql, collectionFk, function(err, result, fields) { - if (err) throw err; - callback(null, result); - }); -} - -exports.selectCollectionSmartTag = selectCollectionSmartTag; + return con.query(sql, collectionFk); +}; diff --git a/methods/bindTicketShelving.js b/methods/bindTicketShelving.js index d5b1c64..cb2e9f0 100644 --- a/methods/bindTicketShelving.js +++ b/methods/bindTicketShelving.js @@ -1,61 +1,51 @@ const express = require('express'); const router = express.Router(); // eslint-disable-line -const select = require('../db/selectCollection'); +const collectionSmartTag = require('../db/selectCollection'); +const dataLogIn = require('../server'); +const got = require('got'); // const insertTicketDB = require('../db/insertTicket'); router.get('/:collectionFk', async(req, res) => { const collectionFk = req.params.collectionFk; - - ticketShelving(collectionFk); + const [collection] = await collectionSmartTag(collectionFk); + await insertTicket(collection); res.json({message: 'SUCCESS'}); }); + module.exports = router; -function ticketShelving(collectionFk) {// parametro collectionFk - select.selectCollectionSmartTag(collectionFk, function(err, data) { - insertTicket(data); - }); -} - -function insertTicket(consultaSql) { - const dataLogIn = require('../server'); - const got = require('got'); - (async() => { - const info = await dataLogIn.dataLogIn; - let key = info.data.token; - let currentUser = info.data.currentUser; - - (async() => { - for (let i = 0; i < consultaSql.length; i++) { - const {body} = await got.post('http://app.etiquetaselectronicas.com:9999/item/batchImportItem', { // eslint-disable-line - json: { - agencyId: currentUser.agencyId, - merchantId: currentUser.merchantId, - storeId: currentUser.storeId, - unitName: currentUser.unitName, - itemList: [ - { - attrCategory: 'verdnatura', - attrName: 'withTicket', - barCode: consultaSql[i].shelvingFk + consultaSql[i].level, // Matricula + nivel - itemTitle: 'tagWithTicket', - productCode: consultaSql[i].ticketFk, // Ticket - qrCode: consultaSql[i].ticketFk, - custFeature1: consultaSql[i].clientFk, // Cliente - custFeature2: consultaSql[i].agencyFk, // Agencia de transporte - custFeature3: '', // consultaSql[i].workerFk, // Trabajador - custFeature4: consultaSql[i].wagon, // Wagon - } - ] - }, - responseType: 'json', - headers: { - 'Authorization': key +async function insertTicket(consultaSql) { + const info = await dataLogIn.dataLogIn; + const key = info.data.token; + const currentUser = info.data.currentUser; + for (let i = 0; i < consultaSql.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, + unitName: currentUser.unitName, + itemList: [ + { + attrCategory: 'verdnatura', + attrName: 'withTicket', + barCode: consultaSql[i].shelvingFk + consultaSql[i].level, // Matricula + nivel + itemTitle: 'tagWithTicket', + productCode: consultaSql[i].ticketFk, // Ticket + qrCode: consultaSql[i].ticketFk, + custFeature1: consultaSql[i].clientFk, // Cliente + custFeature2: consultaSql[i].agencyFk, // Agencia de transporte + custFeature3: '', // consultaSql[i].workerFk, // Trabajador + custFeature4: consultaSql[i].wagon, // Wagon } - }); + ] + }, + responseType: 'json', + headers: { + 'Authorization': key } - })(); - })(); + }); + } } exports.insertTicket = insertTicket; diff --git a/server.js b/server.js index ad35c89..879f1a9 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,8 @@ const config = require('./config'); const app = express(); const dataLogIn = logIn; - exports.dataLogIn = dataLogIn; -// con.connect(function(err) {}); - // app.use('/getNumShelving', require('./methods/setCollection__')); app.use('/bindShelving', require('./methods/bindShelvingTag')); // smartTagFk, shelving, level app.use('/insertTicket', require('./methods/bindTicketShelving')); // collectionFk diff --git a/utilities/encrypted.js b/utilities/encrypted.js index d97ab1d..f8f9d24 100644 --- a/utilities/encrypted.js +++ b/utilities/encrypted.js @@ -1,6 +1,5 @@ const crypto = require('crypto'); const got = require('got'); -// const config = require('../config'); const encryptPassword = async password => { const {body} = await got.get('http://app.etiquetaselectronicas.com:9999/user/getErpPublicKey', {