I am developing a POC where I need to register a sale.
For this, I use this code:
this.salvarVenda = function(connection, objeto, response, callback){
let sqlVenda = utilVenda.createSaveQuery(objeto);
console.log(sqlVenda);
connection.query(sqlVenda, function (error, results){
if(error) throw error;
const id_venda = 0;
//Depois que salva a venda, usa-se o id persistido e salva-se os itens
objeto.produtos.forEach((el) => {
const sql = utilVenda.createSaveQueryItems(el, id_venda);
connection.query(sql, function(erro){
if(erro != null){
console.log("Response: " + response.statusCode);
response.status(500);
console.log("Response depois : " + response.statusCode);
console.log("Deu erro"/*erro*/);
}
});
})
callback();
})
}
I wanted to know how to leave everything in one transaction and, in case something goes wrong in the second operation, rollback the whole sale.
Ah, one last thing: Even when the statusCode of response as 500
, in my frontEnd , callback in Promise I still get statusCode 200
.