2019-08-12 11:26:44 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('isEditable', {
|
|
|
|
description: 'Check if the ticket state is editable',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'stateId',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
http: {source: 'path'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'boolean',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/:stateId/isEditable`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
Self.isEditable = async(ctx, stateId, options) => {
|
2019-08-12 11:26:44 +00:00
|
|
|
const accessToken = ctx.req.accessToken;
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = accessToken.userId;
|
2021-09-21 16:48:59 +00:00
|
|
|
const myOptions = {};
|
2019-08-12 11:26:44 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2019-08-12 11:26:44 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
const isProduction = await models.Account.hasRole(userId, 'production', myOptions);
|
|
|
|
const isSalesPerson = await models.Account.hasRole(userId, 'salesPerson', myOptions);
|
|
|
|
const isAdministrative = await models.Account.hasRole(userId, 'administrative', myOptions);
|
|
|
|
const state = await models.State.findById(stateId, null, myOptions);
|
2019-08-12 11:26:44 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
const salesPersonAllowed = (isSalesPerson && (state.code == 'PICKER_DESIGNED' || state.code == 'PRINTED'));
|
|
|
|
|
|
|
|
const isAllowed = isProduction || isAdministrative || salesPersonAllowed || state.alertLevel == 0;
|
2019-08-12 11:26:44 +00:00
|
|
|
return isAllowed;
|
|
|
|
};
|
|
|
|
};
|