2025-04-30 16:21:56 +02:00
|
|
|
import { defineStore, storeToRefs } from 'pinia';
|
|
|
|
|
import { ref, computed } from 'vue';
|
|
|
|
|
import { useProjectsStore } from './projects';
|
2024-09-10 12:09:53 +02:00
|
|
|
|
2025-04-30 16:21:56 +02:00
|
|
|
export const useUserStore = defineStore('user', () => {
|
2024-09-10 12:09:53 +02:00
|
|
|
const user = ref(null);
|
|
|
|
|
|
2025-04-30 16:21:56 +02:00
|
|
|
const isLogged = computed(() => {
|
|
|
|
|
return user.value?.hasOwnProperty('role');
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-10 17:40:45 +01:00
|
|
|
const { projects } = storeToRefs(useProjectsStore());
|
|
|
|
|
|
2026-01-15 10:31:31 +01:00
|
|
|
/**
|
|
|
|
|
* Liste des notifications agrégées depuis tous les projets.
|
|
|
|
|
* Les notifications sont maintenant dérivées côté backend avec isRead pré-calculé.
|
|
|
|
|
*/
|
2025-01-15 15:06:38 +01:00
|
|
|
const notifications = computed(() => {
|
2026-01-15 10:31:31 +01:00
|
|
|
if (!projects.value || !user.value) return [];
|
|
|
|
|
|
|
|
|
|
return projects.value.flatMap((project) => {
|
2025-01-15 15:06:38 +01:00
|
|
|
if (!project.notifications) return [];
|
|
|
|
|
|
2026-01-15 10:31:31 +01:00
|
|
|
return project.notifications.map((notification) => ({
|
|
|
|
|
...notification,
|
|
|
|
|
project: project,
|
|
|
|
|
// isRead est maintenant fourni par le backend
|
|
|
|
|
}));
|
2025-01-15 15:06:38 +01:00
|
|
|
});
|
|
|
|
|
});
|
2025-01-15 14:56:17 +01:00
|
|
|
|
2026-01-15 10:31:31 +01:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
});
|
2024-11-18 09:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 10:31:31 +01:00
|
|
|
/**
|
|
|
|
|
* Marque toutes les notifications comme lues dans le store local.
|
|
|
|
|
*/
|
|
|
|
|
function markAllNotificationsRead() {
|
|
|
|
|
if (!user.value?.uuid) return;
|
|
|
|
|
|
2025-01-15 15:09:58 +01:00
|
|
|
projects.value = projects.value.map((project) => ({
|
|
|
|
|
...project,
|
2026-01-15 10:31:31 +01:00
|
|
|
notifications: (project.notifications || []).map((notification) => ({
|
2025-01-15 15:09:58 +01:00
|
|
|
...notification,
|
2026-01-15 10:31:31 +01:00
|
|
|
isRead: true,
|
|
|
|
|
readby: [...new Set([...(notification.readby || []), user.value.uuid])],
|
2025-01-15 15:09:58 +01:00
|
|
|
})),
|
|
|
|
|
}));
|
2024-11-18 13:45:40 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 10:31:31 +01:00
|
|
|
// Anciennes fonctions gardées pour rétro-compatibilité
|
|
|
|
|
function readNotification(notificationId, projectId) {
|
|
|
|
|
markNotificationRead(notificationId, projectId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readAllNotifications() {
|
|
|
|
|
markAllNotificationsRead();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 11:33:15 +01:00
|
|
|
function canEditComment(comment) {
|
|
|
|
|
return user.value.uuid === comment.author.uuid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
user,
|
2025-04-30 16:21:56 +02:00
|
|
|
isLogged,
|
2025-01-07 11:33:15 +01:00
|
|
|
notifications,
|
2026-01-15 10:31:31 +01:00
|
|
|
// Nouvelles fonctions
|
|
|
|
|
markNotificationRead,
|
|
|
|
|
markAllNotificationsRead,
|
|
|
|
|
// Anciennes fonctions (rétro-compatibilité)
|
2025-01-07 11:33:15 +01:00
|
|
|
readNotification,
|
|
|
|
|
readAllNotifications,
|
|
|
|
|
canEditComment,
|
|
|
|
|
};
|
2024-09-10 12:09:53 +02:00
|
|
|
});
|