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
140 changes: 0 additions & 140 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions src/app/api/(comment)/comment/create/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* POST /api/comment/create
*
* Body: { message: string, user_id: string, payout_id: string }
* Returns: { success: boolean, comment: Comment, message: string }
*/
import { commentCreateSchema } from "@/components/modules/comment/schema/comment.schema";
import { handleDatabaseError, prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";

export async function POST(request: Request) {
try {
const body = await request.json();
const parsed = commentCreateSchema.safeParse(body);

if (!parsed.success) {
return NextResponse.json(
{ error: "Invalid data", details: parsed.error.errors },
{ status: 400 },
);
}

const { message, user_id, payout_id } = parsed.data;

// Find the user in the database
const user = await prisma.user.findUnique({
where: { user_id },
select: { user_id: true, is_active: true },
});

if (!user) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
}

if (!user.is_active) {
return NextResponse.json(
{ error: "User account is not active" },
{ status: 403 },
);
}

// Find the payout in the database
const payout = await prisma.payout.findUnique({
where: { payout_id },
select: {
payout_id: true,
status: true,
created_by: true,
},
});

if (!payout) {
return NextResponse.json({ error: "Payout not found" }, { status: 404 });
}

// Create the comment
const comment = await prisma.comment.create({
data: {
message,
user_id,
payout_id,
},
include: {
user: {
select: {
user_id: true,
username: true,
profile_url: true,
bio: true,
},
},
},
});

return NextResponse.json(
{
success: true,
comment,
message: "Comment created successfully",
},
{ status: 201 },
);
} catch (error) {
const { message, status } = handleDatabaseError(error);
return NextResponse.json({ error: message }, { status });
}
}
72 changes: 72 additions & 0 deletions src/app/api/(comment)/comment/delete/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* DELETE /api/comment/delete/[id]
* Deletes an existing comment (only author can edit)
*
* Params: { id: string }
* Body: { user_id: string }
* Returns: { success: boolean, message: string }
*/
import { handleDatabaseError, prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";

export async function DELETE(
request: Request,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const { id } = await params;
const body = await request.json();

if (!id || id === "undefined" || id === "null") {
return NextResponse.json(
{ error: "Invalid comment ID parameter" },
{ status: 400 },
);
}

// Get user_id from request body
const user_id: string = body.user_id;

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

// Find the comment in the database
const existingComment = await prisma.comment.findUnique({
where: { comment_id: id },
select: {
comment_id: true,
user_id: true,
payout_id: true,
},
});

if (!existingComment) {
return NextResponse.json({ error: "Comment not found" }, { status: 404 });
}

// Verify that the user owns this comment
if (existingComment.user_id !== user_id) {
return NextResponse.json(
{ error: "You can only delete your own comments" },
{ status: 403 },
);
}

// Delete the comment
await prisma.comment.delete({
where: { comment_id: id },
});

return NextResponse.json({
success: true,
message: "Comment deleted successfully",
});
} catch (error) {
const { message, status } = handleDatabaseError(error);
return NextResponse.json({ error: message }, { status });
}
}
59 changes: 59 additions & 0 deletions src/app/api/(comment)/comment/find-by-payout/[payout_id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* GET /api/comment/find-by-payout/[payout_id]
* Retrieves comments from a specific payout
*
* Params: { payout_id: string }
* Returns: { success: boolean, comments: Comment[], count: number }
*/
import { handleDatabaseError, prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";

export async function GET({
params,
}: { params: Promise<{ payout_id: string }> }) {
try {
const { payout_id } = await params;

if (!payout_id || payout_id === "undefined" || payout_id === "null") {
return NextResponse.json(
{ error: "Invalid payout_id parameter" },
{ status: 400 },
);
}

// Find the payout in the database
const payout = await prisma.payout.findUnique({
where: { payout_id },
select: { payout_id: true },
});

if (!payout) {
return NextResponse.json({ error: "Payout not found" }, { status: 404 });
}

// Get comments for the payout, ordered chronologically
const comments = await prisma.comment.findMany({
where: { payout_id },
include: {
user: {
select: {
user_id: true,
username: true,
profile_url: true,
bio: true,
},
},
},
orderBy: { created_at: "asc" },
});

return NextResponse.json({
success: true,
comments,
count: comments.length,
});
} catch (error) {
const { message, status } = handleDatabaseError(error);
return NextResponse.json({ error: message }, { status });
}
}
Loading