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
1 change: 1 addition & 0 deletions app/src/app/admin/components/SubscriptionEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const SubscriptionEdit = ({ recording, onClose }: Props) => {
</label>
<div className="flex items-center gap-2">
<input
type={key === "name" ? "text" : "number"}
className="border border-gray-300 rounded px-2 py-1 w-full"
value={editData[key]}
onChange={(e) =>
Expand Down
31 changes: 18 additions & 13 deletions app/src/app/api/admin/subscriptions/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,27 @@ export const GET = withAdmin(async function (
// PATCH /api/admin/subscriptions/:id
export const PATCH = withAdmin(async function (
req: NextRequest,
{ params }: { params: { id: string } }
{ params }: { params: Promise<{ id: string }> }
) {
const data = await req.json();
const { id } = params;

const { id } = await params;
if (!id) {
return NextResponse.json(
{ error: "Subscription ID is required" },
{ status: 400 }
);
}

const data = await req.json();

const { id: _ignore, createdAt, updatedAt, ...safeData } = data;

try {
const [updated] = await db
.update(subscriptionTable)
.set(data)
.set({
...safeData,
updatedAt: new Date(),
})
.where(eq(subscriptionTable.id, id))
.returning();

Expand All @@ -58,14 +63,14 @@ export const PATCH = withAdmin(async function (

return NextResponse.json(updated);
} catch (error: any) {
// Handle unique constraint error (PostgreSQL code 23505)
if (error.code === "23505") {
if (error.constraint === "subscriptions_name_unique") {
return NextResponse.json(
{ error: "Subscription name already exists" },
{ status: 409 }
);
}
if (
error.code === "23505" &&
error.constraint === "subscriptions_name_unique"
) {
return NextResponse.json(
{ error: "Subscription name already exists" },
{ status: 409 }
);
}

// Generic DB error fallback
Expand Down