Skip to content

Commit ee3f44b

Browse files
EmmanuelSagekoitoror
authored andcommitted
CON-281-bug(activity-page): fix activity page query
- Add filtering events by current user location - Add super admin priviledge to view all locations [Delivers CON-281]
1 parent 9e69d1c commit ee3f44b

4 files changed

Lines changed: 87 additions & 9 deletions

File tree

api/events/schema.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
from helpers.pagination.paginate import ListPaginate
1515
from helpers.devices.devices import update_device_last_seen
1616
from helpers.events_filter.events_filter import (
17-
filter_events_by_date_range,
17+
filter_events_by_date_range_in_location,
1818
validate_page_and_per_page
1919
)
20+
from helpers.auth.user_details import get_user_from_db
2021

2122
utc = pytz.utc
2223

@@ -258,10 +259,13 @@ def resolve_all_events(self, info, **kwargs):
258259
page = kwargs.get('page')
259260
per_page = kwargs.get('per_page')
260261
page, per_page = validate_page_and_per_page(page, per_page)
262+
user = get_user_from_db()
261263
query = Events.get_query(info)
262-
response = filter_events_by_date_range(
263-
query, start_date, end_date
264+
response = filter_events_by_date_range_in_location(
265+
query, start_date, end_date, user
264266
)
267+
if not response:
268+
raise GraphQLError('Events do not exist for the date range')
265269
response.sort(
266270
key=lambda x: parser.parse(x.start_time).astimezone(utc),
267271
reverse=True)

fixtures/events/events_query_by_date_fixtures.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,48 @@
104104
}
105105
}
106106

107+
query_events_with_location = '''
108+
query{
109+
allEvents(startDate: "Jul 11 2018",
110+
endDate: "Jul 11 2018",
111+
page:1,
112+
perPage: 1){
113+
events {
114+
id
115+
roomId
116+
room{
117+
name
118+
locationId
119+
}
120+
},
121+
hasNext,
122+
hasPrevious,
123+
pages,
124+
queryTotal
125+
}
126+
}
127+
'''
128+
129+
event_query_with_location_response = {
130+
'data': {
131+
'allEvents': {
132+
'events': [{
133+
'id': '1',
134+
'roomId': 1,
135+
'room': {
136+
'name': 'Entebbe',
137+
'locationId': 1
138+
}
139+
}],
140+
'hasNext': False,
141+
'hasPrevious': False,
142+
'pages': 1,
143+
'queryTotal': 1
144+
}
145+
}
146+
}
147+
148+
107149
query_events_page_without_per_page = '''
108150
query{
109151
allEvents(

helpers/events_filter/events_filter.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
import pytz
44

55
from api.events.models import Events as EventsModel
6+
from api.location.models import Location as LocationModel
7+
from api.role.models import Role as RoleModel
8+
from api.room.models import Room as RoomModel
69

710
utc = pytz.utc
811

912

10-
def filter_events_by_date_range(query, start_date, end_date):
13+
def filter_events_by_date_range_in_location(query, start_date, end_date, user):
1114
"""
1215
Return events that fall in the date range
16+
and matches the current_user's location with the events in that
17+
location and filters to return the events for that location
18+
but returns all locations for a user with super admin role
1319
"""
1420
if start_date and not end_date:
1521
raise GraphQLError("endDate argument missing")
@@ -21,19 +27,29 @@ def filter_events_by_date_range(query, start_date, end_date):
2127
events = query.filter(
2228
EventsModel.state == 'active'
2329
).all()
24-
if not events:
25-
raise GraphQLError('Events do not exist')
2630
return events
2731

2832
start_date, end_date = format_range_dates(start_date, end_date)
2933

30-
events = query.filter(
34+
admin_role = RoleModel.query.filter_by(
35+
id=user.roles[0].id).first()
36+
if admin_role.role == 'Super Admin':
37+
events = query.filter(
3138
EventsModel.state == 'active',
3239
EventsModel.start_time >= start_date,
3340
EventsModel.end_time <= end_date
3441
).all()
35-
if not events:
36-
raise GraphQLError('Events do not exist for the date range')
42+
return events
43+
44+
location = LocationModel.query.filter_by(
45+
name=user.location
46+
).first()
47+
events = query.join(RoomModel).filter(
48+
EventsModel.state == 'active',
49+
EventsModel.start_time >= start_date,
50+
EventsModel.end_time <= end_date,
51+
RoomModel.location_id == location.id
52+
).all()
3753
return events
3854

3955

tests/test_events/test_query_all_events_by_date.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from tests.base import BaseTestCase, CommonTestCases
2+
23
from fixtures.events.events_query_by_date_fixtures import (
34
query_events,
45
event_query_response,
56
query_events_with_start_date_before_end_date,
67
event_query_with_start_date_before_end_date_response,
78
query_events_with_pagination,
89
event_query_with_pagination_response,
10+
query_events_with_location,
11+
event_query_with_location_response,
912
query_events_page_without_per_page,
1013
event_query_page_without_per_page_response,
1114
query_events_per_page_without_page,
@@ -23,6 +26,7 @@
2326
query_events_without_page_and_per_page,
2427
event_query_without_page_and_per_page_response
2528
)
29+
from tests.base import change_user_role_to_super_admin
2630

2731

2832
class TestEventsQuery(BaseTestCase):
@@ -56,6 +60,18 @@ def test_query_events_with_pagination(self):
5660
self,
5761
query_events_with_pagination,
5862
event_query_with_pagination_response
63+
64+
)
65+
66+
@change_user_role_to_super_admin
67+
def test_query_events_with_location(self):
68+
"""
69+
Test a super_user can query for all events in all locations
70+
"""
71+
CommonTestCases.admin_token_assert_equal(
72+
self,
73+
query_events_with_location,
74+
event_query_with_location_response
5975
)
6076

6177
def test_query_events_without_start_date(self):

0 commit comments

Comments
 (0)