Skip to content

Commit

Permalink
Refactor #2 of /api/events/:eventID/signin.
Browse files Browse the repository at this point in the history
Added RequestToEntityByEmail and AppUserService now doesn't depend on
mappers and payloads anymore.
  • Loading branch information
Thai committed Aug 16, 2020
1 parent a4328a8 commit e9ca196
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
21 changes: 17 additions & 4 deletions src/controllers/EventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ import { ResponseSchema } from 'routing-controllers-openapi';
import { Event, Attendance } from '@Entities';
import { EventRequest, EventResponse, MultipleEventResponse, EventSignInRequest } from '@Payloads';
import { AppUserService, EventService } from '@Services';
import { EventMapper } from '@Mappers';
import { AppUserMapper, EventMapper } from '@Mappers';

@singleton()
@JsonController('/api/events')
export class EventController {
private appUserService: AppUserService;
private appUserMapper: AppUserMapper;
private eventService: EventService;
private eventMapper: EventMapper;

constructor(
@inject(AppUserService) appUserService: AppUserService,
@inject(AppUserMapper) appUserMapper: AppUserMapper,
@inject(EventService) eventService: EventService,
@inject(EventMapper) eventMapper: EventMapper
) {
this.appUserService = appUserService;
this.appUserMapper = appUserMapper;
this.eventService = eventService;
this.eventMapper = eventMapper;
}
Expand Down Expand Up @@ -85,10 +88,20 @@ export class EventController {
async signInToEvent(
@Param('eventID') eventID: number,
@Body() appUserRequest: EventSignInRequest
): Promise<Attendance> {
const savedAppUser = await this.appUserService.saveNonAffiliate(appUserRequest);
): Promise<Attendance | undefined> {
const { email } = appUserRequest;
const appUserFromEmail = await this.appUserService.getAppUserByEmail(email);
const appUserToSave = await this.appUserMapper.requestToEntityByEmail(
appUserFromEmail,
appUserRequest
);

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

const savedAppUser = await this.appUserService.saveNonAffiliate(appUserToSave);

// savedAppUser is undefined if AppUser from appUserRequest is an affiliate
if (savedAppUser === undefined) {
/*
* TODO: Handle the case where user is an affiliate
Expand Down
13 changes: 13 additions & 0 deletions src/mappers/AppUserMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ export class AppUserMapper {
return appUser;
}

async requestToEntityByEmail(
appUserFromEmail: AppUser,
appUserRequest: EventSignInRequest
): Promise<AppUser | undefined> {
if (appUserFromEmail === undefined) {
return this.requestToNewEntity(appUserRequest);
} else {
const { id } = appUserFromEmail;

return await this.requestToExistingEntity(appUserRequest, id);
}
}

/**
* Converts an AppUser entity to an EventSignInResponse payload and returns the
* newly created response payload to the caller.
Expand Down
28 changes: 7 additions & 21 deletions src/services/AppUserService.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { AppUser, AppUserRole } from '@Entities';
import { AppUserMapper } from '@Mappers';
import { EventSignInRequest } from '@Payloads';
import { singleton, inject } from 'tsyringe';
import { Any, Repository, getRepository } from 'typeorm';

@singleton()
export class AppUserService {
private appUserRepository: Repository<AppUser>;
private appUserMapper: AppUserMapper;

constructor(@inject(AppUserMapper) appUserMapper: AppUserMapper) {
constructor() {
this.appUserRepository = getRepository(AppUser);
this.appUserMapper = appUserMapper;
}

/**
Expand Down Expand Up @@ -39,27 +35,17 @@ export class AppUserService {
* @param email The email used to look for the corresponding AppUser.
*
*/
getAppUserByEmail(email: string): Promise<AppUser> {
getAppUserByEmail(email: string): Promise<AppUser | undefined> {
return this.appUserRepository.findOne({ email });
}

async saveNonAffiliate(appUserRequest: EventSignInRequest): Promise<AppUser | undefined> {
const { email } = appUserRequest;
const appUserFromEmail = await this.getAppUserByEmail(email);
let currAppUser = null;
async saveNonAffiliate(appUser: AppUser): Promise<AppUser | undefined> {
const { role } = appUser;

if (appUserFromEmail === undefined) {
currAppUser = this.appUserMapper.requestToNewEntity(appUserRequest);
} else {
const { id, role } = appUserFromEmail;

if (role === AppUserRole.GUEST) {
currAppUser = await this.appUserMapper.requestToExistingEntity(appUserRequest, id);
} else {
return undefined;
}
if (role !== undefined && role !== AppUserRole.GUEST) {
return undefined;
}

return await this.saveAppUser(currAppUser);
return await this.saveAppUser(appUser);
}
}
6 changes: 0 additions & 6 deletions src/services/AttendanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,4 @@ export class AttendanceService {

return newAttendance;
}

async getAttendanceByEventUser(event: Event, attendee: AppUser): Promise<Attendance | undefined> {
const attendanceToCheck = await this.attendanceRepository.findOne({ event, attendee });

return attendanceToCheck;
}
}

0 comments on commit e9ca196

Please sign in to comment.