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