62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { auth } from "@/lib/auth";
|
|
import { userService } from "@/services/users/user.service";
|
|
import { CharacterClass } from "@/prisma/generated/prisma/client";
|
|
import { ValidationError, ConflictError } from "@/services/errors";
|
|
|
|
export async function updateProfile(data: {
|
|
username?: string;
|
|
avatar?: string | null;
|
|
bio?: string | null;
|
|
characterClass?: string | null;
|
|
}) {
|
|
try {
|
|
const session = await auth();
|
|
|
|
if (!session?.user) {
|
|
return { success: false, error: "Non authentifié" };
|
|
}
|
|
|
|
const updatedUser = await userService.validateAndUpdateUserProfile(
|
|
session.user.id,
|
|
{
|
|
username: data.username,
|
|
avatar: data.avatar,
|
|
bio: data.bio,
|
|
characterClass: data.characterClass
|
|
? (data.characterClass as CharacterClass)
|
|
: null,
|
|
},
|
|
{
|
|
id: true,
|
|
email: true,
|
|
username: true,
|
|
avatar: true,
|
|
bio: true,
|
|
characterClass: true,
|
|
hp: true,
|
|
maxHp: true,
|
|
xp: true,
|
|
maxXp: true,
|
|
level: true,
|
|
score: true,
|
|
}
|
|
);
|
|
|
|
revalidatePath("/profile");
|
|
revalidatePath("/");
|
|
|
|
return { success: true, data: updatedUser };
|
|
} catch (error) {
|
|
console.error("Error updating profile:", error);
|
|
|
|
if (error instanceof ValidationError || error instanceof ConflictError) {
|
|
return { success: false, error: error.message };
|
|
}
|
|
|
|
return { success: false, error: "Erreur lors de la mise à jour du profil" };
|
|
}
|
|
}
|