mirror of
https://github.com/beilunyang/moemail.git
synced 2026-05-11 18:11:27 +08:00
feat: profile page & webhook notification
This commit is contained in:
69
app/api/webhook/route.ts
Normal file
69
app/api/webhook/route.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { auth } from "@/lib/auth"
|
||||
import { createDb } from "@/lib/db"
|
||||
import { webhooks } from "@/lib/schema"
|
||||
import { eq } from "drizzle-orm"
|
||||
import { z } from "zod"
|
||||
|
||||
export const runtime = "edge"
|
||||
|
||||
const webhookSchema = z.object({
|
||||
url: z.string().url(),
|
||||
enabled: z.boolean()
|
||||
})
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth()
|
||||
|
||||
const db = createDb()
|
||||
const webhook = await db.query.webhooks.findFirst({
|
||||
where: eq(webhooks.userId, session!.user!.id!)
|
||||
})
|
||||
|
||||
return Response.json(webhook || { enabled: false, url: "" })
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { url, enabled } = webhookSchema.parse(body)
|
||||
|
||||
const db = createDb()
|
||||
const now = new Date()
|
||||
|
||||
const existingWebhook = await db.query.webhooks.findFirst({
|
||||
where: eq(webhooks.userId, session.user.id)
|
||||
})
|
||||
|
||||
if (existingWebhook) {
|
||||
await db
|
||||
.update(webhooks)
|
||||
.set({
|
||||
url,
|
||||
enabled,
|
||||
updatedAt: now
|
||||
})
|
||||
.where(eq(webhooks.userId, session.user.id))
|
||||
} else {
|
||||
await db
|
||||
.insert(webhooks)
|
||||
.values({
|
||||
userId: session.user.id,
|
||||
url,
|
||||
enabled,
|
||||
})
|
||||
}
|
||||
|
||||
return Response.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error("Failed to save webhook:", error)
|
||||
return Response.json(
|
||||
{ error: "Invalid request" },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
}
|
||||
39
app/api/webhook/test/route.ts
Normal file
39
app/api/webhook/test/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { callWebhook } from "@/lib/webhook"
|
||||
import { WEBHOOK_CONFIG } from "@/config"
|
||||
import { z } from "zod"
|
||||
import { EmailMessage } from "@/lib/webhook"
|
||||
|
||||
export const runtime = "edge"
|
||||
|
||||
const testSchema = z.object({
|
||||
url: z.string().url()
|
||||
})
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { url } = testSchema.parse(body)
|
||||
|
||||
await callWebhook(url, {
|
||||
event: WEBHOOK_CONFIG.EVENTS.NEW_MESSAGE,
|
||||
data: {
|
||||
emailId: "123456789",
|
||||
messageId: '987654321',
|
||||
fromAddress: "sender@example.com",
|
||||
subject: "Test Email",
|
||||
content: "This is a test email.",
|
||||
html: "<p>This is a <strong>test</strong> email.</p>",
|
||||
receivedAt: "2023-03-01T12:00:00Z",
|
||||
toAddress: "recipient@example.com"
|
||||
} as EmailMessage
|
||||
})
|
||||
|
||||
return Response.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error("Failed to test webhook:", error)
|
||||
return Response.json(
|
||||
{ error: "Failed to test webhook" },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user