From 81097287fa1381679a17bbef76563f49cfccad59 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Mon, 14 Oct 2019 11:44:54 +0200 Subject: [PATCH] #1755 create claim only for non removed tickets --- .../back/methods/claim/createFromSales.js | 14 ++++++-- modules/ticket/front/sale/index.js | 35 +++++++++++-------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/modules/claim/back/methods/claim/createFromSales.js b/modules/claim/back/methods/claim/createFromSales.js index 1d5597da7..bf62a526e 100644 --- a/modules/claim/back/methods/claim/createFromSales.js +++ b/modules/claim/back/methods/claim/createFromSales.js @@ -1,3 +1,5 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { Self.remoteMethodCtx('createFromSales', { description: 'Create a claim', @@ -25,13 +27,19 @@ module.exports = Self => { }); Self.createFromSales = async(ctx, params) => { - let model = Self.app.models; + let models = Self.app.models; let userId = ctx.req.accessToken.userId; let tx = await Self.beginTransaction({}); try { let options = {transaction: tx}; - const worker = await Self.app.models.Worker.findOne({ + + const ticketId = params.claim.ticketFk; + const ticket = await models.Ticket.findById(ticketId, null, options); + if (ticket.isDeleted) + throw new UserError(`You can't create a claim for a removed ticket`); + + const worker = await models.Worker.findOne({ where: {userFk: userId} }, options); @@ -40,7 +48,7 @@ module.exports = Self => { let promises = []; for (const sale of params.sales) { - const newClaimBeginning = model.ClaimBeginning.create({ + const newClaimBeginning = models.ClaimBeginning.create({ saleFk: sale.id, claimFk: newClaim.id, quantity: sale.quantity diff --git a/modules/ticket/front/sale/index.js b/modules/ticket/front/sale/index.js index 31e2ee155..0f2d8bce4 100644 --- a/modules/ticket/front/sale/index.js +++ b/modules/ticket/front/sale/index.js @@ -11,11 +11,27 @@ class Controller { this.$http = $http; this.edit = {}; this.moreOptions = [ - {callback: this.markAsReserved, name: 'Mark as reserved'}, - {callback: this.unmarkAsReserved, name: 'Unmark as reserved'}, - {callback: this.showEditDialog, name: 'Update discount', show: () => !this.hasInvoice()}, - {callback: this.createClaim, name: 'Add claim'}, - {callback: this.showSMSDialog, name: 'Send SMS'} + {name: 'Send SMS', callback: this.showSMSDialog}, + { + name: 'Mark as reserved', + callback: this.markAsReserved, + show: () => this.isEditable + }, + { + name: 'Unmark as reserved', + callback: this.unmarkAsReserved, + show: () => this.isEditable + }, + { + name: 'Update discount', + callback: this.showEditDialog, + show: () => this.isEditable + }, + { + name: 'Add claim', + callback: this.createClaim, + show: () => this.isEditable + }, ]; this._sales = []; this.imagesPath = '//verdnatura.es/vn-image-data/catalog'; @@ -431,15 +447,6 @@ class Controller { this.$scope.sms.open(); } - /** - * Returns if the current ticket - * is already invoiced - * @return {Boolean} - True if invoiced - */ - hasInvoice() { - return this.ticket.refFk !== null; - } - /** * Inserts a new instance */