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

57 lines
1.8 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('isEditable', {
description: 'Check if a ticket is editable',
accessType: 'READ',
accepts: [{
arg: 'id',
type: 'number',
required: true,
description: 'the ticket id',
http: {source: 'path'}
}],
returns: {
type: 'boolean',
root: true
},
http: {
path: `/:id/isEditable`,
verb: 'get'
}
});
Self.isEditable = async(ctx, id) => {
const userId = ctx.req.accessToken.userId;
let state = await Self.app.models.TicketState.findOne({
where: {ticketFk: id}
});
const isSalesAssistant = await Self.app.models.Account.hasRole(userId, 'salesAssistant');
const isProductionBoss = await Self.app.models.Account.hasRole(userId, 'productionBoss');
const isValidRole = isSalesAssistant || isProductionBoss;
let alertLevel = state ? state.alertLevel : null;
let ticket = await Self.app.models.Ticket.findById(id, {
fields: ['isDeleted', 'clientFk', 'refFk'],
include: [{
relation: 'client',
scope: {
include: {
relation: 'type'
}
}
}]
});
const isDeleted = ticket && ticket.isDeleted;
const isOnDelivery = (alertLevel && alertLevel > 0);
const isNormalClient = ticket && ticket.client().type().code == 'normal';
const isInvoiced = ticket && ticket.refFk;
if (!ticket || isInvoiced || isDeleted || (isOnDelivery && isNormalClient && !isValidRole))
return false;
return true;
};
};