salix/back/methods/workerActivity/add.js

51 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2024-07-30 10:36:02 +00:00
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;
2024-08-06 14:34:46 +00:00
const myOptions = {};
2024-07-30 10:36:02 +00:00
2024-08-06 14:34:46 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
return await Self.rawSql(`
2024-07-30 10:36:02 +00:00
INSERT INTO workerActivity (workerFk, workerActivityTypeFk, model)
2024-08-07 06:19:44 +00:00
SELECT ?, ?, ?
FROM workerTimeControlParams wtcp
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()) > wtcp.dayBreak;`
2024-08-06 14:34:46 +00:00
, [userId, code, model, userId, code], myOptions);
2024-07-30 10:36:02 +00:00
};
};