const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('assign', { description: 'Assign a collection', accessType: 'WRITE', http: { path: `/assign`, verb: 'POST' }, returns: { type: ['object'], root: true }, }); Self.assign = async(ctx, options) => { const userId = ctx.req.accessToken.userId; const myOptions = {userId}; if (typeof options == 'object') Object.assign(myOptions, options); const randStr = Math.random().toString(36).substring(3); const result = await Self.rawSql(` CALL vn.collection_assign(?, @vCollectionFk); SELECT @vCollectionFk ? `, [userId, randStr], myOptions); // Por si entra en SELECT FOR UPDATE una o varias veces const collectionFk = result.find(item => item[0]?.[randStr] !== undefined)?.[0]?.[randStr]; if (!collectionFk) throw new UserError('There are not picking tickets'); await Self.rawSql('CALL vn.collection_printSticker(?, NULL)', [collectionFk], myOptions); return collectionFk; }; };