const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('add', { description: 'Add a new operator', accessType: 'WRITE', http: { path: `/add`, verb: 'POST' } }); Self.add = async(ctx, options) => { const userId = ctx.req.accessToken.userId; const myOptions = {}; const $t = ctx.req.__; let tx; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { await Self.create({ workerFk: userId }); if (tx) await tx.commit(); } catch (e) { if (tx) await tx.rollback(); throw new UserError($t('This worker does not exist')); } }; };