File size: 7,518 Bytes
cd2dce0
cdf6346
dd3c551
 
cd2dce0
dd3c551
 
 
cd2dce0
dd3c551
 
 
 
 
 
cdf6346
 
 
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea81bc0
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
cd2dce0
 
 
 
 
 
 
 
 
ea81bc0
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
cd2dce0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
cd2dce0
 
 
63e1f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2fd41
63e1f68
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import asyncio
from datetime import datetime, timedelta, timezone

from fastapi import HTTPException
from jinja2 import Environment, FileSystemLoader, select_autoescape

from cbh.api.calls.models import CallModel
from cbh.api.calls.dto import CallStatus
from cbh.core.config import settings


def can_edit_call(call: CallModel) -> None:
    """
    Check if the call can be edited.
    """
    now = datetime.now(timezone.utc) if call.event.startDate.tzinfo else datetime.now()
    min_edit_time = now + timedelta(hours=48)

    if call.status != CallStatus.SCHEDULED or call.event.startDate < min_edit_time:
        raise HTTPException(status_code=400, detail="Call cannot be edited.")


def calculate_sleep_time(call: CallModel) -> int:
    """
    Calculate the sleep time for the reminder.
    """
    now = datetime.now(timezone.utc) if call.event.startDate.tzinfo else datetime.now()
    reminder_time = call.event.startDate - timedelta(hours=1)
    sleep_time = (reminder_time - now).total_seconds()

    return max(0, int(sleep_time))


async def send_customer_booking_email(call: CallModel) -> None:
    """
    Send a booking email.
    """
    if not call.event.isActive:
        return
    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("bookingConfirmation.html")

    template_content = template.render(
        coach_name=call.coach.name,
        user_name=call.customer.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        join_link=f"{settings.Audience}/calls/{call.id}",
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
    )

    await settings.EMAIL_CLIENT.send_email(
        call.customer.email,
        f"You're all set! Session with {call.coach.name} confirmed!",
        template_content,
    )


async def send_coach_booking_email(call: CallModel) -> None:
    """
    Send a booking email.
    """
    if not call.event.isActive:
        return
    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("bookingConfirmationCoach.html")
    template_content = template.render(
        coach_name=call.coach.name,
        user_name=call.customer.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        join_link=f"{settings.Audience}/calls/{call.id}",
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
    )

    await settings.EMAIL_CLIENT.send_email(
        call.coach.email,
        f"New session booked with {call.customer.name}!",
        template_content,
    )


async def send_cancel_customer_email(call: CallModel) -> None:
    """
    Send a cancellation email.
    """
    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("sessionCancellation.html")
    template_content = template.render(
        coach_name=call.coach.name,
        user_name=call.customer.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
    )

    await settings.EMAIL_CLIENT.send_email(
        call.customer.email,
        f"Your session with {call.coach.name} has been cancelled.",
        template_content,
    )


async def send_cancel_coach_email(call: CallModel) -> None:
    """
    Send a cancellation email.
    """
    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("sessionCancellationCoach.html")
    template_content = template.render(
        coach_name=call.coach.name,
        user_name=call.customer.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
    )

    await settings.EMAIL_CLIENT.send_email(
        call.coach.email,
        f"{call.customer.name} cancelled their session.",
        template_content,
    )


async def send_session_reminder_email(call: CallModel) -> None:
    """
    Send a session reminder email.
    """
    if not call.event.isActive:
        return
    sleep_time = calculate_sleep_time(call)
    await asyncio.sleep(sleep_time)

    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("sessionReminder.html")
    template_content = template.render(
        coach_name=call.coach.name,
        user_name=call.customer.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        join_link=f"{settings.Audience}/calls/{call.id}",
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
    )

    await settings.EMAIL_CLIENT.send_email(
        call.customer.email,
        f"Your session with {call.coach.name} is starting in 1 hour.",
        template_content,
    )


async def send_session_reminder_coach_email(call: CallModel) -> None:
    """
    Send a session reminder email.
    """
    if not call.event.isActive:
        return
    sleep_time = calculate_sleep_time(call)
    await asyncio.sleep(sleep_time)

    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("sessionReminderCoach.html")
    template_content = template.render(
        coach_name=call.coach.name,
        user_name=call.customer.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        join_link=f"{settings.Audience}/calls/{call.id}",
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
    )

    await settings.EMAIL_CLIENT.send_email(
        call.coach.email,
        f"Your session with {call.customer.name} is starting in 1 hour.",
        template_content,
    )


async def send_anonymous_booking_email(call: CallModel) -> None:
    """
    Send a anonymous booking email.
    """
    templates_path = settings.BASE_DIR / "cbh" / "templates" / "emails"
    env = Environment(
        loader=FileSystemLoader(templates_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    template = env.get_template("anonymousBookingConfirmation.html")
    template_content = template.render(
        coach_name=call.coach.name,
        start_date=call.event.startDate.strftime("%d %B %Y, %H:%M"),
        join_link=f"{settings.Audience}/calls/{call.id}",
        duration=(call.event.endDate - call.event.startDate).total_seconds() // 60,
        registration_link=f"{settings.Audience}/signup/complete?accountId={call.customer.id}",
    )

    await settings.EMAIL_CLIENT.send_email(
        call.customer.email,
        f"You're all set! Session with {call.coach.name} confirmed!",
        template_content,
    )