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

Moving most public types to API layer #452

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
2 changes: 1 addition & 1 deletion api/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ export class AdminController {
async getAllUsersWithAccessLevels(@AuthenticatedUser() user: UserModel): Promise<GetAllUserAccessLevelsResponse> {
if (!PermissionsService.canSeeAllUserAccessLevels(user)) throw new ForbiddenError();
const users = await this.userAccountService.getAllFullUserProfiles();
return { error: null, users };
return { error: null, users: users.map((u) => u.getFullUserProfile()) };
}
}
9 changes: 5 additions & 4 deletions api/controllers/AttendanceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class AttendanceController {
@AuthenticatedUser() user: UserModel): Promise<GetAttendancesForEventResponse> {
if (!PermissionsService.canSeeEventAttendances(user)) throw new ForbiddenError();
const attendances = await this.attendanceService.getAttendancesForEvent(params.uuid);
return { error: null, attendances };
return { error: null, attendances: attendances.map((attendance) => attendance.getPublicAttendance()) };
}

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

@UseBefore(UserAuthentication)
Expand All @@ -44,14 +44,15 @@ export class AttendanceController {
return this.getAttendancesForCurrentUser(currentUser);
}
const attendances = await this.attendanceService.getAttendancesForUser(params.uuid);
return { error: null, attendances };
return { error: null, attendances: attendances.map((attendance) => attendance.getPublicAttendance()) };
}

@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 attendance = await this.attendanceService.attendEvent(user, body.attendanceCode, body.asStaff);
const { event } = attendance.getPublicAttendance();
return { error: null, event };
}

Expand Down
14 changes: 7 additions & 7 deletions api/controllers/EventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class EventController {
@AuthenticatedUser() user: UserModel): Promise<GetPastEventsResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const events = await this.eventService.getPastEvents(canSeeAttendanceCode, options);
return { error: null, events };
return { error: null, events: events.map((e) => e.getPublicEvent(canSeeAttendanceCode)) };
}

@UseBefore(OptionalUserAuthentication)
Expand All @@ -57,7 +57,7 @@ export class EventController {
@AuthenticatedUser() user: UserModel): Promise<GetFutureEventsResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const events = await this.eventService.getFutureEvents(canSeeAttendanceCode, options);
return { error: null, events };
return { error: null, events: events.map((e) => e.getPublicEvent(canSeeAttendanceCode)) };
}

@UseBefore(UserAuthentication)
Expand All @@ -69,7 +69,7 @@ export class EventController {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
const cover = await this.storageService.upload(file, MediaType.EVENT_COVER, params.uuid);
const event = await this.eventService.updateByUuid(params.uuid, { cover });
return { error: null, event };
return { error: null, event: event.getPublicEvent(true) };
}

@UseBefore(UserAuthentication)
Expand All @@ -87,7 +87,7 @@ export class EventController {
@AuthenticatedUser() user: UserModel): Promise<GetOneEventResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const event = await this.eventService.findByUuid(params.uuid, canSeeAttendanceCode);
return { error: null, event };
return { error: null, event: event.getPublicEvent(canSeeAttendanceCode) };
}

@UseBefore(UserAuthentication)
Expand All @@ -97,7 +97,7 @@ export class EventController {
@AuthenticatedUser() user: UserModel): Promise<PatchEventResponse> {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
const event = await this.eventService.updateByUuid(params.uuid, patchEventRequest.event);
return { error: null, event };
return { error: null, event: event.getPublicEvent(true) };
}

@UseBefore(UserAuthentication)
Expand All @@ -115,7 +115,7 @@ export class EventController {
Promise<GetAllEventsResponse> {
const canSeeAttendanceCode = !!user && PermissionsService.canEditEvents(user);
const events = await this.eventService.getAllEvents(canSeeAttendanceCode, options);
return { error: null, events };
return { error: null, events: events.map((e) => e.getPublicEvent(canSeeAttendanceCode)) };
}

@UseBefore(UserAuthentication)
Expand All @@ -124,6 +124,6 @@ export class EventController {
@AuthenticatedUser() user: UserModel): Promise<CreateEventResponse> {
if (!PermissionsService.canEditEvents(user)) throw new ForbiddenError();
const event = await this.eventService.create(createEventRequest.event);
return { error: null, event };
return { error: null, event: event.getPublicEvent() };
}
}
6 changes: 3 additions & 3 deletions api/controllers/FeedbackController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export class FeedbackController {
@AuthenticatedUser() user: UserModel): Promise<GetFeedbackResponse> {
const canSeeAllFeedback = PermissionsService.canSeeAllFeedback(user);
const feedback = await this.feedbackService.getFeedback(canSeeAllFeedback, user, options);
return { error: null, feedback };
return { error: null, feedback: feedback.map((fb) => fb.getPublicFeedback()) };
}

