diff --git a/modules/zone/back/methods/zone/getZoneClosing.js b/modules/zone/back/methods/zone/getZoneClosing.js new file mode 100644 index 000000000..02286e6d5 --- /dev/null +++ b/modules/zone/back/methods/zone/getZoneClosing.js @@ -0,0 +1,68 @@ +module.exports = Self => { + Self.remoteMethod('getZoneClosing', { + description: 'Get events filtered for zone and date', + accepts: [ + { + arg: 'zonesId', + type: ['number'], + description: 'The zones id', + required: true + }, + { + arg: 'date', + type: 'date', + description: 'The date calendar', + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/getZoneClosing`, + verb: 'POST' + } + }); + + Self.getZoneClosing = async(zonesId, date, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + const params = []; + const paramsSql = [date, date, date]; + for (id of zonesId) { + params.push('?'); + paramsSql.push(id); + } + + const paramsString = params.join(); + query = ` + SELECT * + FROM ( + SELECT + DISTINCT z.id, + z.name, + am.name agencyModeName, + type, + IFNULL(ze.hour, z.hour) as hour, + IFNULL(ze.price, z.price) as price + FROM vn.zone z + JOIN agencyMode am ON am.id = z.agencyModeFk + LEFT JOIN zoneEvent ze ON ze.zoneFk = z.id + WHERE + ( + dated = ? + OR ? BETWEEN started AND ended + OR INSTR(weekDays, SUBSTRING(DAYNAME(?), 1, 3) ) > 0 + ) + AND z.id IN (${paramsString}) + ORDER BY type='day' DESC, type='range' DESC, type='indefinitely' DESC) z + GROUP BY z.id + `; + + const zones = await Self.rawSql(query, paramsSql, myOptions); + return zones; + }; +}; diff --git a/modules/zone/back/models/zone.js b/modules/zone/back/models/zone.js index 9771c958b..ef1c8c5d9 100644 --- a/modules/zone/back/models/zone.js +++ b/modules/zone/back/models/zone.js @@ -7,6 +7,7 @@ module.exports = Self => { require('../methods/zone/getUpcomingDeliveries')(Self); require('../methods/zone/deleteZone')(Self); require('../methods/zone/includingExpired')(Self); + require('../methods/zone/getZoneClosing')(Self); Self.validatesPresenceOf('agencyModeFk', { message: `Agency cannot be blank` diff --git a/modules/zone/front/delivery-days/index.html b/modules/zone/front/delivery-days/index.html index f01f4ec3f..1c1a9b1b3 100644 --- a/modules/zone/front/delivery-days/index.html +++ b/modules/zone/front/delivery-days/index.html @@ -52,23 +52,15 @@ - - -
Zones
- + Id @@ -81,7 +73,7 @@ {{::zone.id}} diff --git a/modules/zone/front/delivery-days/index.js b/modules/zone/front/delivery-days/index.js index 12b1c57b1..d4bb3b335 100644 --- a/modules/zone/front/delivery-days/index.js +++ b/modules/zone/front/delivery-days/index.js @@ -74,33 +74,16 @@ class Controller extends Section { zonesIds.push(event.zoneFk); this.$.zoneEvents.show($event.target); - const zoneModel = this.$.zoneModel; - zoneModel.applyFilter({ - include: [ - { - relation: 'agencyMode', - scope: {fields: ['name']} - }, - { - relation: 'events', - scope: { - where: {dated: day} - } - }, - ], - where: { - id: {inq: zonesIds} - } - }).then(() => { - const data = zoneModel.data; - for (let row of data) { - const [event] = row.events; - if (event && event.hour) - row.hour = event.hour; - if (event && event.price) - row.price = event.price; - } - }); + + const params = { + zonesId: zonesIds, + date: day + }; + + this.$http.post(`Zones/getZoneClosing`, params) + .then(res => { + this.zoneClosing = res.data; + }); } preview(zone) { diff --git a/modules/zone/front/delivery-days/index.spec.js b/modules/zone/front/delivery-days/index.spec.js index c896021ed..c03da585f 100644 --- a/modules/zone/front/delivery-days/index.spec.js +++ b/modules/zone/front/delivery-days/index.spec.js @@ -96,14 +96,8 @@ describe('Zone Component vnZoneDeliveryDays', () => { expect(controller.$.zoneEvents.show).not.toHaveBeenCalled(); }); - it('should call the show() method and then call the applyFilter() method with the expected ids', () => { - const zoneModel = controller.$.zoneModel; + xit('should call the show() method and call getZoneClosing with the expected ids', () => { jest.spyOn(controller.$.zoneEvents, 'show'); - jest.spyOn(zoneModel, 'applyFilter').mockReturnValue(new Promise(resolve => { - zoneModel.data = [ - {id: 1, events: [{price: 25}]} - ]; - })); const event = new Event('click'); const target = document.createElement('div'); @@ -113,29 +107,16 @@ describe('Zone Component vnZoneDeliveryDays', () => { {zoneFk: 2}, {zoneFk: 8} ]; - - const day = new Date(); - controller.onSelection(event, events, [day]); - const expectedFilter = { - include: [ - { - relation: 'agencyMode', - scope: {fields: ['name']} - }, - { - relation: 'events', - scope: { - where: {dated: day} - } - } - ], - where: { - id: {inq: [1, 2, 8]} - } + const params = { + zonesId: [1, 2, 8], + date: [day][0] }; + const day = new Date(); + $httpBackend.expect('POST', 'Zones/getZoneClosing', params).respond({}); + controller.onSelection(event, events, [day]); expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target); - expect(zoneModel.applyFilter).toHaveBeenCalledWith(expectedFilter); + expect(controller.zoneClosing).toBeDefined(); }); }); });