salix/back/methods/collection/assign.js

56 lines
1.8 KiB
JavaScript

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);
let threadId;
try {
const result =
await Self.rawSql(`
CALL vn.collection_assign(?, @vCollectionFk);
SELECT @vCollectionFk collectionFk,
CONNECTION_ID() threadId
`,
[userId], myOptions);
threadId = result[2][0].threadId;
const collectionFk = result[2][0].collectionFk;
if (!collectionFk) throw new UserError('There are not picking tickets');
await Self.rawSql('CALL vn.collection_printSticker(?, NULL)', [collectionFk], myOptions);
return collectionFk;
} catch (e) {
// Error deadlock refs #7486
if (e.code === 'ER_LOCK_DEADLOCK') {
const [hasLock] = await Self.rawSql(`
SELECT COUNT(*) FROM INFORMATION_SCHEMA.METADATA_LOCK_INFO
WHERE LOCK_MODE = 'MDL_SHARED_NO_WRITE'
AND THREAD_ID = ?
`,
[threadId], myOptions);
if (hasLock) await Self.rawSql(`KILL ?`, [threadId], myOptions);
}
throw e;
}
};
};