2023-06-21 12:07:36 +00:00
|
|
|
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
|
|
|
|
const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
|
|
|
|
|
2023-05-31 08:15:16 +00:00
|
|
|
module.exports = Self => {
|
2023-06-21 12:07:36 +00:00
|
|
|
Self.remoteMethodCtx('search', {
|
2023-08-24 05:40:37 +00:00
|
|
|
description: 'Returns an array of search results for a specified worker',
|
2023-05-31 08:15:16 +00:00
|
|
|
accepts: [{
|
|
|
|
arg: 'filter',
|
2023-06-21 12:07:36 +00:00
|
|
|
type: 'object',
|
2023-08-24 05:40:37 +00:00
|
|
|
description: 'Filter to define conditions and paginate the data.',
|
2023-05-31 08:15:16 +00:00
|
|
|
required: true
|
2023-06-21 12:07:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'departmentCodes',
|
|
|
|
type: ['string'],
|
|
|
|
description: 'Department codes to search workers',
|
2023-05-31 08:15:16 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: ['object'],
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/search`,
|
|
|
|
verb: 'GET'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-21 12:07:36 +00:00
|
|
|
Self.search = async(ctx, filter, departmentCodes) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const conn = Self.dataSource.connector;
|
2023-06-08 11:12:30 +00:00
|
|
|
|
2023-06-21 12:07:36 +00:00
|
|
|
if (departmentCodes) {
|
|
|
|
const departments = await models.Department.find({
|
2023-06-21 13:00:46 +00:00
|
|
|
fields: ['id', 'sons'],
|
2023-06-21 12:07:36 +00:00
|
|
|
where: {code: {inq: departmentCodes}}
|
|
|
|
});
|
2023-06-08 11:12:30 +00:00
|
|
|
|
2023-06-21 13:00:46 +00:00
|
|
|
const allLeaves = await getAllLeaves(ctx, departments);
|
2023-07-13 10:59:02 +00:00
|
|
|
const where = {'departmentFk': {inq: allLeaves}};
|
2023-06-21 12:07:36 +00:00
|
|
|
filter = mergeFilters(filter, {where});
|
|
|
|
}
|
|
|
|
|
2023-07-12 11:12:51 +00:00
|
|
|
const stmt = new ParameterizedSQL(`
|
2024-01-17 09:01:38 +00:00
|
|
|
SELECT *
|
2024-01-17 13:07:04 +00:00
|
|
|
FROM(
|
2024-01-19 09:11:33 +00:00
|
|
|
SELECT w.id, w.code, u.name, u.nickname, u.active, wd.departmentFk
|
2024-01-17 13:07:04 +00:00
|
|
|
FROM worker w
|
|
|
|
JOIN account.user u ON u.id = w.id
|
2024-01-19 09:11:33 +00:00
|
|
|
LEFT JOIN workerDepartment wd ON wd.workerFk = w.id
|
2024-01-17 13:39:11 +00:00
|
|
|
) w`);
|
|
|
|
|
2024-01-19 09:11:33 +00:00
|
|
|
stmt.merge(conn.makeSuffix(filter));
|
2023-06-21 12:07:36 +00:00
|
|
|
|
|
|
|
return conn.executeStmt(stmt);
|
2023-05-31 08:15:16 +00:00
|
|
|
};
|
2023-06-21 13:00:46 +00:00
|
|
|
|
|
|
|
async function getAllLeaves(ctx, departments) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const leaves = [];
|
|
|
|
for (const department of departments) {
|
|
|
|
if (department.sons > 0) {
|
|
|
|
const subLeaves = await models.Department.getLeaves(ctx, department.id, null);
|
|
|
|
const grandLeaves = await getAllLeaves(ctx, subLeaves);
|
|
|
|
leaves.push(...grandLeaves);
|
|
|
|
}
|
|
|
|
leaves.push(department.id);
|
|
|
|
}
|
|
|
|
return leaves;
|
|
|
|
}
|
2023-05-31 08:15:16 +00:00
|
|
|
};
|