feat: support delete email with api

This commit is contained in:
oiov
2025-04-23 21:55:20 +08:00
parent e5a173f25a
commit cfba5a7628
6 changed files with 85 additions and 4 deletions

View File

@@ -20,6 +20,12 @@ export async function GET(req: NextRequest) {
{ status: 401 },
);
}
if (user.active === 0) {
return Response.json("Forbidden", {
status: 403,
statusText: "Forbidden",
});
}
const { searchParams } = new URL(req.url);
const emailAddress = searchParams.get("emailAddress");

View File

@@ -1,7 +1,11 @@
import { NextRequest, NextResponse } from "next/server";
import { checkApiKey } from "@/lib/dto/api-key";
import { createUserEmail, getAllUserEmailsCount } from "@/lib/dto/email";
import {
createUserEmail,
deleteUserEmailByAddress,
getAllUserEmailsCount,
} from "@/lib/dto/email";
import { reservedAddressSuffix } from "@/lib/enums";
import { Team_Plan_Quota } from "@/lib/team";
@@ -24,6 +28,12 @@ export async function POST(req: NextRequest) {
{ status: 401 },
);
}
if (user.active === 0) {
return Response.json("Forbidden", {
status: 403,
statusText: "Forbidden",
});
}
// check quota
const user_address_count = await getAllUserEmailsCount(user.id);
@@ -71,3 +81,45 @@ export async function POST(req: NextRequest) {
return NextResponse.json(error.message, { status: 500 });
}
}
export async function DELETE(req: NextRequest) {
const custom_api_key = req.headers.get("wrdo-api-key");
if (!custom_api_key) {
return Response.json("Unauthorized", {
status: 401,
});
}
// Check if the API key is valid
const user = await checkApiKey(custom_api_key);
if (!user?.id) {
return Response.json(
"Invalid API key. You can get your API key from https://wr.do/dashboard/settings.",
{ status: 401 },
);
}
if (user.active === 0) {
return Response.json("Forbidden", {
status: 403,
statusText: "Forbidden",
});
}
const { emailAddress } = await req.json();
if (!emailAddress) {
return NextResponse.json("Missing email address parameter", {
status: 400,
});
}
try {
await deleteUserEmailByAddress(emailAddress);
return NextResponse.json("success", { status: 201 });
} catch (error) {
console.error("Error deleting user email:", error);
if (error.message === "User email not found or already deleted") {
return NextResponse.json(error.message, { status: 404 });
}
return NextResponse.json("Internal Server Error", { status: 500 });
}
}

View File

@@ -156,4 +156,11 @@ On success (Status 200):
The `DELETE /api/v1/email` endpoint allows you to delete a specific email address.
working on it.
```bash
curl -X DELETE \
-H "wrdo-api-key: YOUR_API_KEY" \
-d '{
"emailAddress": "your-suffix@wr.do"
}' \
"https://wr.do/api/v1/email"
```

View File

@@ -12,7 +12,7 @@ export const getApiKeyByUserId = async (userId: string) => {
export const checkApiKey = async (apiKey: string) => {
return prisma.user.findFirst({
where: { apiKey, active: 1 },
select: { id: true, team: true, name: true },
select: { id: true, team: true, name: true, active: true },
});
};

View File

@@ -230,6 +230,22 @@ export async function deleteUserEmail(id: string) {
});
}
}
// 删除 UserEmail (软删除)
export async function deleteUserEmailByAddress(email: string) {
const userEmail = await prisma.userEmail.findFirst({
where: { emailAddress: email, deletedAt: null },
});
console.log("userEmail", userEmail);
if (userEmail) {
await prisma.userEmail.update({
where: { emailAddress: email },
data: { deletedAt: new Date() },
});
} else {
throw new Error("User email not found or already deleted");
}
}
// 通过 emailAddress 查询邮件列表
export async function getEmailsByEmailAddress(

File diff suppressed because one or more lines are too long