From ea11cfd23baebb7cffdaf3e01f0bf12fdd4dd376 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 14 Jan 2026 12:29:24 +0100 Subject: [PATCH] Vision Refactor --- app/api/groups/route.ts | 50 +++++++++++++++++++++++++++- app/api/missions/route.ts | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/app/api/groups/route.ts b/app/api/groups/route.ts index 24d0382..e011d20 100644 --- a/app/api/groups/route.ts +++ b/app/api/groups/route.ts @@ -2,6 +2,7 @@ import { getServerSession } from "next-auth/next"; import { authOptions } from "@/app/api/auth/options"; import { NextResponse } from "next/server"; import { logger } from '@/lib/logger'; +import { prisma } from '@/lib/prisma'; async function getAdminToken() { try { @@ -200,8 +201,55 @@ export async function POST(req: Request) { throw new Error('Échec de la création du groupe'); } + const groupData = await response.json(); + const groupId = groupData.id || Date.now().toString(); + + // Create calendar for the group creator + // Color palette for calendars + const colorPalette = [ + "#4f46e5", // Indigo + "#0891b2", // Cyan + "#0e7490", // Teal + "#16a34a", // Green + "#65a30d", // Lime + "#ca8a04", // Amber + "#d97706", // Orange + "#dc2626", // Red + "#e11d48", // Rose + "#9333ea", // Purple + "#7c3aed", // Violet + "#2563eb", // Blue + ]; + + // Select a color based on group name hash for consistency + const colorIndex = name.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0) % colorPalette.length; + const calendarColor = colorPalette[colorIndex]; + + try { + await prisma.calendar.create({ + data: { + name: `Groupe: ${name}`, + color: calendarColor, + description: `Calendrier pour le groupe "${name}"`, + userId: session.user.id, + }, + }); + logger.debug('Group calendar created successfully', { + groupId: groupId, + groupName: name, + userId: session.user.id + }); + } catch (calendarError) { + logger.error('Error creating group calendar', { + error: calendarError instanceof Error ? calendarError.message : String(calendarError), + groupId: groupId, + groupName: name + }); + // Don't fail group creation if calendar creation fails + } + return NextResponse.json({ - id: Date.now().toString(), + id: groupId, name, path: `/${name}`, membersCount: 0 diff --git a/app/api/missions/route.ts b/app/api/missions/route.ts index d71d90f..a657ff1 100644 --- a/app/api/missions/route.ts +++ b/app/api/missions/route.ts @@ -266,6 +266,74 @@ export async function POST(request: Request) { name: mission.name }); + // Step 1.5: Create calendar for mission members + // Color palette for calendars + const colorPalette = [ + "#4f46e5", // Indigo + "#0891b2", // Cyan + "#0e7490", // Teal + "#16a34a", // Green + "#65a30d", // Lime + "#ca8a04", // Amber + "#d97706", // Orange + "#dc2626", // Red + "#e11d48", // Rose + "#9333ea", // Purple + "#7c3aed", // Violet + "#2563eb", // Blue + ]; + + // Select a color based on mission name hash for consistency + const colorIndex = mission.name.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0) % colorPalette.length; + const calendarColor = colorPalette[colorIndex]; + + // Collect all user IDs who should have access to this mission calendar + const missionMemberIds = new Set([userId]); // Start with creator + + // Add guardians + if (body.guardians) { + for (const guardianId of Object.values(body.guardians)) { + if (guardianId) { + missionMemberIds.add(guardianId); + } + } + } + + // Add volunteers + if (body.volunteers && body.volunteers.length > 0) { + body.volunteers.forEach((volunteerId: string) => { + if (volunteerId) { + missionMemberIds.add(volunteerId); + } + }); + } + + // Create calendar for each mission member + try { + const calendarPromises = Array.from(missionMemberIds).map(async (memberId) => { + return prisma.calendar.create({ + data: { + name: `Mission: ${mission.name}`, + color: calendarColor, + description: `Calendrier pour la mission "${mission.name}"`, + userId: memberId, + }, + }); + }); + + await Promise.all(calendarPromises); + logger.debug('Mission calendars created successfully', { + missionId: mission.id, + calendarsCount: missionMemberIds.size + }); + } catch (calendarError) { + logger.error('Error creating mission calendars', { + error: calendarError instanceof Error ? calendarError.message : String(calendarError), + missionId: mission.id + }); + // Don't fail mission creation if calendar creation fails + } + // Step 2: Create mission users (guardians and volunteers) const missionUsers = [];