Skip to content

Commit b490a3c

Browse files
authored
Merge pull request #332 from LleidaHack/reminder-endpoint
Reminder endpoint
2 parents 5ba9707 + b5f12d2 commit b490a3c

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

src/impl/Event/router_v1.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,20 @@ def resend_accept_mail(
379379
return {"success": True}
380380

381381

382-
@router.get("/{event_id}/send_slack_mail/")
383-
def send_slack_mail(event_id: int, token: BaseToken = Depends(JWTBearer())):
384-
event_service.send_slack_mail(event_id, token)
382+
@router.post("/{event_id}/send_slack_mail/")
383+
def send_slack_mail(event_id: int, slackUrl: str, token: BaseToken = Depends(JWTBearer())):
384+
event_service.send_slack_mail(event_id, slackUrl, token)
385+
return {"success": True}
386+
387+
@router.post("/{event_id}/send_reminder_mails/")
388+
def send_reminder_mails(
389+
event_id: int,
390+
token: BaseToken = Depends(JWTBearer()),
391+
):
392+
"""
393+
Send reminder mails to all accepted hackers of an event
394+
"""
395+
event_service.send_reminder_mails(event_id, token)
385396
return {"success": True}
386397

387398

src/impl/Event/service.py

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def resend_mail(self, event_id: int, hacker_id: int, data: BaseToken):
972972
db.session.commit()
973973

974974
@BaseService.needs_service(MailClient)
975-
def send_slack_mail(self, event_id: int, data: BaseToken):
975+
def send_slack_mail(self, event_id: int, slackUrl: str, data: BaseToken):
976976
if not data.check([UserType.LLEIDAHACKER]):
977977
raise AuthenticationException("Not authorized")
978978
event = self.get_by_id(event_id)
@@ -984,16 +984,81 @@ def send_slack_mail(self, event_id: int, data: BaseToken):
984984
hackers = event.accepted_hackers
985985

986986
for hacker in hackers:
987-
self.mail_client.create_mail(
987+
mail = self.mail_client.create_mail(
988988
MailCreate(
989989
template_id=self.mail_client.get_internall_template_id(
990990
InternalTemplate.EVENT_SLACK_INVITE
991991
),
992992
subject="HackEPS2024 slack invitation",
993993
receiver_id=str(hacker.id),
994994
receiver_mail=str(hacker.email),
995-
fields="https://join.slack.com/t/hackeps2024/shared_invite/zt-2usc9qny9-z3NkybNlCXFAI9m0Cl~FsQ",
995+
fields=slackUrl,
996996
)
997997
)
998+
# send the created mail
999+
self.mail_client.send_mail_by_id(mail.id)
9981000

9991001
db.session.commit()
1002+
1003+
@BaseService.needs_service(MailClient)
1004+
def send_reminder_mails(
1005+
self,
1006+
event_id: int,
1007+
data: BaseToken,
1008+
):
1009+
if not data.check([UserType.LLEIDAHACKER]):
1010+
raise AuthenticationException("Not authorized")
1011+
event = self.get_by_id(event_id)
1012+
if event.archived:
1013+
raise InvalidDataException(
1014+
"Unable to operate with an archived event, unarchive it first"
1015+
)
1016+
1017+
hackers = event.accepted_hackers
1018+
1019+
for hacker in hackers:
1020+
try:
1021+
reg = (
1022+
db.session.query(HackerRegistration)
1023+
.filter(
1024+
HackerRegistration.user_id == hacker.id,
1025+
HackerRegistration.event_id == event.id,
1026+
)
1027+
.first()
1028+
)
1029+
# send reminder only if registration exists and assistance not confirmed
1030+
if reg is None or reg.confirmed_assistance:
1031+
continue
1032+
1033+
# ensure there is a confirmation token for this registration
1034+
if not reg.confirm_assistance_token:
1035+
reg.confirm_assistance_token = AssistenceToken(hacker, event.id).to_token()
1036+
1037+
# compute days left until event (rounded down)
1038+
try:
1039+
delta = event.start_date - datetime.now()
1040+
days_left = max(0, int(delta.total_seconds() // 86400))
1041+
except Exception:
1042+
days_left = 0
1043+
1044+
fields = f"{hacker.name},{event.name},{days_left},{reg.confirm_assistance_token}"
1045+
1046+
mail = self.mail_client.create_mail(
1047+
MailCreate(
1048+
template_id=self.mail_client.get_internall_template_id(
1049+
InternalTemplate.EVENT_HACKER_REMINDER
1050+
),
1051+
subject=f"{event.name} - Recordatori de confirmació d'assistència",
1052+
receiver_id=str(hacker.id),
1053+
receiver_mail=str(hacker.email),
1054+
fields=fields,
1055+
)
1056+
)
1057+
# send the created mail
1058+
self.mail_client.send_mail_by_id(mail.id)
1059+
db.session.commit()
1060+
except Exception as e:
1061+
db.session.rollback()
1062+
# Optionally log the error here, e.g.:
1063+
# print(f"Failed to send reminder to hacker {hacker.id}: {e}")
1064+
continue

src/impl/Mail/internall_templates.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ class InternalTemplate(Enum):
1111

1212
EVENT_HACKER_REGISTERED = "event_hacker_registered"
1313
EVENT_HACKER_ACCEPTED = "event_hacker_accepted"
14+
15+
EVENT_HACKER_REMINDER = "event_hacker_reminder"
1416
# EVENT_HACKER_REJECTED = 'event_hacker_accepted'
1517
# EVENT_HACKER_CONFIRMATION = 'event_hacker_confirmation'

0 commit comments

Comments
 (0)