Skip to content

Commit

Permalink
Merge pull request #133 from COS301-SE-2023/api_chat_feature_dev
Browse files Browse the repository at this point in the history
Api chat feature dev
  • Loading branch information
Stefan-vdm authored Sep 27, 2023
2 parents 3bde7ba + 035cc61 commit 922692f
Show file tree
Hide file tree
Showing 26 changed files with 220 additions and 17 deletions.
13 changes: 11 additions & 2 deletions apps/api/src/app/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { Socket, Server } from 'socket.io';
import { SocketServiceService as SocketService } from './socket-service.service';
import { Logger } from '@nestjs/common';
import { types as MediasoupTypes } from 'mediasoup';
import { EventService } from '@event-participation-trends/api/event/feature';

@WebSocketGateway({path: '/api/ws'})
export class SocketGateway implements OnGatewayDisconnect{
@WebSocketServer()
server: Server;
constructor(private readonly appService: SocketService) {
constructor(
private readonly appService: SocketService,
private readonly eventService: EventService,
) {
}

handleDisconnect(client: any) {
Expand All @@ -33,7 +37,12 @@ export class SocketGateway implements OnGatewayDisconnect{

@SubscribeMessage('message')
message(client: Socket, payload: any){
Logger.debug(this.appService.users.get(client.id).eventID);
const eventId = this.appService.users.get(client.id).eventID;
Logger.debug(eventId);
this.eventService.addEventChatMessage({
eventId: eventId,
messagePacket: payload,
});
this.server.to(this.appService.users.get(client.id).eventID).emit('message', payload);
}

Expand Down
26 changes: 25 additions & 1 deletion libs/api/core/feature/src/controllers/event.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import {
IUpdateEventFloorLayoutImgResponse,
IGetEventFloorlayoutImageResponse,
IGetEventStatisticsResponse,
IGetEventChatMessagesRequest,
IGetEventChatMessagesResponse,
} from '@event-participation-trends/api/event/util';
import {
Body,
Expand Down Expand Up @@ -593,10 +595,32 @@ export class EventController {
imageType: requestBody.imageType,
};
return this.eventService.updateEventFloorLayoutImage(extractRequest);
}

}

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

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

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

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


function computePreviousDayDate(): Date{
const currentDate = moment();
const dayBefore = currentDate.subtract(1, 'day');
Expand Down
6 changes: 4 additions & 2 deletions libs/api/event/data-access/schemas/event.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import mongoose, { HydratedDocument, Types } from 'mongoose';
import { Stall } from './stall.schema';
import { Sensor } from './sensor.schema';
import { Image } from './image.schema';
import { Position } from '../src/interfaces';
import { Position ,ChatMessage } from '../src/interfaces';

export type EventDocument = HydratedDocument<Event>;

Expand Down Expand Up @@ -52,6 +51,9 @@ export class Event{
@Prop({ type: Boolean })
PublicEvent: boolean | undefined | null;

@Prop( [ChatMessage] )
eventChats: ChatMessage[] | undefined | null;

}

export const EventSchema = SchemaFactory.createForClass(Event);
Expand Down
12 changes: 12 additions & 0 deletions libs/api/event/data-access/src/event.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Event,
Image,
} from '../schemas';
import { Types } from 'mongoose';
import { ChatMessage } from './interfaces';

@Injectable()
export class EventRepository {
Expand All @@ -22,6 +23,17 @@ export class EventRepository {
await this.eventModel.create(event);
}

async createChatMessage(eventId: Types.ObjectId, message: ChatMessage){
await this.eventModel.updateOne(
{ _id: {$eq: eventId}},
{ $addToSet: {eventChats :message}});
}

async getEventChatMessages(eventId: Types.ObjectId){
return await this.eventModel.find(
{_id :{$eq: eventId}}).select("eventChats");
}

async uploadImage(image: Image){
await this.imageModel.create(image);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export interface IChatMessage {
text: string | undefined | null,
timestamp: Date | undefined | null,
user: {
id: string | undefined | null,
fullname: string | undefined | null,
profilePic: string | undefined | null,
role: string | undefined | null,
},
};

export class ChatMessage {
text: string | undefined | null;
timestamp: Date | undefined | null;
user: {
id: string | undefined | null;
fullname: string | undefined | null;
profilePic: string | undefined | null;
role: string | undefined | null;
} | undefined | null;
};
3 changes: 2 additions & 1 deletion libs/api/event/data-access/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './position.interface';
export * from './position.interface';
export * from './chat-message.interface';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AddChatMessageCommand } from '@event-participation-trends/api/event/util';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { EventRepository } from '@event-participation-trends/api/event/data-access';
import { Types } from 'mongoose';

@CommandHandler(AddChatMessageCommand)
export class AddChatMessageHandler implements ICommandHandler<AddChatMessageCommand, void> {
constructor(
private readonly eventRepository: EventRepository,

) {}

async execute(command: AddChatMessageCommand) {
console.log(`${AddChatMessageHandler.name}`);

const request = command.request;

if(request.messagePacket)
this.eventRepository.createChatMessage(
<Types.ObjectId> <unknown> request.eventId,
request.messagePacket
);
}
}
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 @@ -16,4 +16,5 @@ 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 './update-event-floorlayout.handler';
export * from './update-event-floorlayout.handler';
export * from './add-event-chat-message.handler';
4 changes: 4 additions & 0 deletions libs/api/event/feature/src/event.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
RemoveEventImageHandler,
DeleteEventImageHandler,
UpdateEventFloorLayoutImgHandler,
AddChatMessageHandler,
} from './commands';

import {
Expand All @@ -37,6 +38,7 @@ import {
GetAllActiveEventsHandler,
GetEventFloorlayoutImageHandler,
GetEventStatisticsHandler,
GetEventChatMessagesHandler,
} from './queries';

import {
Expand Down Expand Up @@ -81,6 +83,7 @@ export const CommandHandlers = [
RemoveEventImageHandler,
DeleteEventImageHandler,
UpdateEventFloorLayoutImgHandler,
AddChatMessageHandler,
]

export const EventHandlers = [
Expand Down Expand Up @@ -117,6 +120,7 @@ export const QueryHandlers = [
GetAllActiveEventsHandler,
GetEventFloorlayoutImageHandler,
GetEventStatisticsHandler,
GetEventChatMessagesHandler,
];


Expand Down
15 changes: 14 additions & 1 deletion libs/api/event/feature/src/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ import {
IUpdateEventFloorLayoutImgResponse,
IGetEventStatisticsResponse,
IGetEventFloorlayoutImageResponse,
IAddChatMessageRequest,
AddChatMessageCommand,
IGetEventChatMessagesRequest,
IGetEventChatMessagesResponse,
GetEventChatMessagesQuery,
} from '@event-participation-trends/api/event/util';
import { Injectable } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
Expand All @@ -83,7 +88,7 @@ import { CommandBus, QueryBus } from '@nestjs/cqrs';
export class EventService {
constructor(
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus
private readonly queryBus: QueryBus,
) {}

async createEvent(request: ICreateEventRequest) {
Expand Down Expand Up @@ -205,4 +210,12 @@ export class EventService {
async updateEventFloorLayoutImage(request: IUpdateEventFloorLayoutImgRequest) {
return await this.commandBus.execute<UpdateEventFloorLayoutImgCommand, IUpdateEventFloorLayoutImgResponse>(new UpdateEventFloorLayoutImgCommand(request));
}

async addEventChatMessage(request: IAddChatMessageRequest){
return await this.commandBus.execute<AddChatMessageCommand, void>(new AddChatMessageCommand(request));
}

async getEventChats(request: IGetEventChatMessagesRequest) {
return await this.queryBus.execute<GetEventChatMessagesQuery, IGetEventChatMessagesResponse>(new GetEventChatMessagesQuery(request));
}
}
3 changes: 2 additions & 1 deletion libs/api/event/feature/src/events/create-event.handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CreateEventEvent, Position } from '@event-participation-trends/api/event/util';
import { IEventHandler, EventsHandler } from '@nestjs/cqrs';
import { IEvent } from "@event-participation-trends/api/event/util";
import { EventRepository } from '@event-participation-trends/api/event/data-access';
import { ChatMessage, EventRepository } from '@event-participation-trends/api/event/data-access';
import { Types } from 'mongoose';
import { EmailService } from '@event-participation-trends/api/email/feature'
import { EmailContent, EmailSubject} from '@event-participation-trends/api/email/util';
Expand Down Expand Up @@ -37,6 +37,7 @@ export class CreateEventEventHandler implements IEventHandler<CreateEventEvent>
Requesters: new Array<Types.ObjectId>(),
Viewers: ViewersArr,
PublicEvent: event.event.PublicEvent,
eventChats: Array<ChatMessage>(),
}

await this.eventRepository.createEvent(eventToCreate);
Expand Down
8 changes: 6 additions & 2 deletions libs/api/event/feature/src/models/event.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
IEvent,
CreateEventEvent,
IFloorLayout,
IFloorLayout,
ChatMessage,
} from '@event-participation-trends/api/event/util';
import {
IStall,
Expand All @@ -27,6 +28,7 @@ export class Event extends AggregateRoot implements IEvent {
public Requesters?: Types.ObjectId[] | undefined | null,
public Viewers?: Types.ObjectId[] | undefined | null,
public PublicEvent?: boolean | undefined | null,
public eventChats?: ChatMessage[] | undefined | null,
){
super();
}
Expand All @@ -50,7 +52,8 @@ export class Event extends AggregateRoot implements IEvent {
event.Manager,
event.Requesters,
event.Viewers,
event.PublicEvent
event.PublicEvent,
event.eventChats,
);
return instance;
}
Expand All @@ -71,6 +74,7 @@ export class Event extends AggregateRoot implements IEvent {
Requesters: this.Requesters,
Viewers: this.Viewers,
PublicEvent: this.PublicEvent,
eventChats: this.eventChats,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { EventRepository } from '@event-participation-trends/api/event/data-access';
import { ChatMessage, GetEventChatMessagesQuery, IGetEventChatMessagesResponse } from '@event-participation-trends/api/event/util';
import { QueryHandler, IQueryHandler } from '@nestjs/cqrs';
import { Types } from 'mongoose';

@QueryHandler(GetEventChatMessagesQuery)
export class GetEventChatMessagesHandler implements IQueryHandler<GetEventChatMessagesQuery, IGetEventChatMessagesResponse> {
constructor(
private readonly eventRepository: EventRepository,
) {}

async execute(query: GetEventChatMessagesQuery) {
console.log(`${GetEventChatMessagesHandler.name}`);
const request = query.request;
const eventIdObj = <Types.ObjectId> <unknown> request.eventId;

const chats = new Array<ChatMessage>();
const chatDocs = await this.eventRepository.getEventChatMessages(eventIdObj);
chatDocs[0]?.eventChats?.forEach(doc=>{
chats.push({
text: doc.text,
timestamp: doc.timestamp,
user: doc.user,
})
})

return {messages :<ChatMessage[]><unknown> chats};
}

}
3 changes: 2 additions & 1 deletion libs/api/event/feature/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './get-managed-event-categories.handler';
export * from './get-floorplan-boundaries.handler';
export * from './get-all-active-events.handler';
export * from './get-event-floorlayout-image.handler';
export * from './get-event-statistics.handler';
export * from './get-event-statistics.handler';
export * from './get-event-chat-messages.handler';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IAddChatMessageRequest} from '../requests';

export class AddChatMessageCommand {
constructor(public readonly request: IAddChatMessageRequest) {}
}
3 changes: 2 additions & 1 deletion libs/api/event/util/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export * from './upload-image.command';
export * from './add-image-to-event.command';
export * from './delete-event-image.command';
export * from './remove-image-from-event.command';
export * from './update-event-floorlayout-image.command';
export * from './update-event-floorlayout-image.command';
export * from './add-event-chat-message.command';
22 changes: 22 additions & 0 deletions libs/api/event/util/src/interfaces/chat-message.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export interface IChatMessage {
text: string | undefined | null,
timestamp: Date | undefined | null,
user: {
id: string | undefined | null,
fullname: string | undefined | null,
profilePic: string | undefined | null,
role: string | undefined | null,
},
};

export class ChatMessage {
text: string | undefined | null;
timestamp: Date | undefined | null;
user: {
id: string | undefined | null;
fullname: string | undefined | null;
profilePic: string | undefined | null;
role: string | undefined | null;
} | undefined | null;
};
Loading

0 comments on commit 922692f

Please sign in to comment.