#1656 ticket.regularizar eliminar ticket + tests
gitea/salix/dev This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2019-09-03 13:27:02 +02:00
parent e1b5b449ce
commit 219614b44f
6 changed files with 40 additions and 41 deletions

View File

@ -53,7 +53,7 @@ module.exports = Self => {
Self.priceDifference = async(ctx, id, landed, addressId, agencyModeId, zoneId, warehouseId) => {
const models = Self.app.models;
const isEditable = await models.Ticket.isEditable(ctx, id);
const isEditable = await Self.isEditable(ctx, id);
const userId = ctx.req.accessToken.userId;
if (!isEditable)
@ -67,19 +67,18 @@ module.exports = Self => {
throw new UserError(`You don't have privileges to change the zone`);
}
let salesObj = {};
salesObj.items = await models.Sale.find({
where: {
ticketFk: id
},
order: 'concept ASC',
include: [{
relation: 'item'
}]
});
salesObj.totalUnitPrice = 0.00;
salesObj.totalNewPrice = 0.00;
salesObj.totalDifference = 0.00;
let salesObj = {
items: await models.Sale.find({
where: {
ticketFk: id
},
order: 'concept ASC',
include: 'item'
}),
totalUnitPrice: 0.00,
totalNewPrice: 0.00,
totalDifference: 0.00,
};
const query = `CALL vn.ticket_priceDifference(?, ?, ?, ?, ?)`;
const args = [id, landed, addressId, zoneId, warehouseId];
@ -91,20 +90,25 @@ module.exports = Self => {
map.set(difComponent.saleFk, difComponent);
});
function round(value) {
return Math.round(value * 100) / 100;
}
salesObj.items.forEach(sale => {
const difComponent = map.get(sale.id);
if (difComponent) {
sale.component = difComponent;
salesObj.totalNewPrice += sale.component.newPrice;
salesObj.totalDifference += sale.component.difference;
salesObj.totalUnitPrice = Math.round(salesObj.totalUnitPrice * 100) / 100;
salesObj.totalNewPrice = Math.round(salesObj.totalNewPrice * 100) / 100;
salesObj.totalDifference += difComponent.difference;
salesObj.totalDifference = round(salesObj.totalDifference);
salesObj.totalNewPrice += difComponent.newPrice;
salesObj.totalNewPrice = round(salesObj.totalNewPrice);
}
salesObj.totalUnitPrice += sale.price;
salesObj.totalDifference = Math.round(salesObj.totalDifference * 100) / 100;
salesObj.totalUnitPrice = round(salesObj.totalUnitPrice);
});
return salesObj;

View File

@ -1,33 +1,33 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('delete', {
Self.remoteMethod('setDeleted', {
description: 'Sets true the isDeleted value of a ticket',
accessType: 'WRITE',
accepts: [{
arg: 'ticketFk',
type: 'Object',
arg: 'id',
type: 'Number',
required: true,
description: 'TicketFk',
http: {source: 'body'}
description: 'The ticket id',
http: {source: 'path'}
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/delete`,
path: `/:id/setDeleted`,
verb: 'post'
}
});
Self.delete = async params => {
Self.setDeleted = async id => {
try {
let claimOfATicket = await Self.app.models.Claim.findOne({where: {ticketFk: params.id}});
let claimOfATicket = await Self.app.models.Claim.findOne({where: {ticketFk: id}});
if (claimOfATicket)
throw new UserError('You must delete the claim id %d first', 'DELETE_CLAIM_FIRST', claimOfATicket.id);
let currentTicket = await Self.app.models.Ticket.findById(params.id);
let currentTicket = await Self.app.models.Ticket.findById(id);
return await currentTicket.updateAttributes({isDeleted: true});
} catch (e) {
throw e;

View File

@ -21,25 +21,20 @@ describe('ticket deleted()', () => {
expect(ticket.isDeleted).toEqual(false);
});
it('should set a ticket to deleted and log the change on TicketState table', async() => {
let ctx = {req: {accessToken: {userId: 9}}};
let params = {id: ticket.id};
await app.models.Ticket.delete(ctx, params);
it('should set a ticket to deleted', async() => {
await app.models.Ticket.setDeleted(ticket.id);
let deletedTicket = await app.models.Ticket.findOne({where: {id: ticket.id}, fields: ['isDeleted']});
let changedState = await app.models.TicketState.findOne({where: {ticketFk: ticket.id}});
expect(deletedTicket.isDeleted).toEqual(true);
expect(changedState.stateFk).toEqual(17);
});
it('should throw an error if the given ticket has a claim', async() => {
let ctx = {req: {accessToken: {userId: 9}}};
let params = {id: 16};
let ticketId = 16;
let error;
try {
await app.models.Ticket.delete(ctx, params);
await app.models.Ticket.setDeleted(ticketId);
} catch (e) {
error = e;
}

View File

@ -11,7 +11,7 @@ module.exports = Self => {
require('../methods/ticket/componentUpdate')(Self);
require('../methods/ticket/new')(Self);
require('../methods/ticket/isEditable')(Self);
require('../methods/ticket/delete')(Self);
require('../methods/ticket/setDeleted')(Self);
require('../methods/ticket/getVAT')(Self);
require('../methods/ticket/getSales')(Self);
require('../methods/ticket/getSalesPersonMana')(Self);

View File

@ -126,8 +126,8 @@ class Controller {
deleteTicket(response) {
if (response === 'ACCEPT') {
let params = {id: this.ticket.id};
this.$http.post(`/api/Tickets/delete`, params).then(() => {
const query = `/api/Tickets/${this.ticket.id}/setDeleted`;
this.$http.post(query).then(() => {
this.$state.go('ticket.index');
this.vnApp.showSuccess(this.$translate.instant('Ticket deleted'));
});

View File

@ -62,7 +62,7 @@ describe('Ticket Component vnTicketDescriptor', () => {
spyOn(controller.$state, 'go');
spyOn(controller.vnApp, 'showSuccess');
$httpBackend.expectPOST(`/ticket/api/Tickets/delete`, {id: 2}).respond();
$httpBackend.expectPOST(`/api/Tickets/2/setDeleted`).respond();
controller.deleteTicket('ACCEPT');
$httpBackend.flush();