Skip to content

Commit 4fd86cf

Browse files
authored
Merge pull request #179 from Hello-Kitchen/173-route-for-kds-graph
173 route for kds graph
2 parents 5d177c6 + 8b8a726 commit 4fd86cf

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

docs/swagger.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3529,6 +3529,63 @@
35293529
}
35303530
]
35313531
}
3532+
},
3533+
"/{restaurant_id}/kpi/ordersCount": {
3534+
"get": {
3535+
"tags": ["KPI"],
3536+
"summary": "Count the number of orders in a given interval, with optional grouping by time slot.",
3537+
"parameters": [
3538+
{
3539+
"name": "restaurant_id",
3540+
"in": "path",
3541+
"description": "ID of the restaurant",
3542+
"required": true,
3543+
"schema": { "type": "integer" }
3544+
},
3545+
{
3546+
"name": "timeBegin",
3547+
"in": "query",
3548+
"description": "Start date/time of the interval (ISO 8601)",
3549+
"required": true,
3550+
"schema": { "type": "string", "format": "date-time" }
3551+
},
3552+
{
3553+
"name": "timeEnd",
3554+
"in": "query",
3555+
"description": "End date/time of the interval (ISO 8601)",
3556+
"required": true,
3557+
"schema": { "type": "string", "format": "date-time" }
3558+
},
3559+
{
3560+
"name": "breakdown",
3561+
"in": "query",
3562+
"description": "Slot duration in minutes for grouping (optional)",
3563+
"required": false,
3564+
"schema": { "type": "integer" }
3565+
}
3566+
],
3567+
"responses": {
3568+
"200": {
3569+
"description": "Returns either the total number of orders, or an object grouped by time slot.",
3570+
"content": {
3571+
"application/json": {
3572+
"examples": {
3573+
"Grouped": {
3574+
"value": { "00:00": 5, "00:15": 8 }
3575+
},
3576+
"Total": {
3577+
"value": 23
3578+
}
3579+
}
3580+
}
3581+
}
3582+
},
3583+
"400": { "description": "Invalid parameters" },
3584+
"401": { "$ref": "#/components/responses/Unauthorized" },
3585+
"5XX": { "$ref": "#/components/responses/ServerError" }
3586+
},
3587+
"security": [{ "BearerAuth": [] }]
3588+
}
35323589
}
35333590
},
35343591
"components": {

src/modules/kpi/kpi.controller.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,65 @@ export class KpiController {
440440
return res;
441441
}
442442
}
443+
444+
/**
445+
* Counts the number of orders within a given interval, with optional grouping by time slots (in minutes).
446+
* @param idRestaurant - The restaurant identifier (must be positive)
447+
* @param timeBegin - Start date/time of the interval (required)
448+
* @param timeEnd - End date/time of the interval (required)
449+
* @param breakdown - (optional) Slot duration in minutes for grouping
450+
* @returns Either the total number of orders, or an object grouped by time slot
451+
* @throws {BadRequestException} If parameters are invalid
452+
* @throws {InternalServerErrorException} In case of server error
453+
* @example
454+
* GET /api/1/kpi/ordersCount?timeBegin=2024-01-01T00:00:00Z&timeEnd=2024-01-01T01:00:00Z&breakdown=15
455+
* // returns { "00:00": 5, "00:15": 8, ... }
456+
* GET /api/1/kpi/ordersCount?timeBegin=2024-01-01T00:00:00Z&timeEnd=2024-01-01T01:00:00Z
457+
* // returns 23
458+
*/
459+
@Get('ordersCount')
460+
async kpiOrdersCount(
461+
@Param('idRestaurant', PositiveNumberPipe) idRestaurant: number,
462+
@Query('timeBegin', DatePipe) timeBegin: string,
463+
@Query('timeEnd', DatePipe) timeEnd: string,
464+
@Query('breakdown') breakdown?: number,
465+
) {
466+
if (breakdown) {
467+
const slots: { timeBegin: string; timeEnd: string }[] = [];
468+
let current = new Date(timeBegin);
469+
const end = new Date(timeEnd);
470+
const orders = {};
471+
472+
while (current < end) {
473+
const slotStart = new Date(current);
474+
const slotEnd = new Date(current.getTime() + breakdown * 60000);
475+
slots.push({
476+
timeBegin: slotStart.toISOString(),
477+
timeEnd: (slotEnd < end ? slotEnd : end).toISOString(),
478+
});
479+
current = slotEnd;
480+
}
481+
for (const slot of slots) {
482+
const date = new Date(slot.timeBegin);
483+
const hours = date.getUTCHours().toString().padStart(2, '0');
484+
const minutes = date.getUTCMinutes().toString().padStart(2, '0');
485+
orders[`${hours}:${minutes}`] = await this.kpiService.clientsCount(
486+
idRestaurant,
487+
slot.timeBegin,
488+
slot.timeEnd,
489+
undefined,
490+
undefined,
491+
);
492+
}
493+
return orders;
494+
} else {
495+
return this.kpiService.clientsCount(
496+
idRestaurant,
497+
timeBegin,
498+
timeEnd,
499+
undefined,
500+
undefined,
501+
);
502+
}
503+
}
443504
}

0 commit comments

Comments
 (0)