use alternative backend because vercel sucks
This commit is contained in:
parent
a96cb500ca
commit
0bcd3a13f5
@ -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 },
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user