53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
"use server";
|
|
|
|
import { prisma } from "@/lib/prisma";
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
export async function reactivateUserAccount(userId: string, reason: string) {
|
|
try {
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: userId },
|
|
select: { id: true, name: true, email: true, deletedAt: true }
|
|
});
|
|
|
|
if (!user) {
|
|
return { success: false, error: "Utilisateur non trouvé" };
|
|
}
|
|
|
|
if (!user.deletedAt) {
|
|
return { success: false, error: "Ce compte est déjà actif" };
|
|
}
|
|
|
|
await prisma.user.update({
|
|
where: { id: userId },
|
|
data: {
|
|
deletedAt: null,
|
|
deletionScheduledAt: null,
|
|
reactivationReason: reason
|
|
}
|
|
});
|
|
|
|
// Optionnel : Envoyer un mail de confirmation
|
|
const settings = await prisma.siteSetting.findUnique({ where: { id: "singleton" } });
|
|
if (settings?.reactivateAccountTemplate) {
|
|
const { sendEmail } = await import("@/lib/mail");
|
|
const html = settings.reactivateAccountTemplate
|
|
.replace(/{name}/g, user.name)
|
|
.replace(/{siteName}/g, settings.siteName)
|
|
.replace(/{reason}/g, reason);
|
|
|
|
await sendEmail({
|
|
to: user.email,
|
|
subject: settings.reactivateAccountSubject,
|
|
html
|
|
});
|
|
}
|
|
|
|
revalidatePath("/");
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error("Reactivation Error:", error);
|
|
return { success: false, error: "Une erreur est survenue lors de la réactivation" };
|
|
}
|
|
}
|