from datetime import datetime from typing import Any, Dict, Optional from zoneinfo import ZoneInfo from bson import ObjectId from discord import ( ApplicationContext, ) from modules.database import col_events async def validate_event_validity( ctx: ApplicationContext, name: str, start_date: datetime, end_date: datetime, event_id: Optional[ObjectId] = None, to_utc: bool = False, ) -> bool: start_date_internal: datetime = start_date.astimezone(ZoneInfo("UTC")) if to_utc else start_date end_date_internal: datetime = end_date.astimezone(ZoneInfo("UTC")) if to_utc else end_date if start_date_internal < datetime.now(tz=ZoneInfo("UTC")): # TODO Make a nice message await ctx.respond("Start date must not be in the past") return False if end_date_internal < datetime.now(tz=ZoneInfo("UTC")): # TODO Make a nice message await ctx.respond("End date must not be in the past") return False if start_date_internal >= end_date_internal: # TODO Make a nice message await ctx.respond("Start date must be before end date") return False # TODO Add validation for concurrent events. # Only one event can take place at the same time. query: Dict[str, Any] = { "name": name, "ended": None, "ends": {"$gt": datetime.now(tz=ZoneInfo("UTC"))}, "is_cancelled": {"$ne": True}, } if event_id is not None: query["_id"] = {"$ne": event_id} if (await col_events.find_one(query)) is not None: # TODO Make a nice message await ctx.respond("There can only be one active event with the same name") return False return True