module.exports = Self => { Self.remoteMethodCtx('add', { description: 'Add activity if the activity is different or is the same but have exceed time for break', accessType: 'WRITE', accepts: [ { arg: 'code', type: 'string', description: 'Code for activity' }, { arg: 'model', type: 'string', description: 'Origin model from insert' }, ], http: { path: `/add`, verb: 'POST' } }); Self.add = async(ctx, code, model, options) => { const userId = ctx.req.accessToken.userId; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); return await Self.rawSql(` INSERT INTO workerActivity (workerFk, workerActivityTypeFk, model) SELECT ?, ?, ? FROM workerTimeControlConfig wtcc LEFT JOIN ( SELECT wa.workerFk, wa.created, wat.code FROM workerActivity wa LEFT JOIN workerActivityType wat ON wat.code = wa.workerActivityTypeFk WHERE wa.workerFk = ? ORDER BY wa.created DESC LIMIT 1 ) sub ON TRUE WHERE sub.workerFk IS NULL OR sub.code <> ? OR TIMESTAMPDIFF(SECOND, sub.created, util.VN_NOW()) > wtcc.dayBreak;` , [userId, code, model, userId, code], myOptions); }; };