Remplace le système de notifications stockées par un système de providers qui dérivent les notifications des données existantes (commentaires, réponses, demandes de projet, demandes de rendez-vous, validations de brief). - Ajout du NotificationCollector et de l'interface NotificationProvider - Création de 5 providers : Comment, Reply, ProjectRequest, AppointmentRequest, Content - Métadonnées de notifications stockées directement sur les entités source - Nouvelles routes mark-as-read et mark-all-read - Mise à jour du frontend pour le nouveau système - Route de migration pour les données existantes Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
102 lines
2.9 KiB
JavaScript
102 lines
2.9 KiB
JavaScript
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,
|
|
};
|
|
});
|