Initial commit

This commit is contained in:
UltraLionFr
2026-01-02 17:52:07 +01:00
parent b29bd25170
commit 972bafc4e0
36 changed files with 8742 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
import NextAuth, { NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
const ALLOWED_DISCORD_IDS =
process.env.ALLOWED_DISCORD_IDS?.split(",").map((id) => id.trim()) || [];
const ADMIN_DISCORD_IDS =
process.env.ADMIN_DISCORD_IDS?.split(",").map((id) => id.trim()) || [];
export const authOptions: NextAuthOptions = {
providers: [
DiscordProvider({
clientId: process.env.DISCORD_CLIENT_ID!,
clientSecret: process.env.DISCORD_CLIENT_SECRET!,
}),
],
callbacks: {
async signIn({ profile }) {
if (profile?.id && ALLOWED_DISCORD_IDS.includes(profile.id)) {
return true;
}
return "/unauthorized";
},
async jwt({ token, profile }) {
if (profile) {
token.discordId = profile.id;
token.isAdmin = ADMIN_DISCORD_IDS.includes(
profile.id as string
);
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.discordId = token.discordId as string;
session.user.isAdmin = token.isAdmin as boolean;
}
return session;
},
},
pages: {
signIn: "/login",
error: "/unauthorized",
},
secret: process.env.NEXTAUTH_SECRET,
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
+171
View File
@@ -0,0 +1,171 @@
import { initDatabase, query } from "@/lib/db";
import { getServerSession } from "next-auth";
import { NextRequest, NextResponse } from "next/server";
import { authOptions } from "../auth/[...nextauth]/route";
async function isAdmin() {
const session = await getServerSession(authOptions);
return session?.user?.isAdmin === true;
}
export async function GET(request: NextRequest) {
try {
await initDatabase();
const { searchParams } = new URL(request.url);
const mode = searchParams.get("mode");
let result;
if (mode) {
result = await query(
"SELECT * FROM questions WHERE mode = $1 ORDER BY order_index ASC, id ASC",
[mode]
);
} else {
result = await query(
"SELECT * FROM questions ORDER BY mode, order_index ASC, id ASC"
);
}
return NextResponse.json(result.rows);
} catch (error) {
console.error("Error fetching questions:", error);
return NextResponse.json(
{ error: "Failed to fetch questions" },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
if (!(await isAdmin())) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 403 }
);
}
await initDatabase();
const body = await request.json();
const { category, question, mode } = body;
if (!category || !question || !mode) {
return NextResponse.json(
{ error: "Missing required fields" },
{ status: 400 }
);
}
const maxOrder = await query(
"SELECT COALESCE(MAX(order_index), -1) as max_order FROM questions WHERE mode = $1",
[mode]
);
const newOrder = parseInt(maxOrder.rows[0].max_order) + 1;
const result = await query(
"INSERT INTO questions (category, question, mode, order_index) VALUES ($1, $2, $3, $4) RETURNING *",
[category, question, mode, newOrder]
);
return NextResponse.json(result.rows[0], { status: 201 });
} catch (error) {
console.error("Error creating question:", error);
return NextResponse.json(
{ error: "Failed to create question" },
{ status: 500 }
);
}
}
export async function PATCH(request: NextRequest) {
try {
if (!(await isAdmin())) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 403 }
);
}
const body = await request.json();
const { id, category, question, order_index } = body;
if (!id) {
return NextResponse.json(
{ error: "Missing question id" },
{ status: 400 }
);
}
const updates: string[] = [];
const args: (string | number)[] = [];
let paramIndex = 1;
if (category !== undefined) {
updates.push(`category = $${paramIndex++}`);
args.push(category);
}
if (question !== undefined) {
updates.push(`question = $${paramIndex++}`);
args.push(question);
}
if (order_index !== undefined) {
updates.push(`order_index = $${paramIndex++}`);
args.push(order_index);
}
if (updates.length === 0) {
return NextResponse.json(
{ error: "No fields to update" },
{ status: 400 }
);
}
args.push(id);
const result = await query(
`UPDATE questions SET ${updates.join(
", "
)} WHERE id = $${paramIndex} RETURNING *`,
args
);
return NextResponse.json(result.rows[0]);
} catch (error) {
console.error("Error updating question:", error);
return NextResponse.json(
{ error: "Failed to update question" },
{ status: 500 }
);
}
}
export async function DELETE(request: NextRequest) {
try {
if (!(await isAdmin())) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: 403 }
);
}
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
if (!id) {
return NextResponse.json(
{ error: "Missing question id" },
{ status: 400 }
);
}
await query("DELETE FROM questions WHERE id = $1", [id]);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Error deleting question:", error);
return NextResponse.json(
{ error: "Failed to delete question" },
{ status: 500 }
);
}
}
+104
View File
@@ -0,0 +1,104 @@
import { initDatabase, query } from "@/lib/db";
import { SessionAnswer } from "@/types";
import { NextRequest, NextResponse } from "next/server";
export async function GET() {
try {
await initDatabase();
const sessionsResult = await query(
"SELECT * FROM sessions ORDER BY created_at DESC"
);
const sessions = await Promise.all(
sessionsResult.rows.map(async (session: any) => {
const answersResult = await query(
"SELECT * FROM session_answers WHERE session_id = $1 ORDER BY question_index",
[session.id]
);
return {
id: session.id,
mode: session.mode,
created_at: session.created_at,
exported_at: session.exported_at,
answers: answersResult.rows.map((a: any) => ({
questionIndex: a.question_index,
category: a.category,
question: a.question,
answer: a.answer,
})),
};
})
);
return NextResponse.json(sessions);
} catch (error) {
console.error("Error fetching sessions:", error);
return NextResponse.json(
{ error: "Failed to fetch sessions" },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
await initDatabase();
const body = await request.json();
const { mode, answers } = body;
if (!mode || !answers) {
return NextResponse.json(
{ error: "Missing required fields" },
{ status: 400 }
);
}
const sessionResult = await query(
"INSERT INTO sessions (mode) VALUES ($1) RETURNING *",
[mode]
);
const sessionId = sessionResult.rows[0].id;
for (const answer of answers as SessionAnswer[]) {
if (answer.answer.trim()) {
await query(
"INSERT INTO session_answers (session_id, question_index, category, question, answer) VALUES ($1, $2, $3, $4, $5)",
[
sessionId,
answer.questionIndex,
answer.category,
answer.question,
answer.answer,
]
);
}
}
const answersData = await query(
"SELECT * FROM session_answers WHERE session_id = $1 ORDER BY question_index",
[sessionId]
);
const newSession = {
id: sessionResult.rows[0].id,
mode: sessionResult.rows[0].mode,
created_at: sessionResult.rows[0].created_at,
answers: answersData.rows.map((a: any) => ({
questionIndex: a.question_index,
category: a.category,
question: a.question,
answer: a.answer,
})),
};
return NextResponse.json(newSession, { status: 201 });
} catch (error) {
console.error("Error creating session:", error);
return NextResponse.json(
{ error: "Failed to create session" },
{ status: 500 }
);
}
}
+135
View File
@@ -0,0 +1,135 @@
import { initDatabase, query } from "@/lib/db";
import { NextRequest, NextResponse } from "next/server";
export async function GET() {
try {
await initDatabase();
const result = await query(
"SELECT * FROM tasks ORDER BY created_at DESC"
);
return NextResponse.json(result.rows);
} catch (error) {
console.error("Error fetching tasks:", error);
return NextResponse.json(
{ error: "Failed to fetch tasks" },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
await initDatabase();
const body = await request.json();
const { text, category, description } = body;
if (!text || !category) {
return NextResponse.json(
{ error: "Missing required fields" },
{ status: 400 }
);
}
const added = new Date().toLocaleDateString("fr-FR");
const result = await query(
"INSERT INTO tasks (text, description, checklist, category, status, added) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *",
[text, description || null, "[]", category, "todo", added]
);
return NextResponse.json(result.rows[0], { status: 201 });
} catch (error) {
console.error("Error creating task:", error);
return NextResponse.json(
{ error: "Failed to create task" },
{ status: 500 }
);
}
}
export async function PATCH(request: NextRequest) {
try {
const body = await request.json();
const { id, status, category, text, description, checklist } = body;
if (!id) {
return NextResponse.json(
{ error: "Missing task id" },
{ status: 400 }
);
}
const updates: string[] = [];
const args: (string | number | null)[] = [];
let paramIndex = 1;
if (status !== undefined) {
updates.push(`status = $${paramIndex++}`);
args.push(status);
}
if (category !== undefined) {
updates.push(`category = $${paramIndex++}`);
args.push(category);
}
if (text !== undefined) {
updates.push(`text = $${paramIndex++}`);
args.push(text);
}
if (description !== undefined) {
updates.push(`description = $${paramIndex++}`);
args.push(description);
}
if (checklist !== undefined) {
updates.push(`checklist = $${paramIndex++}`);
args.push(JSON.stringify(checklist));
}
if (updates.length === 0) {
return NextResponse.json(
{ error: "No fields to update" },
{ status: 400 }
);
}
args.push(id);
const result = await query(
`UPDATE tasks SET ${updates.join(
", "
)} WHERE id = $${paramIndex} RETURNING *`,
args
);
return NextResponse.json(result.rows[0]);
} catch (error) {
console.error("Error updating task:", error);
return NextResponse.json(
{ error: "Failed to update task" },
{ status: 500 }
);
}
}
export async function DELETE(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const id = searchParams.get("id");
if (!id) {
return NextResponse.json(
{ error: "Missing task id" },
{ status: 400 }
);
}
await query("DELETE FROM tasks WHERE id = $1", [id]);
return NextResponse.json({ success: true });
} catch (error) {
console.error("Error deleting task:", error);
return NextResponse.json(
{ error: "Failed to delete task" },
{ status: 500 }
);
}
}