2018-12-27 11:54:16 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2018-11-20 09:32:54 +00:00
|
|
|
|
2018-06-06 14:54:40 +00:00
|
|
|
module.exports = Self => {
|
2019-11-12 13:53:58 +00:00
|
|
|
Self.remoteMethodCtx('setDeleted', {
|
2019-09-03 07:57:53 +00:00
|
|
|
description: 'Sets true the isDeleted value of a ticket',
|
2019-05-22 10:35:24 +00:00
|
|
|
accessType: 'WRITE',
|
2018-06-06 14:54:40 +00:00
|
|
|
accepts: [{
|
2019-09-03 11:27:02 +00:00
|
|
|
arg: 'id',
|
|
|
|
type: 'Number',
|
2018-06-06 14:54:40 +00:00
|
|
|
required: true,
|
2019-09-03 11:27:02 +00:00
|
|
|
description: 'The ticket id',
|
|
|
|
http: {source: 'path'}
|
2018-06-06 14:54:40 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2019-09-03 11:27:02 +00:00
|
|
|
path: `/:id/setDeleted`,
|
2018-06-06 14:54:40 +00:00
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-12 13:53:58 +00:00
|
|
|
Self.setDeleted = async(ctx, id) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const isEditable = await Self.isEditable(ctx, id);
|
|
|
|
const $t = ctx.req.__; // $translate
|
2018-11-20 09:32:54 +00:00
|
|
|
|
2019-11-12 13:53:58 +00:00
|
|
|
if (!isEditable)
|
2019-11-14 13:19:39 +00:00
|
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
2019-11-12 13:53:58 +00:00
|
|
|
|
|
|
|
// Check if has sales with shelving
|
|
|
|
const sales = await models.Sale.find({
|
|
|
|
include: {relation: 'itemShelving'},
|
|
|
|
where: {ticketFk: id}
|
|
|
|
});
|
|
|
|
const hasItemShelvingSales = sales.some(sale => {
|
|
|
|
return sale.itemShelving();
|
|
|
|
});
|
|
|
|
if (hasItemShelvingSales)
|
2019-11-13 06:04:47 +00:00
|
|
|
throw new UserError(`You cannot delete a ticket that part of it is being prepared`);
|
2019-11-12 13:53:58 +00:00
|
|
|
|
|
|
|
// Check for existing claim
|
2019-11-12 14:07:37 +00:00
|
|
|
const claimOfATicket = await models.Claim.findOne({where: {ticketFk: id}});
|
2019-11-12 13:53:58 +00:00
|
|
|
if (claimOfATicket)
|
|
|
|
throw new UserError('You must delete the claim id %d first', 'DELETE_CLAIM_FIRST', claimOfATicket.id);
|
|
|
|
|
|
|
|
// Check for existing purchase requests
|
|
|
|
const hasPurchaseRequests = await models.TicketRequest.count({
|
|
|
|
ticketFk: id,
|
|
|
|
isOk: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hasPurchaseRequests)
|
|
|
|
throw new UserError('You must delete all the buy requests first');
|
|
|
|
|
|
|
|
// Remove ticket greuges
|
|
|
|
const ticketGreuges = await models.Greuge.find({where: {ticketFk: id}});
|
|
|
|
const ownGreuges = ticketGreuges.every(greuge => {
|
|
|
|
return greuge.ticketFk = id;
|
|
|
|
});
|
|
|
|
if (ownGreuges) {
|
|
|
|
for (const greuge of ticketGreuges) {
|
|
|
|
const instance = await models.Greuge.findById(greuge.id);
|
|
|
|
|
|
|
|
await instance.destroy();
|
|
|
|
}
|
2019-08-13 10:44:34 +00:00
|
|
|
}
|
2019-11-12 13:53:58 +00:00
|
|
|
|
|
|
|
const ticket = await models.Ticket.findById(id, {
|
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'salesPersonFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'salesPerson',
|
|
|
|
scope: {
|
|
|
|
fields: ['id', 'userFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'user'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send notification to salesPerson
|
|
|
|
const salesPerson = ticket.client().salesPerson();
|
|
|
|
if (salesPerson) {
|
|
|
|
const salesPersonUser = salesPerson.user().name;
|
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
const message = $t(`Has deleted the ticket id`, {
|
|
|
|
id: id,
|
|
|
|
url: `${origin}/#!/ticket/${id}/summary`
|
|
|
|
});
|
2020-01-20 10:59:15 +00:00
|
|
|
await models.Chat.send(ctx, `@${salesPersonUser}`, message);
|
2019-11-12 13:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ticket.updateAttribute('isDeleted', true);
|
2018-06-06 14:54:40 +00:00
|
|
|
};
|
|
|
|
};
|