Füge E-Mail-Benachrichtigungen für Sperrungen und zeitlich begrenzte Sperrungen hinzu
This commit is contained in:
@@ -56,6 +56,7 @@ import { setStandardName } from "../../../../../../helper/discord";
|
||||
import { penaltyColumns } from "(app)/admin/penalty/columns";
|
||||
import { addPenalty, editPenaltys } from "(app)/admin/penalty/actions";
|
||||
import { reportColumns } from "(app)/admin/report/columns";
|
||||
import { sendMail, sendMailByTemplate } from "../../../../../../helper/mail";
|
||||
|
||||
interface ProfileFormProps {
|
||||
user: User;
|
||||
@@ -349,6 +350,12 @@ export const UserPenalties = ({ user }: { user: User }) => {
|
||||
userId: user.id,
|
||||
createdUserId: createdUser.id,
|
||||
});
|
||||
if (user.email) {
|
||||
await sendMailByTemplate(user.email, "timeban-notice", {
|
||||
user,
|
||||
staffName: createdUser.firstname + " " + createdUser.lastname,
|
||||
});
|
||||
}
|
||||
penaltyTable.current?.refresh();
|
||||
toast.success("Time-Ban wurde hinzugefügt!");
|
||||
}}
|
||||
@@ -370,6 +377,12 @@ export const UserPenalties = ({ user }: { user: User }) => {
|
||||
userId: user.id,
|
||||
createdUserId: createdUser.id,
|
||||
});
|
||||
if (user.email) {
|
||||
await sendMailByTemplate(user.email, "ban-notice", {
|
||||
user,
|
||||
staffName: createdUser.firstname + " " + createdUser.lastname,
|
||||
});
|
||||
}
|
||||
await editUser(user.id, { isBanned: true });
|
||||
penaltyTable.current?.refresh();
|
||||
toast.success("Ban wurde hinzugefügt!");
|
||||
|
||||
@@ -58,42 +58,6 @@ export const deleteUser = async (id: string) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const checkEmailCode = async (code: string) => {
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
emailVerificationToken: code,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
emailVerificationToken: true,
|
||||
emailVerificationExpiresAt: true,
|
||||
},
|
||||
});
|
||||
const user = users[0];
|
||||
|
||||
if (!user || !user.emailVerificationExpiresAt) {
|
||||
return { error: "Code ist ungültig" };
|
||||
}
|
||||
|
||||
if (user.emailVerificationExpiresAt < new Date()) {
|
||||
return { error: "Code ist nicht mehr gültig" };
|
||||
}
|
||||
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
data: {
|
||||
emailVerified: true,
|
||||
emailVerificationToken: null,
|
||||
emailVerificationExpiresAt: null,
|
||||
},
|
||||
});
|
||||
return {
|
||||
message: "Email bestätigt!",
|
||||
};
|
||||
};
|
||||
|
||||
export const sendVerificationLink = async (userId: string) => {
|
||||
const code = Math.floor(10000 + Math.random() * 90000).toString();
|
||||
|
||||
|
||||
@@ -24,13 +24,16 @@ import { UserOptionalDefaults, UserOptionalDefaultsSchema } from "@repo/db/zod";
|
||||
import { Bell, CircleAlert, Plane, Trash2 } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { deleteUser, sendVerificationLink } from "(app)/admin/user/action";
|
||||
import { setStandardName } from "../../../../helper/discord";
|
||||
|
||||
export const ProfileForm = ({
|
||||
user,
|
||||
penaltys,
|
||||
discordAccount,
|
||||
}: {
|
||||
user: User;
|
||||
penaltys: Penalty[];
|
||||
discordAccount?: DiscordAccount;
|
||||
}): React.JSX.Element => {
|
||||
const canEdit = penaltys.length === 0 && !user.isBanned;
|
||||
|
||||
@@ -67,12 +70,19 @@ export const ProfileForm = ({
|
||||
},
|
||||
resolver: zodResolver(schema),
|
||||
});
|
||||
console.log(user);
|
||||
return (
|
||||
<form
|
||||
className="card-body"
|
||||
onSubmit={form.handleSubmit(async (values) => {
|
||||
setIsLoading(true);
|
||||
await updateUser(values);
|
||||
if (discordAccount) {
|
||||
await setStandardName({
|
||||
memberId: discordAccount.discordId,
|
||||
userId: user.id,
|
||||
});
|
||||
}
|
||||
form.reset(values);
|
||||
if (user.email !== values.email) {
|
||||
await sendVerificationLink(user.id);
|
||||
|
||||
@@ -40,7 +40,7 @@ export default async function Page() {
|
||||
</p>
|
||||
</div>
|
||||
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
|
||||
<ProfileForm user={user} penaltys={userPenaltys} />
|
||||
<ProfileForm user={user} penaltys={userPenaltys} discordAccount={discordAccount} />
|
||||
</div>
|
||||
<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
|
||||
<SocialForm discordAccount={discordAccount} user={user} />
|
||||
|
||||
39
apps/hub/app/(auth)/email-verification/action.ts
Normal file
39
apps/hub/app/(auth)/email-verification/action.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
"use server";
|
||||
|
||||
import { prisma } from "@repo/db";
|
||||
|
||||
export const checkEmailCode = async (code: string) => {
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
emailVerificationToken: code,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
emailVerificationToken: true,
|
||||
emailVerificationExpiresAt: true,
|
||||
},
|
||||
});
|
||||
const user = users[0];
|
||||
|
||||
if (!user || !user.emailVerificationExpiresAt) {
|
||||
return { error: "Code ist ungültig" };
|
||||
}
|
||||
|
||||
if (user.emailVerificationExpiresAt < new Date()) {
|
||||
return { error: "Code ist nicht mehr gültig" };
|
||||
}
|
||||
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
data: {
|
||||
emailVerified: true,
|
||||
emailVerificationToken: null,
|
||||
emailVerificationExpiresAt: null,
|
||||
},
|
||||
});
|
||||
return {
|
||||
message: "Email bestätigt!",
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
"use client";
|
||||
import { checkEmailCode } from "(app)/admin/user/action";
|
||||
import { checkEmailCode } from "(auth)/email-verification/action";
|
||||
import { Check } from "lucide-react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
@@ -13,11 +13,16 @@ export const sendMail = async (email: string, subject: string, html: string) =>
|
||||
|
||||
export const sendMailByTemplate = async (
|
||||
email: string,
|
||||
template: "password-change" | "course-completed" | "email-verification",
|
||||
template:
|
||||
| "password-change"
|
||||
| "course-completed"
|
||||
| "email-verification"
|
||||
| "ban-notice"
|
||||
| "timeban-notice",
|
||||
data: any,
|
||||
) => {
|
||||
try {
|
||||
const res = await fetch(`${process.env.NEXT_PUBLIC_HUB_SERVER_URL}/mail/template/${template}`, {
|
||||
await fetch(`${process.env.NEXT_PUBLIC_HUB_SERVER_URL}/mail/template/${template}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
|
||||
@@ -26,7 +31,6 @@ export const sendMailByTemplate = async (
|
||||
data,
|
||||
}),
|
||||
});
|
||||
console.log(res);
|
||||
} catch (error) {
|
||||
console.error("Error sending mail:", error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user