Skip to content

Commit

Permalink
Add endpoint to get RSVPs, add extra functionality to query params of…
Browse files Browse the repository at this point in the history
… getEventAttendances() (#169)

* Add endpoint to get RSVPs, add extra functionality to query params of
getEventAttendances()

* Change getAllEventAttendances -> getEventAttendances and
getAllEventRSVPs -> getEventRSVPs
  • Loading branch information
thai-truong authored Feb 14, 2021
1 parent 20931a5 commit 6ed96a6
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/controllers/EventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
MultipleEventQuery,
AppUserEventRequest,
RSVPResponse,
MultipleRSVPResponse,
} from '@Payloads';
import {
AttendanceService,
Expand Down Expand Up @@ -144,13 +145,33 @@ export class EventController {
multipleAttendanceQuery
);

if (attendances === undefined) {
return undefined;
}

const mappedAttendances = attendances.map(attendance =>
this.attendanceMapper.entityToResponse(attendance)
);

return { attendances: mappedAttendances };
}

@Get('/:eventID/rsvp')
@ResponseSchema(MultipleRSVPResponse)
@UseBefore(OfficerAuthMiddleware)
@OpenAPI({ security: [{ TokenAuth: [] }] })
async getEventRSVP(@Param('eventID') eventID: number): Promise<MultipleRSVPResponse | undefined> {
const rsvps = await this.eventService.getEventRSVPs(eventID);

if (rsvps === undefined) {
return undefined;
}

const mappedRSVPs = rsvps.map(rsvp => this.rsvpMapper.entityToResponse(rsvp));

return { rsvps: mappedRSVPs };
}

@Post('/:eventID/attendance')
@ResponseSchema(AttendanceResponse)
@UseBefore(OfficerAuthMiddleware)
Expand Down
10 changes: 9 additions & 1 deletion src/payloads/RSVP.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IsInstance } from 'class-validator';
import { IsInstance, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

import { AppUserEventResponse } from './AppUser';
import { EventRSVPResponse } from './Event';

Expand All @@ -9,3 +11,9 @@ export class RSVPResponse {
@IsInstance(AppUserEventResponse)
appUser: AppUserEventResponse;
}

export class MultipleRSVPResponse {
@ValidateNested({ each: true })
@Type(() => RSVPResponse)
rsvps: RSVPResponse[];
}
2 changes: 1 addition & 1 deletion src/payloads/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export {
MultipleAttendanceQuery,
} from './Attendance';
export { InterviewDatesResponse } from './InterviewDates';
export { RSVPResponse } from './RSVP';
export { RSVPResponse, MultipleRSVPResponse } from './RSVP';
export { InducteePointsResponse, MultipleInducteePointsResponse } from './Points';
12 changes: 8 additions & 4 deletions src/services/AttendanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Attendance, AppUser, AppUserRole, Event, EventType } from '@Entities';
import { MultipleAttendanceQuery } from '@Payloads';
import { AppUserService, AppUserServiceImpl } from './AppUserService';

import { getRepository, FindManyOptions } from 'typeorm';
import { getRepository, FindManyOptions, Not, IsNull } from 'typeorm';
import { differenceInMinutes } from 'date-fns';

export class AttendanceService {
Expand All @@ -26,8 +26,12 @@ export class AttendanceService {
query.where = { event: event };
query.relations = ['attendee', 'officer', 'event'];

if (unchecked) {
query.where = { ...query.where, officer: null };
if (unchecked !== undefined) {
if (unchecked) {
query.where = { ...query.where, officer: IsNull() };
} else {
query.where = { ...query.where, officer: Not(IsNull()) };
}
}

if (inductee) {
Expand Down Expand Up @@ -75,7 +79,7 @@ export class AttendanceService {
* @param {MultipleAttendanceQuery} multipleAttendanceQuery Query parameters to filter attendances.
* @returns {Promise} Array of attendances from the specified event.
*/
async getAllEventAttendances(
async getEventAttendances(
event: Event,
multipleAttendanceQuery: MultipleAttendanceQuery
): Promise<Attendance[]> {
Expand Down
14 changes: 13 additions & 1 deletion src/services/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,26 @@ export class EventService {
return undefined;
}

const attendances = await this.attendanceService.getAllEventAttendances(
const attendances = await this.attendanceService.getEventAttendances(
event,
multipleAttendanceQuery
);

return attendances;
}

async getEventRSVPs(eventId: number): Promise<RSVP[] | undefined> {
const event = await this.getEventById(eventId);

if (event === undefined) {
return undefined;
}

const rsvps = await this.rsvpService.getEventRSVPs(event);

return rsvps;
}

/**
* Creates an Attendance entity using the event obtained from eventId and
* the passed-in AppUser entity, then stores that Attendance entity to the
Expand Down
22 changes: 21 additions & 1 deletion src/services/RSVPService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { AppUser, Event, RSVP } from '@Entities';
import { getRepository } from 'typeorm';
import { getRepository, FindManyOptions } from 'typeorm';

export class RSVPService {
private buildMultipleRSVPQuery(event: Event, cacheOn: boolean): FindManyOptions<RSVP> {
const query: FindManyOptions<RSVP> = {};

query.where = { event: event };
query.relations = ['appUser', 'event'];

if (cacheOn) {
query.cache = true;
}

return query;
}

/**
* Creates a new RSVP entity, then attempts to insert it into the DB.
*
Expand All @@ -22,6 +35,13 @@ export class RSVPService {

return newRSVP;
}

async getEventRSVPs(event: Event): Promise<RSVP[]> {
const rsvpRepository = getRepository(RSVP);
const query = this.buildMultipleRSVPQuery(event, true);

return rsvpRepository.find(query);
}
}

export const RSVPServiceImpl = new RSVPService();

0 comments on commit 6ed96a6

Please sign in to comment.