-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor matching and update gen-ai prompting
- Loading branch information
1 parent
913370a
commit 76d8a7d
Showing
12 changed files
with
818 additions
and
1,849 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 48 additions & 62 deletions
110
peerprep/backend/matching-service/src/controllers/requestController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,61 @@ | ||
import { Request, Response } from 'express'; | ||
import { addToQueue, findMatchInQueue } from '../services/queueManager'; | ||
//import { redisDelete } from '../utils/redisUtils'; | ||
import { addToQueue, removeFromQueue, cancelSession} from '../services/queueManager'; | ||
import ReqObj from '../models/ReqObj'; | ||
|
||
// new request | ||
export const createRequest = async (req: Request, res: Response): Promise<void> => { | ||
const { userId, topic, difficulty } = req.body; | ||
|
||
const newRequest: ReqObj = { | ||
userId, | ||
topic, | ||
difficulty, | ||
status: 'pending', | ||
createdAt: new Date(), | ||
userId, | ||
topic, | ||
difficulty, | ||
status: 'pending', | ||
createdAt: new Date(), | ||
}; | ||
|
||
|
||
await addToQueue(userId, topic, difficulty); | ||
|
||
// find within 30 seconds | ||
let matchedUser = null; | ||
const timeout = 30000; | ||
const startTime = Date.now(); | ||
|
||
while (!matchedUser && Date.now() - startTime < timeout) { | ||
matchedUser = await findMatchInQueue(userId); | ||
|
||
if (matchedUser) { | ||
break; | ||
} | ||
// Wait one second before checking againe | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
} | ||
|
||
if (matchedUser) { | ||
res.status(200).json({ message: 'Match found', data: matchedUser }); | ||
} else { | ||
//await redisDelete(newRequest.userId); // Remove request if no match | ||
res.status(408).json({ message: 'No match found, request timed out' }); | ||
|
||
try { | ||
// Try to add the user to the queue | ||
await addToQueue(userId, topic, difficulty); | ||
res.status(200).json({ message: 'Request successfully added to the queue' }); | ||
// Match user | ||
|
||
} catch (error) { | ||
const err = error as Error; | ||
if (err.name === "UserInQueueError") { | ||
res.status(409).json({ message: 'User is already in the queue' }); | ||
} else if (err.name === "UserInSessionError") { | ||
res.status(409).json({ message: 'User is already in an active session' }); | ||
} else { | ||
console.error("Error in createRequest:", err); | ||
res.status(500).json({ message: 'Failed to add request to the queue due to an unknown error' }); | ||
} | ||
} | ||
}; | ||
/* | ||
import { Request, Response } from 'express'; | ||
import { addToQueue } from '../services/queueManager'; | ||
import ReqObj from '../models/ReqObj'; | ||
// Create a new matching request | ||
export const createRequest = async (req: Request, res: Response): Promise<void> => { | ||
const { userId, topic, difficulty } = req.body; | ||
|
||
// Construct request object | ||
const newRequest: ReqObj = { | ||
userId, | ||
topic, | ||
difficulty, | ||
status: 'pending', | ||
createdAt: new Date(), | ||
}; | ||
console.log("FROM CONTROLLER: USER ID IS" + userId); | ||
console.log("FROM CONTROLLER: TOPIC IS" + topic); | ||
console.log("FROM CONTROLLER: DIFFICULTY IS" + difficulty); | ||
// Add the user to the queue | ||
export const deleteRequest = async (req: Request, res: Response): Promise<void> => { | ||
const userId = req.params.userId as string; // Get userId from URL parameters | ||
|
||
try { | ||
await addToQueue(userId, topic, difficulty); | ||
res.status(201).json({ message: 'Match request created successfully', data: newRequest }); | ||
// Try to remove the user from the queue | ||
console.log("Attempting to remove user from queue"); | ||
await removeFromQueue(userId); | ||
res.status(200).json({ message: 'Request successfully removed from the queue' }); | ||
} catch (error) { | ||
console.error('Error adding to queue:', error); | ||
res.status(500).json({ message: 'Internal Server Error' }); | ||
console.error("Error in deleteRequest:", error); | ||
res.status(500).json({ message: 'Failed to remove request from the queue due to an unknown error' }); | ||
} | ||
}; | ||
*/ | ||
|
||
} | ||
|
||
// API to delete a session | ||
export const deleteSession = async (req: Request, res: Response): Promise<void> => { | ||
const sessionId = req.params.sessionId as string; // Get userId from URL parameters | ||
|
||
try { | ||
// Cancel the timeout and remove session from Redis | ||
await cancelSession(sessionId); | ||
res.status(200).json({ message: 'Session successfully deleted' }); | ||
} catch (error) { | ||
console.error("Error in deleteSession:", error); | ||
res.status(500).json({ message: 'Failed to delete session due to an unknown error' }); | ||
} | ||
}; |
61 changes: 0 additions & 61 deletions
61
peerprep/backend/matching-service/src/controllers/requestController2.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.