diff --git a/packages/webhooks/source/listener.test.ts b/packages/webhooks/source/listener.test.ts index 7aa5c7e46..abb3fba3e 100644 --- a/packages/webhooks/source/listener.test.ts +++ b/packages/webhooks/source/listener.test.ts @@ -146,6 +146,30 @@ describe<{ expectFinishedEventData(spyOnDispatchArguments[1]); }); + it("should broadcast satisfied webhook condition with nested key", async ({ database, listener }) => { + const spyOnPost = stub(Utils.http, "post").resolvedValue({ + statusCode: 200, + }); + const spyOnDispatch = stub(eventDispatcher, "dispatch"); + + webhook.conditions = [ + { + condition: "eq", + key: "some.nested.prop", + value: 1, + }, + ]; + database.create(webhook); + + await listener.handle({ data: { some: { nested: { prop: 1 } } }, name: "event" }); + + spyOnPost.calledOnce(); + spyOnDispatch.calledOnce(); + const spyOnDispatchArguments = spyOnDispatch.getCallArgs(0); + assert.equal(spyOnDispatchArguments[0], WebhookEvent.Broadcasted); + expectFinishedEventData(spyOnDispatchArguments[1]); + }); + it("should not broadcast if webhook condition is not satisfied", async ({ database, listener }) => { const spyOnPost = stub(Utils.http, "post"); @@ -163,6 +187,23 @@ describe<{ spyOnPost.neverCalled(); }); + it("should not broadcast if webhook condition with nested key is not satisfied", async ({ database, listener }) => { + const spyOnPost = stub(Utils.http, "post"); + + webhook.conditions = [ + { + condition: "eq", + key: "some.nested.prop", + value: 1, + }, + ]; + database.create(webhook); + + await listener.handle({ data: { some: { nested: { prop: 2 } } }, name: "event" }); + + spyOnPost.neverCalled(); + }); + it("should not broadcast if webhook condition throws error", async ({ database, listener }) => { const spyOnEq = stub(conditions, "eq").callsFake(() => { throw new Error("dummy error"); diff --git a/packages/webhooks/source/listener.ts b/packages/webhooks/source/listener.ts index ea4e24422..1b0bc1816 100644 --- a/packages/webhooks/source/listener.ts +++ b/packages/webhooks/source/listener.ts @@ -1,6 +1,7 @@ import { inject, injectable } from "@mainsail/container"; import { Contracts, Identifiers } from "@mainsail/contracts"; import { Utils } from "@mainsail/kernel"; +import { get } from "@mainsail/utils"; import { performance } from "perf_hooks"; import { conditions } from "./conditions.js"; @@ -102,7 +103,7 @@ export class Listener { try { const satisfies = conditions[condition.condition]; - if (satisfies(payload[condition.key], condition.value)) { + if (satisfies(get(payload, condition.key), condition.value)) { return true; } } catch {