Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();