Merge dev - test #1881

Merged
jsegarra merged 71 commits from dev into test 2023-12-07 08:28:25 +00:00
4 changed files with 130 additions and 130 deletions
Showing only changes of commit acfa0fd857 - Show all commits

View File

@ -2351,9 +2351,11 @@ INSERT INTO `vn`.`zoneEvent`(`zoneFk`, `type`, `weekDays`)
(8, 'indefinitely', 'mon,tue,wed,thu,fri,sat,sun'), (8, 'indefinitely', 'mon,tue,wed,thu,fri,sat,sun'),
(10, 'indefinitely', 'mon,tue,wed,thu,fri,sat,sun'); (10, 'indefinitely', 'mon,tue,wed,thu,fri,sat,sun');
INSERT INTO `vn`.`zoneEvent`(`zoneFk`, `type`, `started`, `ended`) INSERT INTO `vn`.`zoneEvent`(`zoneFk`, `type`, `started`, `ended`, `weekDays`)
VALUES VALUES
(9, 'range', DATE_ADD(util.VN_CURDATE(), INTERVAL -1 YEAR), DATE_ADD(util.VN_CURDATE(), INTERVAL +1 YEAR)); (9, 'range', DATE_ADD(util.VN_CURDATE(), INTERVAL -1 YEAR), DATE_ADD(util.VN_CURDATE(), INTERVAL +1 YEAR), 'mon'),
(9, 'range', util.VN_CURDATE(), NULL, 'tue'),
(9, 'range', NULL, util.VN_CURDATE(), 'wed');
INSERT INTO `vn`.`workerTimeControl`(`userFk`, `timed`, `manual`, `direction`, `isSendMail`) INSERT INTO `vn`.`workerTimeControl`(`userFk`, `timed`, `manual`, `direction`, `isSendMail`)
VALUES VALUES

View File

@ -3,99 +3,101 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter;
const mergeFilters = require('vn-loopback/util/filter').mergeFilters; const mergeFilters = require('vn-loopback/util/filter').mergeFilters;
module.exports = Self => { module.exports = Self => {
Self.remoteMethod('getExternalCmrs', { Self.remoteMethod('getExternalCmrs', {
description: 'Returns an array of external cmrs', description: 'Returns an array of external cmrs',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
{ {
arg: 'filter', arg: 'filter',
type: 'object', type: 'object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
}, },
{ {
arg: 'cmrFk', arg: 'cmrFk',
type: 'integer', type: 'integer',
description: 'Searchs the route by id', description: 'Searchs the route by id',
}, },
{ {
arg: 'ticketFk', arg: 'ticketFk',
type: 'integer', type: 'integer',
description: 'The worker id', description: 'The worker id',
}, },
{ {
arg: 'routeFk', arg: 'routeFk',
type: 'integer', type: 'integer',
description: 'The route id', description: 'The route id',
}, },
{ {
arg: 'country', arg: 'country',
type: 'string', type: 'string',
description: 'The agencyMode id', description: 'The agencyMode id',
}, },
{ {
arg: 'clientFk', arg: 'clientFk',
type: 'integer', type: 'integer',
description: 'The vehicle id', description: 'The vehicle id',
}, },
{ {
arg: 'hasCmrDms', arg: 'hasCmrDms',
type: 'boolean', type: 'boolean',
description: 'The vehicle id', description: 'The vehicle id',
}, },
{ {
arg: 'shipped', arg: 'shipped',
type: 'date', type: 'date',
description: 'The to date filter', description: 'The to date filter',
}, },
], ],
returns: { returns: {
type: ['object'], type: ['object'],
root: true root: true
}, },
http: { http: {
path: `/getExternalCmrs`, path: `/getExternalCmrs`,
verb: 'GET' verb: 'GET'
} }
}); });
Self.getExternalCmrs = async( Self.getExternalCmrs = async(
filter, filter,
cmrFk, cmrFk,
ticketFk, ticketFk,
routeFk, routeFk,
country, country,
clientFk, clientFk,
hasCmrDms, hasCmrDms,
shipped, shipped,
options options
) => { ) => {
const params = { const params = {
cmrFk, cmrFk,
ticketFk, ticketFk,
routeFk, routeFk,
country, country,
clientFk, clientFk,
hasCmrDms, hasCmrDms,
shipped, shipped,
}; };
const conn = Self.dataSource.connector; const conn = Self.dataSource.connector;
let where = buildFilter(params, (param, value) => {return {[param]: value}}); let where = buildFilter(params, (param, value) => {
filter = mergeFilters(filter, {where}); return {[param]: value};
});
filter = mergeFilters(filter, {where});
if (!filter.where) { if (!filter.where) {
const yesterday = new Date(); const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1); yesterday.setDate(yesterday.getDate() - 1);
filter.where = {'shipped': yesterday.toISOString().split('T')[0]} filter.where = {'shipped': yesterday.toISOString().split('T')[0]};
} }
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
let stmts = []; let stmts = [];
const stmt = new ParameterizedSQL(` const stmt = new ParameterizedSQL(`
SELECT * SELECT *
FROM ( FROM (
SELECT t.cmrFk, SELECT t.cmrFk,
@ -129,13 +131,13 @@ module.exports = Self => {
AND dm.code = 'DELIVERY' AND dm.code = 'DELIVERY'
AND t.cmrFk AND t.cmrFk
) sub ) sub
`); `);
stmt.merge(conn.makeSuffix(filter)); stmt.merge(conn.makeSuffix(filter));
const itemsIndex = stmts.push(stmt) - 1; const itemsIndex = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await conn.executeStmt(sql); const result = await conn.executeStmt(sql);
return itemsIndex === 0 ? result : result[itemsIndex]; return itemsIndex === 0 ? result : result[itemsIndex];
}; };
}; };

