diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 5a2188cb5..0609a6a6a 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2230,6 +2230,10 @@ INSERT INTO `vn`.`zoneEvent`(`zoneFk`, `type`, `weekDays`) (8, '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`) + VALUES + (9, 'range', DATE_ADD(util.VN_CURDATE(), INTERVAL -1 YEAR), DATE_ADD(util.VN_CURDATE(), INTERVAL +1 YEAR)); + INSERT INTO `vn`.`workerTimeControl`(`userFk`, `timed`, `manual`, `direction`) VALUES (1106, CONCAT(util.VN_CURDATE(), ' 07:00'), TRUE, 'in'), diff --git a/modules/zone/back/methods/zone/getEventsFiltered.js b/modules/zone/back/methods/zone/getEventsFiltered.js index 5e9cbae5a..316652fa3 100644 --- a/modules/zone/back/methods/zone/getEventsFiltered.js +++ b/modules/zone/back/methods/zone/getEventsFiltered.js @@ -44,12 +44,16 @@ module.exports = Self => { OR (type = 'range' AND ( (started BETWEEN ? AND ?) - OR (ended 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], myOptions); + const events = await Self.rawSql(query, + [zoneFk, started, ended, started, ended, started, ended, started, ended], myOptions); query = ` SELECT * diff --git a/modules/zone/back/methods/zone/specs/getEventsFiltered.spec.js b/modules/zone/back/methods/zone/specs/getEventsFiltered.spec.js index ffa416282..8160ee05e 100644 --- a/modules/zone/back/methods/zone/specs/getEventsFiltered.spec.js +++ b/modules/zone/back/methods/zone/specs/getEventsFiltered.spec.js @@ -6,8 +6,9 @@ describe('zone getEventsFiltered()', () => { try { const options = {transaction: tx}; + const today = new Date(); - let result = await models.Zone.getEventsFiltered(10, '2021-10-01', '2021-10-02', options); + const result = await models.Zone.getEventsFiltered(10, today, today, options); expect(result.events.length).toEqual(1); expect(result.exclusions.length).toEqual(0); @@ -18,4 +19,46 @@ describe('zone getEventsFiltered()', () => { throw e; } }); + + describe('event range type', () => { + it('should return events and exclusions for the zone 9 in a range of dates', async() => { + const tx = await models.Zone.beginTransaction({}); + + try { + const options = {transaction: tx}; + const today = new Date(); + + const result = await models.Zone.getEventsFiltered(9, today, today, options); + + expect(result.events.length).toEqual(1); + expect(result.exclusions.length).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + + it('should not return events and exclusions for the zone 9 in a range of dates', async() => { + const tx = await models.Zone.beginTransaction({}); + + try { + const options = {transaction: tx}; + const date = new Date(); + date.setFullYear(date.getFullYear() - 2); + const dateTomorrow = new Date(date.setDate(date.getDate() + 1)); + + const result = await models.Zone.getEventsFiltered(9, date, dateTomorrow, options); + + expect(result.events.length).toEqual(0); + expect(result.exclusions.length).toEqual(0); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); + }); });