Create ticket with transaction

This commit is contained in:
Joan Sanchez 2018-10-10 09:59:42 +02:00
parent 8c0e68313b
commit 31d655b130
11 changed files with 72 additions and 36 deletions

View File

@ -75,8 +75,6 @@ class Controller {
this.$http.post(`order/api/Orders/new`, params).then(res => { this.$http.post(`order/api/Orders/new`, params).then(res => {
this.vnApp.showSuccess(this.translate.instant('Data saved!')); this.vnApp.showSuccess(this.translate.instant('Data saved!'));
this.$state.go("order.card.catalog", {id: res.data}); this.$state.go("order.card.catalog", {id: res.data});
}).catch(e => {
this.vnApp.showError(this.translate.instant(e.data.error.message));
}); });
} }
} }

View File

@ -189,7 +189,7 @@ class Controller {
let sales = this.getCheckedLines(); let sales = this.getCheckedLines();
this.$http.post(`/api/Sales/MoveToNewTicket`, {ticket: ticket, sales: sales}).then(res => { 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'); window.open(url, '_blank');
this.$scope.transfer.hide(); this.$scope.transfer.hide();
this.$scope.model.refresh(); this.$scope.model.refresh();

View File

@ -19,12 +19,12 @@ module.exports = Self => {
} }
}); });
async function addSalesToTicket(salesToRefund, newRefundTicket, transaction) { async function addSalesToTicket(salesToRefund, ticketFk, transaction) {
let formatedSales = []; let formatedSales = [];
salesToRefund.forEach(sale => { salesToRefund.forEach(sale => {
let formatedSale = { let formatedSale = {
itemFk: sale.sale().itemFk, itemFk: sale.sale().itemFk,
ticketFk: newRefundTicket, ticketFk: ticketFk,
concept: sale.sale().concept, concept: sale.sale().concept,
quantity: -Math.abs(sale.quantity), quantity: -Math.abs(sale.quantity),
price: sale.sale().price, price: sale.sale().price,
@ -113,14 +113,30 @@ module.exports = Self => {
try { try {
let newRefundTicket = await models.Ticket.new(params, {transaction: transaction}); 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 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 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}); 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(); await transaction.commit();
return newRefundTicket; return newRefundTicket;
} catch (e) { } catch (e) {
await transaction.rollback(); await transaction.rollback();

View File

@ -1,16 +1,16 @@
const app = require(`${servicesDir}/claim/server/server`); const app = require(`${servicesDir}/claim/server/server`);
describe('claimBeginning', () => { describe('claimBeginning', () => {
let ticketId; let ticket;
let refundTicketObservations; let refundTicketObservations;
let refundTicketSales; let refundTicketSales;
let salesInsertedInClaimEnd; let salesInsertedInClaimEnd;
afterAll(async() => { afterAll(async() => {
let promises = []; 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); 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() => { 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 ctxOfSalesAssistant = {req: {accessToken: {userId: 21}}};
let claimId = 1; let claimId = 1;
ticketId = await app.models.ClaimBeginning.importToNewRefundTicket(ctxOfSalesAssistant, claimId); ticket = await app.models.ClaimBeginning.importToNewRefundTicket(ctxOfSalesAssistant, claimId);
await app.models.Ticket.findById(ticketId);
refundTicketSales = await app.models.Sale.find({where: {ticketFk: ticketId}}); await app.models.Ticket.findById(ticket.id);
refundTicketObservations = await app.models.TicketObservation.find({where: {ticketFk: ticketId}});
let refundTicketState = await app.models.TicketState.findById(ticketId); 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}}); salesInsertedInClaimEnd = await app.models.ClaimEnd.find({where: {claimFk: claimId}});
expect(refundTicketSales.length).toEqual(2); expect(refundTicketSales.length).toEqual(2);

View File

@ -133,7 +133,7 @@ module.exports = Self => {
} }
async function createTicket(params, transaction) { async function createTicket(params, transaction) {
return await Self.app.models.Ticket.new({ let ticket = await Self.app.models.Ticket.new({
shipped: new Date(), shipped: new Date(),
landed: new Date(), landed: new Date(),
clientFk: params.clientFk, clientFk: params.clientFk,
@ -142,6 +142,8 @@ module.exports = Self => {
addressFk: params.addressFk, addressFk: params.addressFk,
userId: params.userId userId: params.userId
}, {transaction: transaction}); }, {transaction: transaction});
return ticket.id;
} }
async function sendMessage(ctx, params, transaction) { async function sendMessage(ctx, params, transaction) {

View File

@ -52,5 +52,6 @@
"Warehouse cannot be blank": "El almacén no puede quedar en blanco", "Warehouse cannot be blank": "El almacén no puede quedar en blanco",
"Agency cannot be blank": "La agencia 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", "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"
} }

View File

@ -51,14 +51,21 @@ module.exports = Self => {
userId: userId 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 = []; await model.Sale.updateAll(
for (let i = 0; i < params.sales.length; i++) { {ticketFk: params.ticket.oldTicketFk},
promises.push(model.Sale.update({id: params.sales[i].id}, {ticketFk: newTicket})); {ticketFk: newTicket.id},
} {transaction: transaction});
await Promise.all(promises);
await transaction.commit();
return newTicket; return newTicket;
} catch (e) {
await transaction.rollback();
throw e;
}
}; };
}; };

View File

@ -22,7 +22,12 @@ module.exports = Self => {
}); });
Self.new = async(params, transaction) => { 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) if (!existsAddress)
throw new UserError(`This address doesn't exist`); throw new UserError(`This address doesn't exist`);
@ -40,6 +45,8 @@ module.exports = Self => {
params.userId params.userId
], transaction); ], transaction);
return result[1][0].newTicketId; return await Self.findOne({
where: {id: result[1][0].newTicketId}
}, transaction);
}; };
}; };

View File

@ -1,11 +1,11 @@
const app = require(`${servicesDir}/ticket/server/server`); const app = require(`${servicesDir}/ticket/server/server`);
describe('ticket new()', () => { describe('ticket new()', () => {
let ticketId; let ticket;
let today = new Date(); let today = new Date();
afterAll(async() => { 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() => { it('should throw an error if the address doesnt exist', async() => {
@ -33,10 +33,10 @@ describe('ticket new()', () => {
landed: today landed: today
}; };
ticketId = await app.models.Ticket.new(params); ticket = await app.models.Ticket.new(params);
let newestTicketIdInFixtures = 21; let newestTicketIdInFixtures = 21;
expect(ticketId).toBeGreaterThan(newestTicketIdInFixtures); expect(ticket.id).toBeGreaterThan(newestTicketIdInFixtures);
}); });
}); });

View File

@ -187,8 +187,8 @@ module.exports = function(Self) {
}); });
}; };
Self.rawStmt = function(stmt) { Self.rawStmt = function(stmt, options = {}) {
return this.rawSql(stmt.sql, stmt.params); return this.rawSql(stmt.sql, stmt.params, options);
}; };
Self.escapeName = function(name) { Self.escapeName = function(name) {

View File

@ -20,8 +20,11 @@ module.exports = Self => {
}); });
Self.new = async params => { Self.new = async params => {
let clientFkByAddress = await Self.app.models.Address.findOne({where: {id: params.addressFk}, fields: 'clientFk'}); let address = await Self.app.models.Address.findOne({
let clientFk = clientFkByAddress.clientFk; where: {id: params.addressFk},
fields: 'clientFk'
});
let clientFk = address.clientFk;
let client = await Self.app.models.Client.findOne({ let client = await Self.app.models.Client.findOne({
where: {id: clientFk}, where: {id: clientFk},