All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 6m14s
277 lines
8.1 KiB
TypeScript
277 lines
8.1 KiB
TypeScript
'use server';
|
|
|
|
import { revalidatePath } from 'next/cache';
|
|
import { auth } from '@/lib/auth';
|
|
import * as weatherService from '@/services/weather';
|
|
import { getUserById } from '@/services/auth';
|
|
import { broadcastToWeatherSession } from '@/app/api/weather/[id]/subscribe/route';
|
|
|
|
// ============================================
|
|
// Session Actions
|
|
// ============================================
|
|
|
|
export async function createWeatherSession(data: { title: string; date?: Date }) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
const weatherSession = await weatherService.createWeatherSession(session.user.id, data);
|
|
revalidatePath('/weather');
|
|
revalidatePath('/sessions');
|
|
return { success: true, data: weatherSession };
|
|
} catch (error) {
|
|
console.error('Error creating weather session:', error);
|
|
return { success: false, error: 'Erreur lors de la création' };
|
|
}
|
|
}
|
|
|
|
export async function updateWeatherSession(
|
|
sessionId: string,
|
|
data: { title?: string; date?: Date }
|
|
) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
await weatherService.updateWeatherSession(sessionId, authSession.user.id, data);
|
|
|
|
// Get user info for broadcast
|
|
const user = await getUserById(authSession.user.id);
|
|
if (!user) {
|
|
return { success: false, error: 'Utilisateur non trouvé' };
|
|
}
|
|
|
|
// Emit event for real-time sync
|
|
const event = await weatherService.createWeatherSessionEvent(
|
|
sessionId,
|
|
authSession.user.id,
|
|
'SESSION_UPDATED',
|
|
data
|
|
);
|
|
|
|
// Broadcast immediately via SSE
|
|
broadcastToWeatherSession(sessionId, {
|
|
type: 'SESSION_UPDATED',
|
|
payload: data,
|
|
userId: authSession.user.id,
|
|
user: { id: user.id, name: user.name, email: user.email },
|
|
timestamp: event.createdAt,
|
|
});
|
|
|
|
revalidatePath(`/weather/${sessionId}`);
|
|
revalidatePath('/weather');
|
|
revalidatePath('/sessions');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error updating weather session:', error);
|
|
return { success: false, error: 'Erreur lors de la mise à jour' };
|
|
}
|
|
}
|
|
|
|
export async function deleteWeatherSession(sessionId: string) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
await weatherService.deleteWeatherSession(sessionId, authSession.user.id);
|
|
revalidatePath('/weather');
|
|
revalidatePath('/sessions');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error deleting weather session:', error);
|
|
return { success: false, error: 'Erreur lors de la suppression' };
|
|
}
|
|
}
|
|
|
|
// ============================================
|
|
// Entry Actions
|
|
// ============================================
|
|
|
|
export async function createOrUpdateWeatherEntry(
|
|
sessionId: string,
|
|
data: {
|
|
performanceEmoji?: string | null;
|
|
moralEmoji?: string | null;
|
|
fluxEmoji?: string | null;
|
|
valueCreationEmoji?: string | null;
|
|
notes?: string | null;
|
|
}
|
|
) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
// Check edit permission
|
|
const canEdit = await weatherService.canEditWeatherSession(sessionId, authSession.user.id);
|
|
if (!canEdit) {
|
|
return { success: false, error: 'Permission refusée' };
|
|
}
|
|
|
|
try {
|
|
const entry = await weatherService.createOrUpdateWeatherEntry(sessionId, authSession.user.id, data);
|
|
|
|
// Get user info for broadcast
|
|
const user = await getUserById(authSession.user.id);
|
|
if (!user) {
|
|
return { success: false, error: 'Utilisateur non trouvé' };
|
|
}
|
|
|
|
// Emit event for real-time sync
|
|
const eventType = entry.createdAt.getTime() === entry.updatedAt.getTime() ? 'ENTRY_CREATED' : 'ENTRY_UPDATED';
|
|
const event = await weatherService.createWeatherSessionEvent(
|
|
sessionId,
|
|
authSession.user.id,
|
|
eventType,
|
|
{
|
|
entryId: entry.id,
|
|
userId: entry.userId,
|
|
...data,
|
|
}
|
|
);
|
|
|
|
// Broadcast immediately via SSE
|
|
broadcastToWeatherSession(sessionId, {
|
|
type: eventType,
|
|
payload: {
|
|
entryId: entry.id,
|
|
userId: entry.userId,
|
|
...data,
|
|
},
|
|
userId: authSession.user.id,
|
|
user: { id: user.id, name: user.name, email: user.email },
|
|
timestamp: event.createdAt,
|
|
});
|
|
|
|
revalidatePath(`/weather/${sessionId}`);
|
|
return { success: true, data: entry };
|
|
} catch (error) {
|
|
console.error('Error creating/updating weather entry:', error);
|
|
return { success: false, error: 'Erreur lors de la sauvegarde' };
|
|
}
|
|
}
|
|
|
|
export async function deleteWeatherEntry(sessionId: string) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
// Check edit permission
|
|
const canEdit = await weatherService.canEditWeatherSession(sessionId, authSession.user.id);
|
|
if (!canEdit) {
|
|
return { success: false, error: 'Permission refusée' };
|
|
}
|
|
|
|
try {
|
|
await weatherService.deleteWeatherEntry(sessionId, authSession.user.id);
|
|
|
|
// Get user info for broadcast
|
|
const user = await getUserById(authSession.user.id);
|
|
if (!user) {
|
|
return { success: false, error: 'Utilisateur non trouvé' };
|
|
}
|
|
|
|
// Emit event for real-time sync
|
|
const event = await weatherService.createWeatherSessionEvent(
|
|
sessionId,
|
|
authSession.user.id,
|
|
'ENTRY_DELETED',
|
|
{ userId: authSession.user.id }
|
|
);
|
|
|
|
// Broadcast immediately via SSE
|
|
broadcastToWeatherSession(sessionId, {
|
|
type: 'ENTRY_DELETED',
|
|
payload: { userId: authSession.user.id },
|
|
userId: authSession.user.id,
|
|
user: { id: user.id, name: user.name, email: user.email },
|
|
timestamp: event.createdAt,
|
|
});
|
|
|
|
revalidatePath(`/weather/${sessionId}`);
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error deleting weather entry:', error);
|
|
return { success: false, error: 'Erreur lors de la suppression' };
|
|
}
|
|
}
|
|
|
|
// ============================================
|
|
// Sharing Actions
|
|
// ============================================
|
|
|
|
export async function shareWeatherSession(
|
|
sessionId: string,
|
|
targetEmail: string,
|
|
role: 'VIEWER' | 'EDITOR' = 'EDITOR'
|
|
) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
const share = await weatherService.shareWeatherSession(
|
|
sessionId,
|
|
authSession.user.id,
|
|
targetEmail,
|
|
role
|
|
);
|
|
revalidatePath(`/weather/${sessionId}`);
|
|
return { success: true, data: share };
|
|
} catch (error) {
|
|
console.error('Error sharing weather session:', error);
|
|
const message = error instanceof Error ? error.message : 'Erreur lors du partage';
|
|
return { success: false, error: message };
|
|
}
|
|
}
|
|
|
|
export async function shareWeatherSessionToTeam(
|
|
sessionId: string,
|
|
teamId: string,
|
|
role: 'VIEWER' | 'EDITOR' = 'EDITOR'
|
|
) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
const shares = await weatherService.shareWeatherSessionToTeam(
|
|
sessionId,
|
|
authSession.user.id,
|
|
teamId,
|
|
role
|
|
);
|
|
revalidatePath(`/weather/${sessionId}`);
|
|
return { success: true, data: shares };
|
|
} catch (error) {
|
|
console.error('Error sharing weather session to team:', error);
|
|
const message = error instanceof Error ? error.message : 'Erreur lors du partage à l\'équipe';
|
|
return { success: false, error: message };
|
|
}
|
|
}
|
|
|
|
export async function removeWeatherShare(sessionId: string, shareUserId: string) {
|
|
const authSession = await auth();
|
|
if (!authSession?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
await weatherService.removeWeatherShare(sessionId, authSession.user.id, shareUserId);
|
|
revalidatePath(`/weather/${sessionId}`);
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error removing weather share:', error);
|
|
return { success: false, error: 'Erreur lors de la suppression du partage' };
|
|
}
|
|
}
|