refs #6078 - workerAct #2814

Merged
sergiodt merged 7 commits from 6078_workerAct into dev 2024-08-08 08:26:13 +00:00
4 changed files with 110 additions and 11 deletions
Showing only changes of commit eaa366ff39 - Show all commits

View File

@ -0,0 +1,55 @@
sergiodt marked this conversation as resolved
Review

Aquí se te ha colado un salto de linea

Aquí se te ha colado un salto de linea
Review

Llevat

Llevat
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;
sergiodt marked this conversation as resolved Outdated
Outdated
Review
    const myOptions = {};

    if (typeof options == 'object')
        Object.assign(myOptions, options);
const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options);
const result = await await Self.rawSql(`
sergiodt marked this conversation as resolved Outdated
Outdated
Review

return await Self.rawSql(`

return await Self.rawSql(`
INSERT INTO workerActivity (workerFk, workerActivityTypeFk, model)
SELECT ?,
?,
?
FROM workerTimeControlParams wtcp
LEFT JOIN (
sergiodt marked this conversation as resolved Outdated

Aquí como todas las columnas son parámetros, no metería un salto de linea por cada columna.

A parte a partir de aquí, tiene que ir una tabulación más.

INSERT INTO...
       SELECT ?, ?, ?
              FROM...          
Aquí como todas las columnas son parámetros, no metería un salto de linea por cada columna. A parte a partir de aquí, tiene que ir una tabulación más. ``` INSERT INTO... SELECT ?, ?, ? FROM... ```

Tabulat

Tabulat
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;`
, [userId, code, model, userId, code]);
sergiodt marked this conversation as resolved Outdated
Outdated
Review

, myOptions);

, myOptions);
console.log('*******');
console.log('*******' + userId);
console.log('*******' + code);
console.log('*******' + model);
return result;
};
};

View File

@ -0,0 +1,41 @@
const {models} = require('vn-loopback');
describe('workerActivity insert()', () => {
beforeAll(async() => {
sergiodt marked this conversation as resolved Outdated
Outdated
Review

const ctx = beforeAll.getCtx();

const ctx = beforeAll.getCtx();
ctx = {
req: {
accessToken: {},
headers: {origin: 'http://localhost'},
__: value => value
}
};
});
fit('should insert in workerActivity', async() => {
sergiodt marked this conversation as resolved Outdated

tens foco en el test

tens foco en el test
const tx = await models.WorkerActivity.beginTransaction({});
sergiodt marked this conversation as resolved Outdated
Outdated
Review

result no es gasta

result no es gasta
try {
await models.WorkerActivityType.create(
{'code': 'STOP', 'description': 'STOP'}
);
const options = {transaction: tx};
sergiodt marked this conversation as resolved
Review

Esto arriba, y usar options

Esto arriba, y usar options
ctx.req.accessToken.userId = 1106;
models.WorkerActivity.add(ctx, 'STOP', 'APP', options);
sergiodt marked this conversation as resolved
Review

await

await
const wac = await models.WorkerActivity.find(
{'wokerFk': 1106}
);
console.log('----' + JSON.stringify(wac, null, 2));
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
const count = await models.WorkerActivity.count(
{'workerFK': 1106}
);
expect(count).toEqual(1);
});
});

View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/workerActivity/add')(Self);
};

View File

@ -22,18 +22,18 @@
},
"description": {
"type": "string"
}
},
"relations": {
"workerFk": {
"type": "belongsTo",
"model": "Worker",
"foreignKey": "workerFk"
},
"relations": {
"workerFk": {
"type": "belongsTo",
"model": "Worker",
"foreignKey": "workerFk"
},
"workerActivityTypeFk": {
"type": "belongsTo",
"model": "WorkerActivityType",
"foreignKey": "workerActivityTypeFk"
}
"workerActivityTypeFk": {
"type": "belongsTo",
"model": "WorkerActivityType",
"foreignKey": "workerActivityTypeFk"
}
}
}