View File

@ -35,44 +35,39 @@ module.exports = Self => {
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
query = ` ended = simpleDate(ended);
SELECT * started = simpleDate(started);
FROM vn.zoneEvent
WHERE zoneFk = ?
AND ((type = 'indefinitely')
OR (type = 'day' AND dated BETWEEN ? AND ?)
OR (type = 'range'
AND (
(started BETWEEN ? AND ?)
OR
(ended BETWEEN ? AND ?)
OR
(started <= ? AND ended >= ?)
)
)
)
ORDER BY type='indefinitely' DESC, type='range' DESC, type='day' DESC;`;
const events = await Self.rawSql(query,
[zoneFk, started, ended, started, ended, started, ended, started, ended], myOptions);
query = ` query = `
SELECT e.* SELECT *
FROM vn.zoneExclusion e FROM vn.zoneEvent
LEFT JOIN vn.zoneExclusionGeo eg ON eg.zoneExclusionFk = e.id WHERE zoneFk = ?
WHERE e.zoneFk = ? AND (IFNULL(started, ?) <= ? AND IFNULL(ended,?) >= ?)
AND e.dated BETWEEN ? AND ? ORDER BY type='indefinitely' DESC, type='range' DESC, type='day' DESC;`;
AND eg.zoneExclusionFk IS NULL;`; const events = await Self.rawSql(query,
[zoneFk, started, ended, ended, started], myOptions);
query = `
SELECT e.*
FROM vn.zoneExclusion e
LEFT JOIN vn.zoneExclusionGeo eg ON eg.zoneExclusionFk = e.id
WHERE e.zoneFk = ?
AND e.dated BETWEEN ? AND ?
AND eg.zoneExclusionFk IS NULL;`;
const exclusions = await Self.rawSql(query, [zoneFk, started, ended], myOptions); const exclusions = await Self.rawSql(query, [zoneFk, started, ended], myOptions);
query = ` query = `
SELECT eg.*, e.zoneFk, e.dated, e.created, e.userFk SELECT eg.*, e.zoneFk, e.dated, e.created, e.userFk
FROM vn.zoneExclusion e FROM vn.zoneExclusion e
LEFT JOIN vn.zoneExclusionGeo eg ON eg.zoneExclusionFk = e.id LEFT JOIN vn.zoneExclusionGeo eg ON eg.zoneExclusionFk = e.id
WHERE e.zoneFk = ? WHERE e.zoneFk = ?
AND e.dated BETWEEN ? AND ? AND e.dated BETWEEN ? AND ?
AND eg.zoneExclusionFk IS NOT NULL;`; AND eg.zoneExclusionFk IS NOT NULL;`;
const geoExclusions = await Self.rawSql(query, [zoneFk, started, ended], myOptions); const geoExclusions = await Self.rawSql(query, [zoneFk, started, ended], myOptions);
return {events, exclusions, geoExclusions}; return {events, exclusions, geoExclusions};
}; };
function simpleDate(date) {
return date.toISOString().split('T')[0];
}
}; };

View File

@ -30,7 +30,7 @@ describe('zone getEventsFiltered()', () => {
const result = await models.Zone.getEventsFiltered(9, today, today, options); const result = await models.Zone.getEventsFiltered(9, today, today, options);
expect(result.events.length).toEqual(1); expect(result.events.length).toEqual(3);
expect(result.exclusions.length).toEqual(0); expect(result.exclusions.length).toEqual(0);
await tx.rollback(); await tx.rollback();
@ -47,11 +47,12 @@ describe('zone getEventsFiltered()', () => {
const options = {transaction: tx}; const options = {transaction: tx};
const date = Date.vnNew(); const date = Date.vnNew();
date.setFullYear(date.getFullYear() - 2); date.setFullYear(date.getFullYear() - 2);
const dateTomorrow = new Date(date.setDate(date.getDate() + 1)); const dateTomorrow = new Date(date);
dateTomorrow.setDate(dateTomorrow.getDate() + 1);
const result = await models.Zone.getEventsFiltered(9, date, dateTomorrow, options); const result = await models.Zone.getEventsFiltered(9, date, dateTomorrow, options);
expect(result.events.length).toEqual(0); expect(result.events.length).toEqual(1);
expect(result.exclusions.length).toEqual(0); expect(result.exclusions.length).toEqual(0);
await tx.rollback(); await tx.rollback();