162 lines
5.9 KiB
JavaScript
162 lines
5.9 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 ticket has refunds
|
|
const ticketRefunds = await models.TicketRefund.find({
|
|
where: {originalTicketFk: id},
|
|
fields: ['id']}
|
|
, myOptions);
|
|
if (ticketRefunds.length > 0)
|
|
throw new UserError($t('Tickets with associated refunds', {id: ticketRefunds[0].id}));
|
|
|
|
// 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']
|
|
}
|
|
}
|
|
}
|
|
}]
|
|
}, 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);
|
|
|
|
const [ticketCollection] = await models.TicketCollection.find({
|
|
fields: ['id'],
|
|
where: {
|
|
ticketFk: ticket.id
|
|
}
|
|
});
|
|
|
|
if (ticketCollection)
|
|
await models.TicketCollection.destroyById(ticketCollection.id, myOptions);
|
|
|
|
await Self.rawSql(`
|
|
DELETE sc
|
|
FROM vn.saleGroup sg
|
|
JOIN vn.sectorCollectionSaleGroup scsg ON scsg.saleGroupFk = sg.id
|
|
JOIN vn.sectorCollection sc ON sc.id = scsg.sectorCollectionFk
|
|
JOIN vn.saleGroupDetail sgd ON sgd.saleGroupFk = sg.id
|
|
JOIN vn.sale s ON s.id = sgd.saleFk
|
|
WHERE s.ticketFk = ?;`, [ticket.id], myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedTicket;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|