diff --git a/src/main.py b/src/main.py index 9773102..ecac7ed 100644 --- a/src/main.py +++ b/src/main.py @@ -1,33 +1,18 @@ import click import random import odoo_client -import json +import re from rich import print, console from datetime import datetime from collections import defaultdict -from typing import Dict, List, Optional from config import vfyd_tags, team from tasks import Task from leave import Leave -import re +from storage import DispatchStorage console = console.Console() -def load_dispatch(date: Optional[str] = None) -> Dict[str, List[int]]: - if date is None: - date = datetime.now().strftime("%Y-%m-%d") - with open(f"out/{date}_dispatch.json", "r") as f: - return json.load(f) - - -def save_dispatch(dispatch: Dict[str, List[int]], date: Optional[str] = None) -> None: - if date is None: - date = datetime.now().strftime("%Y-%m-%d") - with open(f"out/{date}_dispatch.json", "w") as f: - json.dump(dispatch, f) - - @click.group() def cli() -> None: pass @@ -68,11 +53,17 @@ def dispatch(): if availability[0] or availability[1] ] + previous_dispatch = DispatchStorage.load_week_to_date() + previous_dispatch_ids = [ + task_id for tasks in previous_dispatch.values() for task_id in tasks + ] + domain = [ ("stage_id", "=", 194), ("user_ids", "=", False), ("project_id", "=", 49), ("tag_ids", "not in", vfyd_tags), + ("id", "not in", previous_dispatch_ids), ] records = client.web_search_read(Task, domain) @@ -83,8 +74,28 @@ def dispatch(): dispatch[available_employees[idx % len(available_employees)]].append(task.id) out[available_employees[idx % len(available_employees)]].append(task) - save_dispatch(dispatch) - for key, tasks in out.items(): + DispatchStorage.save(dispatch) + + domain = [ + ("stage_id", "=", 194), + ("user_ids", "=", False), + ("project_id", "=", 49), + ("tag_ids", "not in", vfyd_tags), + ("id", "in", previous_dispatch_ids), + ] + records = client.web_search_read(Task, domain) + + merged = defaultdict(list) + for member, task_ids in previous_dispatch.items(): + for task_id in task_ids: + task = next((t for t in records if t.id == task_id), None) + if task: + merged[member].append(task) + + for member, tasks in out.items(): + merged[member].extend(tasks) + + for key, tasks in merged.items(): print(f"**{key}:**") for task in tasks: print(task.url) @@ -110,7 +121,7 @@ def status(): def check(): """Check which previously dispatched tasks are still not completed.""" - dispatch = load_dispatch() + dispatch = DispatchStorage.load() reverse_dispatch = {v: k for k, vs in dispatch.items() for v in vs} client = odoo_client.OdooClient() domain = [ diff --git a/src/storage.py b/src/storage.py new file mode 100644 index 0000000..c36ad8d --- /dev/null +++ b/src/storage.py @@ -0,0 +1,43 @@ +from typing import Dict, List, Optional +from datetime import datetime, timedelta +import json + + +class DispatchStorage: + @classmethod + def path(cls, date: Optional[str] = None) -> str: + if date is None: + date = datetime.now().strftime("%Y-%m-%d") + return f"out/{date}_dispatch.json" + + @classmethod + def load(cls, date: Optional[str] = None) -> Dict[str, List[int]]: + with open(cls.path(date), "r") as f: + return json.load(f) + + @classmethod + def save(cls, dispatch: Dict[str, List[int]], date: Optional[str] = None) -> None: + with open(cls.path(date), "w") as f: + json.dump(dispatch, f) + + @classmethod + def load_week_to_date(cls) -> Dict[str, List[int]]: + """ + Loads and combines dispatch data from the start of the current week up to today. + """ + today = datetime.now() + days_since_monday = today.weekday() + current_date = today - timedelta(days=days_since_monday) + + combined_dispatch: Dict[str, List[int]] = {} + while current_date.date() < today.date(): + date_str = current_date.strftime("%Y-%m-%d") + daily_dispatch = cls.load(date_str) + for key, values in daily_dispatch.items(): + if key in combined_dispatch: + combined_dispatch[key].extend(values) + else: + combined_dispatch[key] = values.copy() + current_date += timedelta(days=1) + + return combined_dispatch