Skip to content
Open
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
49 changes: 24 additions & 25 deletions app/api/clan/[clanId]/kick/route.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { ClanService } from "@/lib/clan";
import { NextRequest, NextResponse } from "next/server";
// Using the client-side import as requested.
import { databases } from "@/lib/appwrite";

type RouteContext = { params: Promise<{ clanId: string }> };

export async function GET() {
return NextResponse.json({ error: "Method not allowed" }, { status: 405 });
}

export async function POST(
request: NextRequest,
context: RouteContext
) {
const { clanId } = await context.params;
const DATABASE_ID = process.env.NEXT_PUBLIC_DATABASE_ID as string;
const PROFILES_COLLECTION_ID = process.env
.NEXT_PUBLIC_PROFILES_COLLECTION_ID as string;

// POST to kick a member from a clan
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { memberToKick, requesterId } = body;
const { userIdToKick } = await request.json();

if (!memberToKick || !requesterId) {
if (!userIdToKick) {
return NextResponse.json(
{ error: "memberToKick and requesterId are required" },
{ message: "User ID to kick is required" },
{ status: 400 }
);
}

const success = await ClanService.kickMember(
clanId,
memberToKick,
requesterId
// ⚠️ This may fail if your Appwrite instance isn’t authenticated with admin permissions
await databases.updateDocument(
DATABASE_ID,
PROFILES_COLLECTION_ID,
userIdToKick,
{ clanId: null }
);

return NextResponse.json({ success });
} catch (error) {
return NextResponse.json({ message: "Successfully kicked user" });
} catch (error: unknown) {
const errMessage =
error instanceof Error ? error.message : "Unknown error occurred";

return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }
{ message: "Failed to kick user", error: errMessage },
{ status: 500 }
);
}
}
}
62 changes: 28 additions & 34 deletions app/api/clan/[clanId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
import { NextRequest, NextResponse } from "next/server";
import { ClanService } from "@/lib/clan";
// Using the client-side import as requested.
import { databases } from "@/lib/appwrite";

interface RouteParams {
params: Promise<{
clanId: string;
}>;
}

export async function GET(request: NextRequest, { params }: RouteParams) {
try {
const { clanId } = await params;
const clan = ClanService.getClanById(clanId);
if (!clan) {
return NextResponse.json({ error: "Clan not found" }, { status: 404 });
}
return NextResponse.json({ clan });
} catch (error) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }
);
}
}
const DATABASE_ID = process.env.NEXT_PUBLIC_DATABASE_ID as string;
const CLANS_COLLECTION_ID = process.env
.NEXT_PUBLIC_CLANS_COLLECTION_ID as string;

export async function PUT(request: NextRequest, { params }: RouteParams) {
// GET information for a specific clan
export async function GET(
request: NextRequest,
{ params }: { params: { clanId: string } }
) {
try {
const { clanId } = await params;
const body = await request.json();
const { updates, userId } = body;
const { clanId } = params;

if (!userId) {
if (!clanId) {
return NextResponse.json(
{ error: "UserId is required" },
{ message: "Clan ID is required" },
{ status: 400 }
);
}

const clan = ClanService.updateClan(clanId, updates, userId);
return NextResponse.json({ clan });
} catch (error) {
// ⚠️ May fail if your collection does not allow read access
const clanData = await databases.getDocument(
DATABASE_ID,
CLANS_COLLECTION_ID,
clanId
);

return NextResponse.json(clanData);
} catch (error: unknown) {
const errMessage =
error instanceof Error ? error.message : "Unknown error occurred";

return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }
{ message: "Failed to fetch clan data", error: errMessage },
{ status: 500 }
);
}
}
}
35 changes: 25 additions & 10 deletions app/api/clan/join/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { NextRequest, NextResponse } from "next/server";
import { ClanService } from "@/lib/clan";
// Using the client-side import as requested.
import { databases } from "@/lib/appwrite";

const DATABASE_ID = process.env.NEXT_PUBLIC_DATABASE_ID as string;
const PROFILES_COLLECTION_ID = process.env
.NEXT_PUBLIC_PROFILES_COLLECTION_ID as string;

// POST to join a clan
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { joinKey, userId } = body;
const { userId, clanId } = await request.json();

