Skip to content

Commit 89ffac9

Browse files
feat(rc_site): add tracking fields for registration and synchronization status
1 parent 2d29188 commit 89ffac9

3 files changed

Lines changed: 115 additions & 19 deletions

File tree

raven_cloud/api/notification.py

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import frappe
66
from firebase_admin import messaging
77
from frappe import _
8+
from frappe.utils import now_datetime
89
from frappe.utils.response import Response
910

1011
from raven_cloud.utils.fcm import get_app
@@ -27,6 +28,16 @@ def register_site(site_name: str):
2728
}
2829
).insert()
2930

31+
# update the last registered on and last registered by fields - to keep track of the last time the site was registered and by whom
32+
frappe.db.set_value(
33+
'RC Site',
34+
site_name,
35+
{
36+
'last_registered_on': now_datetime(),
37+
'last_registered_by': frappe.session.user,
38+
}
39+
)
40+
3041
fcm_settings = frappe.get_doc('RC FCM Settings')
3142

3243
return {
@@ -495,26 +506,34 @@ def import_user_tokens(site_name: str, tokens: str):
495506
Tokens for users NOT in the incoming payload are left untouched,
496507
making this safe to call with partial/chunked token lists.
497508
"""
498-
499-
if isinstance(tokens, str):
500-
tokens = json.loads(tokens)
501-
502-
# if no tokens are present, return
503-
if not tokens:
504-
return {
505-
"status": "success",
506-
}
507-
508509
check_if_site_exists(site_name)
509510

510-
# building an incoming set for faster lookup
511-
incoming_users = list({token.get("user") for token in tokens})
512-
incoming_tokens = {(token.get("user"), token.get("fcm_token")) for token in tokens}
511+
try:
512+
if isinstance(tokens, str):
513+
tokens = json.loads(tokens)
514+
515+
# if no tokens are present, return
516+
if not tokens:
517+
frappe.db.set_value(
518+
"RC Site",
519+
site_name,
520+
{
521+
"last_synced_on": now_datetime(),
522+
"last_synced_status": "Success",
523+
"last_sync_error": None,
524+
},
525+
update_modified=False,
526+
)
527+
return {
528+
"status": "success",
529+
}
513530

514-
rc_site_user = frappe.qb.DocType("RC Site User")
515-
rc_site_user_token = frappe.qb.DocType("RC Site User Token")
531+
# building an incoming set for faster lookup
532+
incoming_users = list({token.get("user") for token in tokens})
533+
incoming_tokens = {(token.get("user"), token.get("fcm_token")) for token in tokens}
516534

517-
try:
535+
rc_site_user = frappe.qb.DocType("RC Site User")
536+
rc_site_user_token = frappe.qb.DocType("RC Site User Token")
518537

519538
# fetching all the existing tokens on RC for this site
520539
existing_tokens_query = (
@@ -569,7 +588,29 @@ def import_user_tokens(site_name: str, tokens: str):
569588
"user": user_map[user_id],
570589
"fcm_token": fcm_token,
571590
}).insert()
591+
592+
frappe.db.set_value(
593+
"RC Site",
594+
site_name,
595+
{
596+
"last_synced_on": now_datetime(),
597+
"last_synced_status": "Success",
598+
"last_sync_error": None,
599+
"last_synced_by": frappe.session.user,
600+
},
601+
update_modified=False,
602+
)
572603
except Exception as e:
604+
frappe.db.set_value(
605+
"RC Site",
606+
site_name,
607+
{
608+
"last_synced_status": "Failed",
609+
"last_sync_error": str(e),
610+
"last_synced_by": frappe.session.user,
611+
},
612+
update_modified=False,
613+
)
573614
frappe.log_error(title=f"Error syncing user tokens for {site_name}", message=frappe.get_traceback())
574615
frappe.throw(_(f"Error syncing user tokens for {site_name} - {str(e)}"))
575616

raven_cloud/raven_cloud/doctype/rc_site/rc_site.json

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
"doctype": "DocType",
77
"engine": "InnoDB",
88
"field_order": [
9-
"site"
9+
"site",
10+
"section_break_mqcc",
11+
"last_registered_on",
12+
"last_registered_by",
13+
"column_break_brfq",
14+
"last_synced_on",
15+
"last_synced_by",
16+
"last_synced_status",
17+
"last_sync_error"
1018
],
1119
"fields": [
1220
{
@@ -17,12 +25,53 @@
1725
"length": 300,
1826
"reqd": 1,
1927
"unique": 1
28+
},
29+
{
30+
"fieldname": "section_break_mqcc",
31+
"fieldtype": "Section Break"
32+
},
33+
{
34+
"fieldname": "last_registered_on",
35+
"fieldtype": "Datetime",
36+
"label": "Last Registered On"
37+
},
38+
{
39+
"fieldname": "last_synced_on",
40+
"fieldtype": "Datetime",
41+
"label": "Last Synced On"
42+
},
43+
{
44+
"fieldname": "column_break_brfq",
45+
"fieldtype": "Column Break"
46+
},
47+
{
48+
"fieldname": "last_synced_status",
49+
"fieldtype": "Select",
50+
"label": "Last Synced Status",
51+
"options": "\nFailed\nSuccess"
52+
},
53+
{
54+
"fieldname": "last_sync_error",
55+
"fieldtype": "Small Text",
56+
"label": "Last Sync Error"
57+
},
58+
{
59+
"fieldname": "last_registered_by",
60+
"fieldtype": "Link",
61+
"label": "Last Registered By",
62+
"options": "User"
63+
},
64+
{
65+
"fieldname": "last_synced_by",
66+
"fieldtype": "Link",
67+
"label": "Last Synced By",
68+
"options": "User"
2069
}
2170
],
2271
"grid_page_length": 50,
2372
"index_web_pages_for_search": 1,
2473
"links": [],
25-
"modified": "2025-04-29 16:08:43.664813",
74+
"modified": "2026-03-30 21:03:45.519167",
2675
"modified_by": "Administrator",
2776
"module": "Raven Cloud",
2877
"name": "RC Site",
@@ -59,4 +108,4 @@
59108
"sort_field": "creation",
60109
"sort_order": "DESC",
61110
"states": []
62-
}
111+
}

raven_cloud/raven_cloud/doctype/rc_site/rc_site.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class RCSite(Document):
1616
if TYPE_CHECKING:
1717
from frappe.types import DF
1818

19+
last_registered_by: DF.Link | None
20+
last_registered_on: DF.Datetime | None
21+
last_sync_error: DF.SmallText | None
22+
last_synced_by: DF.Link | None
23+
last_synced_on: DF.Datetime | None
24+
last_synced_status: DF.Literal["", "Failed", "Success"]
1925
site: DF.Data
2026
# ruff: noqa
2127
# end: auto-generated types

0 commit comments

Comments
 (0)