From 0bcd3a13f5222929f22bdaf83650b0e1c557d8a0 Mon Sep 17 00:00:00 2001 From: Keiran Date: Sun, 28 Sep 2025 18:32:31 +0100 Subject: [PATCH] use alternative backend because vercel sucks --- src/app/api/admin/discounts/route.ts | 78 +++++++++++++++------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/src/app/api/admin/discounts/route.ts b/src/app/api/admin/discounts/route.ts index cf99bc6..8a2cfc8 100644 --- a/src/app/api/admin/discounts/route.ts +++ b/src/app/api/admin/discounts/route.ts @@ -1,42 +1,36 @@ import { NextRequest, NextResponse } from "next/server"; -import { promises as fs } from "fs"; -import path from "path"; -import type { DiscountCode } from "../../../../types/discount"; -const discountCodesPath = path.join( - process.cwd(), - "src/data/discountCodes.json", -); +const DATA_SERVER_URL = process.env.DATA_SERVER_URL || "http://localhost:3001"; export async function POST(request: NextRequest) { try { const { code, percentage, expiration, description } = await request.json(); - const fileContent = await fs.readFile(discountCodesPath, "utf8"); - const discountCodes = JSON.parse(fileContent); + const response = await fetch(`${DATA_SERVER_URL}/api/discount-codes`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${process.env.ADMIN_PASSWORD}`, + }, + body: JSON.stringify({ code, percentage, expiration, description }), + }); - const newDiscountCode: DiscountCode = { - percentage, - description, - }; - - if (expiration) { - newDiscountCode.expiration = expiration; + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || "Failed to add discount code"); } - discountCodes[code] = newDiscountCode; - - await fs.writeFile( - discountCodesPath, - JSON.stringify(discountCodes, null, 2), - "utf8", - ); - - return NextResponse.json({ success: true }); + const result = await response.json(); + return NextResponse.json(result); } catch (error) { console.error("Error adding discount code:", error); return NextResponse.json( - { error: "Failed to add discount code" }, + { + error: + error instanceof Error + ? error.message + : "Failed to add discount code", + }, { status: 500 }, ); } @@ -46,22 +40,32 @@ export async function DELETE(request: NextRequest) { try { const { code } = await request.json(); - const fileContent = await fs.readFile(discountCodesPath, "utf8"); - const discountCodes = JSON.parse(fileContent); - - delete discountCodes[code]; - - await fs.writeFile( - discountCodesPath, - JSON.stringify(discountCodes, null, 2), - "utf8", + const response = await fetch( + `${DATA_SERVER_URL}/api/discount-codes/${encodeURIComponent(code)}`, + { + method: "DELETE", + headers: { + Authorization: `Bearer ${process.env.ADMIN_PASSWORD}`, + }, + }, ); - return NextResponse.json({ success: true }); + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || "Failed to delete discount code"); + } + + const result = await response.json(); + return NextResponse.json(result); } catch (error) { console.error("Error deleting discount code:", error); return NextResponse.json( - { error: "Failed to delete discount code" }, + { + error: + error instanceof Error + ? error.message + : "Failed to delete discount code", + }, { status: 500 }, ); }