Deduplicate tag validation and regex escaping into shared auto-imported utils. Add tag validation to wiki patch/batch-tag routes. Remove duplicate tags field from event schema.
30 lines
676 B
JavaScript
30 lines
676 B
JavaScript
import * as z from 'zod'
|
|
import WikiArticle from '../../../models/wikiArticle.js'
|
|
import { connectDB } from '../../../utils/mongoose.js'
|
|
|
|
const wikiTagsSchema = z.object({
|
|
tags: z.array(z.string())
|
|
})
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await requireAdmin(event)
|
|
|
|
const body = await validateBody(event, wikiTagsSchema)
|
|
const id = getRouterParam(event, 'id')
|
|
|
|
await connectDB()
|
|
|
|
await validateTagSlugs(body.tags)
|
|
|
|
const article = await WikiArticle.findByIdAndUpdate(
|
|
id,
|
|
{ tags: body.tags },
|
|
{ new: true }
|
|
)
|
|
|
|
if (!article) {
|
|
throw createError({ statusCode: 404, statusMessage: 'Article not found' })
|
|
}
|
|
|
|
return article
|
|
})
|