feat: enhance ui components and response handling

This commit is contained in:
2025-01-03 18:48:57 +01:00
parent 78fbcebe91
commit 83b3a747a1
11 changed files with 296 additions and 328 deletions
+63 -140
View File
@@ -1,161 +1,84 @@
import pb from "@/lib/pocketbase";
import { ResponseHelper } from "@/lib/helper";
import { RESPONSE } from "@/lib/const";
import { Budgetable } from "@/lib/utils";
const { INTERNAL_SERVER_ERROR, MISSING_ID, FAILED_TO_DELETE_DATA, FAILED_TO_UPDATE_DATA, INVALID_DATA, SUCCESS, CREATED } = RESPONSE;
const { EMAIL, PASSWORD, COLLECTION = "budgetable" } = process.env;
async function authenticateSuperuser() {
if (!EMAIL || !PASSWORD) {
throw new Error("Environment variables EMAIL and PASSWORD must be set");
}
if (!pb.authStore.isValid) {
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
}
async function authenticateSuperuser(): Promise<void> {
if (!EMAIL || !PASSWORD) {
throw new Error("Environment variables EMAIL and PASSWORD must be set");
}
if (!pb.authStore.isValid) {
await pb.collection("_superusers").authWithPassword(EMAIL, PASSWORD);
}
}
export async function GET(
_req: Request,
context: { params: Promise<{ id: string }> },
) {
try {
await authenticateSuperuser();
_req: Request,
context: { params: Promise<{ id: string }> }
): Promise<Response> {
try {
await authenticateSuperuser();
const id = (await context.params)?.id;
if (!id) {
return Response.json(
{
error: {
message: "Missing ID in request",
},
},
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
const id = (await context.params)?.id;
if (!id) {
return ResponseHelper.error(MISSING_ID);
}
const record = await pb.collection(COLLECTION).getOne(id);
return Response.json(record, {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error fetching data:", error);
return Response.json(
{
error: {
message: "Failed to fetch data",
},
},
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
const record: Budgetable = await pb.collection<Budgetable>(COLLECTION).getOne(id);
return ResponseHelper.success<Budgetable>(record, CREATED.STATUS);
} catch (error) {
console.error("Error fetching data:", error);
return ResponseHelper.error(INTERNAL_SERVER_ERROR, error);
}
}
export async function DELETE(
_req: Request,
context: { params: Promise<{ id: string }> },
) {
try {
await authenticateSuperuser();
_req: Request,
context: { params: Promise<{ id: string }> }
): Promise<Response> {
try {
await authenticateSuperuser();
const id = (await context.params)?.id;
if (!id) {
return Response.json(
{
error: {
message: "Missing ID in request",
},
},
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
const id = (await context.params)?.id;
if (!id) {
return ResponseHelper.error(MISSING_ID);
}
await pb.collection(COLLECTION).delete(id);
return Response.json(
{
success: true,
},
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (error) {
console.error("Error deleting data:", error);
return Response.json(
{
error: {
message: "Failed to delete data",
},
},
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
await pb.collection(COLLECTION).delete(id);
return ResponseHelper.success(SUCCESS.MESSAGE);
} catch (error) {
console.error("Error deleting data:", error);
return ResponseHelper.error(FAILED_TO_DELETE_DATA, error);
}
}
export async function PUT(
req: Request,
context: { params: Promise<{ id: string }> },
) {
try {
await authenticateSuperuser();
req: Request,
context: { params: Promise<{ id: string }> }
): Promise<Response> {
try {
await authenticateSuperuser();
const id = (await context.params)?.id;
if (!id) {
return Response.json(
{
error: {
message: "Missing ID in request",
},
},
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
const id = (await context.params)?.id;
if (!id) {
return ResponseHelper.error(MISSING_ID);
}
const body = await req.json();
if (!body.title || typeof body.price !== "number") {
return Response.json(
{
error: {
message: "Invalid data provided",
},
},
{
status: 400,
headers: { "Content-Type": "application/json" },
},
);
}
const body: Partial<Budgetable> = await req.json();
if (!body.title || typeof body.price !== "number") {
return ResponseHelper.error(INVALID_DATA);
}
const updatedRecord = await pb.collection(COLLECTION).update(id, body);
return Response.json(updatedRecord, {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (error) {
console.error("Error updating data:", error);
return Response.json(
{
error: {
message: "Failed to update data",
},
},
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
const updatedRecord: Budgetable = await pb
.collection<Budgetable>(COLLECTION)
.update(id, body);
return ResponseHelper.success<Budgetable>(updatedRecord);
} catch (error) {
console.error("Error updating data:", error);
return ResponseHelper.error(FAILED_TO_UPDATE_DATA, error);
}
}