Skip to content

CON-328 Add HTTP call to the slack when a meeting is ended #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions alembic/versions/859f31384fe2_add_organizer_email_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""add organizer email field

Revision ID: 859f31384fe2
Revises: c058d462a21d
Create Date: 2019-11-18 09:26:07.682060

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '859f31384fe2'
down_revision = 'c058d462a21d'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('events', sa.Column(
'organizer_email', sa.String(), nullable=True))


def downgrade():
op.drop_column('events', 'organizer_email')
1 change: 1 addition & 0 deletions api/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Events(Base, Utility):
meeting_end_time = Column(String, nullable=True)
auto_cancelled = Column(Boolean, nullable=True, default=False)
app_booking = Column(Boolean, nullable=True, default=False)
organizer_email = Column(String, nullable=True)


def filter_event(start_date, end_date, room_id=None):
Expand Down
86 changes: 69 additions & 17 deletions api/events/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
calendar_dates_format,
empty_string_checker
)
from helpers.event.slack_notifier import notify_slack

utc = pytz.utc

Expand All @@ -41,6 +42,18 @@ class Meta:
model = EventsModel


class EventArguments(graphene.ObjectType):

"""
returns common event arguments
"""

calendar_id = graphene.String(required=True)
event_id = graphene.String(required=True)
start_time = graphene.String(required=True)
end_time = graphene.String(required=True)


class BookEvent(graphene.Mutation):
"""
Book Calendar events
Expand All @@ -56,6 +69,7 @@ class Arguments:
organizer = graphene.String(required=False)
description = graphene.String(required=False)
room = graphene.String(required=True)
room_id = graphene.Int(required=True)
time_zone = graphene.String(required=True)

@Auth.user_roles('Admin', 'Default User', 'Super Admin')
Expand All @@ -71,9 +85,11 @@ def mutate(self, info, **kwargs):
attendees: A string of emails of the event guests
description: Any additional information about the event
room: The meeting room where the even will be held [required]
room_id: The id of the meeting room where the even will
be held [required]
time_zone: The timezone of the event location eg. 'Africa/Kigali'
organizer: The email of the co-organizer, the converge email is
the default
organizer: The email of the organizer. it should be gotten
from the user token.

Returns:
A string that communicates a successfully created event.
Expand All @@ -86,12 +102,14 @@ def mutate(self, info, **kwargs):
organizer = kwargs.get('organizer', None)

event_title = kwargs['event_title']
room_id = kwargs['room_id']
empty_string_checker(event_title)
empty_string_checker(room)
empty_string_checker(room_id)
empty_string_checker(time_zone)

start_date, end_date = calendar_dates_format(
kwargs['start_date'], kwargs['start_time'], duration)
kwargs['start_date'], kwargs['start_time'], duration, time_zone)

attendees = attendees.replace(" ", "").split(",")
guests = []
Expand All @@ -117,8 +135,23 @@ def mutate(self, info, **kwargs):
}
}
service = credentials.set_api_credentials()
service.events().insert(calendarId='primary', body=event,
sendNotifications=True).execute()
event_created = service.events() \
.insert(calendarId='primary',
body=event,
sendNotifications=True).execute()
new_event = EventsModel(
event_id=event_created['id'],
event_title=kwargs['event_title'],
start_time=start_date,
end_time=end_date,
number_of_participants=len(guests),
app_booking=True,
checked_in=False,
room_id=room_id,
cancelled=False,
organizer_email=organizer
)
new_event.save()
return BookEvent(response='Event created successfully')


Expand All @@ -127,11 +160,11 @@ class EventCheckin(graphene.Mutation):
Returns the eventcheckin payload
"""
class Arguments:
calendar_id = graphene.String(required=True)
event_id = graphene.String(required=True)
calendar_id = EventArguments.calendar_id
event_id = EventArguments.event_id
event_title = graphene.String(required=True)
start_time = graphene.String(required=True)
end_time = graphene.String(required=True)
start_time = EventArguments.start_time
end_time = EventArguments.end_time
number_of_participants = graphene.Int(required=True)
check_in_time = graphene.String(required=False)
event = graphene.Field(Events)
Expand Down Expand Up @@ -160,11 +193,11 @@ class CancelEvent(graphene.Mutation):
Returns the payload on event cancelation
"""
class Arguments:
calendar_id = graphene.String(required=True)
event_id = graphene.String(required=True)
calendar_id = EventArguments.calendar_id
event_id = EventArguments.event_id
event_title = graphene.String(required=True)
start_time = graphene.String(required=True)
end_time = graphene.String(required=True)
start_time = EventArguments.start_time
end_time = EventArguments.end_time
number_of_participants = graphene.Int()
event = graphene.Field(Events)

Expand Down Expand Up @@ -210,14 +243,32 @@ class EndEvent(graphene.Mutation):
Returns event payload on ending the event
"""
class Arguments:
calendar_id = graphene.String(required=True)
event_id = graphene.String(required=True)
start_time = graphene.String(required=True)
end_time = graphene.String(required=True)
calendar_id = EventArguments.calendar_id
event_id = EventArguments.event_id
organizer_email = graphene.String(required=True)
room_id = graphene.Int(required=True)
start_time = EventArguments.start_time
end_time = EventArguments.end_time
meeting_end_time = graphene.String(required=True)
event = graphene.Field(Events)

def mutate(self, info, **kwargs):
calendar_id = kwargs['calendar_id']
event_id = kwargs['event_id']
organizer_email = kwargs['organizer_email']
room_id = kwargs['room_id']
start_time = kwargs['start_time']
end_time = kwargs['end_time']
meeting_end_time = kwargs['meeting_end_time']

