Skip to content

Commit cc6eed2

Browse files
mnswalehBonifase
authored andcommitted
feature(Device last seen to equal checkin time): update device last seen (#436)
- modify event checkin mutation - modify cancel event mutation - fix tests [finishes #166539920]
1 parent 070299b commit cc6eed2

12 files changed

+184
-11
lines changed

api/events/schema.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
from helpers.calendar.analytics_helper import CommonAnalytics
1212
from helpers.auth.user_details import get_user_from_db
1313
from helpers.pagination.paginate import ListPaginate
14+
from helpers.devices.devices import update_device_last_seen
1415
import pytz
1516
from dateutil import parser
16-
from datetime import datetime
17+
from datetime import datetime, timedelta
1718

1819

1920
class Events(SQLAlchemyObjectType):
@@ -48,6 +49,8 @@ class Arguments:
4849

4950
def mutate(self, info, **kwargs):
5051
room_id, event = check_event_in_db(self, info, "checked_in", **kwargs)
52+
if kwargs.get('check_in_time'):
53+
update_device_last_seen(info, room_id, kwargs['check_in_time'])
5154
if not event:
5255
event = EventsModel(
5356
event_id=kwargs['event_id'],
@@ -78,6 +81,12 @@ class Arguments:
7881
def mutate(self, info, **kwargs):
7982
# mutation to create an event
8083
room_id, event = check_event_in_db(self, info, "cancelled", **kwargs)
84+
try:
85+
device_last_seen = parser.parse(
86+
kwargs['start_time']) + timedelta(minutes=10)
87+
except ValueError:
88+
raise GraphQLError("Invalid start time")
89+
update_device_last_seen(info, room_id, device_last_seen)
8190
if not event:
8291
event = EventsModel(
8392
event_id=kwargs['event_id'],

fixtures/analytics/query_all_analytics_fixtures.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@
5454
"durationInMinutes": 45
5555
}
5656
]
57+
},
58+
{
59+
"roomName": "Tana",
60+
"cancellations": 0,
61+
"cancellationsPercentage": 0.0,
62+
"autoCancellations": 0,
63+
"numberOfBookings": 0,
64+
"checkins": 0,
65+
"checkinsPercentage": 0.0,
66+
"bookingsPercentageShare": 0.0,
67+
"appBookings": 0,
68+
"appBookingsPercentage": 0.0,
69+
"events": [
70+
{
71+
"durationInMinutes": 0
72+
}
73+
]
5774
}
5875
],
5976
"bookingsCount": [

fixtures/events/event_checkin_fixtures.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@
3838
}
3939
}
4040

41+
event_2_checkin_mutation = '''mutation {
42+
eventCheckin(
43+
calendarId:"[email protected]",
44+
eventId:"test_id6", eventTitle:"Onboarding 2", numberOfParticipants: 4,
45+
startTime:"2019-07-10T09:00:00Z",
46+
endTime:"2018-07-10T09:45:00Z",
47+
checkInTime:"2018-07-10T09:00:00Z"){
48+
event{
49+
eventId
50+
roomId
51+
checkedIn
52+
cancelled
53+
room{
54+
id
55+
name
56+
calendarId
57+
}
58+
}
59+
}
60+
}
61+
'''
62+
4163
wrong_calendar_id_checkin_mutation = '''mutation {
4264
eventCheckin(
4365
calendarId:"fake_calendar_id",
@@ -84,6 +106,29 @@
84106

85107
cancel_event_respone = "Event cancelled but email not sent"
86108

109+
cancel_event_invalid_start_time = '''
110+
mutation {
111+
cancelEvent(
112+
calendarId:"[email protected]",
113+
eventId:"test_id5", eventTitle:"Onboarding", numberOfParticipants: 4,
114+
startTime:"invalid",
115+
endTime:"2018-07-10T09:45:00Z")
116+
{
117+
event{
118+
eventId
119+
roomId
120+
checkedIn
121+
cancelled
122+
room{
123+
id
124+
name
125+
calendarId
126+
}
127+
}
128+
}
129+
}
130+
'''
131+
87132
checkin_mutation_for_event_existing_in_db = '''mutation {
88133
eventCheckin(
89134
calendarId:"[email protected]",

fixtures/location/all_locations_fixtures.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
"description": "The description"
3636
}
3737
]
38+
},
39+
{
40+
"name": "Tana",
41+
"roomType": "meeting",
42+
"capacity": 14,
43+
"roomTags": [
44+
{
45+
"name": "Block-B",
46+
"color": "green",
47+
"description": "The description"
48+
}
49+
]
3850
}
3951
]
4052
},
@@ -87,10 +99,18 @@
8799

88100
expected_all_location_no_hierachy = {
89101
'data': {'allLocations': [
90-
{'rooms': [{'name': 'Entebbe',
102+
{'rooms': [
103+
{
104+
'name': 'Entebbe',
105+
'roomType': 'meeting',
106+
'capacity': 6
107+
},
108+
{
109+
'name': 'Tana',
91110
'roomType': 'meeting',
92-
'capacity': 6}
93-
]},
111+
'capacity': 14
112+
},
113+
]},
94114
{'rooms': []},
95115
{'rooms': []}
96116
]

fixtures/location/rooms_in_location_fixtures.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"capacity": 6,
1818
"roomType": "meeting",
1919
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
20+
},
21+
{
22+
"name": "Tana",
23+
"capacity": 14,
24+
"roomType": "meeting",
25+
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
2026
}
2127
]
2228
}

fixtures/room/create_room_fixtures.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,20 @@
237237

