163 lines
4.6 KiB
JavaScript
163 lines
4.6 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('getSummary', {
|
|
description: 'Return the claim summary',
|
|
accessType: 'READ',
|
|
accepts: [{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'The claim id',
|
|
http: {source: 'path'}
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: `/:id/getSummary`,
|
|
verb: 'GET'
|
|
}
|
|
});
|
|
|
|
Self.getSummary = async(ctx, id, options) => {
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
const promises = [];
|
|
const summary = {};
|
|
|
|
// Claim
|
|
let filter = {
|
|
where: {id: id},
|
|
include: [
|
|
{
|
|
relation: 'worker',
|
|
scope: {
|
|
fields: ['id'],
|
|
include: {
|
|
relation: 'user',
|
|
scope: {
|
|
fields: ['nickname']
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
relation: 'claimState',
|
|
scope: {
|
|
fields: ['id', 'code', 'description']
|
|
}
|
|
},
|
|
{
|
|
relation: 'client',
|
|
scope: {
|
|
fields: ['salesPersonFk', 'name'],
|
|
include: {
|
|
relation: 'salesPersonUser',
|
|
scope: {
|
|
fields: ['id', 'name']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
};
|
|
|
|
const models = Self.app.models;
|
|
promises.push(models.Claim.find(filter, myOptions));
|
|
|
|
// Claim detail
|
|
filter = {
|
|
where: {claimFk: id},
|
|
include: [
|
|
{
|
|
relation: 'sale',
|
|
scope: {
|
|
fields: ['concept', 'ticketFk', 'price', 'quantity', 'discount', 'itemFk'],
|
|
include: {
|
|
relation: 'ticket'
|
|
}
|
|
}
|
|
}
|
|
]
|
|
};
|
|
promises.push(models.ClaimBeginning.find(filter, myOptions));
|
|
|
|
// Claim observations
|
|
filter = {
|
|
where: {claimFk: id},
|
|
include: [
|
|
{
|
|
relation: 'worker',
|
|
scope: {
|
|
fields: ['id', 'firstName', 'lastName']
|
|
}
|
|
}
|
|
]
|
|
};
|
|
promises.push(models.ClaimObservation.find(filter, myOptions));
|
|
|
|
// Claim developments
|
|
filter = {
|
|
where: {claimFk: id},
|
|
include: [
|
|
{
|
|
relation: 'claimResponsible'
|
|
},
|
|
{
|
|
relation: 'worker',
|
|
scope: {
|
|
fields: ['id'],
|
|
include: {
|
|
relation: 'user',
|
|
scope: {
|
|
fields: ['nickname']
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
relation: 'claimReason'
|
|
},
|
|
{
|
|
relation: 'claimResult'
|
|
},
|
|
{
|
|
relation: 'claimRedelivery'
|
|
}
|
|
]
|
|
};
|
|
promises.push(models.ClaimDevelopment.find(filter, myOptions));
|
|
|
|
// Claim action
|
|
filter = {
|
|
where: {claimFk: id},
|
|
include: [
|
|
{relation: 'sale',
|
|
scope: {
|
|
fields: ['concept', 'price', 'quantity', 'discount', 'ticketFk', 'itemFk'],
|
|
include: [
|
|
{relation: 'ticket'}
|
|
]
|
|
}
|
|
},
|
|
{relation: 'claimBeggining'}
|
|
]
|
|
};
|
|
promises.push(models.ClaimEnd.find(filter, myOptions));
|
|
|
|
const res = await Promise.all(promises);
|
|
|
|
summary.isEditable = await models.ClaimState.isEditable(ctx, res[0][0].claimStateFk, myOptions);
|
|
[summary.claim] = res[0];
|
|
summary.salesClaimed = res[1];
|
|
summary.observations = res[2];
|
|
summary.developments = res[3];
|
|
summary.actions = res[4];
|
|
|
|
return summary;
|
|
};
|
|
};
|