154 lines
5.5 KiB
JavaScript
154 lines
5.5 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('setDeleted', {
|
|
description: 'Sets true the isDeleted value of a ticket',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The ticket id',
|
|
http: {source: 'path'}
|
|
}],
|
|
returns: {
|
|
type: 'string',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/setDeleted`,
|
|
verb: 'post'
|
|
}
|
|
});
|
|
|
|
Self.setDeleted = async(ctx, id, options) => {
|
|
const models = Self.app.models;
|
|
const $t = ctx.req.__; // $translate
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const userId = ctx.req.accessToken.userId;
|
|
const isEditable = await Self.isEditable(ctx, id, myOptions);
|
|
|
|
if (!isEditable)
|
|
throw new UserError(`The sales of this ticket can't be modified`);
|
|
|
|
// Check if has sales with shelving
|
|
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant', myOptions);
|
|
const sales = await models.Sale.find({
|
|
include: {relation: 'itemShelvingSale'},
|
|
where: {ticketFk: id}
|
|
}, myOptions);
|
|
const hasItemShelvingSales = sales.some(sale => {
|
|
return sale.itemShelvingSale();
|
|
});
|
|
|
|
if (hasItemShelvingSales && !isSalesAssistant)
|
|
throw new UserError(`You cannot delete a ticket that part of it is being prepared`);
|
|
|
|
// Check for existing claim
|
|
const claimOfATicket = await models.Claim.findOne({where: {ticketFk: id}}, myOptions);
|
|
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
|
|
}, myOptions);
|
|
|
|
if (hasPurchaseRequests)
|
|
throw new UserError('You must delete all the buy requests first');
|
|
|
|
// removes item shelvings
|
|
if (hasItemShelvingSales && isSalesAssistant) {
|
|
const promises = [];
|
|
for (let sale of sales) {
|
|
if (sale.itemShelvingSale()) {
|
|
const itemShelvingSale = sale.itemShelvingSale();
|
|
const destroyedShelving = models.ItemShelvingSale.destroyById(itemShelvingSale.id, myOptions);
|
|
promises.push(destroyedShelving);
|
|
}
|
|
}
|
|
await Promise.all(promises);
|
|
}
|
|
|
|
// Remove ticket greuges
|
|
const ticketGreuges = await models.Greuge.find({where: {ticketFk: id}}, myOptions);
|
|
const ownGreuges = ticketGreuges.every(greuge => {
|
|
return greuge.ticketFk == id;
|
|
});
|
|
if (ownGreuges) {
|
|
for (const greuge of ticketGreuges) {
|
|
const instance = await models.Greuge.findById(greuge.id, null, myOptions);
|
|
|
|
await instance.destroy(myOptions);
|
|
}
|
|
}
|
|
|
|
const ticket = await models.Ticket.findById(id, {
|
|
include: [{
|
|
relation: 'client',
|
|
scope: {
|
|
fields: ['id', 'salesPersonFk'],
|
|
include: {
|
|
relation: 'salesPersonUser',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
}
|
|
}
|
|
}, {
|
|
relation: 'ship'
|
|
}, {
|
|
relation: 'stowaway'
|
|
}]
|
|
}, myOptions);
|
|
|
|
// Change state to "fixing" if contains an stowaway and remove the link between them
|
|
let otherTicketId;
|
|
if (ticket.stowaway())
|
|
otherTicketId = ticket.stowaway().shipFk;
|
|
else if (ticket.ship())
|
|
otherTicketId = ticket.ship().id;
|
|
|
|
if (otherTicketId) {
|
|
await models.Ticket.deleteStowaway(ctx, otherTicketId, myOptions);
|
|
await models.TicketTracking.changeState(ctx, {
|
|
ticketFk: otherTicketId,
|
|
code: 'FIXING'
|
|
}, myOptions);
|
|
}
|
|
|
|
// Send notification to salesPerson
|
|
const salesPersonUser = ticket.client().salesPersonUser();
|
|
if (salesPersonUser) {
|
|
const origin = ctx.req.headers.origin;
|
|
const message = $t(`I have deleted the ticket id`, {
|
|
id: id,
|
|
url: `${origin}/#!/ticket/${id}/summary`
|
|
});
|
|
await models.Chat.send(ctx, `@${salesPersonUser.name}`, message);
|
|
}
|
|
|
|
const updatedTicket = await ticket.updateAttribute('isDeleted', true, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedTicket;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|