import { defineStore, storeToRefs } from 'pinia'; import { ref, computed } from 'vue'; import { useProjectsStore } from './projects'; export const useUserStore = defineStore('user', () => { const user = ref(null); const isLogged = computed(() => { return user.value?.hasOwnProperty('role'); }); const { projects } = storeToRefs(useProjectsStore()); /** * Liste des notifications agrégées depuis tous les projets. * Les notifications sont maintenant dérivées côté backend avec isRead pré-calculé. */ const notifications = computed(() => { if (!projects.value || !user.value) return []; return projects.value.flatMap((project) => { if (!project.notifications) return []; return project.notifications.map((notification) => ({ ...notification, project: project, // isRead est maintenant fourni par le backend })); }); }); /** * Marque une notification comme lue dans le store local. * @param {string} notificationId - L'ID de la notification * @param {string} projectUri - L'URI du projet (optionnel, pour retrouver le projet) */ function markNotificationRead(notificationId, projectUri = null) { if (!user.value?.uuid) return; projects.value = projects.value.map((project) => { // Si projectUri fourni, cibler le bon projet if (projectUri && project.uri !== projectUri && `/${project.uri}` !== projectUri) { return project; } return { ...project, notifications: (project.notifications || []).map((notification) => notification.id === notificationId ? { ...notification, isRead: true, readby: [...new Set([...(notification.readby || []), user.value.uuid])], } : notification ), }; }); } /** * Marque toutes les notifications comme lues dans le store local. */ function markAllNotificationsRead() { if (!user.value?.uuid) return; projects.value = projects.value.map((project) => ({ ...project, notifications: (project.notifications || []).map((notification) => ({ ...notification, isRead: true, readby: [...new Set([...(notification.readby || []), user.value.uuid])], })), })); } // Anciennes fonctions gardées pour rétro-compatibilité function readNotification(notificationId, projectId) { markNotificationRead(notificationId, projectId); } function readAllNotifications() { markAllNotificationsRead(); } function canEditComment(comment) { return user.value.uuid === comment.author.uuid; } return { user, isLogged, notifications, // Nouvelles fonctions markNotificationRead, markAllNotificationsRead, // Anciennes fonctions (rétro-compatibilité) readNotification, readAllNotifications, canEditComment, }; });