Skip to content

Instantly share code, notes, and snippets.

@wharley
Created March 9, 2017 05:30
Show Gist options
  • Save wharley/31bf13b7f222516fc062d7984def5a4c to your computer and use it in GitHub Desktop.
Save wharley/31bf13b7f222516fc062d7984def5a4c to your computer and use it in GitHub Desktop.
exports.update = function(req, res){
var dados = req.body.data;
processRequests(dados)
.then(function(data){
res.send(data)
})
};
function processRequests(arrData){
const codes = arrData.produtos.map(code => code)
const promises = codes.map(processRequest)
const auditing = codes.map(processAuditing)
return Promise.all(promises, auditing)
}
function processRequest(code){
return new Promise(function(resolve, reject){
if(code.code == 0)
return resolve(false)
model.Preco
.update({
preco_venda: code.price,
preco_fabrica: code.factory,
data_preco: code.datePrice.substr(6, 4) + '-' + code.datePrice.substr(3, 2) + '-' + code.datePrice.substr(0, 2)
},
{
where: {
cod_produto: code.code,
cod_empresa: code.empresa
}
})
.then(function() {
resolve(true)
}).catch(function(error) {
console.log(error);
reject(error)
});
})
}
function processAuditing(code){
return new Promise(function(resolve, reject){
if(code.code == 0)
return resolve(false)
model.Auditoria
.build({
num_empresa: code.empresa,
cod_usuario: code.usuario,
observacao: 'PrecoImportacaoCsv',
dt_cadastro: new Date(),
tabela: 'ek_preco',
tipo: 'I',
obs_antigo: '',
obs_novo: 'Produto: ' + code.code + ' Preco fabrica: ' + code.factory + ' Preco venda: ' + code.price + ' Data preco: ' + code.datePrice.substr(6, 4) + '-' + code.datePrice.substr(3, 2) + '-' + code.datePrice.substr(0, 2),
seq_pedido_cli: null
})
.save()
.then(function(data) {
resolve(true)
}).catch(function(error) {
console.log(error);
reject(error)
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment