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 { 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(
|
const DATA_SERVER_URL = process.env.DATA_SERVER_URL || "http://localhost:3001";
|
||||||
process.cwd(),
|
|
||||||
"src/data/discountCodes.json",
|
|
||||||
);
|
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const { code, percentage, expiration, description } = await request.json();
|
const { code, percentage, expiration, description } = await request.json();
|
||||||
|
|
||||||
const fileContent = await fs.readFile(discountCodesPath, "utf8");
|
const response = await fetch(`${DATA_SERVER_URL}/api/discount-codes`, {
|
||||||
const discountCodes = JSON.parse(fileContent);
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${process.env.ADMIN_PASSWORD}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ code, percentage, expiration, description }),
|
||||||
|
});
|
||||||
|
|
||||||
const newDiscountCode: DiscountCode = {
|
if (!response.ok) {
|
||||||
percentage,
|
const error = await response.json();
|
||||||
description,
|
throw new Error(error.error || "Failed to add discount code");
|
||||||
};
|
|
||||||
|
|
||||||
if (expiration) {
|
|
||||||
newDiscountCode.expiration = expiration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
discountCodes[code] = newDiscountCode;
|
const result = await response.json();
|
||||||
|
return NextResponse.json(result);
|
||||||
await fs.writeFile(
|
|
||||||
discountCodesPath,
|
|
||||||
JSON.stringify(discountCodes, null, 2),
|
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error adding discount code:", error);
|
console.error("Error adding discount code:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Failed to add discount code" },
|
{
|
||||||
|
error:
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "Failed to add discount code",
|
||||||
|
},
|
||||||
{ status: 500 },
|
{ status: 500 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -46,22 +40,32 @@ export async function DELETE(request: NextRequest) {
|
|||||||
try {
|
try {
|
||||||
const { code } = await request.json();
|
const { code } = await request.json();
|
||||||
|
|
||||||
const fileContent = await fs.readFile(discountCodesPath, "utf8");
|
const response = await fetch(
|
||||||
const discountCodes = JSON.parse(fileContent);
|
`${DATA_SERVER_URL}/api/discount-codes/${encodeURIComponent(code)}`,
|
||||||
|
{
|
||||||
delete discountCodes[code];
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
await fs.writeFile(
|
Authorization: `Bearer ${process.env.ADMIN_PASSWORD}`,
|
||||||
discountCodesPath,
|
},
|
||||||
JSON.stringify(discountCodes, null, 2),
|
},
|
||||||
"utf8",
|
|
||||||
);
|
);
|
||||||
|
|
||||||
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) {
|
} catch (error) {
|
||||||
console.error("Error deleting discount code:", error);
|
console.error("Error deleting discount code:", error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: "Failed to delete discount code" },
|
{
|
||||||
|
error:
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "Failed to delete discount code",
|
||||||
|
},
|
||||||
{ status: 500 },
|
{ status: 500 },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user