From a7138796f256edd2bfbe14d4fff943c6450cf5e1 Mon Sep 17 00:00:00 2001 From: poojakarma Date: Fri, 2 Aug 2024 11:51:07 +0530 Subject: [PATCH] Added search filter for CohortId --- src/modules/event/dto/search-event.dto.ts | 6 ++++ src/modules/event/event.service.ts | 35 +++++++++-------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/modules/event/dto/search-event.dto.ts b/src/modules/event/dto/search-event.dto.ts index d9f8d0e..c92e961 100644 --- a/src/modules/event/dto/search-event.dto.ts +++ b/src/modules/event/dto/search-event.dto.ts @@ -6,6 +6,7 @@ import { IsEnum, IsString, ValidateNested, + IsUUID, } from 'class-validator'; export class FilterDto { @@ -59,6 +60,11 @@ export class FilterDto { @IsOptional() @IsString() title?: string; + + @ApiProperty({ example: 'CohortId', description: 'CohortId' }) + @IsOptional() + @IsUUID('4') + cohortId?: string } export class SearchFilterDto { diff --git a/src/modules/event/event.service.ts b/src/modules/event/event.service.ts index f2e99ff..dc151a8 100644 --- a/src/modules/event/event.service.ts +++ b/src/modules/event/event.service.ts @@ -2,6 +2,7 @@ import { BadRequestException, HttpStatus, Injectable, + NotFoundException, NotImplementedException, } from '@nestjs/common'; import { CreateEventDto } from './dto/create-event.dto'; @@ -123,9 +124,12 @@ export class EventService { // Append LIMIT and OFFSET to the query finalquery += ` LIMIT ${limit} OFFSET ${offset}`; const result = await this.eventRepetitionRepository.query(finalquery); + const totalCount = result[0]?.total_count + // Add isEnded key based on endDateTime const finalResult = result.map((event) => { + delete event.total_count; const endDateTime = new Date(event.endDateTime); return { ...event, @@ -133,37 +137,19 @@ export class EventService { }; }); if (finalResult.length === 0) { - return response - .status(HttpStatus.NOT_FOUND) - .json( - APIResponse.error( - apiId, - 'Event Not Found', - 'No records found.', - 'NOT_FOUND', - ), - ); + throw new NotFoundException('Event Not Found') } return response .status(HttpStatus.OK) .json( APIResponse.success( apiId, - { totalCount: finalResult[0].total_count, events: finalResult }, + { totalCount, events: finalResult }, 'OK`', ), ); } catch (error) { - return response - .status(HttpStatus.INTERNAL_SERVER_ERROR) - .json( - APIResponse.error( - apiId, - ERROR_MESSAGES.INTERNAL_SERVER_ERROR, - error, - '500', - ), - ); + throw error } } @@ -232,6 +218,11 @@ export class EventService { whereClauses.push(`"status" = 'live'`); } + // Handle cohortId filter + if (filters.cohortId) { + whereClauses.push(`ed."metadata"->>'cohortId'='${filters.cohortId}'`) + } + // Construct final query if (whereClauses.length > 0) { finalquery += ` WHERE ${whereClauses.join(' AND ')}`; @@ -548,7 +539,7 @@ export class EventService { if ( config.endCondition.type === 'endDate' && occurrences[occurrences.length - 1]?.endDateTime > - new Date(config.endCondition.value) + new Date(config.endCondition.value) ) { occurrences.pop(); }