if (!joinKey || !userId) {
if (!userId || !clanId) {
return NextResponse.json(
{ error: "Join key and userId are required" },
{ message: "User ID and Clan ID are required" },
{ status: 400 }
);
}

const clan = ClanService.joinClan(joinKey, userId);
return NextResponse.json({ clan });
} catch (error) {
// ⚠️ This may fail if the Appwrite instance is not authenticated
await databases.updateDocument(
DATABASE_ID,
PROFILES_COLLECTION_ID,
userId,
{ clanId }
);

return NextResponse.json({ message: "Successfully joined clan" });
} catch (error: unknown) {
const errMessage =
error instanceof Error ? error.message : "Unknown error occurred";

return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }
{ message: "Failed to join clan", error: errMessage },
{ status: 500 }
);
}
}
33 changes: 24 additions & 9 deletions app/api/clan/leave/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
import { NextRequest, NextResponse } from "next/server";
import { ClanService } from "@/lib/clan";
// Using the client-side import as requested.
import { databases } from "@/lib/appwrite";

const DATABASE_ID = process.env.NEXT_PUBLIC_DATABASE_ID as string;
const PROFILES_COLLECTION_ID = process.env
.NEXT_PUBLIC_PROFILES_COLLECTION_ID as string;

// POST to leave a clan
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { userId } = body;
const { userId } = await request.json();

if (!userId) {
return NextResponse.json(
{ error: "UserId is required" },
{ message: "User ID is required" },
{ status: 400 }
);
}

const success = ClanService.leaveClan(userId);
return NextResponse.json({ success });
} catch (error) {
// ⚠️ This may fail with 401 Unauthorized unless Appwrite is authenticated
await databases.updateDocument(
DATABASE_ID,
PROFILES_COLLECTION_ID,
userId,
{ clanId: null }
);

return NextResponse.json({ message: "Successfully left clan" });
} catch (error: unknown) {
const errMessage =
error instanceof Error ? error.message : "Unknown error occurred";

return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }
{ message: "Failed to leave clan", error: errMessage },
{ status: 500 }
);
}
}
74 changes: 47 additions & 27 deletions app/api/clan/route.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,66 @@
import { NextRequest, NextResponse } from "next/server";
import { ClanService } from "@/lib/clan";
import { getAppwriteDatabases } from "@/lib/appwrite"; // server-side client

const DATABASE_ID = process.env.NEXT_PUBLIC_DATABASE_ID as string;
const CLANS_COLLECTION_ID = process.env
.NEXT_PUBLIC_CLANS_COLLECTION_ID as string;
const PROFILES_COLLECTION_ID = process.env
.NEXT_PUBLIC_PROFILES_COLLECTION_ID as string;

// GET the current user's clan
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const userId = searchParams.get("userId");

if (userId) {
// Get user's clan
const clan = ClanService.getUserClan(userId);
return NextResponse.json({ clan });
} else {
// Get all clans
const clans = ClanService.getAllClans();
return NextResponse.json({ clans });
if (!userId) {
return NextResponse.json(
{ message: "User ID is required" },
{ status: 400 }
);
}
} catch (error) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }

const databases = getAppwriteDatabases();

const profile = await databases.getDocument(
DATABASE_ID,
PROFILES_COLLECTION_ID,
userId
);
}
}

export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { name, creatorId, description } = body;
if (!profile.clanId) {
return NextResponse.json(
{ message: "User not in a clan" },
{ status: 404 }
);
}

if (!name || !creatorId) {
const clanData = await databases.getDocument(
DATABASE_ID,
CLANS_COLLECTION_ID,
profile.clanId as string
);

return NextResponse.json(clanData);
} catch (error: unknown) {
if (
error &&
typeof error === "object" &&
"code" in error &&
(error as any).code === 404
) {
return NextResponse.json(
{ error: "Name and creatorId are required" },
{ status: 400 }
{ message: "User not in a clan" },
{ status: 404 }
);
}

const clan = ClanService.createClan(name, creatorId, description);
return NextResponse.json({ clan }, { status: 201 });
} catch (error) {
const errMessage =
error instanceof Error ? error.message : "Unknown server error";

return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 400 }
{ message: "Server error", error: errMessage },
{ status: 500 }
);
}
}
Loading