88 lines
4.0 KiB
TypeScript

import { z } from 'zod'
import {
AUTO_LISTING_AUTH_TYPE,
AUTO_LISTING_DELIVERY_STATUS,
SUPPORTED_AUTO_LISTING_EVENT_TYPES,
XIAOYI_AUTO_LISTING_EVENT_TYPE,
backfillXiaoyiDeliveriesForTarget,
buildExternalNotificationTargetAdminView,
createExternalNotificationTarget,
listExternalNotificationDeliveries,
listExternalNotificationTargets,
setExternalNotificationTargetActive,
summarizeExternalNotificationDelivery,
updateExternalNotificationTarget,
} from '@/lib/external-auto-listing'
import { autoListingFailure, autoListingResponse, targetIdSchema, withAutoListingSuperAdmin } from './_shared'
const saveTarget = z.object({
id: targetIdSchema.optional(),
name: z.string().trim().min(1).max(120),
callbackUrl: z.string().trim().min(1).max(2048),
authType: z.nativeEnum(AUTO_LISTING_AUTH_TYPE).optional(),
authConfig: z.object({
token: z.string().max(4096).optional(),
headerName: z.string().max(128).optional(),
headerValue: z.string().max(4096).optional(),
}).strict().optional(),
eventTypes: z.array(z.enum(SUPPORTED_AUTO_LISTING_EVENT_TYPES)).min(1).optional(),
isActive: z.boolean().optional(),
backfillHistorical: z.boolean().optional(),
}).strict()
const toggleTarget = z.object({ id: targetIdSchema, isActive: z.boolean(), backfillHistorical: z.boolean().optional() }).strict()
export async function GET(request: Request) {
return withAutoListingSuperAdmin(async () => {
const { searchParams } = new URL(request.url)
const status = z.nativeEnum(AUTO_LISTING_DELIVERY_STATUS).safeParse(searchParams.get('status'))
const query = searchParams.get('q')?.trim()
if (query && query.length > 160) return autoListingFailure('检索内容过长', 400)
const targets = await listExternalNotificationTargets()
const deliveries = await listExternalNotificationDeliveries({
canAccessOperationsG: true,
status: status.success ? status.data : undefined,
...(query && { query }),
limit: 50,
})
return autoListingResponse({
success: true,
data: {
targets: targets.map(buildExternalNotificationTargetAdminView),
recentDeliveries: deliveries.map(summarizeExternalNotificationDelivery),
},
})
})
}
export async function POST(request: Request) {
return withAutoListingSuperAdmin(async () => {
const result = saveTarget.safeParse(await request.json().catch(() => null))
if (!result.success) return autoListingFailure('请检查目标名称、回调地址、鉴权和订阅事件', 400)
const { id, backfillHistorical, ...input } = result.data
const target = id
? await updateExternalNotificationTarget(id, input)
: await createExternalNotificationTarget({
...input,
eventTypes: input.eventTypes ?? [XIAOYI_AUTO_LISTING_EVENT_TYPE],
isActive: input.isActive ?? false,
})
if (!target) return autoListingFailure('回调目标不存在', 404)
const shouldBackfill = backfillHistorical !== false && target.isActive && (!id || input.isActive === true)
const backfill = shouldBackfill ? await backfillXiaoyiDeliveriesForTarget(target.id) : { enqueued: 0 }
return autoListingResponse({ success: true, data: buildExternalNotificationTargetAdminView(target), backfill })
})
}
export async function PATCH(request: Request) {
return withAutoListingSuperAdmin(async () => {
const result = toggleTarget.safeParse(await request.json().catch(() => null))
if (!result.success) return autoListingFailure('请提供回调目标和启停状态', 400)
const target = await setExternalNotificationTargetActive(result.data.id, result.data.isActive)
if (!target) return autoListingFailure('回调目标不存在', 404)
const shouldBackfill = result.data.backfillHistorical !== false && target.isActive
const backfill = shouldBackfill ? await backfillXiaoyiDeliveriesForTarget(target.id) : { enqueued: 0 }
return autoListingResponse({ success: true, data: buildExternalNotificationTargetAdminView(target), backfill })
})
}