Refactor admin actions and improve code formatting: Standardize import statements, enhance error handling messages, and apply consistent formatting across event, user, and preference management functions for better readability and maintainability.
Some checks failed
Deploy with Docker Compose / deploy (push) Has been cancelled
Some checks failed
Deploy with Docker Compose / deploy (push) Has been cancelled
This commit is contained in:
@@ -1,48 +1,50 @@
|
||||
'use server'
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from 'next/cache'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { sitePreferencesService } from '@/services/preferences/site-preferences.service'
|
||||
import { Role } from '@/prisma/generated/prisma/client'
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { sitePreferencesService } from "@/services/preferences/site-preferences.service";
|
||||
import { Role } from "@/prisma/generated/prisma/client";
|
||||
|
||||
function checkAdminAccess() {
|
||||
return async () => {
|
||||
const session = await auth()
|
||||
const session = await auth();
|
||||
if (!session?.user || session.user.role !== Role.ADMIN) {
|
||||
throw new Error('Accès refusé')
|
||||
throw new Error("Accès refusé");
|
||||
}
|
||||
return session
|
||||
}
|
||||
return session;
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateSitePreferences(data: {
|
||||
homeBackground?: string | null
|
||||
eventsBackground?: string | null
|
||||
leaderboardBackground?: string | null
|
||||
homeBackground?: string | null;
|
||||
eventsBackground?: string | null;
|
||||
leaderboardBackground?: string | null;
|
||||
}) {
|
||||
try {
|
||||
await checkAdminAccess()()
|
||||
await checkAdminAccess()();
|
||||
|
||||
const preferences = await sitePreferencesService.updateSitePreferences({
|
||||
homeBackground: data.homeBackground,
|
||||
eventsBackground: data.eventsBackground,
|
||||
leaderboardBackground: data.leaderboardBackground,
|
||||
})
|
||||
});
|
||||
|
||||
revalidatePath('/admin')
|
||||
revalidatePath('/')
|
||||
revalidatePath('/events')
|
||||
revalidatePath('/leaderboard')
|
||||
revalidatePath("/admin");
|
||||
revalidatePath("/");
|
||||
revalidatePath("/events");
|
||||
revalidatePath("/leaderboard");
|
||||
|
||||
return { success: true, data: preferences }
|
||||
return { success: true, data: preferences };
|
||||
} catch (error) {
|
||||
console.error('Error updating admin preferences:', error)
|
||||
console.error("Error updating admin preferences:", error);
|
||||
|
||||
if (error instanceof Error && error.message === 'Accès refusé') {
|
||||
return { success: false, error: 'Accès refusé' }
|
||||
if (error instanceof Error && error.message === "Accès refusé") {
|
||||
return { success: false, error: "Accès refusé" };
|
||||
}
|
||||
|
||||
return { success: false, error: 'Erreur lors de la mise à jour des préférences' }
|
||||
return {
|
||||
success: false,
|
||||
error: "Erreur lors de la mise à jour des préférences",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user