178 lines
6.5 KiB
JavaScript
178 lines
6.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 = {userId: ctx.req.accessToken.userId};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const ticketToDelete = await models.Ticket.findById(id, {fields: ['isDeleted']}, myOptions);
|
|
if (ticketToDelete.isDeleted) return false;
|
|
|
|
await Self.isEditableOrThrow(ctx, id, myOptions);
|
|
|
|
// Check if ticket has refunds
|
|
const ticketRefunds = await models.TicketRefund.find({
|
|
include: [
|
|
{relation: 'refundTicket'}
|
|
],
|
|
where: {originalTicketFk: id}
|
|
}, myOptions);
|
|
|
|
const hasRefund = !!ticketRefunds?.length;
|
|
|
|
const allDeleted = ticketRefunds.every(refund => refund.refundTicket().isDeleted);
|
|
|
|
if (!hasRefund) await models.TicketRefund.destroyAll({refundTicketFk: id}, myOptions);
|
|
if (hasRefund && !allDeleted) {
|
|
const notDeleted = [];
|
|
for (const refund of ticketRefunds)
|
|
if (!refund.refundTicket().isDeleted) notDeleted.push(refund.refundTicket().id);
|
|
|
|
throw new UserError('Tickets with associated refunds', 'TICKET_REFUND',
|
|
notDeleted.join(', '));
|
|
}
|
|
|
|
// Check if has sales with shelving
|
|
const canDeleteTicketWithPartPrepared =
|
|
await models.ACL.checkAccessAcl(ctx, 'Ticket', 'deleteTicketWithPartPrepared', 'WRITE');
|
|
const sales = await models.Sale.find({
|
|
include: {relation: 'itemShelvingSale'},
|
|
where: {ticketFk: id}
|
|
}, myOptions);
|
|
const hasItemShelvingSales = sales.some(sale => {
|
|
return sale.itemShelvingSale();
|
|
});
|
|
|
|
if (hasItemShelvingSales && !canDeleteTicketWithPartPrepared)
|
|
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,
|
|
or: [
|
|
{isOk: true},
|
|
{isOk: null}
|
|
]
|
|
}, myOptions);
|
|
|
|
if (hasPurchaseRequests)
|
|
throw new UserError('You must delete all the buy requests first');
|
|
|
|
// removes item shelvings
|
|
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 && sales.length) {
|
|
const url = await Self.app.models.Url.getUrl();
|
|
const message = $t(`I have deleted the ticket id`, {
|
|
id: id,
|
|
url: `${url}ticket/${id}/summary`
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, salesPersonUser.id, 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;
|
|
}
|
|
};
|
|
};
|