Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/tournaments registration logic #138

Merged
merged 2 commits into from
Dec 18, 2024
Merged
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
10 changes: 8 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
DATABASE_URL=""
MONGODB_URL=''
DATABASE_URL=''
RESEND_API_KEY=''
NEXTAUTH_SECRET=''
NEXTAUTH_URL=''
NEWS_URL=''

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

DISCORD_CLIENT_ID=
DISCORD_CLIENT_SECRET=

# For those who working on /blogs page
SANITY_STUDIO_PROJECT_ID=''
SANITY_STUDIO_DATASET=''
52 changes: 41 additions & 11 deletions app/api/tournaments/[id]/registration/route.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
import { NextResponse } from "next/server";
import dbConnect from "../../../../../lib/dbConnect";
import Tournament from "../../../../../model/Tournament";
import mongoose from "mongoose";

async function handler(request, { params }) {
await dbConnect();

const id = params.id;
const { registrationData } = await request.json();

try {
const { name, members, email, selectedPlatform, participantType } =
await request.json();

if (!name || !members || !email || !selectedPlatform || !participantType) {
return NextResponse.json(
{ message: "All fields are required for registration" },
{ status: 400 }
);
}

if (!Array.isArray(members) || members.some((member) => typeof member !== "string")) {
return NextResponse.json(
{ message: "Invalid members format. Members must be an array of strings." },
{ status: 400 }
);
}

const teamRegistration = {
id: new mongoose.Types.ObjectId(),
name,
members,
email,
selectedPlatform,
participantType,
};

const updatedTournament = await Tournament.findByIdAndUpdate(
id,
{ registrations: registrationData },
{ $push: { teamsRegistered: teamRegistration } },
{ new: true }
);

if (!updatedTournament) {
return NextResponse.status(404).json({ message: "Tournament not found" });
return NextResponse.json(
{ message: "Tournament not found" },
{ status: 404 }
);
}

return NextResponse.status(200).json({
message: "Registration updated successfully",
tournament: updatedTournament,
return NextResponse.json({
message: "Registration successful",
teamRegistration,
});
} catch (error) {
console.error("Error while updating tournament", error);
return NextResponse.status(500).json({
message: "Error while updating the registration",
});
console.error("Error while registering for the tournament:", error);
return NextResponse.json(
{ message: "An error occurred during registration" },
{ status: 500 }
);
}
}

export { handler as POST };
export { handler as POST };
38 changes: 25 additions & 13 deletions model/Tournament.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const TournamentSchema = new Schema({
const TournamentSchema = new Schema(
{
tournamentName: { type: String, required: true },
tournamentDates: {
created: { type: Date, default: Date.now },
started: Date,
ended: Date
created: { type: Date, default: Date.now },
started: Date,
ended: Date,
},
schedules: Schema.Types.Mixed,
organizerId: { type: Schema.Types.ObjectId, ref: 'Organizer', required: true },
Expand All @@ -15,15 +16,20 @@ const TournamentSchema = new Schema({
links: Schema.Types.Mixed,
gameBannerPhoto: String,
results: [Schema.Types.Mixed],
teamsRegistered: [{
id: Schema.Types.ObjectId,
teamsRegistered: [
{
id: { type: mongoose.Schema.Types.ObjectId, required: true },
name: { type: String, required: true },
members: [{ type: Schema.Types.ObjectId, ref: 'User' }]
}],
members: [{ type: String }],
email: { type: String, required: true, lowercase: true, trim: true },
selectedPlatform: { type: String, required: true },
participantType: { type: String, required: true },
},
],
participantCount: {
type: Number,
required: true,
min: 1
type: Number,
required: true,
min: 1,
},
rounds: [Schema.Types.Mixed],
teamSize: { type: Number, min: 1 },
Expand All @@ -41,7 +47,11 @@ const TournamentSchema = new Schema({
minTeamMembers: { type: Number, min: 1 },
maxTeams: { type: Number, min: 1 },
minTeams: { type: Number, min: 1 },
tournamentVisibility: { type: String, enum: ['public', 'private'], default: 'public' },
tournamentVisibility: {
type: String,
enum: ['public', 'private'],
default: 'public',
},
inviteCode: String,
prizeConfig: [Schema.Types.Mixed],
sponsors: [Schema.Types.Mixed],
Expand All @@ -60,7 +70,9 @@ const TournamentSchema = new Schema({
selectedTimezone: String,
size: String,
brackets: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Bracket' }],
}, { timestamps: true });
},
{ timestamps: true }
);

// Indexes remain the same

Expand Down
8 changes: 5 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ type TournamentDates {
type TeamRegistration {
id String @db.ObjectId
name String
members String[] @db.ObjectId
members String[]
email String
selectedPlatform String
participantType String
}

enum GameType {
SQUAD
SOLO
DUO
}

}
Loading