diff --git a/src/i18n/en/index.js b/src/i18n/en/index.js index 18fcf78c3..f36636139 100644 --- a/src/i18n/en/index.js +++ b/src/i18n/en/index.js @@ -292,11 +292,8 @@ export default { notificationsManager: { activeNotifications: 'Active notifications', availableNotifications: 'Available notifications', - table: { - name: 'Permission description', - subscribed: 'Subscribed', - role: 'Necessary role', - } + subscribed: 'Subscribed to the notification', + unsubscribed: 'Unsubscribed from the notification', }, }, components: { diff --git a/src/i18n/es/index.js b/src/i18n/es/index.js index c079c0629..22044e7cc 100644 --- a/src/i18n/es/index.js +++ b/src/i18n/es/index.js @@ -290,6 +290,8 @@ export default { notificationsManager: { activeNotifications: 'Notificaciones activas', availableNotifications: 'Notificaciones disponibles', + subscribed: 'Te has suscrito a la notificación', + unsubscribed: 'Te has dado de baja de la notificación', }, }, components: { diff --git a/src/pages/Worker/Card/WorkerNotificationsManager.vue b/src/pages/Worker/Card/WorkerNotificationsManager.vue index 19728db17..eca1c38de 100644 --- a/src/pages/Worker/Card/WorkerNotificationsManager.vue +++ b/src/pages/Worker/Card/WorkerNotificationsManager.vue @@ -2,6 +2,7 @@ import { onMounted, computed, ref } from 'vue'; import { useRoute } from 'vue-router'; import { useI18n } from 'vue-i18n'; +import { useQuasar } from 'quasar'; import { useSession } from 'src/composables/useSession'; import axios from 'axios'; @@ -18,8 +19,7 @@ onMounted(async () => await fetch()); const route = useRoute(); const { t } = useI18n(); const { getToken } = useSession(); - -t == t; +const quasar = useQuasar(); const entityId = computed(() => { return $props.id || route.params.id; @@ -47,6 +47,9 @@ const workerFilter = { }, ], }; + +const notificationAcls = ref([]); + async function fetch() { const { data } = await axios.get(`Workers/${entityId.value}`, { params: { @@ -81,14 +84,15 @@ async function fetch() { data.subscribedNotifs = subscribedNotifs.data; const filterAcl = { - where: { - roleFk: data.user.roleFk, - }, include: [ { relation: 'notification', scope: { - relation: 'notificationSubscription', + include: [ + { + relation: 'subscription', + }, + ], }, }, { @@ -106,13 +110,121 @@ async function fetch() { }, }); - data.notifsAcl = notifsAcl.data; + let notifications = []; + + notifsAcl.data.forEach((acl) => { + let notification = { + id: acl.notification.id, + name: acl.notification.name, + description: acl.notification.description, + active: false, + allowed: false, + }; + + if (acl.roleFk == data.user.roleFk) { + notification.allowed = true; + } else { + notification.allowed = false; + } + + if (data.subscribedNotifs.length > 0) { + data.subscribedNotifs.forEach((sub) => { + if (sub.notificationFk == acl.notification.id) { + if (notification.allowed) { + notification.active = true; + } + } + }); + } + + // Check if notification is already in the array + let found = false; + notifications.forEach((notif) => { + if (notif.id == notification.id) { + found = true; + } + }); + + if (!found) { + if (!notification.allowed) { + notification.active = null; + } + notifications.push(notification); + } else { + // Check if current notification is allowed and the other one is not + // If so, replace the existing one with the current one + notifications.forEach((notif) => { + if (notif.id == notification.id) { + if (notification.allowed && !notif.allowed) { + notif.allowed = true; + notif.active = notification.active; + } + } + }); + } + }); + notificationAcls.value = notifications; + sortNotifs(); } catch (e) { console.log(e); } worker.value = data; } + +function sortNotifs() { + notificationAcls.value.sort((a, b) => { + if (a.allowed && !b.allowed) { + return -1; + } else if (!a.allowed && b.allowed) { + return 1; + } else { + if (a.active && !b.active) { + return 1; + } else if (!a.active && b.active) { + return -1; + } else { + return 0; + } + } + }); +} + +async function toggleNotif(notif, chip) { + if (chip) { + notif.active = !notif.active; + } + if (notif.active) { + await axios.post( + `NotificationSubscriptions`, + { + notificationFk: notif.id, + userFk: entityId.value, + }, + { + headers: { + Authorization: getToken(), + }, + } + ); + quasar.notify({ + type: 'positive', + message: t('worker.notificationsManager.subscribed'), + }); + } else { + await axios.post(`NotificationSubscriptions/deleteSubscription`, { + notificationId: notif.id + '', + userId: entityId.value, + headers: { + Authorization: getToken(), + }, + }); + quasar.notify({ + type: 'positive', + message: t('worker.notificationsManager.unsubscribed'), + }); + } +}