From 7372593f6fdc3b14fff63ecf7896b4a191bb77dd Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Thu, 16 Nov 2023 14:13:00 +0100
Subject: [PATCH] fix nomination invitation logic add checklist step for
 sending start email

---
 .../_nominations_invitation_checklist.html    | 28 +++++-
 scipost_django/colleges/views.py              | 99 +++++++++++--------
 2 files changed, 81 insertions(+), 46 deletions(-)

diff --git a/scipost_django/colleges/templates/colleges/_nominations_invitation_checklist.html b/scipost_django/colleges/templates/colleges/_nominations_invitation_checklist.html
index 84d0a2f93..2d9735973 100644
--- a/scipost_django/colleges/templates/colleges/_nominations_invitation_checklist.html
+++ b/scipost_django/colleges/templates/colleges/_nominations_invitation_checklist.html
@@ -31,18 +31,38 @@
     {% if invitation.response == 'accepted' or invitation.response == 'postponed' %}
       <li class="list-group-item p-2">
 
-        {% if invitation.nomination.fellowship %}
+        {% if not invitation.nomination.fellowship %}
           <span class="text-danger">{% include 'bi/x-square-fill.html' %}</span>
           <span>&nbsp;This nominee has no associated Fellowship with this college.</span>
-          <a href="{% url 'colleges:fellowship_create' contributor_id=invitation.nomination.profile.contributor.id %}"
-             target="_blank">Set up a Fellowship</a>
+          <div>
+            <a href="{% url 'colleges:fellowship_create' contributor_id=invitation.nomination.profile.contributor.id %}"
+               target="_blank">Set up a Fellowship</a>
+          </div>
         {% else %}
           <span class="text-success">{% include 'bi/check-square-fill.html' %}</span>
           <span>&nbsp;A fellowship has been created from this nomination.</span>
         {% endif %}
 
- 
       </li>
+
+      {% if invitation.nomination.fellowship %}
+        <li class="list-group-item p-2">
+ 
+
+          {% if not start_email_sent %}
+            <span class="text-primary">{% include 'bi/question-square-fill.html' %}</span>
+            <span>&nbsp;Uncertain whether a nomination start email has been sent.</span>
+            <div>
+              <a href="{% url 'colleges:fellowship_email_start' pk=nomination.fellowship.id %}"
+                 target="_blank">Send Fellowship start email</a>
+            </div>
+          {% else %}
+            <span class="text-success">{% include 'bi/check-square-fill.html' %}</span>
+            <span>&nbsp;Nomination start email has been sent.</span>
+          {% endif %}
+
+        </li>
+      {% endif %}
     {% endif %}
 
   </ul>
diff --git a/scipost_django/colleges/views.py b/scipost_django/colleges/views.py
index ff1015b45..481b7f7cc 100644
--- a/scipost_django/colleges/views.py
+++ b/scipost_django/colleges/views.py
@@ -277,6 +277,16 @@ class FellowshipStartEmailView(PermissionsMixin, MailView):
     mail_code = "fellows/email_fellow_fellowship_start"
     success_url = reverse_lazy("colleges:fellowships")
 
+    def form_valid(self, form):
+        """Create an event associated to this outgoing email."""
+        event = FellowshipNominationEvent(
+            nomination=self.object.nomination,
+            description="Fellowship start email sent",
+            by=self.request.user.contributor,
+        )
+        event.save()
+        return super().form_valid(form)
+
 
 @login_required
 @permission_required("scipost.can_manage_college_composition", raise_exception=True)
@@ -760,8 +770,12 @@ def _hx_nomination_round_remove_voter(request, round_id, voter_id):
 def _hx_nomination_details_contents(request, nomination_id):
     """For (re)loading the details if modified."""
     nomination = get_object_or_404(FellowshipNomination, pk=nomination_id)
+    start_email_sent = nomination.events.filter(
+        description__contains="start email sent",
+    ).exists()
     context = {
         "nomination": nomination,
+        "start_email_sent": start_email_sent,
     }
     return render(request, "colleges/_hx_nomination_details_contents.html", context)
 
@@ -1031,64 +1045,65 @@ def _hx_fellowship_invitation_update_response(request, invitation_id):
             by=request.user.contributor,
         )
 
