59 lines
1.7 KiB
TypeScript
59 lines
1.7 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";
|
|
import { safeUrl } from "@/lib/utils";
|
|
|
|
export async function createEvent(data: {
|
|
title: string;
|
|
description: string;
|
|
date: string;
|
|
location: string;
|
|
thumbnailUrl: string;
|
|
link?: string;
|
|
tags?: string[];
|
|
}) {
|
|
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, '');
|
|
|
|
let validatedLink: string | null = null;
|
|
if (data.link && data.link.trim()) {
|
|
const checked = safeUrl(data.link.trim());
|
|
if (checked !== '#') {
|
|
validatedLink = checked;
|
|
}
|
|
}
|
|
|
|
const event = await prisma.event.create({
|
|
data: {
|
|
title: data.title.trim().slice(0, 200),
|
|
description: sanitizeHTML(data.description || ''),
|
|
date: new Date(data.date),
|
|
location: data.location.trim().slice(0, 200),
|
|
thumbnailUrl: data.thumbnailUrl,
|
|
link: validatedLink,
|
|
tags: Array.isArray(data.tags) ? data.tags.slice(0, 20) : [],
|
|
status: 'PENDING' as ContentStatus, // Always pending for user submission
|
|
slug: `${slug}-${Math.random().toString(36).substring(2, 7)}`
|
|
}
|
|
});
|
|
|
|
revalidatePath("/evenements");
|
|
return { success: true, event };
|
|
} catch (error) {
|
|
console.error("Failed to create event:", error);
|
|
return { success: false, error: "Erreur lors de la création de l'événement" };
|
|
}
|
|
}
|