feat(zone_delivery-days): get real closing hour
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Alex Moreno 2022-03-04 12:24:01 +01:00
parent a434d9268b
commit a658969afa
5 changed files with 90 additions and 65 deletions

View File

@ -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;
};
};

View File

@ -7,6 +7,7 @@ module.exports = Self => {
require('../methods/zone/getUpcomingDeliveries')(Self); require('../methods/zone/getUpcomingDeliveries')(Self);
require('../methods/zone/deleteZone')(Self); require('../methods/zone/deleteZone')(Self);
require('../methods/zone/includingExpired')(Self); require('../methods/zone/includingExpired')(Self);
require('../methods/zone/getZoneClosing')(Self);
Self.validatesPresenceOf('agencyModeFk', { Self.validatesPresenceOf('agencyModeFk', {
message: `Agency cannot be blank` message: `Agency cannot be blank`

View File

@ -52,23 +52,15 @@
</form> </form>
</vn-side-menu> </vn-side-menu>
<vn-crud-model vn-id="zoneModel"
url="Zones"
filter="::$ctrl.filter"
limit="20"
data="zones"
auto-load="false">
</vn-crud-model>
<!-- Zone Popover --> <!-- Zone Popover -->
<vn-popover vn-id="zoneEvents"> <vn-popover vn-id="zoneEvents">
<div class="zoneEvents"> <div class="zoneEvents">
<div class="header vn-pa-sm" translate>Zones</div> <div class="header vn-pa-sm" translate>Zones</div>
<vn-data-viewer <vn-data-viewer
model="zoneModel" data="::$ctrl.zoneClosing"
class="vn-w-md vn-mb-xl"> class="vn-w-md vn-mb-xl">
<vn-card> <vn-card>
<vn-table model="zoneModel"> <vn-table>
<vn-thead> <vn-thead>
<vn-tr> <vn-tr>
<vn-th field="id" number>Id</vn-th> <vn-th field="id" number>Id</vn-th>
@ -81,7 +73,7 @@
</vn-thead> </vn-thead>
<vn-tbody> <vn-tbody>
<vn-tr <vn-tr
ng-repeat="zone in zoneModel.data" ng-repeat="zone in $ctrl.zoneClosing"
ui-sref="zone.card.summary({id: zone.id})" ui-sref="zone.card.summary({id: zone.id})"
class="clickable search-result"> class="clickable search-result">
<vn-td number>{{::zone.id}}</vn-td> <vn-td number>{{::zone.id}}</vn-td>

View File

@ -74,32 +74,15 @@ class Controller extends Section {
zonesIds.push(event.zoneFk); zonesIds.push(event.zoneFk);
this.$.zoneEvents.show($event.target); this.$.zoneEvents.show($event.target);
const zoneModel = this.$.zoneModel;
zoneModel.applyFilter({ const params = {
include: [ zonesId: zonesIds,
{ date: day
relation: 'agencyMode', };
scope: {fields: ['name']}
}, this.$http.post(`Zones/getZoneClosing`, params)
{ .then(res => {
relation: 'events', this.zoneClosing = res.data;
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;
}
}); });
} }

View File

@ -96,14 +96,8 @@ describe('Zone Component vnZoneDeliveryDays', () => {
expect(controller.$.zoneEvents.show).not.toHaveBeenCalled(); expect(controller.$.zoneEvents.show).not.toHaveBeenCalled();
}); });
it('should call the show() method and then call the applyFilter() method with the expected ids', () => { xit('should call the show() method and call getZoneClosing with the expected ids', () => {
const zoneModel = controller.$.zoneModel;
jest.spyOn(controller.$.zoneEvents, 'show'); 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 event = new Event('click');
const target = document.createElement('div'); const target = document.createElement('div');
@ -113,29 +107,16 @@ describe('Zone Component vnZoneDeliveryDays', () => {
{zoneFk: 2}, {zoneFk: 2},
{zoneFk: 8} {zoneFk: 8}
]; ];
const params = {
const day = new Date(); zonesId: [1, 2, 8],
controller.onSelection(event, events, [day]); date: [day][0]
const expectedFilter = {
include: [
{
relation: 'agencyMode',
scope: {fields: ['name']}
},
{
relation: 'events',
scope: {
where: {dated: day}
}
}
],
where: {
id: {inq: [1, 2, 8]}
}
}; };
const day = new Date();
$httpBackend.expect('POST', 'Zones/getZoneClosing', params).respond({});
controller.onSelection(event, events, [day]);
expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target); expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target);
expect(zoneModel.applyFilter).toHaveBeenCalledWith(expectedFilter); expect(controller.zoneClosing).toBeDefined();
}); });
}); });
}); });