empty_string_checker(calendar_id)
empty_string_checker(event_id)
empty_string_checker(organizer_email)
empty_string_checker(room_id)
empty_string_checker(start_time)
empty_string_checker(end_time)
empty_string_checker(meeting_end_time)

room_id, event = check_event_in_db(self, info, "ended", **kwargs)
if kwargs.get('meeting_end_time'):
update_device_last_activity(
Expand All @@ -229,6 +280,7 @@ def mutate(self, info, **kwargs):
)
event.save()

notify_slack.delay(event_id, organizer_email, room_id)
return EndEvent(event=event)


Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Config:
},
}

# slack bot url
NOTIFY_URL = os.getenv('SLACK_NOTIFICATION_URL')

@staticmethod
def init_app(app):
pass
Expand Down
18 changes: 12 additions & 6 deletions fixtures/events/book_event_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
duration:60
attendees: "[email protected], [email protected]",
timeZone: "Africa/Kigali",
room:"Yankara"
room:"Yankara",
roomId:1
){
response
}
Expand All @@ -21,7 +22,8 @@
duration:60
attendees: "[email protected], [email protected]",
timeZone: "Africa/Kigali",
room:"Yankara"
room:"Yankara",
roomId:1
){
response
}
Expand All @@ -36,7 +38,8 @@
duration:60
attendees: "[email protected], [email protected]",
timeZone: "Africa/Kigali",
room:""
room:"",
roomId:1
){
response
}
Expand All @@ -51,7 +54,8 @@
duration:60
attendees: "[email protected], [email protected]",
timeZone: "Africa/Kigali",
room:"Yankara"
room:"Yankara",
roomId:1
){
response
}
Expand All @@ -66,7 +70,8 @@
duration:60
attendees: "[email protected], [email protected]",
timeZone: "Africa/Kigali",
room:"Yankara"
room:"Yankara",
roomId:1
){
response
}
Expand All @@ -81,7 +86,8 @@
duration:60
attendees: "[email protected], [email protected]",
timeZone: "",
room:"Yankara"
room:"Yankara",
roomId:1
){
response
}
Expand Down
6 changes: 6 additions & 0 deletions fixtures/events/end_event_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
end_event_mutation = '''mutation {
endEvent(calendarId:"[email protected]",
eventId:"test_id5",
organizerEmail:"[email protected]",
roomId:1,
startTime:"2018-07-10T09:00:00Z",
endTime:"2018-07-10T09:45:00Z",
meetingEndTime: "2018-07-10T09:45:00Z"){
Expand Down Expand Up @@ -43,6 +45,8 @@
endEvent(calendarId:"[email protected]",
eventId:"test_id5",
startTime:"2018-07-11T09:00:00Z",
organizerEmail:"[email protected]",
roomId:1,
endTime:"2018-07-11T09:45:00Z",
meetingEndTime: "2018-07-11T09:45:00Z"){
event{
Expand All @@ -69,6 +73,8 @@
endEvent(calendarId:"invalid_calendar_id",
eventId:"test_id5",
startTime:"2018-08-11T09:00:00Z",
organizerEmail:"[email protected]",
roomId:1,
endTime:"2018-08-11T09:45:00Z",
meetingEndTime: "2018-08-11T09:45:00Z"){
event{
Expand Down
1 change: 1 addition & 0 deletions helpers/calendar/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def sync_single_room_events(self, room):
recurring_event_id=event.get("recurringEventId"),
room_id=room.id,
event_title=event.get("summary"),
organizer_email=organizer.get('email'),
start_time=event["start"].get(
"dateTime") or event["start"].get("date"),
end_time=event["end"].get(
Expand Down
Empty file added helpers/event/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions helpers/event/slack_notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import requests
import celery
from config import Config


@celery.task(name="notify-slack")
def notify_slack(event_id, organizer_email, room_id):
"""
This function calls the slack BOT URL
and notifies the organizer.
"""
params_data = {
"event_id": event_id,
"organizer_email": organizer_email,
"room_id": room_id
}
response = requests.get(url=Config.NOTIFY_URL, params=params_data)
return(response.status_code)
8 changes: 5 additions & 3 deletions helpers/events_filter/events_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


utc = pytz.utc
timezone = pytz.timezone


def validate_date_input(start_date, end_date):
Expand Down Expand Up @@ -97,14 +98,15 @@ def date_time_format_validator(date_text, time_text):
raise ValueError("start time should be in this format: 'HH:MM'")


def calendar_dates_format(start_date, start_time, duration):
def calendar_dates_format(start_date, start_time, duration, time_zone):
"""Converts user date, time and duration input into start_date
and end_date format that is acceptable by the Google Calendar API

Args:
start_date: Date string of the day of the event
start_time: Time sting of the time of the event
duration: A float of the duration of the event
time_zone: The timezone of the event location eg. 'Africa/Kigali'

Returns:
start_date and end_date in this format "%Y-%m-%dT%H:%M:%S".
Expand All @@ -121,7 +123,7 @@ def calendar_dates_format(start_date, start_time, duration):

end_date = start_date + timedelta(minutes=duration)

start_date = start_date.strftime('%Y-%m-%dT%H:%M:%S')
end_date = end_date.strftime('%Y-%m-%dT%H:%M:%S')
start_date = start_date.replace(tzinfo=timezone(time_zone)).isoformat()
end_date = end_date.replace(tzinfo=timezone(time_zone)).isoformat()

return (start_date, end_date)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ flake8==3.5.0
coveralls==1.8.2
validators==0.12.4
graphql-core==2.2.1
requests==2.22.0
graphql-core==2.2.1
Loading