Repository: fossasia/open-event-server

Issue:
Implement api end points to list all notifications and mail-notifications (non-self, for admin)
**I'm submitting a ...**  (check one with "x")
- [ ] bug report
- [x] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-orga-server

According to ,
- https://github.com/fossasia/open-event/blob/master/dev-handbook/resources-permissions.md#notifications---eg-v1notifications
- https://github.com/fossasia/open-event/blob/master/dev-handbook/resources-permissions.md#notifications---eg-v1notifications


Discussion:

Resolving patch:
diff --git a/app/api/__init__.py b/app/api/__init__.py
--- a/app/api/__init__.py
+++ b/app/api/__init__.py
@@ -3,8 +3,8 @@
     StripeAuthorizationRelationship, StripeAuthorizationListPost
 from app.api.ticket_fees import TicketFeeList, TicketFeeDetail
 from app.api.users import UserList, UserDetail, UserRelationship, VerifyUser
-from app.api.notifications import NotificationList, NotificationDetail, NotificationRelationship
-from app.api.email_notifications import EmailNotificationList, EmailNotificationListPost, EmailNotificationDetail,\
+from app.api.notifications import NotificationList, NotificationListAdmin, NotificationDetail, NotificationRelationship
+from app.api.email_notifications import EmailNotificationList, EmailNotificationListAdmin, EmailNotificationDetail,\
     EmailNotificationRelationshipOptional, EmailNotificationRelationshipRequired
 from app.api.tickets import TicketList, TicketListPost, TicketDetail, TicketRelationshipRequired,\
     TicketRelationshipOptional
@@ -64,13 +64,14 @@
 api.route(UserRelationship, 'user_email_notifications', '/users/<int:id>/relationships/email-notifications')
 
 # notifications
+api.route(NotificationListAdmin, 'notification_list_admin', '/notifications')
 api.route(NotificationList, 'notification_list', '/users/<int:user_id>/notifications')
 api.route(NotificationDetail, 'notification_detail', '/notifications/<int:id>')
 api.route(NotificationRelationship, 'notification_user',
           '/notifications/<int:id>/relationships/user')
 
 # email_notifications
-api.route(EmailNotificationListPost, 'email_notification_list_post', '/email-notifications')
+api.route(EmailNotificationListAdmin, 'email_notification_list_admin', '/email-notifications')
 api.route(EmailNotificationList, 'email_notification_list', '/users/<int:id>/email-notifications')
 api.route(EmailNotificationDetail, 'email_notification_detail', '/email-notifications/<int:id>')
 api.route(EmailNotificationRelationshipRequired, 'email_notification_user',
diff --git a/app/api/email_notifications.py b/app/api/email_notifications.py
--- a/app/api/email_notifications.py
+++ b/app/api/email_notifications.py
@@ -9,9 +9,6 @@
 from app.models.user import User
 from app.api.helpers.permissions import jwt_required
 from app.api.helpers.db import safe_query
-from app.api.helpers.utilities import require_relationship
-from app.api.helpers.permission_manager import has_access
-from app.api.helpers.exceptions import ForbiddenException
 
 
 class EmailNotificationSchema(Schema):
@@ -53,19 +50,13 @@ class Meta:
                         )
 
 
-class EmailNotificationListPost(ResourceList):
+class EmailNotificationListAdmin(ResourceList):
     """
     List and create email notifications
     """
-
-    def before_post(self, args, kwargs, data):
-        require_relationship(['user'], data)
-        if not has_access('is_user_itself', id=data['user']):
-            raise ForbiddenException({'source': ''}, 'You need to be the user.')
-
-    view_kwargs = True
-    methods = ['POST', ]
+    methods = ['GET', ]
     schema = EmailNotificationSchema
+    decorators = (api.has_permission('is_admin'),)
     data_layer = {'session': db.session,
                   'model': EmailNotification}
 
diff --git a/app/api/notifications.py b/app/api/notifications.py
--- a/app/api/notifications.py
+++ b/app/api/notifications.py
@@ -42,6 +42,17 @@ class Meta:
                         )
 
 
+class NotificationListAdmin(ResourceList):
+    """
+    List all the Notification
+    """
+    decorators = (api.has_permission('is_admin'),)
+    methods = ['GET']
+    schema = NotificationSchema
+    data_layer = {'session': db.session,
+                  'model': Notification}
+
+
 class NotificationList(ResourceList):
     """
     List all the Notification


Test patch:
diff --git a/tests/hook_main.py b/tests/hook_main.py
--- a/tests/hook_main.py
+++ b/tests/hook_main.py
@@ -1717,6 +1717,19 @@ def notification_get_list(transaction):
         db.session.commit()
 
 
+@hooks.before("Notifications > Notifications Admin Collection > List All Notifications")
+def notification_get_admin_list(transaction):
+    """
+    GET /notifications
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        notification = NotificationFactory()
+        db.session.add(notification)
+        db.session.commit()
+
+
 @hooks.before("Notifications > Notification Detail > Notification Detail")
 def notification_get_detail(transaction):
     """
@@ -1757,7 +1770,20 @@ def notification_delete(transaction):
 
 
 # ------------------------- Email Notifications -------------------------
-@hooks.before("Email Notifications > Email Notifications Collection Get > List All Email Notifications")
+@hooks.before("Email Notifications > Email Notifications Admin Collection > List All Email Notifications")
+def email_notification_get_admin_list(transaction):
+    """
+    GET /email-notifications
+    :param transaction:
+    :return:
+    """
+    with stash['app'].app_context():
+        email_notification = EmailNotificationFactory()
+        db.session.add(email_notification)
+        db.session.commit()
+
+
+@hooks.before("Email Notifications > Email Notifications Collection > List All Email Notifications")
 def email_notification_get_list(transaction):
     """
     GET /users/2/email-notifications