salix/modules/ticket/back/methods/ticket/isEmpty.js

54 lines
1.4 KiB
JavaScript

module.exports = function(Self) {
Self.remoteMethod('isEmpty', {
description: 'Checks if the ticket has no packages, componenets and purchase requests',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'Ticket id',
http: {source: 'path'}
}
],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/isEmpty`,
verb: 'GET'
}
});
Self.isEmpty = async(id, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const hasSales = await models.Sale.count({
ticketFk: id
}, myOptions);
const hasPackages = await models.TicketPackaging.count({
ticketFk: id
}, myOptions);
const hasServices = await models.TicketService.count({
ticketFk: id
}, myOptions);
const hasPurchaseRequests = await models.TicketRequest.count({
ticketFk: id
}, myOptions);
const isEmpty = !hasSales && !hasPackages &&
!hasServices && !hasPurchaseRequests;
return isEmpty;
};
};