diff --git a/modules/ticket/back/methods/sale/payBack.js b/modules/ticket/back/methods/sale/payBack.js index 3bb056465..098da4d5a 100644 --- a/modules/ticket/back/methods/sale/payBack.js +++ b/modules/ticket/back/methods/sale/payBack.js @@ -40,7 +40,6 @@ module.exports = Self => { try { const salesIds = []; - const params = []; const userId = ctx.req.accessToken.userId; const isClaimManager = await Self.app.models.Account.hasRole(userId, 'claimManager'); @@ -50,23 +49,19 @@ module.exports = Self => { if (!hasValidRole) throw new UserError(`You don't have privileges to create pay back`); - sales.forEach(sale => { + for (let sale of sales) salesIds.push(sale.id); - params.push('?'); - }); - - const paramsString = params.join(); const query = ` DROP TEMPORARY TABLE IF EXISTS tmp.sale; CREATE TEMPORARY TABLE tmp.sale SELECT s.id, s.itemFk, - s.quantity, s.concept, s.price, s.discount FROM sale s - WHERE s.id IN (${paramsString}); - CALL vn.ticket_doRefund(${ticketId}, @newTicket); + WHERE s.id IN (?); + CALL vn.ticket_doRefund(?, @newTicket); DROP TEMPORARY TABLE tmp.sale;`; - await Self.rawSql(query, salesIds, myOptions); + await Self.rawSql(query, [salesIds, ticketId], myOptions); const [newTicket] = await Self.rawSql('SELECT @newTicket id', null, myOptions); ticketId = newTicket.id; diff --git a/modules/ticket/back/methods/sale/recalculatePrice.js b/modules/ticket/back/methods/sale/recalculatePrice.js index 8a390223d..59c7d3535 100644 --- a/modules/ticket/back/methods/sale/recalculatePrice.js +++ b/modules/ticket/back/methods/sale/recalculatePrice.js @@ -35,11 +35,8 @@ module.exports = Self => { try { const salesIds = []; - const params = []; - sales.forEach(sale => { + for (let sale of sales) salesIds.push(sale.id); - params.push('?'); - }); const isEditable = await models.Ticket.isEditable(ctx, sales[0].ticketFk, myOptions); if (!isEditable) @@ -49,14 +46,12 @@ module.exports = Self => { if (!canEditSale) throw new UserError(`Sale(s) blocked, please contact production`); - const paramsString = params.join(); - const query = ` DROP TEMPORARY TABLE IF EXISTS tmp.recalculateSales; CREATE TEMPORARY TABLE tmp.recalculateSales SELECT s.id FROM sale s - WHERE s.id IN (${paramsString}); + WHERE s.id IN (?); CALL vn.sale_recalcComponent(null); DROP TEMPORARY TABLE tmp.recalculateSales;`; diff --git a/modules/zone/back/methods/zone/getEventsFiltered.js b/modules/zone/back/methods/zone/getEventsFiltered.js index 2788f45d0..5e9cbae5a 100644 --- a/modules/zone/back/methods/zone/getEventsFiltered.js +++ b/modules/zone/back/methods/zone/getEventsFiltered.js @@ -11,12 +11,12 @@ module.exports = Self => { { arg: 'started', type: 'date', - description: 'The date calendar start', + description: 'The calendar date start', }, { arg: 'ended', type: 'date', - description: 'The date calendar end', + description: 'The calendar date end', } ], returns: { diff --git a/modules/zone/back/methods/zone/getZoneClosing.js b/modules/zone/back/methods/zone/getZoneClosing.js new file mode 100644 index 000000000..2a0088203 --- /dev/null +++ b/modules/zone/back/methods/zone/getZoneClosing.js @@ -0,0 +1,57 @@ +module.exports = Self => { + Self.remoteMethod('getZoneClosing', { + description: 'Get zone events filtered for date and prioritized by type', + accepts: [ + { + arg: 'zoneIds', + type: ['number'], + description: 'The zone ids', + required: true + }, + { + arg: 'date', + type: 'date', + description: 'The calendar date', + } + ], + returns: { + type: 'object', + root: true + }, + http: { + path: `/getZoneClosing`, + verb: 'POST' + } + }); + + Self.getZoneClosing = async(zoneIds, date, options) => { + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + query = ` + SELECT * + FROM ( + SELECT + DISTINCT z.id, + z.name, + am.name agencyModeName, + 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 (?) + ORDER BY type='day' DESC, type='range' DESC, type='indefinitely' DESC) z + GROUP BY z.id`; + + return Self.rawSql(query, [date, date, date, zoneIds], myOptions); + }; +}; diff --git a/modules/zone/back/methods/zone/specs/getZoneClosing.spec.js b/modules/zone/back/methods/zone/specs/getZoneClosing.spec.js new file mode 100644 index 000000000..0c479dda0 --- /dev/null +++ b/modules/zone/back/methods/zone/specs/getZoneClosing.spec.js @@ -0,0 +1,23 @@ +const models = require('vn-loopback/server/server').models; + +describe('zone getZoneClosing()', () => { + it('should return closing time of zones', async() => { + const tx = await models.Zone.beginTransaction({}); + + try { + const options = {transaction: tx}; + const date = new Date(); + const today = date.toISOString().split('T')[0]; + + const result = await models.Zone.getZoneClosing([1, 2, 3], today, options); + + expect(result.length).toEqual(3); + expect(result[0].hour).toBeDefined(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } + }); +}); 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..21c65678f 100644 --- a/modules/zone/front/delivery-days/index.js +++ b/modules/zone/front/delivery-days/index.js @@ -74,33 +74,14 @@ 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..3d71bc93f 100644 --- a/modules/zone/front/delivery-days/index.spec.js +++ b/modules/zone/front/delivery-days/index.spec.js @@ -96,46 +96,31 @@ 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; + it('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'); target.dispatchEvent(event); + + const day = new Date(); const events = [ {zoneFk: 1}, {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 response = [{id: 1, hour: ''}]; + + $httpBackend.when('POST', 'Zones/getZoneClosing', params).respond({response}); + controller.onSelection(event, events, [day]); + $httpBackend.flush(); expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target); - expect(zoneModel.applyFilter).toHaveBeenCalledWith(expectedFilter); + expect(controller.zoneClosing.id).toEqual(response.id); }); }); }); diff --git a/modules/zone/front/upcoming-deliveries/style.scss b/modules/zone/front/upcoming-deliveries/style.scss index d3f33260a..b52231a09 100644 --- a/modules/zone/front/upcoming-deliveries/style.scss +++ b/modules/zone/front/upcoming-deliveries/style.scss @@ -15,6 +15,7 @@ vn-upcoming-deliveries { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; + background-color: $color-bg; } vn-table vn-th.waste-family,