7863-devToTest_2434 #2852
|
@ -0,0 +1,55 @@
|
||||||
|
|
||||||
|
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 result = await await Self.rawSql(`
|
||||||
|
INSERT INTO workerActivity (workerFk, workerActivityTypeFk, model)
|
||||||
|
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;`
|
||||||
|
, [userId, code, model, userId, code]);
|
||||||
|
console.log('*******');
|
||||||
|
console.log('*******' + userId);
|
||||||
|
console.log('*******' + code);
|
||||||
|
console.log('*******' + model);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
};
|
|
@ -0,0 +1,41 @@
|
||||||
|
const {models} = require('vn-loopback');
|
||||||
|
|
||||||
|
describe('workerActivity insert()', () => {
|
||||||
|
beforeAll(async() => {
|
||||||
|
ctx = {
|
||||||
|
req: {
|
||||||
|
accessToken: {},
|
||||||
|
headers: {origin: 'http://localhost'},
|
||||||
|
__: value => value
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
fit('should insert in workerActivity', async() => {
|
||||||
|
const tx = await models.WorkerActivity.beginTransaction({});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await models.WorkerActivityType.create(
|
||||||
|
{'code': 'STOP', 'description': 'STOP'}
|
||||||
|
);
|
||||||
|
const options = {transaction: tx};
|
||||||
|
ctx.req.accessToken.userId = 1106;
|
||||||
|
|
||||||
|
models.WorkerActivity.add(ctx, 'STOP', 'APP', options);
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,3 @@
|
||||||
|
module.exports = Self => {
|
||||||
|
require('../methods/workerActivity/add')(Self);
|
||||||
|
};
|
|
@ -22,18 +22,18 @@
|
||||||
},
|
},
|
||||||
"description": {
|
"description": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"relations": {
|
||||||
|
"workerFk": {
|
||||||
|
"type": "belongsTo",
|
||||||
|
"model": "Worker",
|
||||||
|
"foreignKey": "workerFk"
|
||||||
},
|
},
|
||||||
"relations": {
|
"workerActivityTypeFk": {
|
||||||
"workerFk": {
|
"type": "belongsTo",
|
||||||
"type": "belongsTo",
|
"model": "WorkerActivityType",
|
||||||
"model": "Worker",
|
"foreignKey": "workerActivityTypeFk"
|
||||||
"foreignKey": "workerFk"
|
|
||||||
},
|
|
||||||
"workerActivityTypeFk": {
|
|
||||||
"type": "belongsTo",
|
|
||||||
"model": "WorkerActivityType",
|
|
||||||
"foreignKey": "workerActivityTypeFk"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue