46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
module.exports = Self => {
|
|
Self.remoteMethodCtx('transferClient', {
|
|
description: 'Transfering ticket to another client',
|
|
accessType: 'WRITE',
|
|
accepts: [
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
required: true,
|
|
description: 'the ticket id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'clientFk',
|
|
type: 'number',
|
|
required: true,
|
|
},
|
|
],
|
|
http: {
|
|
path: `/:id/transferClient`,
|
|
verb: 'PATCH'
|
|
}
|
|
});
|
|
|
|
Self.transferClient = async(ctx, id, clientFk, options) => {
|
|
const models = Self.app.models;
|
|
const myOptions = {};
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
await Self.isEditableOrThrow(ctx, id, myOptions);
|
|
|
|
const ticket = await models.Ticket.findById(
|
|
id,
|
|
{fields: ['id', 'shipped', 'clientFk', 'addressFk']},
|
|
myOptions
|
|
);
|
|
const client = await models.Client.findById(clientFk, {fields: ['id', 'defaultAddressFk']}, myOptions);
|
|
|
|
await ticket.updateAttributes({
|
|
clientFk,
|
|
addressFk: client.defaultAddressFk,
|
|
});
|
|
};
|
|
};
|