50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
"use server";
|
|
|
|
import { prisma } from "@/lib/prisma";
|
|
import { revalidatePath } from "next/cache";
|
|
import { ContentStatus } from "@prisma/client";
|
|
import { getAuthUser } from "@/lib/auth-utils";
|
|
import { sanitizeHTML } from "@/lib/sanitize";
|
|
|
|
export async function createNews(data: {
|
|
title: string;
|
|
excerpt: string;
|
|
content: string;
|
|
author: string;
|
|
imageUrl: string;
|
|
tags?: string[];
|
|
sources?: any;
|
|
}) {
|
|
try {
|
|
const authUser = await getAuthUser();
|
|
if (!authUser) {
|
|
return { success: false, error: "Authentification requise" };
|
|
}
|
|
|
|
const slug = data.title
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/(^-|-$)/g, '');
|
|
|
|
const post = await prisma.blogPost.create({
|
|
data: {
|
|
title: data.title.trim().slice(0, 200),
|
|
excerpt: data.excerpt.trim().slice(0, 500),
|
|
content: sanitizeHTML(data.content || ''),
|
|
author: data.author.trim().slice(0, 100),
|
|
imageUrl: data.imageUrl,
|
|
tags: Array.isArray(data.tags) ? data.tags.slice(0, 20) : [],
|
|
sources: data.sources || [],
|
|
status: 'PENDING' as ContentStatus, // Always pending for user submission
|
|
slug: `${slug}-${Math.random().toString(36).substring(2, 7)}`
|
|
}
|
|
});
|
|
|
|
revalidatePath("/actualites");
|
|
return { success: true, post };
|
|
} catch (error) {
|
|
console.error("Failed to create news:", error);
|
|
return { success: false, error: "Erreur lors de la création de l'actualité" };
|
|
}
|
|
}
|