@Post()
async submitFeedback(@Body() submitFeedbackRequest: SubmitFeedbackRequest,
@AuthenticatedUser() user: UserModel): Promise<SubmitFeedbackResponse> {
if (!PermissionsService.canSubmitFeedback(user)) throw new ForbiddenError();
const feedback = await this.feedbackService.submitFeedback(user, submitFeedbackRequest.feedback);
return { error: null, feedback };
return { error: null, feedback: feedback.getPublicFeedback() };
}

@Patch('/:uuid')
Expand All @@ -44,6 +44,6 @@ export class FeedbackController {
@AuthenticatedUser() user: UserModel): Promise<UpdateFeedbackStatusResponse> {
if (!PermissionsService.canSeeAllFeedback(user)) throw new ForbiddenError();
const feedback = await this.feedbackService.updateFeedbackStatus(params.uuid, updateFeedbackStatusRequest.status);
return { error: null, feedback };
return { error: null, feedback: feedback.getPublicFeedback() };
}
}
2 changes: 1 addition & 1 deletion api/controllers/LeaderboardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export class LeaderboardController {
async getLeaderboard(@QueryParams() filters: SlidingLeaderboardQueryParams): Promise<GetLeaderboardResponse> {
const { from, to, offset, limit } = filters;
const leaderboard = await this.userAccountService.getLeaderboard(from, to, offset, limit);
return { error: null, leaderboard };
return { error: null, leaderboard: leaderboard.map((u) => u.getPublicProfile()) };
}
}
10 changes: 5 additions & 5 deletions api/controllers/MerchStoreController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ export class MerchStoreController {
if (!PermissionsService.canAccessMerchStore(user)) throw new ForbiddenError();
const canSeeHiddenItems = PermissionsService.canEditMerchStore(user);
const collection = await this.merchStoreService.findCollectionByUuid(params.uuid, canSeeHiddenItems);
return { error: null, collection };
return { error: null, collection: canSeeHiddenItems ? collection : collection.getPublicMerchCollection() };
}

@Get('/collection')
async getAllMerchCollections(@AuthenticatedUser() user: UserModel): Promise<GetAllMerchCollectionsResponse> {
if (!PermissionsService.canAccessMerchStore(user)) throw new ForbiddenError();
const canSeeInactiveCollections = PermissionsService.canEditMerchStore(user);
const collections = await this.merchStoreService.getAllCollections(canSeeInactiveCollections);
return { error: null, collections };
return { error: null, collections: collections.map((c) => c.getPublicMerchCollection(canSeeInactiveCollections)) };
}

@Post('/collection')
Expand Down Expand Up @@ -153,7 +153,7 @@ export class MerchStoreController {
params.uuid, { uploadedPhoto, position },
);

return { error: null, collectionPhoto };
return { error: null, collectionPhoto: collectionPhoto.getPublicMerchCollectionPhoto() };
}

@UseBefore(UserAuthentication)
Expand Down Expand Up @@ -222,7 +222,7 @@ export class MerchStoreController {
params.uuid, { uploadedPhoto, position },
);

return { error: null, merchPhoto };
return { error: null, merchPhoto: merchPhoto.getPublicMerchItemPhoto() };
}

@UseBefore(UserAuthentication)
Expand All @@ -242,7 +242,7 @@ export class MerchStoreController {
Promise<CreateMerchItemOptionResponse> {
if (!PermissionsService.canEditMerchStore(user)) throw new ForbiddenError();
const option = await this.merchStoreService.createItemOption(params.uuid, createItemOptionRequest.option);
return { error: null, option };
return { error: null, option: option.getPublicMerchItemOption() };
}

@Delete('/option/:uuid')
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export class UserController {
return this.getCurrentUserActivityStream(currentUser);
}
const activityStream = await this.userAccountService.getUserActivityStream(params.uuid);
return { error: null, activity: activityStream };
return { error: null, activity: activityStream.map((activity) => activity.getPublicActivity()) };
}

