From 31d655b1309233104fc889abb2cbd8272cc28c77 Mon Sep 17 00:00:00 2001 From: Joan Date: Wed, 10 Oct 2018 09:59:42 +0200 Subject: [PATCH] Create ticket with transaction --- client/order/src/create/card.js | 2 -- client/ticket/src/sale/index.js | 2 +- .../importToNewRefundTicket.js | 28 +++++++++++++++---- .../importToNewRefundTicket.spec.js | 18 ++++++------ .../common/methods/claim/regularizeClaim.js | 4 ++- services/loopback/common/locale/es.json | 3 +- .../common/methods/sale/moveToNewTicket.js | 21 +++++++++----- .../loopback/common/methods/ticket/new.js | 11 ++++++-- .../common/methods/ticket/specs/new.spec.js | 8 +++--- services/loopback/common/models/vn-model.js | 4 +-- services/order/common/methods/order/new.js | 7 +++-- 11 files changed, 72 insertions(+), 36 deletions(-) diff --git a/client/order/src/create/card.js b/client/order/src/create/card.js index ba605ae73..6a477f59e 100644 --- a/client/order/src/create/card.js +++ b/client/order/src/create/card.js @@ -75,8 +75,6 @@ class Controller { this.$http.post(`order/api/Orders/new`, params).then(res => { this.vnApp.showSuccess(this.translate.instant('Data saved!')); this.$state.go("order.card.catalog", {id: res.data}); - }).catch(e => { - this.vnApp.showError(this.translate.instant(e.data.error.message)); }); } } diff --git a/client/ticket/src/sale/index.js b/client/ticket/src/sale/index.js index 71c17158d..fd4a7b705 100644 --- a/client/ticket/src/sale/index.js +++ b/client/ticket/src/sale/index.js @@ -189,7 +189,7 @@ class Controller { let sales = this.getCheckedLines(); this.$http.post(`/api/Sales/MoveToNewTicket`, {ticket: ticket, sales: sales}).then(res => { - let url = this.$state.href("ticket.card.sale", {id: res.data}, {absolute: true}); + let url = this.$state.href("ticket.card.sale", {id: res.data.id}, {absolute: true}); window.open(url, '_blank'); this.$scope.transfer.hide(); this.$scope.model.refresh(); diff --git a/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js b/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js index 16efadb8d..f2c691a6e 100644 --- a/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js +++ b/services/claim/common/methods/claim-beginning/importToNewRefundTicket.js @@ -19,12 +19,12 @@ module.exports = Self => { } }); - async function addSalesToTicket(salesToRefund, newRefundTicket, transaction) { + async function addSalesToTicket(salesToRefund, ticketFk, transaction) { let formatedSales = []; salesToRefund.forEach(sale => { let formatedSale = { itemFk: sale.sale().itemFk, - ticketFk: newRefundTicket, + ticketFk: ticketFk, concept: sale.sale().concept, quantity: -Math.abs(sale.quantity), price: sale.sale().price, @@ -113,14 +113,30 @@ module.exports = Self => { try { let newRefundTicket = await models.Ticket.new(params, {transaction: transaction}); - let observation = {description: `Reclama ticket: ${claim.ticketFk}`, ticketFk: newRefundTicket, observationTypeFk: obsevationType.id}; + + let observation = { + description: `Reclama ticket: ${claim.ticketFk}`, + ticketFk: newRefundTicket.id, + observationTypeFk: obsevationType.id + }; await saveObservation(observation, {transaction: transaction}); - await models.TicketTracking.create({ticketFk: newRefundTicket, stateFk: state.id, workerFk: worker.id}, {transaction: transaction}); + + await models.TicketTracking.create({ + ticketFk: newRefundTicket.id, + stateFk: state.id, + workerFk: worker.id + }, {transaction: transaction}); + let salesToRefund = await models.ClaimBeginning.find(salesFilter); - let createdSales = await addSalesToTicket(salesToRefund, newRefundTicket, {transaction: transaction}); + let createdSales = await addSalesToTicket(salesToRefund, newRefundTicket.id, {transaction: transaction}); insertIntoClaimEnd(createdSales, id, worker.id, {transaction: transaction}); - await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [newRefundTicket, claim.ticketFk], {transaction: transaction}); + + await Self.rawSql('CALL vn.ticketCalculateClon(?, ?)', [ + newRefundTicket.id, claim.ticketFk + ], {transaction: transaction}); + await transaction.commit(); + return newRefundTicket; } catch (e) { await transaction.rollback(); diff --git a/services/claim/common/methods/claim-beginning/importToNewRefundTicket.spec.js b/services/claim/common/methods/claim-beginning/importToNewRefundTicket.spec.js index e5ae10bf9..542f5c0e9 100644 --- a/services/claim/common/methods/claim-beginning/importToNewRefundTicket.spec.js +++ b/services/claim/common/methods/claim-beginning/importToNewRefundTicket.spec.js @@ -1,16 +1,16 @@ const app = require(`${servicesDir}/claim/server/server`); describe('claimBeginning', () => { - let ticketId; + let ticket; let refundTicketObservations; let refundTicketSales; let salesInsertedInClaimEnd; afterAll(async() => { let promises = []; - promises.push(app.models.Ticket.destroyById(ticketId)); + promises.push(app.models.Ticket.destroyById(ticket.id)); - promises.push(app.models.Ticket.rawSql(`DELETE FROM vn.orderTicket WHERE ticketFk ='${ticketId}';`)); + promises.push(app.models.Ticket.rawSql(`DELETE FROM vn.orderTicket WHERE ticketFk ='${ticket.id}';`)); await Promise.all(promises); }); @@ -19,11 +19,13 @@ describe('claimBeginning', () => { it('should create a new ticket with negative sales, save an observation, update the state and insert the negative sales into claimEnd', async() => { let ctxOfSalesAssistant = {req: {accessToken: {userId: 21}}}; let claimId = 1; - ticketId = await app.models.ClaimBeginning.importToNewRefundTicket(ctxOfSalesAssistant, claimId); - await app.models.Ticket.findById(ticketId); - refundTicketSales = await app.models.Sale.find({where: {ticketFk: ticketId}}); - refundTicketObservations = await app.models.TicketObservation.find({where: {ticketFk: ticketId}}); - let refundTicketState = await app.models.TicketState.findById(ticketId); + ticket = await app.models.ClaimBeginning.importToNewRefundTicket(ctxOfSalesAssistant, claimId); + + await app.models.Ticket.findById(ticket.id); + + refundTicketSales = await app.models.Sale.find({where: {ticketFk: ticket.id}}); + refundTicketObservations = await app.models.TicketObservation.find({where: {ticketFk: ticket.id}}); + let refundTicketState = await app.models.TicketState.findById(ticket.id); salesInsertedInClaimEnd = await app.models.ClaimEnd.find({where: {claimFk: claimId}}); expect(refundTicketSales.length).toEqual(2); diff --git a/services/claim/common/methods/claim/regularizeClaim.js b/services/claim/common/methods/claim/regularizeClaim.js index 4aedcb45c..59d8635a5 100644 --- a/services/claim/common/methods/claim/regularizeClaim.js +++ b/services/claim/common/methods/claim/regularizeClaim.js @@ -133,7 +133,7 @@ module.exports = Self => { } async function createTicket(params, transaction) { - return await Self.app.models.Ticket.new({ + let ticket = await Self.app.models.Ticket.new({ shipped: new Date(), landed: new Date(), clientFk: params.clientFk, @@ -142,6 +142,8 @@ module.exports = Self => { addressFk: params.addressFk, userId: params.userId }, {transaction: transaction}); + + return ticket.id; } async function sendMessage(ctx, params, transaction) { diff --git a/services/loopback/common/locale/es.json b/services/loopback/common/locale/es.json index 1309790ef..bf4bedd4f 100644 --- a/services/loopback/common/locale/es.json +++ b/services/loopback/common/locale/es.json @@ -52,5 +52,6 @@ "Warehouse cannot be blank": "El almacén no puede quedar en blanco", "Agency cannot be blank": "La agencia no puede quedar en blanco", "You don't have enough privileges to do that": "No tienes permisos para para hacer esto", - "This address doesn't exist": "This address doesn't exist" + "This address doesn't exist": "Este consignatario no existe", + "The sales of this ticket can't be modified": "Los movimientos de este tiquet no pueden ser modificadas" } \ No newline at end of file diff --git a/services/loopback/common/methods/sale/moveToNewTicket.js b/services/loopback/common/methods/sale/moveToNewTicket.js index f69227302..a3cc4c057 100644 --- a/services/loopback/common/methods/sale/moveToNewTicket.js +++ b/services/loopback/common/methods/sale/moveToNewTicket.js @@ -51,14 +51,21 @@ module.exports = Self => { userId: userId }; - let newTicket = await model.Ticket.new(newTicketParams); + let transaction = await Self.beginTransaction({}); + try { + let newTicket = await model.Ticket.new(newTicketParams, {transaction: transaction}); - let promises = []; - for (let i = 0; i < params.sales.length; i++) { - promises.push(model.Sale.update({id: params.sales[i].id}, {ticketFk: newTicket})); + await model.Sale.updateAll( + {ticketFk: params.ticket.oldTicketFk}, + {ticketFk: newTicket.id}, + {transaction: transaction}); + + await transaction.commit(); + + return newTicket; + } catch (e) { + await transaction.rollback(); + throw e; } - await Promise.all(promises); - - return newTicket; }; }; diff --git a/services/loopback/common/methods/ticket/new.js b/services/loopback/common/methods/ticket/new.js index 890a81b0b..274224bc6 100644 --- a/services/loopback/common/methods/ticket/new.js +++ b/services/loopback/common/methods/ticket/new.js @@ -22,7 +22,12 @@ module.exports = Self => { }); Self.new = async(params, transaction) => { - let existsAddress = await Self.app.models.Address.findOne({where: {id: params.addressFk, clientFk: params.clientFk}}); + let existsAddress = await Self.app.models.Address.findOne({ + where: { + id: params.addressFk, + clientFk: params.clientFk} + }); + if (!existsAddress) throw new UserError(`This address doesn't exist`); @@ -40,6 +45,8 @@ module.exports = Self => { params.userId ], transaction); - return result[1][0].newTicketId; + return await Self.findOne({ + where: {id: result[1][0].newTicketId} + }, transaction); }; }; diff --git a/services/loopback/common/methods/ticket/specs/new.spec.js b/services/loopback/common/methods/ticket/specs/new.spec.js index be4c03eb6..cae4fa4cb 100644 --- a/services/loopback/common/methods/ticket/specs/new.spec.js +++ b/services/loopback/common/methods/ticket/specs/new.spec.js @@ -1,11 +1,11 @@ const app = require(`${servicesDir}/ticket/server/server`); describe('ticket new()', () => { - let ticketId; + let ticket; let today = new Date(); afterAll(async() => { - await app.models.Ticket.destroyById(ticketId); + await app.models.Ticket.destroyById(ticket.id); }); it('should throw an error if the address doesnt exist', async() => { @@ -33,10 +33,10 @@ describe('ticket new()', () => { landed: today }; - ticketId = await app.models.Ticket.new(params); + ticket = await app.models.Ticket.new(params); let newestTicketIdInFixtures = 21; - expect(ticketId).toBeGreaterThan(newestTicketIdInFixtures); + expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures); }); }); diff --git a/services/loopback/common/models/vn-model.js b/services/loopback/common/models/vn-model.js index 2b42d0668..bccb23a34 100644 --- a/services/loopback/common/models/vn-model.js +++ b/services/loopback/common/models/vn-model.js @@ -187,8 +187,8 @@ module.exports = function(Self) { }); }; - Self.rawStmt = function(stmt) { - return this.rawSql(stmt.sql, stmt.params); + Self.rawStmt = function(stmt, options = {}) { + return this.rawSql(stmt.sql, stmt.params, options); }; Self.escapeName = function(name) { diff --git a/services/order/common/methods/order/new.js b/services/order/common/methods/order/new.js index e951b3033..f2caf2347 100644 --- a/services/order/common/methods/order/new.js +++ b/services/order/common/methods/order/new.js @@ -20,8 +20,11 @@ module.exports = Self => { }); Self.new = async params => { - let clientFkByAddress = await Self.app.models.Address.findOne({where: {id: params.addressFk}, fields: 'clientFk'}); - let clientFk = clientFkByAddress.clientFk; + let address = await Self.app.models.Address.findOne({ + where: {id: params.addressFk}, + fields: 'clientFk' + }); + let clientFk = address.clientFk; let client = await Self.app.models.Client.findOne({ where: {id: clientFk},