module.exports = Self => { Self.remoteMethodCtx('updateWorkCenter', { description: 'Update the commission work center through user salix connected', accessType: 'WRITE', accepts: { arg: 'id', type: 'number', description: 'Route Id', http: {source: 'path'} }, returns: { type: 'object', root: true }, http: { path: `/:id/updateWorkCenter`, verb: 'POST' } }); Self.updateWorkCenter = async(ctx, id, options) => { const models = Self.app.models; const myOptions = {}; let tx; const userId = ctx.req.accessToken.userId; if (typeof options == 'object') Object.assign(myOptions, options); if (!myOptions.transaction) { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } try { const date = Date.vnNew(); const [result] = await Self.rawSql(` SELECT IFNULL(wl.workCenterFk, r.defaultWorkCenterFk) AS commissionWorkCenter FROM vn.routeConfig r LEFT JOIN vn.workerLabour wl ON wl.workerFk = ? AND ? BETWEEN wl.started AND IFNULL(wl.ended, ?); `, [userId, date, date], myOptions); const route = await models.Route.findById(id, null, myOptions); await route.updateAttribute('commissionWorkCenterFk', result.commissionWorkCenter, myOptions); if (tx) await tx.commit(); return route; } catch (e) { if (tx) await tx.rollback(); throw e; } }; };