diff --git a/crm/api/notifications.py b/crm/api/notifications.py index 70abee02..61a6c02b 100644 --- a/crm/api/notifications.py +++ b/crm/api/notifications.py @@ -1,5 +1,5 @@ import frappe - +from frappe.query_builder import Order @frappe.whitelist() def get_notifications(): @@ -11,8 +11,28 @@ def get_notifications(): frappe.qb.from_(Notification) .select("*") .where(Notification.to_user == frappe.session.user) - .where(Notification.read == False) - .orderby("creation") + .orderby("creation", order=Order.desc) ) notifications = query.run(as_dict=True) - return notifications + + _notifications = [] + for notification in notifications: + reference_doc = frappe.get_value("Comment", notification.comment, ['reference_doctype', 'reference_name']) + _notifications.append({ + "creation": notification.creation, + "from_user": { + "name": notification.from_user, + "full_name": frappe.get_value( + "User", notification.from_user, "full_name" + ), + }, + "type": notification.type, + "to_user": notification.to_user, + "read": notification.read, + "comment": notification.comment, + "reference_doctype": "deal" if reference_doc[0] == "CRM Deal" else "lead", + "reference_name": reference_doc[1], + "route_name": "Deal" if reference_doc[0] == "CRM Deal" else "Lead", + }) + + return _notifications diff --git a/frontend/src/stores/notifications.js b/frontend/src/stores/notifications.js index dbe83977..764da87e 100644 --- a/frontend/src/stores/notifications.js +++ b/frontend/src/stores/notifications.js @@ -8,14 +8,15 @@ export const notificationsStore = defineStore('crm-notifications', () => { let visible = ref(false) let unreadNotifications = reactive([]) + let allNotifications = reactive([]) const notifications = createResource({ url: 'crm.api.notifications.get_notifications', - cache: 'crm-notifications', initialData: [], auto: true, transform(data) { - unreadNotifications = data + allNotifications = data + unreadNotifications = data.filter((d) => !d.read) return data }, }) @@ -28,9 +29,14 @@ export const notificationsStore = defineStore('crm-notifications', () => { return unreadNotifications || [] } + function getAllNotifications() { + return allNotifications || [] + } + return { visible, toggle, + getAllNotifications, getUnreadNotifications, } })