138 lines
4.1 KiB
TypeScript
138 lines
4.1 KiB
TypeScript
'use server';
|
|
|
|
import { revalidatePath } from 'next/cache';
|
|
import { auth } from '@/lib/auth';
|
|
import * as sessionsService from '@/services/sessions';
|
|
|
|
export async function updateSessionTitle(sessionId: string, title: string) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
if (!title.trim()) {
|
|
return { success: false, error: 'Le titre ne peut pas être vide' };
|
|
}
|
|
|
|
try {
|
|
const result = await sessionsService.updateSession(sessionId, session.user.id, {
|
|
title: title.trim(),
|
|
});
|
|
|
|
if (result.count === 0) {
|
|
return { success: false, error: 'Session non trouvée ou non autorisé' };
|
|
}
|
|
|
|
// Emit event for real-time sync
|
|
await sessionsService.createSessionEvent(sessionId, session.user.id, 'SESSION_UPDATED', {
|
|
title: title.trim(),
|
|
});
|
|
|
|
revalidatePath(`/sessions/${sessionId}`);
|
|
revalidatePath('/sessions');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error updating session title:', error);
|
|
return { success: false, error: 'Erreur lors de la mise à jour' };
|
|
}
|
|
}
|
|
|
|
export async function updateSessionCollaborator(sessionId: string, collaborator: string) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
if (!collaborator.trim()) {
|
|
return { success: false, error: 'Le nom du collaborateur ne peut pas être vide' };
|
|
}
|
|
|
|
try {
|
|
const result = await sessionsService.updateSession(sessionId, session.user.id, {
|
|
collaborator: collaborator.trim(),
|
|
});
|
|
|
|
if (result.count === 0) {
|
|
return { success: false, error: 'Session non trouvée ou non autorisé' };
|
|
}
|
|
|
|
// Emit event for real-time sync
|
|
await sessionsService.createSessionEvent(sessionId, session.user.id, 'SESSION_UPDATED', {
|
|
collaborator: collaborator.trim(),
|
|
});
|
|
|
|
revalidatePath(`/sessions/${sessionId}`);
|
|
revalidatePath('/sessions');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error updating session collaborator:', error);
|
|
return { success: false, error: 'Erreur lors de la mise à jour' };
|
|
}
|
|
}
|
|
|
|
export async function updateSwotSession(
|
|
sessionId: string,
|
|
data: { title?: string; collaborator?: string }
|
|
) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
if (data.title !== undefined && !data.title.trim()) {
|
|
return { success: false, error: 'Le titre ne peut pas être vide' };
|
|
}
|
|
|
|
if (data.collaborator !== undefined && !data.collaborator.trim()) {
|
|
return { success: false, error: 'Le nom du collaborateur ne peut pas être vide' };
|
|
}
|
|
|
|
try {
|
|
const updateData: { title?: string; collaborator?: string } = {};
|
|
if (data.title) updateData.title = data.title.trim();
|
|
if (data.collaborator) updateData.collaborator = data.collaborator.trim();
|
|
|
|
const result = await sessionsService.updateSession(sessionId, session.user.id, updateData);
|
|
|
|
if (result.count === 0) {
|
|
return { success: false, error: 'Session non trouvée ou non autorisé' };
|
|
}
|
|
|
|
// Emit event for real-time sync
|
|
await sessionsService.createSessionEvent(
|
|
sessionId,
|
|
session.user.id,
|
|
'SESSION_UPDATED',
|
|
updateData
|
|
);
|
|
|
|
revalidatePath(`/sessions/${sessionId}`);
|
|
revalidatePath('/sessions');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error updating session:', error);
|
|
return { success: false, error: 'Erreur lors de la mise à jour' };
|
|
}
|
|
}
|
|
|
|
export async function deleteSwotSession(sessionId: string) {
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
try {
|
|
const result = await sessionsService.deleteSession(sessionId, session.user.id);
|
|
|
|
if (result.count === 0) {
|
|
return { success: false, error: 'Session non trouvée ou non autorisé' };
|
|
}
|
|
|
|
revalidatePath('/sessions');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error deleting session:', error);
|
|
return { success: false, error: 'Erreur lors de la suppression' };
|
|
}
|
|
}
|