diff --git a/api/controllers/AdminController.ts b/api/controllers/AdminController.ts index 6ab76108..1c6b908a 100644 --- a/api/controllers/AdminController.ts +++ b/api/controllers/AdminController.ts @@ -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 }; } diff --git a/api/controllers/AttendanceController.ts b/api/controllers/AttendanceController.ts index ffc3c6c6..5be8b11e 100644 --- a/api/controllers/AttendanceController.ts +++ b/api/controllers/AttendanceController.ts @@ -25,14 +25,16 @@ export class AttendanceController { async getAttendancesForEvent(@Params() params: UuidParam, @AuthenticatedUser() user: UserModel): Promise { 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 { - 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 }; } @@ -43,7 +45,8 @@ 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 }; } @@ -51,7 +54,8 @@ export class AttendanceController { @Post() async attendEvent(@Body() body: AttendEventRequest, @AuthenticatedUser() user: UserModel): Promise { - 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 }; } @@ -59,7 +63,8 @@ export class AttendanceController { async attendViaExpressCheckin(@Body() body: AttendViaExpressCheckinRequest): Promise { 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 }; } diff --git a/services/AttendanceService.ts b/services/AttendanceService.ts index 166ef16f..87226910 100644 --- a/services/AttendanceService.ts +++ b/services/AttendanceService.ts @@ -3,7 +3,7 @@ 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'; @@ -11,6 +11,7 @@ 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 { @@ -20,31 +21,31 @@ export default class AttendanceService { this.transactions = new TransactionsManager(entityManager); } - public async getAttendancesForEvent(event: Uuid): Promise { + public async getAttendancesForEvent(event: Uuid): Promise { 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 { + public async getAttendancesForCurrentUser(user: UserModel): Promise { 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 { + public async getAttendancesForUser(uuid: Uuid): Promise { 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 { + public async attendEvent(user: UserModel, attendanceCode: string, asStaff = false): Promise { return this.transactions.readWrite(async (txn) => { const event = await Repositories.event(txn).findByAttendanceCode(attendanceCode); @@ -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; }); } @@ -86,7 +87,7 @@ export default class AttendanceService { }); } - public async attendViaExpressCheckin(attendanceCode: string, email: string): Promise { + public async attendViaExpressCheckin(attendanceCode: string, email: string): Promise { return this.transactions.readWrite(async (txn) => { const event = await Repositories.event(txn).findByAttendanceCode(attendanceCode); @@ -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 { + proxyUser: UserModel): Promise { return this.transactions.readWrite(async (txn) => { const event = await Repositories.event(txn).findByUuid(eventUuid); if (!event) throw new NotFoundError('This event doesn\'t exist'); @@ -181,7 +182,7 @@ export default class AttendanceService { return Repositories.attendance(txn).writeAttendanceBatch(attendances); } - public async submitEventFeedback(feedback: string[], eventUuid: Uuid, user: UserModel): Promise { + public async submitEventFeedback(feedback: string[], eventUuid: Uuid, user: UserModel): Promise { return this.transactions.readWrite(async (txn) => { const attendanceRepository = Repositories.attendance(txn); @@ -206,7 +207,7 @@ export default class AttendanceService { }); await Repositories.user(txn).addPoints(user, pointsEarned); - return attendanceWithFeedback.getPublicAttendance(); + return attendanceWithFeedback; }); } }