diff --git a/shatter-backend/src/controllers/bingo_controller.ts b/shatter-backend/src/controllers/bingo_controller.ts index acf7661..568b14c 100644 --- a/shatter-backend/src/controllers/bingo_controller.ts +++ b/shatter-backend/src/controllers/bingo_controller.ts @@ -40,7 +40,7 @@ export async function createBingo(req: Request, res: Response) { grid.every( (row: any) => Array.isArray(row) && - row.every((cell: any) => typeof cell === "string") + row.every((cell: any) => typeof cell === "string"), ); if (!is2DStringArray) { @@ -62,6 +62,11 @@ export async function createBingo(req: Request, res: Response) { grid, }); + // update event with bingo id + await Event.findByIdAndUpdate(_eventId, { + bingoGameId: bingo._id, + }); + return res.status(201).json({ success: true, bingoId: bingo._id, @@ -72,13 +77,9 @@ export async function createBingo(req: Request, res: Response) { } } - -/** - * @param req.body.id - Bingo _id (string) OR Event _id (ObjectId string) (required) - */ export async function getBingo(req: Request, res: Response) { try { - const { id } = req.body; + const { id } = req.params; if (!id) { return res.status(400).json({ @@ -112,7 +113,6 @@ export async function getBingo(req: Request, res: Response) { } } - /** * @param req.body.id - Bingo _id (string) OR Event _id (ObjectId string) (required) * @param req.body.description - New bingo description (string) (optional) @@ -143,7 +143,7 @@ export async function updateBingo(req: Request, res: Response) { grid.every( (row: any) => Array.isArray(row) && - row.every((cell: any) => typeof cell === "string") + row.every((cell: any) => typeof cell === "string"), ); if (!is2DStringArray) { @@ -163,13 +163,17 @@ export async function updateBingo(req: Request, res: Response) { }); } - let bingo = await Bingo.findByIdAndUpdate(id, { $set: update }, { new: true }); + let bingo = await Bingo.findByIdAndUpdate( + id, + { $set: update }, + { new: true }, + ); if (!bingo && Types.ObjectId.isValid(id)) { bingo = await Bingo.findOneAndUpdate( { _eventId: id }, { $set: update }, - { new: true } + { new: true }, ); } @@ -181,4 +185,4 @@ export async function updateBingo(req: Request, res: Response) { } catch (err: any) { return res.status(500).json({ success: false, error: err.message }); } -} \ No newline at end of file +} diff --git a/shatter-backend/src/controllers/event_controller.ts b/shatter-backend/src/controllers/event_controller.ts index 9d1468b..f9613bc 100644 --- a/shatter-backend/src/controllers/event_controller.ts +++ b/shatter-backend/src/controllers/event_controller.ts @@ -94,10 +94,9 @@ export async function getEventByJoinCode(req: Request, res: Response) { .json({ success: false, error: "joinCode is required" }); } - const event = await Event.findOne({ joinCode }).populate( - "participantIds", - "name userId", - ); + const event = await Event.findOne({ joinCode }) + .populate("participantIds", "name userId") + .populate("bingoGameId"); if (!event) { return res.status(404).json({ success: false, error: "Event not found" }); @@ -311,10 +310,9 @@ export async function getEventById(req: Request, res: Response) { .json({ success: false, error: "eventId is required" }); } - const event = await Event.findById(eventId).populate( - "participantIds", - "name userId", - ); + const event = await Event.findById(eventId) + .populate("participantIds", "name userId") + .populate("bingoGameId"); if (!event) { return res.status(404).json({ success: false, error: "Event not found" }); diff --git a/shatter-backend/src/models/bingo_model.ts b/shatter-backend/src/models/bingo_model.ts index 8ba81b3..5fdfe99 100644 --- a/shatter-backend/src/models/bingo_model.ts +++ b/shatter-backend/src/models/bingo_model.ts @@ -10,7 +10,6 @@ export interface BingoDocument extends Document { const bingoSchema = new Schema( { - _id: { type: String }, _eventId: { type: Schema.Types.ObjectId, ref: "Event", @@ -24,11 +23,4 @@ const bingoSchema = new Schema( } ); -bingoSchema.pre("save", function (next) { - if (!this._id) { - this._id = `bingo_${Math.random().toString(36).slice(2, 10)}`; - } - next(); -}); - export const Bingo = model("Bingo", bingoSchema); diff --git a/shatter-backend/src/models/event_model.ts b/shatter-backend/src/models/event_model.ts index 04dc55f..43c4918 100644 --- a/shatter-backend/src/models/event_model.ts +++ b/shatter-backend/src/models/event_model.ts @@ -13,6 +13,7 @@ export interface IEvent extends Document { participantIds: Schema.Types.ObjectId[]; currentState: string; createdBy: Schema.Types.ObjectId; + bingoGameId?: Types.ObjectId | null; } const EventSchema = new Schema( @@ -29,10 +30,15 @@ const EventSchema = new Schema( type: Schema.Types.ObjectId, required: true, }, + bingoGameId: { + type: Types.ObjectId, + ref: "Bingo", + default: null, + }, }, { timestamps: true, - } + }, ); // Optional validation: ensure endDate is after startDate diff --git a/shatter-backend/src/routes/bingo_routes.ts b/shatter-backend/src/routes/bingo_routes.ts index 2e9238f..f89a4b5 100644 --- a/shatter-backend/src/routes/bingo_routes.ts +++ b/shatter-backend/src/routes/bingo_routes.ts @@ -1,16 +1,17 @@ import { Router } from 'express'; import { createBingo, getBingo, updateBingo} from '../controllers/bingo_controller'; +import { authMiddleware } from '../middleware/auth_middleware'; const router = Router(); // POST /api/bingo/createEvent - create new event -router.post('/createBingo', createBingo); +router.post('/createBingo', authMiddleware, createBingo); // POST /api/bingo/getBingo - get bingo details -router.post('/getBingo', getBingo); +router.get('/getBingo/:id', getBingo); // POST /api/bingo/updateBingo - update bingo details -router.put("/updateBingo", updateBingo); +router.put("/updateBingo", authMiddleware, updateBingo); export default router;