From 5c14e7d57c2242986d18f665810b89357738b7f1 Mon Sep 17 00:00:00 2001 From: Kervyn <73232587+kervyntan@users.noreply.github.com> Date: Wed, 13 Nov 2024 20:23:25 +0800 Subject: [PATCH] Added API to allow user to cancel match request --- message-queue/src/index.ts | 14 ++++- peer-prep-fe/src/app/models/match.model.ts | 7 +++ .../match-modal/match-modal.component.ts | 3 +- peer-prep-fe/src/services/match.service.ts | 63 +++++++++++-------- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/message-queue/src/index.ts b/message-queue/src/index.ts index 0051187e38..b266213e07 100644 --- a/message-queue/src/index.ts +++ b/message-queue/src/index.ts @@ -251,7 +251,19 @@ app.post("/match", async (req: Request, res: Response) => { }) app.post("/match/cancel", async (req: Request, res: Response) => { - + const user_id = req.body.user_id; + if (user_id) { + delete waitingUsers[user_id] + await db.collection("usersQueue").deleteOne({ user_id: user_id }) + + res.status(200).json({ + message: "User successfully removed from waiting queue." + }) + } else { + res.status(400).json({ + message: "Invalid user_id provided. Please try again." + }) + } }) const port = process.env.PORT || 3002; diff --git a/peer-prep-fe/src/app/models/match.model.ts b/peer-prep-fe/src/app/models/match.model.ts index a86f55135d..d49ae248d7 100644 --- a/peer-prep-fe/src/app/models/match.model.ts +++ b/peer-prep-fe/src/app/models/match.model.ts @@ -5,9 +5,16 @@ export interface MatchRequest { key: string; // easy_queue, medium_queue, hard_queue } +export interface MatchRequestCancel { + user_id: string +} + export interface MatchResponse { matchedUsers: UserData[]; // 0 or 2 - is an array of UserDatas sessionId: string; timeout: boolean; } +export interface MatchRequestCancelResponse { + message: string +} diff --git a/peer-prep-fe/src/loading-screen/match-modal/match-modal.component.ts b/peer-prep-fe/src/loading-screen/match-modal/match-modal.component.ts index b1c006a7fa..5b0c135a66 100644 --- a/peer-prep-fe/src/loading-screen/match-modal/match-modal.component.ts +++ b/peer-prep-fe/src/loading-screen/match-modal/match-modal.component.ts @@ -245,8 +245,9 @@ export class MatchModalComponent implements OnInit, OnDestroy { } // TODO: HANDLE CANCEL MATCH - SYNC W MATCHING SVC BE PEEPS @KERVYN @IVAN - cancelMatch() { + async cancelMatch() { this.isVisible = false; + await this.matchService.cancelMatchRequest(this.userData.user_id); if (this.countdownSubscription) { this.countdownSubscription.unsubscribe(); } diff --git a/peer-prep-fe/src/services/match.service.ts b/peer-prep-fe/src/services/match.service.ts index 274657a30e..90684a686c 100644 --- a/peer-prep-fe/src/services/match.service.ts +++ b/peer-prep-fe/src/services/match.service.ts @@ -1,37 +1,48 @@ -import { CategoryService } from '../services/category.service'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; -import { Injectable} from '@angular/core'; -import {MatchRequest, MatchResponse} from '../app/models/match.model'; -import { UserData } from '../types/userdata'; -import { baseUrlProduction } from '../../constants'; +import { HttpClient, HttpHeaders } from "@angular/common/http" +import { Injectable } from "@angular/core" +import { lastValueFrom, Observable } from "rxjs" -import { lastValueFrom, Observable } from 'rxjs'; +import { baseUrlProduction } from "../../constants" +import { MatchRequest, MatchRequestCancel, MatchRequestCancelResponse, MatchResponse } from "../app/models/match.model" +import { CategoryService } from "../services/category.service" +import { UserData } from "../types/userdata" @Injectable({ - providedIn: 'root' + providedIn: "root" }) - export class MatchService { - private apiUrl = this.isProduction() ? `${baseUrlProduction}/match` : 'http://localhost:3002/match'; + private apiUrl = this.isProduction() ? `${baseUrlProduction}/match` : "http://localhost:3002/match" + + constructor(private http: HttpClient) {} + + isProduction(): boolean { + return window.location.hostname !== "localhost" + } - constructor(private http: HttpClient) {} + // send user data and difficulty to rabbitMQ (match request) + // this is a post request, which returns a response + async sendMatchRequest(userData: UserData, queueName: string): Promise { + const matchRequest: MatchRequest = { + userData, + key: queueName + } + // return lastValueFrom(this.http.post(`${this.apiUrl}`, matchRequest)); + const response = await this.http.post(`${this.apiUrl}`, matchRequest).toPromise() + if (!response) { + throw new Error("Match response is undefined") + } + return response + } - isProduction (): boolean { - return window.location.hostname !== 'localhost'; + async cancelMatchRequest(user_id: string) { + const cancelMatchRequest: MatchRequestCancel = { + user_id } - // send user data and difficulty to rabbitMQ (match request) - // this is a post request, which returns a response - async sendMatchRequest(userData: UserData, queueName: string): Promise { - const matchRequest: MatchRequest = { - userData, - key: queueName - } - // return lastValueFrom(this.http.post(`${this.apiUrl}`, matchRequest)); - const response = await this.http.post(`${this.apiUrl}`, matchRequest).toPromise(); - if (!response) { - throw new Error('Match response is undefined'); - } - return response; + const response = await this.http.post(`${this.apiUrl}/cancel`, cancelMatchRequest).toPromise() + if (!response) { + throw new Error("Given user_id is invalid, request cancelled unsuccessfully") } + return response; + } }