167 lines
5.0 KiB
TypeScript
167 lines
5.0 KiB
TypeScript
import { getPayload } from 'payload'
|
||
import config from '@payload-config'
|
||
import { NextResponse } from 'next/server'
|
||
|
||
/**
|
||
* 清理数据库数据(保留 Users 和 Media)
|
||
* GET /api/clear-data?confirm=true
|
||
*/
|
||
export async function GET(request: Request) {
|
||
try {
|
||
const { searchParams } = new URL(request.url)
|
||
const confirm = searchParams.get('confirm')
|
||
|
||
// 安全检查:必须明确确认
|
||
if (confirm !== 'true') {
|
||
return NextResponse.json(
|
||
{
|
||
success: false,
|
||
error: '需要确认参数:?confirm=true',
|
||
message:
|
||
'此操作将删除 Products, Announcements, Articles 的所有数据(保留 Users 和 Media)',
|
||
},
|
||
{ status: 400 },
|
||
)
|
||
}
|
||
|
||
const payload = await getPayload({ config })
|
||
|
||
const results = {
|
||
products: 0,
|
||
preorderProducts: 0,
|
||
announcements: 0,
|
||
articles: 0,
|
||
errors: [] as string[],
|
||
}
|
||
|
||
// 清理 Products
|
||
try {
|
||
const deletedProducts = await payload.delete({
|
||
collection: 'products',
|
||
where: {},
|
||
})
|
||
results.products = deletedProducts.docs?.length || 0
|
||
console.log(`✅ 已清理 ${results.products} 个商品`)
|
||
} catch (error) {
|
||
const errorMsg = `清理 Products 失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||
console.error('❌', errorMsg)
|
||
results.errors.push(errorMsg)
|
||
}
|
||
|
||
// 清理 Preorder Products
|
||
try {
|
||
const deletedPreorderProducts = await payload.delete({
|
||
collection: 'preorder-products',
|
||
where: {},
|
||
})
|
||
results.preorderProducts = deletedPreorderProducts.docs?.length || 0
|
||
console.log(`✅ 已清理 ${results.preorderProducts} 个预购商品`)
|
||
} catch (error) {
|
||
const errorMsg = `清理 Preorder Products 失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||
console.error('❌', errorMsg)
|
||
results.errors.push(errorMsg)
|
||
}
|
||
|
||
// 清理 Announcements
|
||
try {
|
||
const deletedAnnouncements = await payload.delete({
|
||
collection: 'announcements',
|
||
where: {},
|
||
})
|
||
results.announcements = deletedAnnouncements.docs?.length || 0
|
||
console.log(`✅ 已清理 ${results.announcements} 个公告`)
|
||
} catch (error) {
|
||
const errorMsg = `清理 Announcements 失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||
console.error('❌', errorMsg)
|
||
results.errors.push(errorMsg)
|
||
}
|
||
|
||
// 清理 Articles
|
||
try {
|
||
const deletedArticles = await payload.delete({
|
||
collection: 'articles',
|
||
where: {},
|
||
})
|
||
results.articles = deletedArticles.docs?.length || 0
|
||
console.log(`✅ 已清理 ${results.articles} 个文章`)
|
||
} catch (error) {
|
||
const errorMsg = `清理 Articles 失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||
console.error('❌', errorMsg)
|
||
results.errors.push(errorMsg)
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: `数据清理完成!已删除 ${results.products} 个商品、${results.preorderProducts} 个预购商品、${results.announcements} 个公告、${results.articles} 个文章`,
|
||
results,
|
||
})
|
||
} catch (error) {
|
||
console.error('Clear data error:', error)
|
||
return NextResponse.json(
|
||
{
|
||
success: false,
|
||
error: error instanceof Error ? error.message : 'Unknown error',
|
||
},
|
||
{ status: 500 },
|
||
)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* POST /api/clear-data
|
||
* 带认证的清理接口
|
||
*/
|
||
export async function POST(request: Request) {
|
||
try {
|
||
const payload = await getPayload({ config })
|
||
|
||
// 可选:添加认证检查
|
||
// const { user } = await payload.auth({ headers: request.headers })
|
||
// if (!user?.roles?.includes('admin')) {
|
||
// return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||
// }
|
||
|
||
const body = await request.json()
|
||
const { collections = ['products', 'announcements', 'articles'] } = body
|
||
|
||
const results: Record<string, number> = {}
|
||
const errors: string[] = []
|
||
|
||
for (const collection of collections) {
|
||
if (['users', 'media'].includes(collection)) {
|
||
errors.push(`跳过受保护的集合: ${collection}`)
|
||
continue
|
||
}
|
||
|
||
try {
|
||
const deleted = await payload.delete({
|
||
collection,
|
||
where: {},
|
||
})
|
||
results[collection] = deleted.docs?.length || 0
|
||
console.log(`✅ 已清理 ${results[collection]} 个 ${collection}`)
|
||
} catch (error) {
|
||
const errorMsg = `清理 ${collection} 失败: ${error instanceof Error ? error.message : '未知错误'}`
|
||
console.error('❌', errorMsg)
|
||
errors.push(errorMsg)
|
||
}
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
message: '数据清理完成',
|
||
results,
|
||
errors: errors.length > 0 ? errors : undefined,
|
||
})
|
||
} catch (error) {
|
||
console.error('Clear data error:', error)
|
||
return NextResponse.json(
|
||
{
|
||
success: false,
|
||
error: error instanceof Error ? error.message : 'Unknown error',
|
||
},
|
||
{ status: 500 },
|
||
)
|
||
}
|
||
}
|