Skip to content

Commit

Permalink
feat: allow filtering events by campus ID
Browse files Browse the repository at this point in the history
  • Loading branch information
vfurmane committed May 16, 2024
1 parent 1c07b9e commit 5d7dd3a
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 34 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.2",
"cache-manager": "^5.5.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"ical-generator": "^7.1.0",
"parse-link-header": "^2.0.0",
"reflect-metadata": "^0.2.0",
Expand Down
100 changes: 71 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/common/utils/filter-events-by-campus-ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FindEventsResponseDto } from '../../ft-api/dto/find-events-response.dto';
import { FindAllEventsDto } from '../../events/dto/find-all-events.dto';

export function filterEventsByCampusIds(events: FindEventsResponseDto, campusIds: FindAllEventsDto['campusIds']) {
if (campusIds === undefined) {
return events;
}
return events.filter((event) => {
return event.campus_ids.some((campusId) => campusIds.includes(campusId));
});
}
7 changes: 7 additions & 0 deletions src/events/dto/find-all-events.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsNumberString, IsOptional } from 'class-validator';

export class FindAllEventsDto {
@IsOptional()
@IsNumberString({}, { each: true })
campusIds?: Array<number>;
}
9 changes: 6 additions & 3 deletions src/events/events.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { Controller, Get, ParseArrayPipe, Query, UseInterceptors } from '@nestjs/common';
import { EventsService } from './events.service';
import { EventsToIcsInterceptor } from '../events-to-ics/events-to-ics.interceptor';
import { CALENDAR_NAME_ALL_EVENTS } from '../common/constants/calendars-name';
import { FindAllEventsDto } from './dto/find-all-events.dto';

@Controller('events')
export class EventsController {
constructor(private readonly eventsService: EventsService) {}

@Get()
@UseInterceptors(new EventsToIcsInterceptor({ name: CALENDAR_NAME_ALL_EVENTS }))
findAll() {
return this.eventsService.findAll();
findAll(
@Query('campusIds', new ParseArrayPipe({ items: Number, separator: ',' })) campusIds: FindAllEventsDto['campusIds'],
) {
return this.eventsService.findAll({ campusIds });
}
}
6 changes: 6 additions & 0 deletions src/events/events.http
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
### GET all events
{{base_url}}/events

### GET all events from Paris campus
{{base_url}}/events?campusIds=1

### GET all events from Paris and Málaga campuses
{{base_url}}/events?campusIds=1,37
10 changes: 8 additions & 2 deletions src/events/events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
} from '../common/constants/ft-api-cache';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { FindAllEventsDto } from './dto/find-all-events.dto';
import { FindEventsResponseDto } from '../ft-api/dto/find-events-response.dto';
import { filterEventsByCampusIds } from '../common/utils/filter-events-by-campus-ids';

@Injectable()
export class EventsService {
Expand All @@ -27,8 +30,11 @@ export class EventsService {
await this.cacheManager.set(FT_CACHED_EVENTS_CACHE_KEY, events, FT_CACHED_EVENTS_TTL);
}

async findAll() {
return (await this.cacheManager.get(FT_CACHED_EVENTS_CACHE_KEY)) ?? FT_DEFAULT_EVENTS_LIST;
async findAll({ campusIds }: FindAllEventsDto) {
return filterEventsByCampusIds(
(await this.cacheManager.get<FindEventsResponseDto>(FT_CACHED_EVENTS_CACHE_KEY)) ?? FT_DEFAULT_EVENTS_LIST,
campusIds,
);
}

async findAllFromDefaultCampus() {
Expand Down
1 change: 1 addition & 0 deletions src/ft-api/dto/find-events-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';
export const findEventsResponseSchema = z.array(
z.object({
name: z.string(),
campus_ids: z.array(z.number()),
description: z.string(),
location: z.string(),
begin_at: z.string(),
Expand Down
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
}),
);
await app.listen(3000);
}
bootstrap();

0 comments on commit 5d7dd3a

Please sign in to comment.