-        nonexpired_fellowship = (
-            Fellowship.objects.exclude(
-                until_date__lte=timezone.now().date(),
-            )
-            .filter(
-                college=invitation.nomination.college,
-                contributor=invitation.nomination.profile.contributor,
-            )
-            .order_by("-start_date")
-            .first()
-        )
-
         # If the invitation is accepted or postponed, create a Fellowship
         if invitation.response in [
             FellowshipInvitation.RESPONSE_ACCEPTED,
             FellowshipInvitation.RESPONSE_POSTPONED,
         ]:
+            nonexpired_fellowship = (
+                Fellowship.objects.exclude(
+                    until_date__lte=timezone.now().date(),
+                )
+                .filter(
+                    college=invitation.nomination.college,
+                    contributor=invitation.nomination.profile.contributor,
+                )
+                .order_by("-start_date")
+                .first()
+            )
+
             # Create a new Fellowship if no object exists
             if not nonexpired_fellowship:
+                start_date = invitation.postpone_start_to or timezone.now()
                 fellowship = Fellowship.objects.create(
                     college=invitation.nomination.college,
                     contributor=invitation.nomination.profile.contributor,
-                    start_date=timezone.now()
-                    if invitation.response == FellowshipInvitation.RESPONSE_ACCEPTED
-                    else invitation.postpone_start_to,
-                    until_date=None,
+                    start_date=start_date,
+                    until_date=start_date + timezone.timedelta(days=365 * 5 + 1),
                 )
 
                 invitation.nomination.add_event(
                     description=f"Fellowship created (start: {fellowship.start_date.strftime('%Y-%m-%d')})",
                     by=request.user.contributor,
                 )
-            else:
-                # Update the start date of the Fellowship if an object already exists
-                nonexpired_fellowship.start_date = (
-                    timezone.now()
-                    if invitation.response == FellowshipInvitation.RESPONSE_ACCEPTED
-                    else invitation.postpone_start_to
-                )
-                nonexpired_fellowship.until_date = None
-                invitation.nomination.add_event(
-                    description=f"Fellowship start date updated (start: {nonexpired_fellowship.start_date.strftime('%Y-%m-%d')})",
-                    by=request.user.contributor,
-                )
-                nonexpired_fellowship.save()
-        # Terminate the Fellowship if the invitation is declined
-        elif invitation.response == FellowshipInvitation.RESPONSE_DECLINED:
-            if nonexpired_fellowship:
-                nonexpired_fellowship.until_date = (
-                    timezone.now().date()
-                    if nonexpired_fellowship.is_active()
-                    else nonexpired_fellowship.start_date
-                )
-                invitation.nomination.add_event(
-                    description=f"Fellowship ended (end: {nonexpired_fellowship.until_date.strftime('%Y-%m-%d')})",
-                    by=request.user.contributor,
-                )
-                nonexpired_fellowship.save()
+
+                invitation.nomination.fellowship = fellowship
+        #     else:
+        #         # Update the start date of the Fellowship if an object already exists
+        #         nonexpired_fellowship.start_date = (
+        #             timezone.now()
+        #             if invitation.response == FellowshipInvitation.RESPONSE_ACCEPTED
+        #             else invitation.postpone_start_to
+        #         )
+        #         nonexpired_fellowship.until_date = None
+        #         invitation.nomination.add_event(
+        #             description=f"Fellowship start date updated (start: {nonexpired_fellowship.start_date.strftime('%Y-%m-%d')})",
+        #             by=request.user.contributor,
+        #         )
+        #         nonexpired_fellowship.save()
+        # # Terminate the Fellowship if the invitation is declined
+        # elif invitation.response == FellowshipInvitation.RESPONSE_DECLINED:
+        #     if nonexpired_fellowship:
+        #         nonexpired_fellowship.until_date = (
+        #             timezone.now().date()
+        #             if nonexpired_fellowship.is_active()
+        #             else nonexpired_fellowship.start_date
+        #         )
+        #         invitation.nomination.add_event(
+        #             description=f"Fellowship ended (end: {nonexpired_fellowship.until_date.strftime('%Y-%m-%d')})",
+        #             by=request.user.contributor,
+        #         )
+        #         nonexpired_fellowship.save()
 
         return HTMXResponse(
             f"Response updated to: {invitation.get_response_display()}",
-- 
GitLab