Vision Refactor
This commit is contained in:
parent
6ffa76f3cf
commit
ea11cfd23b
@ -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
|
||||
|
||||
@ -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<string>([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 = [];
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user