60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = Self => {
|
|
Self.remoteMethodCtx('updateEditableTicket', {
|
|
description: 'Change data of a ticket',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'Ticket id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'data',
|
|
description: 'Model instance data',
|
|
type: 'object',
|
|
required: true,
|
|
http: {source: 'body'}
|
|
}
|
|
],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
path: '/:id/updateEditableTicket',
|
|
verb: 'POST'
|
|
}
|
|
|
|
});
|
|
Self.updateEditableTicket = async(ctx, id, data, options) => {
|
|
const myOptions = {};
|
|
let tx;
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
const ticketIsEditable = await Self.app.models.Ticket.isEditable(ctx, id, myOptions);
|
|
if (!ticketIsEditable)
|
|
throw new UserError('This ticket can not be modified');
|
|
|
|
const ticket = await Self.app.models.Ticket.findById(id, null, myOptions);
|
|
await ticket.updateAttributes(data, myOptions);
|
|
|
|
if (tx) await tx.commit();
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|