Skip to content

Commit 1a61971

Browse files
Fix bug for collab (#92)
* Fix bug - Previously, on successful forfeit call, we still set a new entry in the yjs doc. This causes the yjs doc to be reintialise when both users has already forfeited * Refactoring in Collaboration Service: - Refactor comments in collab service - Move interface from controller folder to types folder - Refactor README.md - Remove unused variable in queue: MATCH_FOUND * Update message for when user agrees to submit --------- Co-authored-by: KhoonSun47 <[email protected]>
1 parent 82f1389 commit 1a61971

File tree

14 files changed

+133
-67
lines changed

14 files changed

+133
-67
lines changed

frontend/src/app/collaboration/forfeit-dialog/forfeit-dialog.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export class ForfeitDialogComponent implements OnInit {
6464
if (userId) {
6565
this.collabService.forfeit(this.roomId).subscribe({
6666
next: () => {
67-
this.yforfeit.set(userId, true);
67+
if (this.yforfeit.size == 0) {
68+
this.yforfeit.set(userId, true);
69+
}
6870
this.message = 'You have forfeited. \n\n Redirecting you to homepage...';
6971
this.isForfeit = true;
7072
this.hideButtons = true;

frontend/src/app/collaboration/submit-dialog/submit-dialog.component.html

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ <h2 class="mt-0 mb-0">Submit</h2>
1616
</div>
1717
<p-progressBar mode="indeterminate" [style]="{ height: '6px' }" />
1818
<ng-template pTemplate="footer">
19-
<div class="flex w-full justify-content-end gap-4">
20-
<p-button type="button" label="Cancel" severity="danger" [outlined]="true" (click)="cancel()" />
21-
@if (!isInitiator || (numForfeit !== 2 && numUniqueUsers === 1)) {
22-
<p-button type="button" label="Agree" severity="success" [outlined]="true" (click)="agreeSubmit()" />
23-
}
24-
</div>
19+
@if (!isConsent) {
20+
<div class="flex w-full justify-content-end gap-4">
21+
<p-button type="button" label="Cancel" severity="danger" [outlined]="true" (click)="cancel()" />
22+
@if (!isInitiator || (numForfeit !== 2 && numUniqueUsers === 1)) {
23+
<p-button
24+
type="button"
25+
label="Agree"
26+
severity="success"
27+
[outlined]="true"
28+
(click)="agreeSubmit()" />
29+
}
30+
</div>
31+
}
2532
</ng-template>
2633
</p-dialog>

frontend/src/app/collaboration/submit-dialog/submit-dialog.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class SubmitDialogComponent implements AfterViewInit {
3131
ysubmit!: Y.Map<boolean>;
3232
yforfeit!: Y.Map<boolean>;
3333
userId!: string;
34+
isConsent = false;
3435

3536
constructor(
3637
@Inject(DOCUMENT) private document: Document,
@@ -112,18 +113,19 @@ export class SubmitDialogComponent implements AfterViewInit {
112113
}
113114

114115
checkVoteOutcome(counter: number) {
115-
const isConsent = counter == this.numUniqueUsers;
116+
this.isConsent = counter == this.numUniqueUsers;
116117

117-
if (!isConsent) {
118+
if (!this.isConsent) {
118119
return;
119120
}
120121

122+
this.message = 'Successfully submitted. \n\n Redirecting you to homepage...';
123+
121124
this.successfulSubmit.emit();
122125

123126
if (this.isInitiator) {
124127
this.collabService.closeRoom(this.roomId).subscribe({
125128
next: () => {
126-
this.message = 'Successfully submitted. \n\n Redirecting you to homepage...';
127129
setTimeout(() => {
128130
this.router.navigate(['/home']);
129131
}, 1500);

services/collaboration/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ curl -X PATCH http://localhost:8080/api/collaboration/room/6724e9d892fb3e9f04c2e
346346
## Documentation on Queue (RabbitMQ)
347347

348348
The collaboration service uses RabbitMQ as a message broker to facilitate communication between microservices (such as
349-
the `matching service` and `collaboration service`) in an asynchronous manner. The system consists of a consumer and four
349+
the `Matching Service`, `Collaboration Service` and `Question Service`) in an asynchronous manner. The system consists
350+
of a consumer and four
350351
producers:
351352

352353
### Queues Used
@@ -365,9 +366,9 @@ The producer will send a message to the `COLLAB_CREATED` queue when a collaborat
365366

366367
- **Queue**: `COLLAB_CREATED`
367368
- **Data in the Message**:
368-
- `requestId1` (Required) - The request ID of the first user.
369-
- `requestId2` (Required) - The request ID of the second user.
370-
- `collabId` (Required) - The ID of the collaboration room.
369+
- `requestId1` (Required) - The request ID of the first user.
370+
- `requestId2` (Required) - The request ID of the second user.
371+
- `collabId` (Required) - The ID of the collaboration room.
371372

372373
```json
373374
{
@@ -381,9 +382,9 @@ The producer will send a message to the `MATCH_FAILED` queue when a collaboratio
381382

382383
- **Queue**: `MATCH_FAILED`
383384
- **Data Produced**
384-
- `requestId1` (Required) - The first request ID associated with the match failure.
385-
- `requestId2` (Required) - The second request ID associated with the match failure.
386-
- `reason` (Required) - The error encountered.
385+
- `requestId1` (Required) - The first request ID associated with the match failure.
386+
- `requestId2` (Required) - The second request ID associated with the match failure.
387+
- `reason` (Required) - The error encountered.
387388

388389
```json
389390
{
@@ -420,17 +421,19 @@ The producer will send a message to the `CREATE_HISTORY` queue when a collaborat
420421
"topics": [ "Algorithms" ],
421422
"difficulty": "Easy",
422423
"_id": "671a0615dc63fe2d5f3bbae5"
423-
},
424+
}
424425
},
425426
```
426427

427-
The producer will send a message to the `UPDATE_HISTORY` queue when a user forfeits or completes a collaborative session.
428+
The producer will send a message to the `UPDATE_HISTORY` queue when a user forfeits or completes a collaborative
429+
session.
428430

429431
- **Queue**: `UPDATE_HISTORY`
430432
- **Data Produced**
431433
- `roomId` - The ID of the collaboration room.
432434
- `userId` - The user associated with the update.
433-
- `status` - The new status associated with the collaboration room. It may be `"IN_PROGRESS"`, `"FORFEITED"`, or `"COMPLETED"`.
435+
- `status` - The new status associated with the collaboration room. It may be `"IN_PROGRESS"`, `"FORFEITED"`,
436+
or `"COMPLETED"`.
434437

435438
```json
436439
{
@@ -449,9 +452,9 @@ matched.
449452

450453
- **Queue**: `QUESTION_FOUND`
451454
- **Data in the Message**:
452-
- `user1` (Required) - The details of the first user.
453-
- `user2` (Required) - The details of the second user.
454-
- `question` (Required) - The question assigned to the users.
455+
- `user1` (Required) - The details of the first user.
456+
- `user2` (Required) - The details of the second user.
457+
- `question` (Required) - The question assigned to the users.
455458

456459
```json
457460
{

services/collaboration/src/controllers/roomController.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,10 @@ import {
99
updateRoomUserStatus,
1010
} from '../services/mongodbService';
1111
import { handleHttpNotFound, handleHttpSuccess, handleHttpServerError, handleHttpBadRequest } from '../utils/helper';
12-
import { Room } from './types';
12+
import { Room, Question } from '../types/collab';
1313
import { produceUpdateHistory } from '../events/producer';
1414
import { HistoryStatus } from '../types/message';
1515

16-
export enum Difficulty {
17-
Easy = 'Easy',
18-
Medium = 'Medium',
19-
Hard = 'Hard',
20-
}
21-
22-
export interface Question {
23-
id: number;
24-
description: string;
25-
difficulty: Difficulty;
26-
title: string;
27-
topics?: string[];
28-
}
29-
3016
/**
3117
* Create a room with users, question details, and Yjs document
3218
* @param user1

services/collaboration/src/controllers/types.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

services/collaboration/src/events/consumer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { createRoomWithQuestion } from '../controllers/roomController';
44
import { QuestionFoundEvent } from '../types/event';
55
import { produceCollabCreated, produceCollabCreateFailedEvent, produceCreateHistory } from './producer';
66

7+
/**
8+
* Consume the question found event and create a room
9+
* @param message
10+
*/
711
async function consumeQuestionFound(message: QuestionFoundEvent) {
812
console.log('Attempting to create room:', message);
913
const { user1, user2, question } = message;
@@ -27,6 +31,9 @@ async function consumeQuestionFound(message: QuestionFoundEvent) {
2731
}
2832
}
2933

34+
/**
35+
* Initialize the consumers for the collaboration service
36+
*/
3037
export async function initializeConsumers() {
3138
messageBroker.consume(Queues.QUESTION_FOUND, consumeQuestionFound);
3239
}

services/collaboration/src/events/producer.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { Queues } from './queues';
55

66
const COLLAB_CREATED_ERROR = 'Failed to create room';
77

8+
/**
9+
* Produce a collab created event
10+
* @param requestId1
11+
* @param requestId2
12+
* @param collabId
13+
* @param question
14+
*/
815
export async function produceCollabCreated(
916
requestId1: IdType,
1017
requestId2: IdType,
@@ -15,16 +22,34 @@ export async function produceCollabCreated(
1522
await messageBroker.produce(Queues.COLLAB_CREATED, message);
1623
}
1724

25+
/**
26+
* Produce a collab create failed event
27+
* @param requestId1
28+
* @param requestId2
29+
*/
1830
export async function produceCollabCreateFailedEvent(requestId1: IdType, requestId2: IdType) {
1931
const message: MatchFailedEvent = { requestId1, requestId2, reason: COLLAB_CREATED_ERROR };
2032
await messageBroker.produce(Queues.MATCH_FAILED, message);
2133
}
2234

35+
/**
36+
* Produce a create history event
37+
* @param roomId
38+
* @param user1
39+
* @param user2
40+
* @param question
41+
*/
2342
export async function produceCreateHistory(roomId: IdType, user1: User, user2: User, question: Question) {
2443
const message: CreateHistoryMessage = { roomId, user1, user2, question };
2544
await messageBroker.produce(Queues.CREATE_HISTORY, message);
2645
}
2746

47+
/**
48+
* Produce an update history event
49+
* @param roomId
50+
* @param userId
51+
* @param status
52+
*/
2853
export async function produceUpdateHistory(roomId: IdType, userId: IdType, status: HistoryStatus) {
2954
const message: UpdateHistoryMessage = { roomId, userId, status };
3055
await messageBroker.produce(Queues.UPDATE_HISTORY, message);

services/collaboration/src/events/queues.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Enum for queues
33
*/
44
export enum Queues {
5-
MATCH_FOUND = 'MATCH_FOUND',
65
QUESTION_FOUND = 'QUESTION_FOUND',
76
COLLAB_CREATED = 'COLLAB_CREATED',
87
MATCH_FAILED = 'MATCH_FAILED',

services/collaboration/src/routes/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import { getHealth } from '../controllers';
3+
34
const router = express.Router();
45

56
router.get('/ht', getHealth);

0 commit comments

Comments
 (0)