1
0
forked from test/crm
jcrm/crm/api/notifications.py

74 lines
2.5 KiB
Python

import jingrow
from jingrow.query_builder import Order
@jingrow.whitelist()
def get_notifications():
Notification = jingrow.qb.PageType("CRM Notification")
query = (
jingrow.qb.from_(Notification)
.select("*")
.where(Notification.to_user == jingrow.session.user)
.orderby("creation", order=Order.desc)
)
notifications = query.run(as_dict=True)
_notifications = []
for notification in notifications:
_notifications.append(
{
"creation": notification.creation,
"from_user": {
"name": notification.from_user,
"full_name": jingrow.get_value(
"User", notification.from_user, "full_name"
),
},
"type": notification.type,
"to_user": notification.to_user,
"read": notification.read,
"hash": get_hash(notification),
"notification_text": notification.notification_text,
"notification_type_pagetype": notification.notification_type_pagetype,
"notification_type_pg": notification.notification_type_pg,
"reference_pagetype": (
"deal" if notification.reference_pagetype == "CRM Deal" else "lead"
),
"reference_name": notification.reference_name,
"route_name": (
"Deal" if notification.reference_pagetype == "CRM Deal" else "Lead"
),
}
)
return _notifications
@jingrow.whitelist()
def mark_as_read(user=None, pg=None):
user = user or jingrow.session.user
filters = {"to_user": user, "read": False}
or_filters = []
if pg:
or_filters = [
{"comment": pg},
{"notification_type_pg": pg},
]
for n in jingrow.get_all("CRM Notification", filters=filters, or_filters=or_filters):
d = jingrow.get_pg("CRM Notification", n.name)
d.read = True
d.save()
def get_hash(notification):
_hash = ""
if notification.type == "Mention" and notification.notification_type_pg:
_hash = "#" + notification.notification_type_pg
if notification.type == "WhatsApp":
_hash = "#whatsapp"
if notification.type == "Assignment" and notification.notification_type_pagetype == "CRM Task":
_hash = "#tasks"
if "has been removed by" in notification.message:
_hash = ""
return _hash