Skip to content

Commit

Permalink
Merge branch 'main' of github.com:tekdi/event-management-service into…
Browse files Browse the repository at this point in the history
… main
  • Loading branch information
Xitija committed Aug 2, 2024
2 parents 2009d1c + 462d4e0 commit 9aafede
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/modules/event/dto/search-event.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IsEnum,
IsString,
ValidateNested,
IsUUID,
} from 'class-validator';

export class FilterDto {
Expand Down Expand Up @@ -59,6 +60,11 @@ export class FilterDto {
@IsOptional()
@IsString()
title?: string;

@ApiProperty({ example: 'CohortId', description: 'CohortId' })
@IsOptional()
@IsUUID('4')
cohortId?: string;
}

export class SearchFilterDto {
Expand Down
32 changes: 11 additions & 21 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BadRequestException,
HttpStatus,
Injectable,
NotFoundException,
NotImplementedException,
} from '@nestjs/common';
import { CreateEventDto } from './dto/create-event.dto';
Expand Down Expand Up @@ -123,47 +124,31 @@ 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,
isEnded: endDateTime < today,
};
});
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;
}
}

Expand Down Expand Up @@ -232,6 +217,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 ')}`;
Expand Down

0 comments on commit 9aafede

Please sign in to comment.