Skip to content

Commit

Permalink
Merge pull request #122 from COS301-SE-2023/api_refactor_dev
Browse files Browse the repository at this point in the history
Api refactor dev
  • Loading branch information
DieSeeKat authored Sep 12, 2023
2 parents c6d1583 + fd10de4 commit 414a888
Show file tree
Hide file tree
Showing 27 changed files with 452 additions and 12 deletions.
51 changes: 50 additions & 1 deletion libs/api/core/feature/src/controllers/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ import {
IGetEventFloorlayoutImageRequest,
IDeleteEventImageRequest,
IDeleteEventImageResponse,
IUpdateEventFloorLayoutImgRequest,
IUpdateEventFloorLayoutImgResponse,
IGetEventFloorlayoutImageResponse,
IGetEventStatisticsResponse,
} from '@event-participation-trends/api/event/util';
import {
Body,
Expand Down Expand Up @@ -534,7 +538,7 @@ export class EventController {
async getFloorLayoutImage(
@Req() req: Request,
@Query() query: any
): Promise<IGetEventFloorlayoutImageRequest> {
): Promise<IGetEventFloorlayoutImageResponse> {
const request: any = req;

if (request.user['email'] == undefined || request.user['email'] == null)
Expand All @@ -546,6 +550,51 @@ export class EventController {
return this.eventService.getEventFloorLayoutImage(extractRequest);
}

@Get('getEventStatistics')
@SetMetadata('role', Role.VIEWER)
@UseGuards(JwtGuard,RbacGuard, CsrfGuard)
async getEventStatistics(
@Req() req: Request,
@Query() query: any
): Promise<IGetEventStatisticsResponse> {
const request: any = req;

if (request.user['email'] == undefined || request.user['email'] == null)
throw new HttpException('Bad Request: Manager email not provided', 400);

const extractRequest: IGetEventFloorlayoutImageRequest = {
eventId: query.eventId,
};
return this.eventService.getEventStatistics(extractRequest);
}

@Post('updateEventFloorlayoutImage')
@SetMetadata('role',Role.MANAGER)
@UseGuards(JwtGuard, RbacGuard, CsrfGuard)
async updateEventFloorlayoutImage(
@Req() req: Request,
@Body() requestBody: IUpdateEventFloorLayoutImgRequest,
): Promise<IUpdateEventFloorLayoutImgResponse> {
const request: any = req;

if(!requestBody.imageId)
throw new HttpException("Bad Request: imageId not provided", 400);

if(!requestBody.eventId)
throw new HttpException("Bad Request: eventId not provided", 400);

const extractRequest: IUpdateEventFloorLayoutImgRequest = {
eventId: requestBody.eventId,
imageId: requestBody.imageId,
managerEmail: request.user['email'],
imgBase64: requestBody.imgBase64,
imageObj: requestBody.imageObj,
imageScale: requestBody.imageScale,
imageType: requestBody.imageType,
};
return this.eventService.updateEventFloorLayoutImage(extractRequest);
}

}

function computePreviousDayDate(): Date{
Expand Down
1 change: 1 addition & 0 deletions libs/api/email/util/src/enums/email-subjects.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export enum EmailSubject{
ROLE_CHANGED= "Event Participation Trends: Role Change",
EVENT_DETAILS_UPDATED= "Event Participation Trends: Event Details Updated",
EVENT_DELETED= "Event Participation Trends: Event Deleted",
EVENT_FLOORLAYOUT_IMAGE_UPDATED = "Event Participation Trends: Event Floorlayout image Updated",
}
24 changes: 24 additions & 0 deletions libs/api/event/data-access/src/event.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,28 @@ export class EventRepository {
return await this.eventModel.find(
{Manager: {$eq: managerID}}).select("Category").distinct("Category");
}

async updateEventFloorlayoutImageimgBase64(imageId: Types.ObjectId, imgBase64: string){
return await this.imageModel.updateOne(
{_id :{$eq: imageId}},
{$set: {imageBase64 :imgBase64}})
}

async updateEventFloorlayoutImageimageObj(imageId: Types.ObjectId, imageObj: string){
return await this.imageModel.updateOne(
{_id :{$eq: imageId}},
{$set: {imageObj :imageObj}})
}

async updateEventFloorlayoutImageimageScale(imageId: Types.ObjectId, imageScale: number){
return await this.imageModel.updateOne(
{_id :{$eq: imageId}},
{$set: {imageScale :imageScale}})
}

async updateEventFloorlayoutImageimageType(imageId: Types.ObjectId, imageType: string){
return await this.imageModel.updateOne(
{_id :{$eq: imageId}},
{$set: {imageType :imageType}})
}
}
3 changes: 2 additions & 1 deletion libs/api/event/feature/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export * from './remove-event-from-viewers.handler';
export * from './upload-image.handler';
export * from './add-image-to-event.handler';
export * from './delete-event-image.handler';
export * from './remove-image-from-event.handler';
export * from './remove-image-from-event.handler';
export * from './update-event-floorlayout.handler';
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { UpdateEventFloorLayoutImgCommand, IUpdateFloorLayoutImage, IUpdateEventFloorLayoutImgResponse } from '@event-participation-trends/api/event/util';
import { CommandHandler, EventPublisher, ICommandHandler } from '@nestjs/cqrs';
import { Status } from'@event-participation-trends/api/user/util';
import { UpdateFloorLayoutImage } from '../models';
import { Types } from 'mongoose';
import { EventRepository } from '@event-participation-trends/api/event/data-access';
import { UserRepository } from '@event-participation-trends/api/user/data-access';
import { HttpException } from '@nestjs/common';

@CommandHandler(UpdateEventFloorLayoutImgCommand)
export class UpdateEventFloorLayoutImgHandler implements ICommandHandler<UpdateEventFloorLayoutImgCommand, IUpdateEventFloorLayoutImgResponse> {
constructor(
private readonly publisher: EventPublisher,
private readonly eventRepository: EventRepository,
private readonly userRepository: UserRepository,
) {}

async execute(command: UpdateEventFloorLayoutImgCommand) {
console.log(`${UpdateEventFloorLayoutImgHandler.name}`);

const request = command.request;

const eventIdObj = <Types.ObjectId> <unknown> request.eventId;
const imageIdObj = <Types.ObjectId> <unknown> request.imageId;

const eventDoc = await this.eventRepository.getEventById(eventIdObj);
const userDoc = await this.userRepository.getUser(request.managerEmail || "");
const imageDoc = await this.eventRepository.getImageById(imageIdObj);

if(eventDoc && userDoc && !eventDoc[0].Manager?.equals(userDoc[0]._id))
throw new HttpException(`Bad Request: Manager with Email ${request.managerEmail} does not manage Event with id ${request.eventId}`, 400);

if(imageDoc && imageDoc[0].eventId && !eventDoc[0]._id?.equals(imageDoc[0].eventId))
throw new HttpException(`Bad Request: Manager with Email ${request.managerEmail} does not manage photo with id ${request.imageId}`, 400);

const data: IUpdateFloorLayoutImage={
eventId: eventIdObj,
imageId: imageIdObj,
managerEmail: request.managerEmail,
imgBase64: request.imgBase64,
imageObj: request.imageObj,
imageScale: request.imageScale,
imageType: request.imageType,
}

const event = this.publisher.mergeObjectContext(UpdateFloorLayoutImage.fromData(data));
event.update();
event.commit();

return { status : Status.SUCCESS };
}
}
6 changes: 6 additions & 0 deletions libs/api/event/feature/src/event.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
AddImageToEventHandler,
RemoveEventImageHandler,
DeleteEventImageHandler,
UpdateEventFloorLayoutImgHandler,
} from './commands';

import {
Expand All @@ -35,6 +36,7 @@ import {
GetFloorplanBoundariesQueryHandler,
GetAllActiveEventsHandler,
GetEventFloorlayoutImageHandler,
GetEventStatisticsHandler,
} from './queries';

import {
Expand All @@ -54,6 +56,7 @@ import {
AddImageToEventEventHandler,
RemoveEventImageEventHandler,
DeleteEventImageEventHandler,
UpdateEventFloorLayoutImgEventHandler,
} from './events';

import { GetAllViewRequestsHandler } from './queries/get-all-view-requests.handler';
Expand All @@ -77,6 +80,7 @@ export const CommandHandlers = [
AddImageToEventHandler,
RemoveEventImageHandler,
DeleteEventImageHandler,
UpdateEventFloorLayoutImgHandler,
]

export const EventHandlers = [
Expand All @@ -96,6 +100,7 @@ export const EventHandlers = [
AddImageToEventEventHandler,
RemoveEventImageEventHandler,
DeleteEventImageEventHandler,
UpdateEventFloorLayoutImgEventHandler,
];

export const QueryHandlers = [
Expand All @@ -111,6 +116,7 @@ export const QueryHandlers = [
GetFloorplanBoundariesQueryHandler,
GetAllActiveEventsHandler,
GetEventFloorlayoutImageHandler,
GetEventStatisticsHandler,
];


Expand Down
17 changes: 16 additions & 1 deletion libs/api/event/feature/src/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ import {
IDeleteEventImageRequest,
DeleteEventImageCommand,
IDeleteEventImageResponse,
IGetEventStatisticsRequest,
GetEventStatisticsQuery,
IUpdateEventFloorLayoutImgRequest,
UpdateEventFloorLayoutImgCommand,
IUpdateEventFloorLayoutImgResponse,
IGetEventStatisticsResponse,
IGetEventFloorlayoutImageResponse,
} from '@event-participation-trends/api/event/util';
import { Injectable } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
Expand Down Expand Up @@ -184,10 +191,18 @@ export class EventService {
}

async getEventFloorLayoutImage(request: IGetEventFloorlayoutImageRequest) {
return await this.queryBus.execute<GetEventFloorlayoutImageQuery, IGetEventFloorlayoutImageRequest>(new GetEventFloorlayoutImageQuery(request));
return await this.queryBus.execute<GetEventFloorlayoutImageQuery, IGetEventFloorlayoutImageResponse>(new GetEventFloorlayoutImageQuery(request));
}

async deleteImage(request: IDeleteEventImageRequest) {
return await this.commandBus.execute<DeleteEventImageCommand, IDeleteEventImageResponse>(new DeleteEventImageCommand(request));
}

async getEventStatistics(request: IGetEventStatisticsRequest) {
return await this.queryBus.execute<GetEventStatisticsQuery, IGetEventStatisticsResponse>(new GetEventStatisticsQuery(request));
}

async updateEventFloorLayoutImage(request: IUpdateEventFloorLayoutImgRequest) {
return await this.commandBus.execute<UpdateEventFloorLayoutImgCommand, IUpdateEventFloorLayoutImgResponse>(new UpdateEventFloorLayoutImgCommand(request));
}
}
3 changes: 2 additions & 1 deletion libs/api/event/feature/src/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export * from './remove-event-from-viewers.handler';
export * from './upload-image-event.handler';
export * from './add-image-to-event.event';
export * from './delete-event-image.handler';
export * from './remove-image-from-event.handler';
export * from './remove-image-from-event.handler';
export * from './update-event-floorlayout.handler';
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { IUpdateFloorLayoutImage, UpdateEventFloorLayoutImgEvent } from '@event-participation-trends/api/event/util';
import { EventRepository } from '@event-participation-trends/api/event/data-access';
import { IEventHandler, EventsHandler } from '@nestjs/cqrs';
import { EmailService } from '@event-participation-trends/api/email/feature'
import { EmailContent, EmailSubject} from '@event-participation-trends/api/email/util';
import { UserRepository } from '@event-participation-trends/api/user/data-access';

@EventsHandler(UpdateEventFloorLayoutImgEvent)
export class UpdateEventFloorLayoutImgEventHandler implements IEventHandler<UpdateEventFloorLayoutImgEvent> {
constructor(
private readonly eventRepository: EventRepository,
private readonly userRepository: UserRepository,
private readonly emailService: EmailService,
) {}

async handle(event: UpdateEventFloorLayoutImgEvent) {
console.log(`${UpdateEventFloorLayoutImgEventHandler.name}`);

const request = <IUpdateFloorLayoutImage> event.event;

if(request.eventId && request.imageId){
let emailContent= "Updated Event Floorlayout Image: "+ EmailContent.NEW_LINE;
const eventDoc = await this.eventRepository.getEventById(request.eventId);
emailContent += "Event Name: " + eventDoc[0].Name + EmailContent.NEW_LINE;

if(request.imgBase64){
await this.eventRepository.updateEventFloorlayoutImageimgBase64(request.imageId, request.imgBase64);
emailContent+= "\t imgBase64 has been updated" + EmailContent.NEW_LINE;
}

if(request.imageObj){
await this.eventRepository.updateEventFloorlayoutImageimageObj(request.imageId, request.imageObj);
emailContent+= "\t New imageObj: "+ request.imageObj + EmailContent.NEW_LINE;
}

if(request.imageScale){
await this.eventRepository.updateEventFloorlayoutImageimageScale(request.imageId, request.imageScale);
emailContent+= "\t New imageScale: "+ request.imageScale.toString() + EmailContent.NEW_LINE;
}

if(request.imageType){
await this.eventRepository.updateEventFloorlayoutImageimageType(request.imageId,request.imageType);
emailContent+= "\t New imageType: "+ request.imageType + EmailContent.NEW_LINE;
}

let userDoc;
if(eventDoc[0].Manager != null && eventDoc[0].Manager != undefined)
userDoc = await this.userRepository.getUserById(eventDoc[0].Manager);

if(userDoc != undefined && userDoc != null && userDoc[0])
this.emailService.sendEmail(
userDoc[0]?.Email || "",
EmailSubject.EVENT_FLOORLAYOUT_IMAGE_UPDATED,
emailContent
);

}
}

}
3 changes: 2 additions & 1 deletion libs/api/event/feature/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from './stall.model';
export * from './update-stall.model';
export * from './delete-event.model';
export * from './image.model';
export * from './add-image.model';
export * from './add-image.model';
export * from './update-image-floorlayout.model';
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
IUpdateFloorLayoutImage,
UpdateEventFloorLayoutImgEvent,
} from '@event-participation-trends/api/event/util';
import { Types } from 'mongoose';
import { AggregateRoot } from '@nestjs/cqrs';

export class UpdateFloorLayoutImage extends AggregateRoot implements IUpdateFloorLayoutImage {
constructor(
public eventId: Types.ObjectId | undefined | null,
public imageId: Types.ObjectId | undefined | null,
public managerEmail: string | undefined | null,
public imgBase64: string | undefined | null,
public imageObj: string | undefined | null,
public imageScale: number | undefined | null,
public imageType: string | undefined | null,
){
super();
}

update(){
this.apply(new UpdateEventFloorLayoutImgEvent(this.toJSON()));
}

static fromData(event: IUpdateFloorLayoutImage): UpdateFloorLayoutImage {
const instance = new UpdateFloorLayoutImage(
event.eventId,
event.imageId,
event.managerEmail,
event.imgBase64,
event.imageObj,
event.imageScale,
event.imageType,
);
return instance;
}

toJSON(): IUpdateFloorLayoutImage {
return {
eventId: this.eventId,
imageId: this.imageId,
managerEmail: this.managerEmail,
imgBase64: this.imgBase64,
imageObj: this.imageObj,
imageScale: this.imageScale,
imageType: this.imageType,
};
}
}
Loading

0 comments on commit 414a888

Please sign in to comment.