const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('assignCollection', { description: 'Assign a collection', accessType: 'WRITE', http: { path: `/assignCollection`, verb: 'POST' }, returns: { type: ['object'], root: true }, }); Self.assignCollection = async(ctx, options) => { const userId = ctx.req.accessToken.userId; const myOptions = {userId}; const $t = ctx.req.__; if (typeof options == 'object') Object.assign(myOptions, options); await Self.rawSql('CALL vn.collection_assign(?, @vCollectionFk)', [userId], myOptions); const [assignedCollection] = await Self.rawSql('SELECT @vCollectionFk'); const {'@vCollectionFk': collectionFk} = assignedCollection; if (!collectionFk) throw new UserError($t('There are not picking tickets')); await Self.rawSql('CALL vn.collection_printSticker(?, NULL)', [collectionFk], myOptions); return collectionFk; }; };