Skip to content

Commit

Permalink
Merge pull request #25 from Clubs-Council-IIITH/sendmail_cc_to_slo
Browse files Browse the repository at this point in the history
add approval times for the event on each approval.
  • Loading branch information
bhavberi authored May 27, 2024
2 parents 873a1cd + 84da904 commit 01683d6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 31 deletions.
20 changes: 18 additions & 2 deletions mtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,50 @@ class Event_State_Status(StrEnum):

@strawberry.type
class Event_Status:
state: Event_State_Status = Event_State_Status.incomplete
state: Event_State_Status = Event_State_Status.incomplete # type: ignore
# room: Event_Room_Status = Event_Room_Status.unapproved
# budget: Event_Budget_Status = Event_Budget_Status.unapproved
room: bool = False
budget: bool = False

cc_approver: str | None = None
cc_approver_time: str | None = None

slo_approver: str | None = None
slo_approver_time: str | None = None

slc_approver: str | None = None
slc_approver_time: str | None = None

# def __init__ (self, state: Event_State_Status = None, room: Event_Room_Status = None, budget: Event_Budget_Status = None) :
# self.state: Event_State_Status = Event_State_Status.incomplete if state is None else state
# self.room: Event_Room_Status = Event_Room_Status.unapproved if room is None else room
# self.budget: Event_Budget_Status = Event_Budget_Status.unapproved if budget is None else budget

def __init__(
self,
state: Event_State_Status = Event_State_Status.incomplete,
state: Event_State_Status = Event_State_Status.incomplete, # type: ignore
room: bool = False,
budget: bool = False,
cc_approver: str | None = None,
cc_approver_time: str | None = None,
slo_approver: str | None = None,
slo_approver_time: str | None = None,
slc_approver: str | None = None,
slc_approver_time: str | None = None,
):
self.state = state
self.room = room
self.budget = budget

self.cc_approver = cc_approver
self.cc_approver_time = cc_approver_time

self.slo_approver = slo_approver
self.slo_approver_time = slo_approver_time

self.slc_approver = slc_approver
self.slc_approver_time = slc_approver_time


