simplify data logic since this is only for webhooks

This commit is contained in:
Michael Genson 2025-07-27 03:53:15 +00:00
commit 18ba809d5c

View file

@ -3,7 +3,6 @@ import json
from abc import ABC, abstractmethod
from collections.abc import Generator
from datetime import UTC, datetime
from typing import cast
from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit
from fastapi.encoders import jsonable_encoder
@ -148,21 +147,23 @@ class WebhookEventListener(EventListenerBase):
def publish_to_subscribers(self, event: Event, subscribers: list[ReadWebhook]) -> None:
with self.ensure_repos(self.group_id, self.household_id) as repos:
webhook_data = cast(EventWebhookData, event.document_data)
if not isinstance(event.document_data, EventWebhookData):
return
match event.document_data.document_type:
case EventDocumentType.mealplan:
meal_repo = repos.meals
meal_data = meal_repo.get_meals_by_date_range(
webhook_data.webhook_start_dt, webhook_data.webhook_end_dt
event.document_data.webhook_start_dt, event.document_data.webhook_end_dt
)
webhook_data.webhook_body = meal_data or None
event.document_data.webhook_body = meal_data or None
case _:
if event.event_type is EventTypes.test_message:
# make sure the webhook has a valid body so it gets sent
webhook_data.webhook_body = webhook_data.webhook_body or []
event.document_data.webhook_body = event.document_data.webhook_body or []
# Only publish to subscribers if we have a webhook body to send
if webhook_data.webhook_body is not None:
if event.document_data.webhook_body is not None:
self.publisher.publish(event, [webhook.url for webhook in subscribers])
def get_scheduled_webhooks(self, start_dt: datetime, end_dt: datetime) -> list[ReadWebhook]: