4
4
from baseapp_core .graphql .utils import get_pk_from_relay_id
5
5
from django .utils .translation import gettext_lazy as _
6
6
7
- from .object_types import NotificationNode , NotificationsNode
7
+ from .object_types import (
8
+ NotificationChannelTypesEnum ,
9
+ NotificationNode ,
10
+ NotificationSettingNode ,
11
+ NotificationsInterface ,
12
+ )
8
13
9
14
Notification = swapper .load_model ("notifications" , "Notification" )
15
+ NotificationSetting = swapper .load_model ("baseapp_notifications" , "NotificationSetting" )
10
16
11
17
12
18
class NotificationsMarkAllAsRead (RelayMutation ):
13
- recipient = graphene .Field (NotificationsNode )
19
+ recipient = graphene .Field (NotificationsInterface )
14
20
15
21
class Input :
16
22
read = graphene .Boolean (required = True , description = _ ("Mark as read or unread" ))
@@ -31,7 +37,7 @@ def mutate_and_get_payload(cls, root, info, read, **input):
31
37
32
38
33
39
class NotificationsMarkAsRead (RelayMutation ):
34
- recipient = graphene .Field (NotificationsNode )
40
+ recipient = graphene .Field (NotificationsInterface )
35
41
notifications = graphene .List (NotificationNode )
36
42
37
43
class Input :
@@ -59,6 +65,63 @@ def mutate_and_get_payload(cls, root, info, notification_ids, read, **input):
59
65
)
60
66
61
67
68
+ class NotificationSettingToggle (RelayMutation ):
69
+ notification_setting = graphene .Field (NotificationSettingNode )
70
+
71
+ class Input :
72
+ verb = graphene .String (required = True )
73
+ channel = graphene .Field (NotificationChannelTypesEnum , required = True )
74
+
75
+ @classmethod
76
+ @login_required
77
+ def mutate_and_get_payload (cls , root , info , verb , channel , ** input ):
78
+ # Determine if a setting other than 'ALL' exists for the given verb
79
+ is_channel_all = channel == NotificationSetting .NotificationChannelTypes .ALL
80
+
81
+ # Create or update the notification setting
82
+ notification_setting , created = NotificationSetting .objects .get_or_create (
83
+ user = info .context .user ,
84
+ verb = verb ,
85
+ channel = channel ,
86
+ defaults = {"is_active" : False },
87
+ )
88
+
89
+ if not created :
90
+ notification_setting .is_active = not notification_setting .is_active
91
+ notification_setting .save (update_fields = ["is_active" ])
92
+
93
+ # Update other settings based on 'ALL' channel logic
94
+ if is_channel_all :
95
+ has_non_active_setting = (
96
+ NotificationSetting .objects .filter (
97
+ user = info .context .user , verb = verb , is_active = False
98
+ )
99
+ .exclude (channel = NotificationSetting .NotificationChannelTypes .ALL )
100
+ .exists ()
101
+ )
102
+ if has_non_active_setting :
103
+ # If a non-active setting exists, update 'ALL' setting to False
104
+ NotificationSetting .objects .filter (
105
+ user = info .context .user ,
106
+ verb = verb ,
107
+ channel = NotificationSetting .NotificationChannelTypes .ALL ,
108
+ ).update (is_active = False )
109
+ # Update all settings to match the 'ALL' setting
110
+ NotificationSetting .objects .filter (user = info .context .user , verb = verb ).update (
111
+ is_active = notification_setting .is_active
112
+ )
113
+ else :
114
+ # If the current channel is not 'ALL', ensure 'ALL' settings are updated to match
115
+ NotificationSetting .objects .filter (
116
+ user = info .context .user ,
117
+ verb = verb ,
118
+ channel = NotificationSetting .NotificationChannelTypes .ALL ,
119
+ ).update (is_active = notification_setting .is_active )
120
+
121
+ return NotificationSettingToggle (notification_setting = notification_setting )
122
+
123
+
62
124
class NotificationsMutations (object ):
63
125
notifications_mark_as_read = NotificationsMarkAsRead .Field ()
64
126
notifications_mark_all_as_read = NotificationsMarkAllAsRead .Field ()
127
+ notification_setting_toggle = NotificationSettingToggle .Field ()
0 commit comments