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

Removing Public Data Types from Service Layer (WIP) #444

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion api/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export class AdminController {
if (!PermissionsService.canSubmitAttendanceForUsers(currentUser)) throw new ForbiddenError();
const { users, event, asStaff } = submitAttendanceForUsersRequest;
const emails = users.map((e) => e.toLowerCase());
const attendances = await this.attendanceService.submitAttendanceForUsers(emails, event, asStaff, currentUser);
const attendanceModels = await this.attendanceService.submitAttendanceForUsers(emails, event, asStaff, currentUser);
const attendances = attendanceModels.map(attendanceModel => attendanceModel.getPublicAttendance());
return { error: null, attendances };
}

Expand Down
15 changes: 10 additions & 5 deletions api/controllers/AttendanceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ export class AttendanceController {
async getAttendancesForEvent(@Params() params: UuidParam,
@AuthenticatedUser() user: UserModel): Promise<GetAttendancesForEventResponse> {
if (!PermissionsService.canSeeEventAttendances(user)) throw new ForbiddenError();
const attendances = await this.attendanceService.getAttendancesForEvent(params.uuid);
const attendanceModels = await this.attendanceService.getAttendancesForEvent(params.uuid);
const attendances = attendanceModels.map((attendance) => attendance.getPublicAttendance());
return { error: null, attendances };
}

@UseBefore(UserAuthentication)
@Get()
async getAttendancesForCurrentUser(@AuthenticatedUser() user: UserModel): Promise<GetAttendancesForUserResponse> {
const attendances = await this.attendanceService.getAttendancesForCurrentUser(user);
const attendanceModels = await this.attendanceService.getAttendancesForCurrentUser(user);
const attendances = attendanceModels.map((attendance) => attendance.getPublicAttendance());
return { error: null, attendances };
}

Expand All @@ -43,23 +45,26 @@ export class AttendanceController {
if (params.uuid === currentUser.uuid) {
return this.getAttendancesForCurrentUser(currentUser);
}
const attendances = await this.attendanceService.getAttendancesForUser(params.uuid);
const attendanceModels = await this.attendanceService.getAttendancesForUser(params.uuid);
const attendances = attendanceModels.map((attendance) => attendance.getPublicAttendance())
return { error: null, attendances };
}

@UseBefore(UserAuthentication)
@Post()
async attendEvent(@Body() body: AttendEventRequest,
@AuthenticatedUser() user: UserModel): Promise<AttendEventResponse> {
const { event } = await this.attendanceService.attendEvent(user, body.attendanceCode, body.asStaff);
const attendanceModel = await this.attendanceService.attendEvent(user, body.attendanceCode, body.asStaff);
const { event } = attendanceModel.getPublicAttendance();
return { error: null, event };
}

@Post('/expressCheckin')
async attendViaExpressCheckin(@Body() body: AttendViaExpressCheckinRequest): Promise<AttendEventResponse> {
body.email = body.email.toLowerCase();
const { email, attendanceCode } = body;
const { event } = await this.attendanceService.attendViaExpressCheckin(attendanceCode, email);
const eventModel = await this.attendanceService.attendViaExpressCheckin(attendanceCode, email);
const { event } = eventModel.getPublicExpressCheckin();
await this.emailService.sendExpressCheckinConfirmation(email, event.title, event.pointValue);
return { error: null, event };
}
Expand Down
29 changes: 15 additions & 14 deletions services/AttendanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { InjectManager } from 'typeorm-typedi-extensions';
import { BadRequestError, ForbiddenError, NotFoundError } from 'routing-controllers';
import { EntityManager } from 'typeorm';
import * as moment from 'moment';
import { ActivityType, PublicAttendance, PublicExpressCheckin, Uuid } from '../types';
import { ActivityType, Uuid } from '../types';
import { Config } from '../config';
import { UserModel } from '../models/UserModel';
import { EventModel } from '../models/EventModel';
import { AttendanceModel } from '../models/AttendanceModel';
import { UserError } from '../utils/Errors';
import Repositories, { TransactionsManager } from '../repositories';
import { Activity, Attendance } from '../types/internal';
import { ExpressCheckinModel } from 'models/ExpressCheckinModel';

@Service()
export default class AttendanceService {
Expand All @@ -20,31 +21,31 @@ export default class AttendanceService {
this.transactions = new TransactionsManager(entityManager);
}

public async getAttendancesForEvent(event: Uuid): Promise<PublicAttendance[]> {
public async getAttendancesForEvent(event: Uuid): Promise<AttendanceModel[]> {
const attendances = await this.transactions.readOnly(async (txn) => Repositories
.attendance(txn)
.getAttendancesForEvent(event));
return attendances.map((attendance) => attendance.getPublicAttendance());
return attendances;
}

public async getAttendancesForCurrentUser(user: UserModel): Promise<PublicAttendance[]> {
public async getAttendancesForCurrentUser(user: UserModel): Promise<AttendanceModel[]> {
const attendances = await this.transactions.readOnly(async (txn) => Repositories
.attendance(txn)
.getAttendancesForUser(user));
return attendances.map((attendance) => attendance.getPublicAttendance());
return attendances;
}

public async getAttendancesForUser(uuid: Uuid): Promise<PublicAttendance[]> {
public async getAttendancesForUser(uuid: Uuid): Promise<AttendanceModel[]> {
return this.transactions.readOnly(async (txn) => {
const user = await Repositories.user(txn).findByUuid(uuid);
if (!user) throw new NotFoundError('User does not exist');
if (!user.isAttendancePublic) throw new ForbiddenError();
const attendances = await Repositories.attendance(txn).getAttendancesForUser(user);
return attendances.map((attendance) => attendance.getPublicAttendance());
return attendances;
});
}

public async attendEvent(user: UserModel, attendanceCode: string, asStaff = false): Promise<PublicAttendance> {
public async attendEvent(user: UserModel, attendanceCode: string, asStaff = false): Promise<AttendanceModel> {
return this.transactions.readWrite(async (txn) => {
const event = await Repositories.event(txn).findByAttendanceCode(attendanceCode);

Expand All @@ -54,7 +55,7 @@ export default class AttendanceService {
if (hasAlreadyAttended) throw new UserError('You have already attended this event');

const attendance = await this.writeEventAttendance(user, event, asStaff, txn);
return attendance.getPublicAttendance();
return attendance;
});
}

Expand Down Expand Up @@ -86,7 +87,7 @@ export default class AttendanceService {
});
}

public async attendViaExpressCheckin(attendanceCode: string, email: string): Promise<PublicExpressCheckin> {
public async attendViaExpressCheckin(attendanceCode: string, email: string): Promise<ExpressCheckinModel> {
return this.transactions.readWrite(async (txn) => {
const event = await Repositories.event(txn).findByAttendanceCode(attendanceCode);

Expand Down Expand Up @@ -117,12 +118,12 @@ export default class AttendanceService {
}

const expressCheckin = await expressCheckinRepository.createExpressCheckin(email, event);
return expressCheckin.getPublicExpressCheckin();
return expressCheckin;
});
}

public async submitAttendanceForUsers(emails: string[], eventUuid: Uuid, asStaff = false,
proxyUser: UserModel): Promise<PublicAttendance[]> {
proxyUser: UserModel): Promise<AttendanceModel[]> {
return this.transactions.readWrite(async (txn) => {
const event = await Repositories.event(txn).findByUuid(eventUuid);
if (!event) throw new NotFoundError('This event doesn\'t exist');
Expand Down Expand Up @@ -181,7 +182,7 @@ export default class AttendanceService {
return Repositories.attendance(txn).writeAttendanceBatch(attendances);
}

public async submitEventFeedback(feedback: string[], eventUuid: Uuid, user: UserModel): Promise<PublicAttendance> {
public async submitEventFeedback(feedback: string[], eventUuid: Uuid, user: UserModel): Promise<AttendanceModel> {
return this.transactions.readWrite(async (txn) => {
const attendanceRepository = Repositories.attendance(txn);

Expand All @@ -206,7 +207,7 @@ export default class AttendanceService {
});
await Repositories.user(txn).addPoints(user, pointsEarned);

return attendanceWithFeedback.getPublicAttendance();
return attendanceWithFeedback;
});
}
}
Loading