Skip to content

Commit

Permalink
fix: Restore Webhook Test Functionality (#3857)
Browse files Browse the repository at this point in the history
Co-authored-by: Kuchenpirat <[email protected]>
  • Loading branch information
michael-genson and Kuchenpirat authored Jul 6, 2024
1 parent aa6e109 commit 6e6ae80
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
2 changes: 0 additions & 2 deletions frontend/components/Domain/Group/GroupWebhookEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
icon: $globals.icons.testTube,
text: $tc('general.test'),
event: 'test',
// TODO: There is no functionality hooked up to this. Enable it when there is
disabled: true,
},
{
icon: $globals.icons.save,
Expand Down
8 changes: 7 additions & 1 deletion frontend/composables/use-group-webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export const useGroupWebhooks = function () {
newDt.setMinutes(Number(minutes));

updateData.scheduledTime = `${pad(newDt.getUTCHours(), 2)}:${pad(newDt.getUTCMinutes(), 2)}`;
console.log(updateData.scheduledTime);

const payload = {
...updateData,
Expand All @@ -85,7 +84,14 @@ export const useGroupWebhooks = function () {
if (data) {
this.refreshAll();
}
loading.value = false;
},

async testOne(id: string | number) {
loading.value = true;
await api.groupWebhooks.testOne(id);
loading.value = false;
}
};

const webhooks = actions.getAll();
Expand Down
6 changes: 6 additions & 0 deletions frontend/lib/api/user/group-webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ const prefix = "/api";
const routes = {
webhooks: `${prefix}/groups/webhooks`,
webhooksId: (id: string | number) => `${prefix}/groups/webhooks/${id}`,
webhooksIdTest: (id: string | number) => `${prefix}/groups/webhooks/${id}/test`,
};

export class WebhooksAPI extends BaseCRUDAPI<CreateWebhook, ReadWebhook> {
baseRoute = routes.webhooks;
itemRoute = routes.webhooksId;
itemTestRoute = routes.webhooksIdTest;

async testOne(itemId: string | number) {
return await this.requests.post<null>(`${this.itemTestRoute(itemId)}`, {});
}
}
5 changes: 3 additions & 2 deletions frontend/pages/group/webhooks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
</v-card-text>
</BasePageTitle>

<BannerExperimental />

<BaseButton create @click="actions.createOne()" />
<v-expansion-panels class="mt-2">
<v-expansion-panel v-for="(webhook, index) in webhooks" :key="index" class="my-2 left-border rounded">
Expand All @@ -36,6 +34,7 @@
:webhook="webhook"
@save="actions.updateOne($event)"
@delete="actions.deleteOne($event)"
@test="actions.testOne($event).then(() => alert.success($tc('events.test-message-sent')))"
/>
</v-expansion-panel-content>
</v-expansion-panel>
Expand All @@ -47,6 +46,7 @@
import { defineComponent } from "@nuxtjs/composition-api";
import { useGroupWebhooks, timeUTC } from "~/composables/use-group-webhooks";
import GroupWebhookEditor from "~/components/Domain/Group/GroupWebhookEditor.vue";
import { alert } from "~/composables/use-toast";
export default defineComponent({
components: { GroupWebhookEditor },
Expand All @@ -55,6 +55,7 @@ export default defineComponent({
const { actions, webhooks } = useGroupWebhooks();
return {
alert,
webhooks,
actions,
timeUTC
Expand Down
9 changes: 7 additions & 2 deletions mealie/routes/groups/controller_webhooks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from functools import cached_property

from fastapi import APIRouter, Depends
from fastapi import APIRouter, BackgroundTasks, Depends
from pydantic import UUID4

from mealie.routes._base.base_controllers import BaseUserController
Expand All @@ -10,7 +10,7 @@
from mealie.schema import mapper
from mealie.schema.group.webhook import CreateWebhook, ReadWebhook, SaveWebhook, WebhookPagination
from mealie.schema.response.pagination import PaginationQuery
from mealie.services.scheduler.tasks.post_webhooks import post_group_webhooks
from mealie.services.scheduler.tasks.post_webhooks import post_group_webhooks, post_single_webhook

router = APIRouter(prefix="/groups/webhooks", tags=["Groups: Webhooks"])

Expand Down Expand Up @@ -52,6 +52,11 @@ def rerun_webhooks(self):
def get_one(self, item_id: UUID4):
return self.mixins.get_one(item_id)

@router.post("/{item_id}/test")
def test_one(self, item_id: UUID4, bg_tasks: BackgroundTasks):
webhook = self.mixins.get_one(item_id)
bg_tasks.add_task(post_single_webhook, webhook, "Test Webhook")

@router.put("/{item_id}", response_model=ReadWebhook)
def update_one(self, item_id: UUID4, data: CreateWebhook):
return self.mixins.update_one(data, item_id)
Expand Down
4 changes: 2 additions & 2 deletions mealie/services/event_bus_service/event_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import Enum, auto
from typing import Any

from pydantic import UUID4, field_validator
from pydantic import UUID4, SerializeAsAny, field_validator

from ...schema._mealie.mealie_model import MealieModel

Expand Down Expand Up @@ -179,7 +179,7 @@ class Event(MealieModel):
message: EventBusMessage
event_type: EventTypes
integration_id: str
document_data: EventDocumentDataBase
document_data: SerializeAsAny[EventDocumentDataBase]

# set at instantiation
event_id: UUID4 | None = None
Expand Down
25 changes: 25 additions & 0 deletions mealie/services/scheduler/tasks/post_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

from mealie.db.db_setup import session_context
from mealie.repos.all_repositories import get_repositories
from mealie.schema.group.webhook import ReadWebhook
from mealie.schema.response.pagination import PaginationQuery
from mealie.services.event_bus_service.event_bus_listeners import WebhookEventListener
from mealie.services.event_bus_service.event_bus_service import EventBusService
from mealie.services.event_bus_service.event_types import (
INTERNAL_INTEGRATION_ID,
Event,
EventBusMessage,
EventDocumentType,
EventOperation,
EventTypes,
Expand Down Expand Up @@ -61,3 +65,24 @@ def post_group_webhooks(start_dt: datetime | None = None, group_id: UUID4 | None
event_type=event_type,
document_data=event_document_data,
)


def post_single_webhook(webhook: ReadWebhook, message: str = "") -> None:
dt = datetime.min.replace(tzinfo=timezone.utc)
event_type = EventTypes.webhook_task

event_document_data = EventWebhookData(
document_type=EventDocumentType.mealplan,
operation=EventOperation.info,
webhook_start_dt=dt,
webhook_end_dt=dt,
)
event = Event(
message=EventBusMessage.from_type(event_type, body=message),
event_type=event_type,
integration_id=INTERNAL_INTEGRATION_ID,
document_data=event_document_data,
)

listener = WebhookEventListener(webhook.group_id)
listener.publish_to_subscribers(event, [webhook])

0 comments on commit 6e6ae80

Please sign in to comment.