|
| 1 | +"""Mixin for shared goal operations logic.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +from ...models import ( |
| 8 | + ArchivedGoalInfo, |
| 9 | + CreatedGoalInfo, |
| 10 | + GoalInfo, |
| 11 | + GoalStatus, |
| 12 | +) |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from collections.abc import Sequence |
| 16 | + |
| 17 | + |
| 18 | +class GoalOperationsMixin: |
| 19 | + """Shared logic for goal operations.""" |
| 20 | + |
| 21 | + def _transform_goal_list(self, data: Sequence[dict[str, Any]]) -> list[GoalInfo]: |
| 22 | + """Transform API response to list of GoalInfo models. |
| 23 | +
|
| 24 | + Args: |
| 25 | + data: The raw API response data. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + A list of GoalInfo models. |
| 29 | +
|
| 30 | + """ |
| 31 | + return [ |
| 32 | + GoalInfo( |
| 33 | + id=goal["Id"], |
| 34 | + user_id=goal["Owner"]["Id"], |
| 35 | + user_name=goal["Owner"]["Name"], |
| 36 | + title=goal["Name"], |
| 37 | + created_at=goal["CreateTime"], |
| 38 | + due_date=goal["DueDate"], |
| 39 | + status="Completed" if goal.get("Complete") else "Incomplete", |
| 40 | + meeting_id=goal["Origins"][0]["Id"] if goal.get("Origins") else None, |
| 41 | + meeting_title=( |
| 42 | + goal["Origins"][0]["Name"] if goal.get("Origins") else None |
| 43 | + ), |
| 44 | + ) |
| 45 | + for goal in data |
| 46 | + ] |
| 47 | + |
| 48 | + def _transform_archived_goals( |
| 49 | + self, data: Sequence[dict[str, Any]] |
| 50 | + ) -> list[ArchivedGoalInfo]: |
| 51 | + """Transform API response to list of ArchivedGoalInfo models. |
| 52 | +
|
| 53 | + Args: |
| 54 | + data: The raw API response data. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + A list of ArchivedGoalInfo models. |
| 58 | +
|
| 59 | + """ |
| 60 | + return [ |
| 61 | + ArchivedGoalInfo( |
| 62 | + id=goal["Id"], |
| 63 | + title=goal["Name"], |
| 64 | + created_at=goal["CreateTime"], |
| 65 | + due_date=goal["DueDate"], |
| 66 | + status="Complete" if goal.get("Complete") else "Incomplete", |
| 67 | + ) |
| 68 | + for goal in data |
| 69 | + ] |
| 70 | + |
| 71 | + def _transform_created_goal( |
| 72 | + self, data: dict[str, Any], title: str, meeting_id: int, user_id: int |
| 73 | + ) -> CreatedGoalInfo: |
| 74 | + """Transform API response to CreatedGoalInfo model. |
| 75 | +
|
| 76 | + Args: |
| 77 | + data: The raw API response data. |
| 78 | + title: The title of the goal. |
| 79 | + meeting_id: The ID of the meeting associated with the goal. |
| 80 | + user_id: The ID of the user responsible for the goal. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + A CreatedGoalInfo model. |
| 84 | +
|
| 85 | + """ |
| 86 | + # Map completion status |
| 87 | + completion_map = {2: "complete", 1: "on", 0: "off"} |
| 88 | + status = completion_map.get(data.get("Completion", 0), "off") |
| 89 | + |
| 90 | + return CreatedGoalInfo( |
| 91 | + id=data["Id"], |
| 92 | + user_id=user_id, |
| 93 | + user_name=data["Owner"]["Name"], |
| 94 | + title=title, |
| 95 | + meeting_id=meeting_id, |
| 96 | + meeting_title=data["Origins"][0]["Name"], |
| 97 | + status=status, |
| 98 | + created_at=data["CreateTime"], |
| 99 | + ) |
| 100 | + |
| 101 | + def _build_goal_update_payload( |
| 102 | + self, |
| 103 | + accountable_user: int, |
| 104 | + title: str | None = None, |
| 105 | + status: GoalStatus | str | None = None, |
| 106 | + ) -> dict[str, Any]: |
| 107 | + """Build payload for goal update operation. |
| 108 | +
|
| 109 | + Args: |
| 110 | + accountable_user: The ID of the user responsible for the goal. |
| 111 | + title: The new title of the goal. |
| 112 | + status: The status value (GoalStatus enum or string). |
| 113 | +
|
| 114 | + Returns: |
| 115 | + A dictionary containing the update payload. |
| 116 | +
|
| 117 | + Raises: |
| 118 | + ValueError: If an invalid status value is provided. |
| 119 | +
|
| 120 | + """ |
| 121 | + payload: dict[str, Any] = {"accountableUserId": accountable_user} |
| 122 | + |
| 123 | + if title is not None: |
| 124 | + payload["title"] = title |
| 125 | + |
| 126 | + if status is not None: |
| 127 | + valid_status = {"on": "OnTrack", "off": "AtRisk", "complete": "Complete"} |
| 128 | + # Handle both GoalStatus enum and string |
| 129 | + status_value = status.value if isinstance(status, GoalStatus) else status |
| 130 | + status_key = status_value.lower() |
| 131 | + if status_key not in valid_status: |
| 132 | + raise ValueError( |
| 133 | + "Invalid status value. Must be 'on', 'off', or 'complete'." |
| 134 | + ) |
| 135 | + payload["completion"] = valid_status[status_key] |
| 136 | + |
| 137 | + return payload |
0 commit comments