Skip to content

Commit

Permalink
Moving most public types to API layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jennymar committed Oct 10, 2024
1 parent 2d73efb commit 26617ae
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 72 deletions.
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((user) => user.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;
});
}
}
24 changes: 12 additions & 12 deletions services/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 7 additions & 8 deletions services/FeedbackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@ export default class FeedbackService {
}

public async getFeedback(canSeeAllFeedback = false, user: UserModel,
options: FeedbackSearchOptions): Promise<PublicFeedback[]> {
options: FeedbackSearchOptions): Promise<FeedbackModel[]> {
return this.transactions.readOnly(async (txn) => {
const feedbackRepository = Repositories.feedback(txn);
if (canSeeAllFeedback) {
return (await feedbackRepository.getAllFeedback(options))
.map((fb) => fb.getPublicFeedback());
return (await feedbackRepository.getAllFeedback(options));
}

const userFeedback = await feedbackRepository.getStandardUserFeedback(user, options);
return userFeedback.map((fb) => fb.getPublicFeedback());
return userFeedback;
});
}

public async submitFeedback(user: UserModel, feedback: Feedback): Promise<PublicFeedback> {
public async submitFeedback(user: UserModel, feedback: Feedback): Promise<FeedbackModel> {
return this.transactions.readWrite(async (txn) => {
const event = await Repositories.event(txn).findByUuid(feedback.event);
if (!event) throw new NotFoundError('Event not found!');
Expand All @@ -45,11 +44,11 @@ export default class FeedbackService {
type: ActivityType.SUBMIT_FEEDBACK,
});
const addedFeedback = await feedbackRepository.upsertFeedback(FeedbackModel.create({ ...feedback, user, event }));
return addedFeedback.getPublicFeedback();
return addedFeedback;
});
}

public async updateFeedbackStatus(uuid: Uuid, status: FeedbackStatus) {
public async updateFeedbackStatus(uuid: Uuid, status: FeedbackStatus): Promise<FeedbackModel> {
return this.transactions.readWrite(async (txn) => {
const feedbackRepository = Repositories.feedback(txn);
const feedback = await feedbackRepository.findByUuid(uuid);
Expand All @@ -64,7 +63,7 @@ export default class FeedbackService {
type: ActivityType.FEEDBACK_ACKNOWLEDGED,
});
const updatedFeedback = await feedbackRepository.upsertFeedback(feedback, { status });
return updatedFeedback.getPublicFeedback();
return updatedFeedback;
});
}
}
Loading

0 comments on commit 26617ae

Please sign in to comment.