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

141 lines
4.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethod('summary', {
description: 'Returns a ticket summary',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'ticket id',
http: {source: 'path'}
}],
returns: {
type: [this.modelName],
root: true
},
http: {
path: `/:id/summary`,
verb: 'GET'
}
});
Self.summary = async(ticketFk, options) => {
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const summaryObj = await getTicketData(Self, ticketFk, myOptions);
summaryObj.sales = await models.Ticket.getSales(ticketFk, myOptions);
summaryObj.packagings = await models.TicketPackaging.find({
where: {ticketFk: ticketFk},
include: [{relation: 'packaging',
scope: {
fields: ['itemFk', 'name'],
include: {
relation: 'item'
}
}
}]
}, myOptions);
summaryObj.requests = await getRequests(Self, ticketFk, myOptions);
summaryObj.services = await models.TicketService.find({
where: {ticketFk: ticketFk},
include: [{relation: 'taxClass'}]
}, myOptions);
return summaryObj;
};
async function getTicketData(Self, ticketFk, options) {
const filter = {
include: [
{relation: 'warehouse', scope: {fields: ['name']}},
{relation: 'agencyMode', scope: {fields: ['name']}},
{relation: 'zone', scope: {fields: ['name']}},
{relation: 'client',
scope: {
fields: ['salesPersonFk', 'name', 'phone', 'mobile'],
include: {
relation: 'salesPersonUser',
scope: {
fields: ['id', 'name']
}
}
}
}, {
relation: 'address',
scope: {
fields: ['street', 'city', 'provinceFk', 'phone', 'mobile', 'postalCode', 'isEqualizated'],
include: {
relation: 'province',
scope: {
fields: ['name']
}
}
}
}, {
relation: 'notes',
scope: {
fields: ['id', 'observationTypeFk', 'description'],
include: {
relation: 'observationType',
fields: ['description']
}
}
}, {
relation: 'ticketState',
scope: {
fields: ['stateFk'],
include: {
relation: 'state',
fields: ['name']
}
}
}, {
relation: 'invoiceOut',
scope: {
fields: ['id']
}
}
],
where: {id: ticketFk}
};
return Self.findOne(filter, options);
}
async function getRequests(Self, ticketFk, options) {
const filter = {
where: {
ticketFk: ticketFk
},
include: [
{
relation: 'requester',
scope: {
include: {
relation: 'user'
}
}
}, {
relation: 'atender',
scope: {
include: {
relation: 'user'
}
}
}, {
relation: 'sale'
}
]
};
return Self.app.models.TicketRequest.find(filter, options);
}
};