1
0
forked from test/crm

fix: mark all as read and mark comment as read

This commit is contained in:
Shariq Ansari 2024-01-29 23:51:01 +05:30
parent ee66df3827
commit 8a3502bfa1
3 changed files with 62 additions and 43 deletions

View File

@ -1,6 +1,7 @@
import frappe import frappe
from frappe.query_builder import Order from frappe.query_builder import Order
@frappe.whitelist() @frappe.whitelist()
def get_notifications(): def get_notifications():
if frappe.session.user == "Guest": if frappe.session.user == "Guest":
@ -17,22 +18,43 @@ def get_notifications():
_notifications = [] _notifications = []
for notification in notifications: for notification in notifications:
reference_doc = frappe.get_value("Comment", notification.comment, ['reference_doctype', 'reference_name']) reference_doc = frappe.get_value(
_notifications.append({ "Comment", notification.comment, ["reference_doctype", "reference_name"]
"creation": notification.creation, )
"from_user": { _notifications.append(
"name": notification.from_user, {
"full_name": frappe.get_value( "creation": notification.creation,
"User", notification.from_user, "full_name" "from_user": {
), "name": notification.from_user,
}, "full_name": frappe.get_value(
"type": notification.type, "User", notification.from_user, "full_name"
"to_user": notification.to_user, ),
"read": notification.read, },
"comment": notification.comment, "type": notification.type,
"reference_doctype": "deal" if reference_doc[0] == "CRM Deal" else "lead", "to_user": notification.to_user,
"reference_name": reference_doc[1], "read": notification.read,
"route_name": "Deal" if reference_doc[0] == "CRM Deal" else "Lead", "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 return _notifications
@frappe.whitelist()
def mark_as_read(user=None, comment=None):
if frappe.session.user == "Guest":
frappe.throw("Authentication failed", exc=frappe.AuthenticationError)
user = user or frappe.session.user
filters = {"to_user": user, "read": False}
if comment:
filters["comment"] = comment
for n in frappe.get_all("CRM Notification", filters=filters):
d = frappe.get_doc("CRM Notification", n.name)
d.read = True
d.save()

View File

@ -18,7 +18,7 @@
<Button <Button
theme="blue" theme="blue"
variant="ghost" variant="ghost"
@click="() => notificationsStore().clear()" @click="() => notificationsStore().mark_as_read.reload()"
> >
<template #icon> <template #icon>
<FeatherIcon name="trash-2" class="h-4 w-4" /> <FeatherIcon name="trash-2" class="h-4 w-4" />
@ -37,11 +37,11 @@
</div> </div>
<div class="divide-y text-base"> <div class="divide-y text-base">
<RouterLink <RouterLink
v-for="n in allNotifications" v-for="n in notificationsStore().allNotifications"
:key="n.name" :key="n.comment"
class="flex cursor-pointer items-start gap-3.5 px-5 py-2.5 hover:bg-gray-100"
:to="getRoute(n)" :to="getRoute(n)"
@click="() => toggleNotificationPanel()" class="flex cursor-pointer items-start gap-3.5 px-5 py-2.5 hover:bg-gray-100"
@click="mark_as_read(n.comment)"
> >
<UserAvatar :user="n.from_user.name" size="md" /> <UserAvatar :user="n.from_user.name" size="md" />
<span> <span>
@ -94,9 +94,9 @@ function toggleNotificationPanel() {
notificationsStore().toggle() notificationsStore().toggle()
} }
const allNotifications = computed(() => { function mark_as_read(comment) {
return notificationsStore().getAllNotifications() notificationsStore().mark_comment_as_read(comment)
}) }
function getRoute(notification) { function getRoute(notification) {
let params = { let params = {

View File

@ -1,42 +1,39 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { sessionStore } from '@/stores/session'
import { createResource } from 'frappe-ui' import { createResource } from 'frappe-ui'
import { reactive, ref } from 'vue' import { computed, ref } from 'vue'
export const notificationsStore = defineStore('crm-notifications', () => { export const notificationsStore = defineStore('crm-notifications', () => {
const { user } = sessionStore()
let visible = ref(false) let visible = ref(false)
let unreadNotifications = reactive([])
let allNotifications = reactive([])
const notifications = createResource({ const notifications = createResource({
url: 'crm.api.notifications.get_notifications', url: 'crm.api.notifications.get_notifications',
initialData: [], initialData: [],
auto: true, auto: true,
transform(data) { })
allNotifications = data
unreadNotifications = data.filter((d) => !d.read) const mark_as_read = createResource({
return data url: 'crm.api.notifications.mark_as_read',
}, auto: false,
onSuccess: () => notifications.reload(),
}) })
function toggle() { function toggle() {
visible.value = !visible.value visible.value = !visible.value
} }
function getUnreadNotifications() { const allNotifications = computed(() => notifications.data || [])
return unreadNotifications || []
}
function getAllNotifications() { function mark_comment_as_read(comment) {
return allNotifications || [] mark_as_read.params = { comment: comment }
mark_as_read.reload()
toggle()
} }
return { return {
visible, visible,
allNotifications,
mark_as_read,
mark_comment_as_read,
toggle, toggle,
getAllNotifications,
getUnreadNotifications,
} }
}) })