Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions shatter-backend/src/controllers/bingo_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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 },
);
}

Expand All @@ -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 });
}
}
}
14 changes: 6 additions & 8 deletions shatter-backend/src/controllers/event_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down Expand Up @@ -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" });
Expand Down
8 changes: 0 additions & 8 deletions shatter-backend/src/models/bingo_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface BingoDocument extends Document {

const bingoSchema = new Schema<BingoDocument>(
{
_id: { type: String },
_eventId: {
type: Schema.Types.ObjectId,
ref: "Event",
Expand All @@ -24,11 +23,4 @@ const bingoSchema = new Schema<BingoDocument>(
}
);

bingoSchema.pre("save", function (next) {
if (!this._id) {
this._id = `bingo_${Math.random().toString(36).slice(2, 10)}`;
}
next();
});

export const Bingo = model<BingoDocument>("Bingo", bingoSchema);
8 changes: 7 additions & 1 deletion shatter-backend/src/models/event_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEvent>(
Expand All @@ -29,10 +30,15 @@ const EventSchema = new Schema<IEvent>(
type: Schema.Types.ObjectId,
required: true,
},
bingoGameId: {
type: Types.ObjectId,
ref: "Bingo",
default: null,
},
},
{
timestamps: true,
}
},
);

// Optional validation: ensure endDate is after startDate
Expand Down
7 changes: 4 additions & 3 deletions shatter-backend/src/routes/bingo_routes.ts
Original file line number Diff line number Diff line change
@@ -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;