diff --git a/apps/discord-server/modules/discord.ts b/apps/discord-server/modules/discord.ts index 2445a20f..95bc7052 100644 --- a/apps/discord-server/modules/discord.ts +++ b/apps/discord-server/modules/discord.ts @@ -1,7 +1,7 @@ import { Client, GatewayIntentBits } from "discord.js"; const client = new Client({ - intents: [GatewayIntentBits.Guilds], + intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers], }); const token = process.env.DISCORD_BOT_TOKEN; diff --git a/apps/discord-server/routes/member.ts b/apps/discord-server/routes/member.ts index 4a2ddc99..f6278887 100644 --- a/apps/discord-server/routes/member.ts +++ b/apps/discord-server/routes/member.ts @@ -51,12 +51,15 @@ const handleRoleChange = (action: "add" | "remove") => async (req: Request, res: ? roleIds.filter((id: string) => !currentRoleIds.includes(id)) : roleIds.filter((id: string) => currentRoleIds.includes(id)); - console.log(`Attempting to ${action} roles:`, filteredRoleIds, roleIds); + console.log( + `Attempting to ${action} roles: ${filteredRoleIds.join(", ")} to member ${member.nickname || member.user.username}`, + ); // Option to skip if no roles to add/remove - /* if (filteredRoleIds.length === 0) { + if (filteredRoleIds.length === 0) { + console.log(`No roles to ${action}`); res.status(200).json({ message: `No roles to ${action}` }); return; - } */ + } await member.roles[action](roleIds); res.status(200).json({ message: `Roles ${action}ed successfully` }); diff --git a/apps/hub-server/modules/chron.ts b/apps/hub-server/modules/chron.ts index da56e3e8..2caf14df 100644 --- a/apps/hub-server/modules/chron.ts +++ b/apps/hub-server/modules/chron.ts @@ -1,10 +1,11 @@ import { getMoodleCourseCompletionStatus, getMoodleUserById } from "./moodle"; import { CronJob } from "cron"; -import { DISCORD_ROLES, prisma } from "@repo/db"; +import { DISCORD_ROLES, ParticipantLog, prisma } from "@repo/db"; import { sendCourseCompletedEmail } from "modules/mail"; import { handleParticipantFinished } from "modules/event"; import { eventCompleted } from "helper/events"; import { addRolesToMember, removeRolesFromMember } from "modules/discord"; +import { JsonValueType } from "@repo/db/zod"; const syncMoodleIds = async () => { try { @@ -122,9 +123,23 @@ const checkUnfinishedParticipants = async () => { if (completed) return; + if (!p.Event.discordRoleId) { + await prisma.participant.update({ + where: { + id: p.id, + }, + data: { + inscriptionWorkflowCompleted: true, + }, + }); + return; + } + console.log( + `User ${p.User.firstname} ${p.User.lastname} - ${p.User.publicId} did not finish event ${p.Event.name}`, + ); if (p.User.discordAccounts[0] && p.Event.discordRoleId) { await addRolesToMember(p.User.discordAccounts[0].discordId, [p.Event.discordRoleId]); - prisma.participant.update({ + await prisma.participant.update({ where: { id: p.id, }, @@ -135,7 +150,7 @@ const checkUnfinishedParticipants = async () => { event: "Discord-Rolle hinzugefügt", timestamp: new Date(), user: "system", - }, + } as ParticipantLog as any, }, }, }); diff --git a/apps/hub-server/modules/mail-templates/CourseCompleted.tsx b/apps/hub-server/modules/mail-templates/CourseCompleted.tsx index e09953da..fe8e8c25 100644 --- a/apps/hub-server/modules/mail-templates/CourseCompleted.tsx +++ b/apps/hub-server/modules/mail-templates/CourseCompleted.tsx @@ -117,7 +117,7 @@ const Template = ({ event, user }: { user: User; event: Event }) => ( {event.finishedBadges.map((badge) => ( - + ))} diff --git a/apps/hub/app/(app)/admin/event/_components/ParticipantModal.tsx b/apps/hub/app/(app)/admin/event/_components/ParticipantModal.tsx index ab12c784..3217ed2a 100644 --- a/apps/hub/app/(app)/admin/event/_components/ParticipantModal.tsx +++ b/apps/hub/app/(app)/admin/event/_components/ParticipantModal.tsx @@ -11,17 +11,12 @@ interface ParticipantModalProps { ref: RefObject; } -export const ParticipantModal = ({ - participantForm, - ref, -}: ParticipantModalProps) => { +export const ParticipantModal = ({ participantForm, ref }: ParticipantModalProps) => { return (
- +

Teilnehmer bearbeiten

- - + + + Termin ausgewählt am - - {new Date( - participantForm.watch("enscriptionDate"), - ).toLocaleString()} - + {new Date(participantForm.watch("enscriptionDate")).toLocaleString()}

Verlauf

- {( - participantForm.watch("statusLog") as unknown as ParticipantLog[] - )?.map((s) => ( + {(participantForm.watch("statusLog") as unknown as ParticipantLog[])?.map((s) => (

{s.event}

{s.user}

diff --git a/apps/hub/app/(app)/admin/user/[id]/_components/forms.tsx b/apps/hub/app/(app)/admin/user/[id]/_components/forms.tsx index 4829b591..9b3202ab 100644 --- a/apps/hub/app/(app)/admin/user/[id]/_components/forms.tsx +++ b/apps/hub/app/(app)/admin/user/[id]/_components/forms.tsx @@ -161,13 +161,6 @@ export const ConnectionHistory: React.FC<{ user: User }> = ({ user }: { user: Us

Dispo-Verbindungs Historie -

({ ))} ))} + {table.getRowModel().rows.length === 0 && ( + + + Keine Daten gefunden + + + )}
diff --git a/apps/hub/app/api/discord-redirect/route.ts b/apps/hub/app/api/discord-redirect/route.ts index 5e68eb52..335b8445 100644 --- a/apps/hub/app/api/discord-redirect/route.ts +++ b/apps/hub/app/api/discord-redirect/route.ts @@ -1,7 +1,8 @@ import axios, { AxiosError } from "axios"; import { NextRequest, NextResponse } from "next/server"; -import { DiscordAccount, prisma, PrismaClient } from "@repo/db"; +import { DISCORD_ROLES, DiscordAccount, getPublicUser, prisma, PrismaClient } from "@repo/db"; import { getServerSession } from "../auth/[...nextauth]/auth"; +import { addRolesToMember, removeRolesFromMember, renameMember } from "../../../helper/discord"; export const GET = async (req: NextRequest) => { const session = await getServerSession(); @@ -67,8 +68,25 @@ export const GET = async (req: NextRequest) => { update: discordObject, // Updates if found create: discordObject, // Creates if not found }); + const user = await prisma.user.findUnique({ + where: { id: session.user.id }, + }); + if (user) { + await renameMember(discordUser.id, `${getPublicUser(user).fullName} - ${user?.publicId}`); + } + if (user?.permissions.includes("PILOT")) { + await addRolesToMember(discordUser.id, [DISCORD_ROLES.PILOT]); + } else { + await removeRolesFromMember(discordUser.id, [DISCORD_ROLES.PILOT]); + } + if (user?.permissions.includes("DISPO")) { + await addRolesToMember(discordUser.id, [DISCORD_ROLES.ONLINE_DISPATCHER]); + } else { + await removeRolesFromMember(discordUser.id, [DISCORD_ROLES.PILOT]); + } + return NextResponse.redirect(`${process.env.NEXT_PUBLIC_HUB_URL}/settings`); - } catch (error: any) { + } catch (error) { console.error(error); return NextResponse.json( { diff --git a/apps/hub/helper/discord.ts b/apps/hub/helper/discord.ts index 546f5d9a..f591c9ef 100644 --- a/apps/hub/helper/discord.ts +++ b/apps/hub/helper/discord.ts @@ -2,7 +2,7 @@ import axios from "axios"; const discordAxiosClient = axios.create({ - baseURL: process.env.DISCORD_SERVER_URL || "https://discord.com/api/v10", + baseURL: process.env.DISCORD_SERVER_URL || "http://localhost:3005", }); export const renameMember = async (memberId: string, newName: string) => {