File size: 2,566 Bytes
21fe460
 
 
 
fd42852
21fe460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd42852
 
9ee3555
 
 
fd42852
2926eba
 
fd42852
 
 
 
 
 
 
 
 
57e2194
 
21fe460
 
 
 
 
 
 
 
 
 
 
fd42852
21fe460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd42852
21fe460
 
 
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
from __future__ import annotations

from datetime import datetime, timedelta, timezone
from uuid import uuid4
from zoneinfo import ZoneInfo


HOUR_OPTIONS = [f"{hour:02d}" for hour in range(24)]
MINUTE_OPTIONS = [f"{minute:02d}" for minute in range(60)]


def compute_end_time(start_hour: str, start_minute: str) -> tuple[str, str]:
    start_time = datetime(2000, 1, 1, int(start_hour), int(start_minute), tzinfo=timezone.utc)
    end_time = start_time + timedelta(hours=2)
    return end_time.strftime("%H"), end_time.strftime("%M")


def format_time(hour: str, minute: str) -> str:
    return f"{hour} : {minute}"


def get_next_occurrence_minutes(end_hour: str, end_minute: str) -> int:
    now = datetime.now(ZoneInfo("Asia/Ho_Chi_Minh"))
    end_time = now.replace(hour=int(end_hour), minute=int(end_minute), second=0, microsecond=0)
    if end_time < now:
        end_time += timedelta(days=1)
    return int((end_time - now).total_seconds() // 60)


def sort_cards_by_upcoming_time(cards: list[dict]) -> list[dict[str, str]]:
    return sorted(
        cards,
        key=lambda card: (
            get_next_occurrence_minutes(card.get("endHour", "23"), card.get("endMinute", "59")),
            card.get("title", "").lower(),
            card.get("createdAt", ""),
        ),
    )


def build_card_payload() -> dict[str, str]:
    end_hour, end_minute = compute_end_time("08", "00")
    return {
        "id": "",
        "title": "",
        "startHour": "08",
        "startMinute": "00",
        "endHour": end_hour,
        "endMinute": end_minute,
        "createdAt": "",
        "clientKey": str(uuid4()),
        "tabId": "",
    }


def normalize_cards(cards: list[dict]) -> list[dict[str, str]]:
    normalized = []
    for card in cards:
        start_hour = str(card.get("startHour", "08")).zfill(2)
        start_minute = str(card.get("startMinute", "00")).zfill(2)
        end_hour, end_minute = compute_end_time(start_hour, start_minute)
        normalized.append(
            {
                "id": str(card.get("id", "")),
                "title": str(card.get("title", "")),
                "startHour": start_hour,
                "startMinute": start_minute,
                "endHour": str(card.get("endHour", end_hour)).zfill(2),
                "endMinute": str(card.get("endMinute", end_minute)).zfill(2),
                "createdAt": str(card.get("createdAt", "")),
                "clientKey": str(card.get("clientKey", uuid4())),
                "tabId": str(card.get("tabId", "")),
            }
        )
    return normalized