@Get('/activity')
async getCurrentUserActivityStream(@AuthenticatedUser() user: UserModel):
Promise<GetUserActivityStreamResponse> {
const activityStream = await this.userAccountService.getCurrentUserActivityStream(user.uuid);
return { error: null, activity: activityStream };
return { error: null, activity: activityStream.map((activity) => activity.getPublicActivity()) };
}

@Post('/picture')
Expand Down
20 changes: 10 additions & 10 deletions services/AttendanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,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 +54,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 @@ -181,7 +181,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 +206,7 @@ export default class AttendanceService {
});
await Repositories.user(txn).addPoints(user, pointsEarned);

return attendanceWithFeedback.getPublicAttendance();
return attendanceWithFeedback;
});
}
}
26 changes: 13 additions & 13 deletions services/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InjectManager } from 'typeorm-typedi-extensions';
import { ForbiddenError, NotFoundError } from 'routing-controllers';
import { EntityManager } from 'typeorm';
import { EventModel } from '../models/EventModel';
import { Uuid, PublicEvent, Event, EventSearchOptions } from '../types';
import { Uuid, Event, EventSearchOptions } from '../types';
import Repositories, { TransactionsManager } from '../repositories';
import { UserError } from '../utils/Errors';

Expand All @@ -21,48 +21,48 @@ export default class EventService {
* @param event object with all the properties of the event
* @returns The event that was created
*/
public async create(event: Event): Promise<PublicEvent> {
public async create(event: Event): Promise<EventModel> {
const eventCreated = await this.transactions.readWrite(async (txn) => {
const eventRepository = Repositories.event(txn);
const isUnusedAttendanceCode = await eventRepository.isUnusedAttendanceCode(event.attendanceCode);
if (!isUnusedAttendanceCode) throw new UserError('Attendance code has already been used');
if (event.start > event.end) throw new UserError('Start date after end date');
return eventRepository.upsertEvent(EventModel.create(event));
});
return eventCreated.getPublicEvent();
return eventCreated;
}

public async getAllEvents(canSeeAttendanceCode = false, options: EventSearchOptions): Promise<PublicEvent[]> {
public async getAllEvents(canSeeAttendanceCode = false, options: EventSearchOptions): Promise<EventModel[]> {
const events = await this.transactions.readOnly(async (txn) => Repositories
.event(txn)
.getAllEvents(options));
return events.map((e) => e.getPublicEvent(canSeeAttendanceCode));
return events;
}

public async getPastEvents(canSeeAttendanceCode = false, options: EventSearchOptions): Promise<PublicEvent[]> {
public async getPastEvents(canSeeAttendanceCode = false, options: EventSearchOptions): Promise<EventModel[]> {
options.reverse ??= true;
const events = await this.transactions.readOnly(async (txn) => Repositories
.event(txn)
.getPastEvents(options));
return events.map((e) => e.getPublicEvent(canSeeAttendanceCode));
return events;
}

public async getFutureEvents(canSeeAttendanceCode = false, options: EventSearchOptions): Promise<PublicEvent[]> {
public async getFutureEvents(canSeeAttendanceCode = false, options: EventSearchOptions): Promise<EventModel[]> {
const events = await this.transactions.readOnly(async (txn) => Repositories
.event(txn)
.getFutureEvents(options));
return events.map((e) => e.getPublicEvent(canSeeAttendanceCode));
return events;
}

public async findByUuid(uuid: Uuid, canSeeAttendanceCode = false): Promise<PublicEvent> {
public async findByUuid(uuid: Uuid, canSeeAttendanceCode = false): Promise<EventModel> {
const event = await this.transactions.readOnly(async (txn) => Repositories
.event(txn)
.findByUuid(uuid));
if (!event) throw new NotFoundError('Event not found');
return event.getPublicEvent(canSeeAttendanceCode);
return event;
}

public async updateByUuid(uuid: Uuid, changes: Partial<EventModel>): Promise<PublicEvent> {
public async updateByUuid(uuid: Uuid, changes: Partial<EventModel>): Promise<EventModel> {
const updatedEvent = await this.transactions.readWrite(async (txn) => {
const eventRepository = Repositories.event(txn);
const currentEvent = await eventRepository.findByUuid(uuid);
Expand All @@ -73,7 +73,7 @@ export default class EventService {
}
return eventRepository.upsertEvent(currentEvent, changes);
});
return updatedEvent.getPublicEvent(true);
return updatedEvent;
}

public async deleteByUuid(uuid: Uuid): Promise<void> {
Expand Down
Loading
Loading