Make all articles public

This commit is contained in:
Jennie Robinson Faber 2025-11-15 13:52:12 +00:00
parent 555b200ec7
commit ef432e3f74
23 changed files with 53 additions and 52 deletions

View file

@ -1,36 +1,36 @@
import { Article } from '../../models/Article'
import { requireAuth } from '../../utils/auth'
import { Article } from "../../models/Article";
import { requireAuth } from "../../utils/auth";
export default defineEventHandler(async (event) => {
// Require authentication
const user = await requireAuth(event)
const user = await requireAuth(event);
// Check if user can create articles
if (!user.permissions.canEdit) {
throw createError({
statusCode: 403,
statusMessage: 'You do not have permission to create articles'
})
statusMessage: "You do not have permission to create articles",
});
}
// Get request body
const body = await readBody(event)
const body = await readBody(event);
// Validate required fields
if (!body.title || !body.slug || !body.content) {
throw createError({
statusCode: 400,
statusMessage: 'Title, slug, and content are required'
})
statusMessage: "Title, slug, and content are required",
});
}
// Check if slug already exists
const existing = await Article.findOne({ slug: body.slug })
const existing = await Article.findOne({ slug: body.slug });
if (existing) {
throw createError({
statusCode: 409,
statusMessage: 'An article with this slug already exists'
})
statusMessage: "An article with this slug already exists",
});
}
// Create article
@ -38,34 +38,35 @@ export default defineEventHandler(async (event) => {
const article = await Article.create({
slug: body.slug,
title: body.title,
description: body.description || '',
description: body.description || "",
content: body.content,
category: body.category || 'general',
category: body.category || "general",
tags: body.tags || [],
accessLevel: body.accessLevel || 'member',
accessLevel: body.accessLevel || "public",
cohorts: body.cohorts || [],
author: user.userId,
status: body.status || 'draft',
publishedAt: body.status === 'published' ? new Date() : null,
revisions: [{
content: body.content,
author: user.userId,
message: 'Initial creation',
createdAt: new Date()
}]
})
status: body.status || "draft",
publishedAt: body.status === "published" ? new Date() : null,
revisions: [
{
content: body.content,
author: user.userId,
message: "Initial creation",
createdAt: new Date(),
},
],
});
return {
success: true,
slug: article.slug,
message: 'Article created successfully'
}
message: "Article created successfully",
};
} catch (error) {
console.error('Error creating article:', error)
console.error("Error creating article:", error);
throw createError({
statusCode: 500,
statusMessage: 'Failed to create article'
})
statusMessage: "Failed to create article",
});
}
})
});