238238
db_rooms_query_response = {
239239
"data": {
240-
"rooms": [{
240+
"rooms": [
241+
{
241242
"name": "Entebbe",
242243
"capacity": 6,
243244
"roomType": "meeting",
244245
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
245-
}]
246+
},
247+
{
248+
"name": "Tana",
249+
"capacity": 14,
250+
"roomType": "meeting",
251+
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
252+
}
253+
]
246254
}
247255
}
248256

@@ -261,6 +269,18 @@
261269
}
262270
],
263271
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
272+
},
273+
{
274+
"name": "Tana",
275+
"capacity": 14,
276+
"roomType": "meeting",
277+
"roomTags": [
278+
{
279+
"name": "Block-B",
280+
"color": "green"
281+
}
282+
],
283+
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
264284
}
265285
]
266286
}

fixtures/room/filter_room_fixtures.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
"capacity": 6,
4444
"roomType": "meeting",
4545
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
46+
},
47+
{
48+
"name": "Tana",
49+
"capacity": 14,
50+
"roomType": "meeting",
51+
"imageUrl": "https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg" # noqa: E501
4652
}
4753
]
4854
}
@@ -181,7 +187,7 @@
181187
}
182188

183189
filter_rooms_by_room_labels = '''query {
184-
allRooms(roomLabels:"Wing"){
190+
allRooms(roomLabels:"Wing A"){
185191
rooms{
186192
name
187193
capacity
@@ -209,7 +215,7 @@
209215
}
210216
}
211217
filter_rooms_by_location_room_labels = '''query {
212-
allRooms(roomLabels:"Wing", location:"Kampala"){
218+
allRooms(roomLabels:"Wing A", location:"Kampala"){
213219
rooms{
214220
name
215221
capacity

fixtures/room/query_room_fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
"name": "Entebbe"
3333
}
3434
],
35-
"hasNext": False,
35+
"hasNext": True,
3636
"hasPrevious": False,
37-
"pages": 1
37+
"pages": 2
3838
}
3939
}
4040
}

helpers/devices/__init__.py

Whitespace-only changes.

helpers/devices/devices.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from graphql import GraphQLError
2+
from api.devices.models import Devices as DeviceModel
3+
from api.devices.schema import Devices as DeviceSchema
4+
5+
6+
def update_device_last_seen(info, room_id, check_in_time):
7+
device_query = DeviceSchema.get_query(info)
8+
device = device_query.filter(
9+
DeviceModel.room_id == room_id
10+
).first()
11+
if not device:
12+
raise GraphQLError("Room device not found")
13+
device.last_seen = check_in_time
14+
device.save()

tests/base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ def setUp(self):
9090
room_labels=["1st Floor", "Wing A"])
9191
room.save()
9292
room.room_tags.append(tag)
93+
room_2 = Room(name='Tana',
94+
room_type='meeting',
95+
capacity=14,
96+
location_id=location.id,
97+
structure_id='851ae8b3-48dd-46b5-89bc-ca3f8111ad87',
98+
calendar_id='[email protected]', # noqa: E501
99+
image_url="https://www.officelovin.com/wp-content/uploads/2016/10/andela-office-main-1.jpg", # noqa: E501
100+
room_labels=["1st Floor", "Wing B"])
101+
room_2.save()
102+
room_2.room_tags.append(tag)
93103
resource = Resource(name='Markers',
94104
quantity=3)
95105
resource.save()
@@ -98,7 +108,8 @@ def setUp(self):
98108
date_added="2018-06-08T11:17:58.785136",
99109
name="Samsung",
100110
location="Kampala",
101-
device_type="External Display"
111+
device_type="External Display",
112+
room_id=1
102113
)
103114
device.save()
104115
question_1 = Question(

tests/test_events/test_event_checkin.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
from tests.base import BaseTestCase, CommonTestCases
55
from fixtures.events.event_checkin_fixtures import (
66
event_checkin_mutation,
7+
event_2_checkin_mutation,
78
event_checkin_response,
89
wrong_calendar_id_checkin_mutation,
910
cancel_event_mutation,
1011
cancel_event_respone,
12+
cancel_event_invalid_start_time,
1113
checkin_mutation_for_event_existing_in_db,
1214
response_for_event_existing_in_db_checkin
1315
)
@@ -53,6 +55,17 @@ def test_checkin_with_invalid_calendar_id(self):
5355
"This Calendar ID is invalid"
5456
)
5557

58+
def test_checkin_room_with_no_device(self):
59+
"""
60+
Test that user can not checkin to a room
61+
without a device assigned to the room
62+
"""
63+
CommonTestCases.user_token_assert_in(
64+
self,
65+
event_2_checkin_mutation,
66+
"Room device not found"
67+
)
68+
5669
@patch("api.events.schema.get_single_calendar_event", spec=True)
5770
def test_cancel_event(self, mocked_method):
5871
'''
@@ -65,6 +78,18 @@ def test_cancel_event(self, mocked_method):
6578
cancel_event_respone
6679
)
6780

81+
@patch("api.events.schema.get_single_calendar_event", spec=True)
82+
def test_cancel_event_with_invalid_start_time(self, mocked_method):
83+
'''
84+
Test that user can not cancel event with invalid start time
85+
'''
86+
mocked_method.return_value = get_events_mock_data()['items'][0]
87+
CommonTestCases.user_token_assert_in(
88+
self,
89+
cancel_event_invalid_start_time,
90+
'Invalid start time'
91+
)
92+
6893
@patch("api.events.schema.get_single_calendar_event", spec=True)
6994
def test_cancel_event_twice(self, mocked_method):
7095
'''

0 commit comments

Comments
 (0)