From d91a71ea15ac700962cd0af7ee55062fefdfe62a Mon Sep 17 00:00:00 2001
From: Michael Bauer <michael-bauer@posteo.de>
Date: Fri, 3 May 2024 10:33:50 +0200
Subject: [PATCH] Implement AbsencesBatchCreateMutation (WIP)

---
 aleksis/apps/alsijil/schema/absences.py | 86 +++++++++++++++++++++----
 1 file changed, 74 insertions(+), 12 deletions(-)

diff --git a/aleksis/apps/alsijil/schema/absences.py b/aleksis/apps/alsijil/schema/absences.py
index 42dfce466..6bb9b9481 100644
--- a/aleksis/apps/alsijil/schema/absences.py
+++ b/aleksis/apps/alsijil/schema/absences.py
@@ -1,5 +1,10 @@
 import graphene
+from datetime import datetime
+
+from aleksis.apps.kolego.models import Absence
+
 from .documentation import DocumentationType
+from ..models import ParticipationStatus
 
 class LessonsForPersonType(graphene.ObjectType):
     id = graphene.ID()  # noqa
@@ -16,16 +21,73 @@ class AbsencesBatchCreateMutation(graphene.Mutation):
     ok = graphene.Boolean()
 
     @classmethod
-    def mutate(cls, root, info, input):  # noqa
-        # Get all lesson events for person (share with query)
-        # Look up documentations for lesson events
-        #   (share with models.py Documentation get_for_coursebook;
-        #    has lesson event lookup as well & returns real & fake documentations
-        #    => extract & reuse the documentation lookup)
-        # Create a ParticipationStatus for each documentation
-        #  (OR if no documentation and in past create documentation)
-        # If there are any LessonEvents without a documentation
-        #   create a Kolego Absence for the whole duration
-        # Return ok=True if everything went well.
+    def mutate(cls, root, info, persons, start, end, comment, reason):  # noqa
+        # TODO: More permission checks needed?
+        # DocumentationBatchCreateOrUpdateMutation.create_or_update
+        # at least already checks permissions.
+
+        for person in persons:
+            # Get all documentations for this person between start & end
+            # Could be shared with query
+            event_params = {
+                "type": "PARTICIPANT",
+                "obj_id": PERSON_ID,
+            }
+
+            events = LessonEvent.get_single_events(
+                datetime.combine(start, datetime.min.time()),
+                datetime.combine(end, datetime.max.time()),
+                None,
+                event_params,
+                with_reference_object=True,
+            )
+
+            docs, dummies = Documentation.get_documentations_for_events(events)
+            # till here -> reuse?
 
-        return AbsencesBatchCreateMutation(ok=BOOL)
+            # Create doc for dummies that are already in the past
+            future = false
+            for dummy in dummies:
+                # TODO/MAYBE: This in past logic could be somewhere else OR shared.
+                # The next 5 lines are shared with DocumentationBatchCreateOrUpdateMutation
+                # & could be deduplicated
+                dummy, lesson_event_id, datetime_start_iso, datetime_end_iso = _id.split(";")
+                lesson_event = LessonEvent.objects.get(id=lesson_event_id)
+                start = datetime.fromisoformat(datetime_start_iso).astimezone(
+                    lesson_event.timezone
+                )
+
+                if start < datetime.now():
+                    # In the past -> Create a Documentation
+                    docs.append(
+                        DocumentationBatchCreateOrUpdateMutation.create_or_update(info, dummy)
+                    )
+                else:
+                    future = true
+
+            # Create a ParticipationStatus for each documentation
+            for doc in docs:
+                # TODO: Is ID for enough for person&reason OR should it
+                # resolve to django object first?
+                ParticipationStatus.objects.create(
+                    person=person,
+                    related_documentation=doc,
+                    absence_reason=reason,
+                )
+
+            # If there are still dummy documentations in the future
+            # create a Kolego Absence
+            if future:
+                # TODO: Should probably lookup person&reason as well
+                # TODO: Are date_start & date_end from CalendarEvent enough
+                #       or more needed?
+                Absence.objects.create(
+                    date_start=datetime.now().date(),
+                    date_end=end,
+                    reason=reason,
+                    person=person,
+                    comment=comment,
+                )
+
+        # Return ok=True if everything went well.
+        return AbsencesBatchCreateMutation(ok=True)
-- 
GitLab