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.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));
});
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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);

View File

@ -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) {

View File

@ -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"
}

View File

@ -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;
};
};

View File

@ -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);
};
};

View File

@ -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);
});
});

View File

@ -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) {

View File

@ -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},