# Event Modes
Expand Down
87 changes: 58 additions & 29 deletions mutations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import strawberry
from datetime import timedelta
from datetime import timedelta, datetime
from pydantic import HttpUrl, parse_obj_as
from fastapi.encoders import jsonable_encoder
import os
Expand Down Expand Up @@ -254,6 +254,10 @@ def progressEvent(
raise noaccess_error
event_instance = Event.parse_obj(event_ref)

# get current time
current_time = datetime.now()
time_str = current_time.strftime("%d-%m-%Y %I:%M %p")

if event_instance.status.state == Event_State_Status.incomplete:
if user["role"] != "club" or user["uid"] != event_instance.clubid:
raise noaccess_error
Expand All @@ -263,9 +267,13 @@ def progressEvent(
"room": False,
# or len(event_instance.location) == 0,
"state": Event_State_Status.pending_cc.value,
"cc_approver": event_instance.status.cc_approver,
"slc_approver": event_instance.status.slc_approver,
"slo_approver": event_instance.status.slo_approver,

"cc_approver": None,
"slc_approver": None,
"slo_approver": None,
"cc_approver_time": "Not Approved",
"slc_approver_time": "Not Approved",
"slo_approver_time": "Not Approved",
}

elif event_instance.status.state == Event_State_Status.pending_cc:
Expand All @@ -279,6 +287,9 @@ def progressEvent(
"cc_approver": user["uid"],
"slc_approver": event_instance.status.slc_approver,
"slo_approver": event_instance.status.slo_approver,
"cc_approver_time": time_str,
"slc_approver_time": event_instance.status.slc_approver_time,
"slo_approver_time": event_instance.status.slo_approver_time,
}
if cc_progress_budget is not None:
updation["budget"] = cc_progress_budget
Expand All @@ -292,8 +303,13 @@ def progressEvent(
if not updation["budget"]:
updation["state"] = Event_State_Status.pending_budget.value
elif not updation["room"]:
# if budget is approved
updation["slc_approver_time"] = None
updation["state"] = Event_State_Status.pending_room.value
else:
# if both are approved
updation["slc_approver_time"] = None
updation["slo_approver_time"] = None
updation["state"] = Event_State_Status.approved.value

elif event_instance.status.state == Event_State_Status.pending_budget:
Expand All @@ -307,11 +323,15 @@ def progressEvent(
"slc_approver": user["uid"],
"slo_approver": event_instance.status.slo_approver,
"cc_approver": event_instance.status.cc_approver,
"cc_approver_time": event_instance.status.cc_approver_time,
"slc_approver_time": time_str,
"slo_approver_time": event_instance.status.slo_approver_time,
}

if not updation["room"]:
updation["state"] = Event_State_Status.pending_room.value
else:
updation["slo_approver_time"] = time_str
updation["state"] = Event_State_Status.approved.value

elif event_instance.status.state == Event_State_Status.pending_room:
Expand All @@ -326,6 +346,9 @@ def progressEvent(
"slo_approver": user["uid"],
"slc_approver": event_instance.status.slc_approver,
"cc_approver": event_instance.status.cc_approver,
"cc_approver_time": event_instance.status.cc_approver_time,
"slc_approver_time": event_instance.status.slc_approver_time,
"slo_approver_time": time_str,
}

elif event_instance.status.state == Event_State_Status.approved:
Expand All @@ -341,59 +364,65 @@ def progressEvent(
"cc_approver": event_instance.status.cc_approver,
"slc_approver": event_instance.status.slc_approver,
"slo_approver": event_instance.status.slo_approver,
"cc_approver_time": event_instance.status.cc_approver_time,
"slc_approver_time": event_instance.status.slc_approver_time,
"slo_approver_time": event_instance.status.slo_approver_time,
}

upd_ref = eventsdb.update_one({"_id": eventid}, {"$set": {"status": updation}})
if upd_ref.matched_count == 0:
raise noaccess_error

event_ref = eventsdb.find_one({"_id": eventid})
event = Event.parse_obj(event_ref)
updated_event_instance = Event.parse_obj(event_ref)

# trigger mail notification

# Data Preparation for the mailing
mail_uid = user["uid"]
mail_club = getClubNameEmail(event.clubid, email=True, name=True)
mail_club = getClubNameEmail(updated_event_instance.clubid, email=True, name=True)

if mail_club is None:
raise Exception("Club does not exist.")
else:
clubname, mail_club = mail_club

mail_event_title = event.name
mail_eventlink = getEventLink(event.code)
mail_description = event.description
mail_event_title = updated_event_instance.name
mail_eventlink = getEventLink(updated_event_instance.code)
mail_description = updated_event_instance.description
if mail_description == "":
mail_description = "N/A"

student_count = event.population
student_count = updated_event_instance.population
mail_location = ""
if event.mode == Event_Mode.online:
if updated_event_instance.mode == Event_Mode.online:
mail_location = "online"
student_count = "N/A"
else:
mail_location = ", ".join(
[getattr(Event_Full_Location, loc) for loc in event.location]
[getattr(Event_Full_Location, loc) for loc in updated_event_instance.location]
)

equipment, additional = "N/A", "N/A"
if event.equipment:
equipment = event.equipment
if event.additional:
additional = event.additional
if updated_event_instance.equipment:
equipment = updated_event_instance.equipment
if updated_event_instance.additional:
additional = updated_event_instance.additional

ist_offset = timedelta(hours=5, minutes=30)
start_dt = event.datetimeperiod[0] + ist_offset
end_dt = event.datetimeperiod[1] + ist_offset
start_dt = updated_event_instance.datetimeperiod[0] + ist_offset
end_dt = updated_event_instance.datetimeperiod[1] + ist_offset
event_start_time = (
str(start_dt.strftime("%d-%m-%Y")) + " " + str(start_dt.strftime("%H:%M"))
)
event_end_time = (
str(end_dt.strftime("%d-%m-%Y")) + " " + str(end_dt.strftime("%H:%M"))
)

poc_details, poc_phone = getUser(event.poc, info.context.cookies)
poc = getUser(updated_event_instance.poc, info.context.cookies)
if not poc:
raise Exception("POC does not exist.")
poc_details, poc_phone = poc
poc_name = poc_details["firstName"] + " " + poc_details["lastName"]
poc_email = poc_details["email"]
poc_roll = poc_details["rollno"]
Expand All @@ -413,9 +442,9 @@ def progressEvent(
eventlink=mail_eventlink,
)

if event.status.state == Event_State_Status.pending_room:
if updated_event_instance.status.state == Event_State_Status.pending_room:
mail_body = PROGRESS_EVENT_BODY_FOR_SLO.safe_substitute(
event_id=event.code,
event_id=updated_event_instance.code,
club=clubname,
event=mail_event_title,
description=mail_description,
Expand All @@ -432,21 +461,21 @@ def progressEvent(
)

mail_to = []
if event.status.state == Event_State_Status.pending_cc:
if updated_event_instance.status.state == Event_State_Status.pending_cc:
mail_to = getRoleEmails("cc")

# Mail to club also for the successful submission of the event
mail_to_club = [
mail_club,
]
mail_subject_club = CLUB_EVENT_SUBJECT.safe_substitute(
event_id=event.code,
event_id=updated_event_instance.code,
event=mail_event_title,
)
mail_body_club = SUBMIT_EVENT_BODY_FOR_CLUB.safe_substitute(
event=mail_event_title,
eventlink=mail_eventlink,
event_id=event.code,
event_id=updated_event_instance.code,
club=clubname,
description=mail_description,
start_time=event_start_time,
Expand All @@ -465,17 +494,17 @@ def progressEvent(
toRecipients=mail_to_club,
cookies=info.context.cookies,
)
if event.status.state == Event_State_Status.pending_budget:
if updated_event_instance.status.state == Event_State_Status.pending_budget:
mail_to = getRoleEmails("slc")
if event.status.state == Event_State_Status.pending_room:
if updated_event_instance.status.state == Event_State_Status.pending_room:
mail_to = getRoleEmails("slo")
if event.status.state == Event_State_Status.approved:
if updated_event_instance.status.state == Event_State_Status.approved:
# mail to the club email
mail_to = [
mail_club,
]
mail_subject = CLUB_EVENT_SUBJECT.safe_substitute(
event_id=event.code,
event_id=updated_event_instance.code,
event=mail_event_title,
)
mail_body = APPROVED_EVENT_BODY_FOR_CLUB.safe_substitute(
Expand All @@ -493,7 +522,7 @@ def progressEvent(
cookies=info.context.cookies,
)

return EventType.from_pydantic(event)
return EventType.from_pydantic(updated_event_instance)


@strawberry.mutation
Expand Down

0 comments on commit 01683d6

Please sign in to comment.