Skip to content

Commit

Permalink
add published event trigger (#49)
Browse files Browse the repository at this point in the history
This trigger fires when an admin publishes an event on their Cobot
space.
  • Loading branch information
langalex authored Feb 15, 2024
1 parent ce4d7e7 commit 5a386a7
Show file tree
Hide file tree
Showing 23 changed files with 417 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cobot-zapier",
"version": "2.0.1",
"version": "2.1.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const AUTHORIZE_URL = `${BASE_URL}/oauth/authorize`;
const ACCESS_TOKEN_URL = `${BASE_URL}/oauth/access_token`;
const TEST_AUTH_URL = `${BASE_URL}/api/user`;
const scopes =
"read read_admins read_bookings read_external_bookings read_memberships read_resources read_spaces read_user write_activities write_subscriptions";
"read read_admins read_bookings read_events read_external_bookings read_memberships read_resources read_spaces read_user write_activities write_subscriptions";

const getAccessToken = async (
z: ZObject,
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import triggerMembershipConfirmed from "./triggers/triggerMembershipConfirmed";
import triggerMembershipPlanChanged from "./triggers/triggerMembershipPlanChanged";
import triggerExternalBooking from "./triggers/triggerExternalBookingCreated";
import getSubdomains from "./triggers/dropdowns/getSubdomains";
import triggerEventPublished from "./triggers/triggerEventPublished";

const { version } = require("../package.json");

Expand All @@ -30,6 +31,7 @@ export default {
[triggerBookingWillBegin.key]: triggerBookingWillBegin,
[triggerMembershipConfirmed.key]: triggerMembershipConfirmed,
[triggerMembershipPlanChanged.key]: triggerMembershipPlanChanged,
[triggerEventPublished.key]: triggerEventPublished,
[triggerExternalBooking.key]: triggerExternalBooking,
// Lists for dropdowns
[getSubdomains.key]: getSubdomains,
Expand Down
7 changes: 4 additions & 3 deletions src/test/triggers/triggerBookingCreated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import * as nock from "nock";
import App from "../../index";
import { prepareMocksForWebhookSubscribeTest } from "../utils/prepareMocksForWebhookSubscribeTest";
import triggerBookingCreated from "../../triggers/triggerBookingCreated";
import { HookTrigger } from "../../types/trigger";

const appTester = createAppTester(App);
nock.disableNetConnect();

afterEach(() => nock.cleanAll());

describe("triggerBookingCreated", () => {
it("creates new webhook upon through CM API upon subscribe", async () => {
it("creates new webhook through CM API upon subscribe", async () => {
const bundle = prepareMocksForWebhookSubscribeTest(
triggerBookingCreated.key,
);
const subscribe =
App.triggers[triggerBookingCreated.key].operation.performSubscribe;
const subscribe = (App.triggers[triggerBookingCreated.key] as HookTrigger)
.operation.performSubscribe;

const result = await appTester(subscribe as any, bundle as any);

Expand Down
7 changes: 4 additions & 3 deletions src/test/triggers/triggerBookingWillBegin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ import * as nock from "nock";
import App from "../../index";
import { prepareMocksForWebhookSubscribeTest } from "../utils/prepareMocksForWebhookSubscribeTest";
import triggerBookingWillBegin from "../../triggers/triggerBookingWillBegin";
import { HookTrigger } from "../../types/trigger";

const appTester = createAppTester(App);
nock.disableNetConnect();

afterEach(() => nock.cleanAll());

describe("triggerBookingWillBegin", () => {
it("creates new webhook upon through CM API upon subscribe", async () => {
it("creates new webhook through CM API upon subscribe", async () => {
const bundle = prepareMocksForWebhookSubscribeTest(
triggerBookingWillBegin.key,
);
const subscribe =
App.triggers[triggerBookingWillBegin.key].operation.performSubscribe;
const subscribe = (App.triggers[triggerBookingWillBegin.key] as HookTrigger)
.operation.performSubscribe;

const result = await appTester(subscribe as any, bundle as any);

Expand Down
94 changes: 94 additions & 0 deletions src/test/triggers/triggerEventPublished.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createAppTester } from "zapier-platform-core";
import * as nock from "nock";
import App from "../../index";
import {
prepareBundle,
prepareMocksForWebhookSubscribeTest,
} from "../utils/prepareMocksForWebhookSubscribeTest";
import triggerEventPublished from "../../triggers/triggerEventPublished";
import { HookTrigger } from "../../types/trigger";
import { UserApiResponse, EventApiResponse } from "../../types/api-responses";

const appTester = createAppTester(App);
nock.disableNetConnect();
const trigger = App.triggers[triggerEventPublished.key] as HookTrigger;

afterEach(() => nock.cleanAll());

describe("triggerEventPublished", () => {
it("creates new webhook through CM API upon subscribe", async () => {
const bundle = prepareMocksForWebhookSubscribeTest(
triggerEventPublished.key,
);
const subscribe = trigger.operation.performSubscribe;

const result = await appTester(subscribe as any, bundle as any);

expect(result).toMatchInlineSnapshot(`
{
"url": "https://trial.cobot.me/api/event/callback",
}
`);
});

it("lists recent events", async () => {
const bundle = prepareBundle();
const userResponse: UserApiResponse = {
included: [{ id: "space-1", attributes: { subdomain: "trial" } }],
};
const imageItem = {
url: "https://www.example.com/image.png",
width: 100,
height: 100,
};
const eventResponse: EventApiResponse = {
id: "1",
attributes: {
title: "event 1",
from: "2024-12-20T06:22:29+01:00",
to: "2024-12-20T08:22:29+01:00",
description: "event 1 description",
tags: ["free"],
videoUrl: "https://www.youtube.com/watch?v=123",
capacity: 12,
publicUrl: "https://trial.cobot.me/events/1",
audience: "membersOnly",
color: "#ff0000",
image: {
default: imageItem,
small: imageItem,
icon: imageItem,
medium: imageItem,
large: imageItem,
},
},
};

const scope = nock("https://api.cobot.me");
scope.get("/user?include=adminOf").reply(200, userResponse);
scope
.get(/\/spaces\/space-1\/events/)
.reply(200, { data: [eventResponse] });

const listRecentEvents = trigger.operation.performList;

const results = await appTester(listRecentEvents as any, bundle as any);

expect(results).toStrictEqual([
{
audience: "membersOnly",
capacity: 12,
color: "#ff0000",
description: "event 1 description",
from: "2024-12-20T05:22:29.000Z",
id: "1",
image_url: "https://www.example.com/image.png",
public_url: "https://trial.cobot.me/events/1",
tags: ["free"],
title: "event 1",
to: "2024-12-20T07:22:29.000Z",
video_url: "https://www.youtube.com/watch?v=123",
},
]);
});
});
15 changes: 7 additions & 8 deletions src/test/triggers/triggerExternalBooking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import {
ResourceApiResponse,
UserApiResponse,
} from "../../types/api-responses";
import { HookTrigger } from "../../types/trigger";

const appTester = createAppTester(App);
nock.disableNetConnect();
const trigger = App.triggers[triggerExternalBooking.key] as HookTrigger;

afterEach(() => nock.cleanAll());

describe("triggerExternalBooking", () => {
it("creates new webhook upon through CM API upon subscribe", async () => {
it("creates new webhook through CM API upon subscribe", async () => {
const bundle = prepareMocksForWebhookSubscribeTest("created_booking");
const subscribe =
App.triggers[triggerExternalBooking.key].operation.performSubscribe;
const subscribe = trigger.operation.performSubscribe;

const result = await appTester(subscribe as any, bundle as any);

Expand Down Expand Up @@ -89,8 +90,7 @@ describe("triggerExternalBooking", () => {
.get(/\/spaces\/space-1\/resources/)
.reply(200, { data: [resourceResponse] });

const listRecentExternalBookings =
App.triggers[triggerExternalBooking.key].operation.performList;
const listRecentExternalBookings = trigger.operation.performList;

const results = await appTester(
listRecentExternalBookings as any,
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("triggerExternalBooking", () => {
]);
});

it("returns no booking if no matcyhing resource is found", async () => {
it("returns no booking if no matching resource is found", async () => {
const bundle = prepareBundle();
const userResponse: UserApiResponse = {
included: [{ id: "space-1", attributes: { subdomain: "trial" } }],
Expand Down Expand Up @@ -177,8 +177,7 @@ describe("triggerExternalBooking", () => {
.get(/\/spaces\/space-1\/resources/)
.reply(200, { data: [resourceResponse] });

const listRecentExternalBookings =
App.triggers[triggerExternalBooking.key].operation.performList;
const listRecentExternalBookings = trigger.operation.performList;

const results = await appTester(
listRecentExternalBookings as any,
Expand Down
8 changes: 5 additions & 3 deletions src/test/triggers/triggerMembershipConfirmed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import * as nock from "nock";
import App from "../../index";
import { prepareMocksForWebhookSubscribeTest } from "../utils/prepareMocksForWebhookSubscribeTest";
import triggerMembershipConfirmed from "../../triggers/triggerMembershipConfirmed";
import { HookTrigger } from "../../types/trigger";

const appTester = createAppTester(App);
nock.disableNetConnect();

afterEach(() => nock.cleanAll());

describe("triggerMembershipConfirmed", () => {
it("creates new webhook upon through CM API upon subscribe", async () => {
it("creates new webhook through CM API upon subscribe", async () => {
const bundle = prepareMocksForWebhookSubscribeTest(
triggerMembershipConfirmed.key,
);
const subscribe =
App.triggers[triggerMembershipConfirmed.key].operation.performSubscribe;
const subscribe = (
App.triggers[triggerMembershipConfirmed.key] as HookTrigger
).operation.performSubscribe;

const result = await appTester(subscribe as any, bundle as any);

Expand Down
8 changes: 5 additions & 3 deletions src/test/triggers/triggerMembershipPlanChanged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import * as nock from "nock";
import App from "../../index";
import { prepareMocksForWebhookSubscribeTest } from "../utils/prepareMocksForWebhookSubscribeTest";
import triggerMembershipPlanChanged from "../../triggers/triggerMembershipPlanChanged";
import { HookTrigger } from "../../types/trigger";

const appTester = createAppTester(App);
nock.disableNetConnect();

afterEach(() => nock.cleanAll());

describe("triggerMembershipPlanChanged", () => {
it("creates new webhook upon through CM API upon subscribe", async () => {
it("creates new webhook through CM API upon subscribe", async () => {
const bundle = prepareMocksForWebhookSubscribeTest(
triggerMembershipPlanChanged.key,
);
const subscribe =
App.triggers[triggerMembershipPlanChanged.key].operation.performSubscribe;
const subscribe = (
App.triggers[triggerMembershipPlanChanged.key] as HookTrigger
).operation.performSubscribe;

const result = await appTester(subscribe as any, bundle as any);

Expand Down
6 changes: 4 additions & 2 deletions src/triggers/dropdowns/getSubdomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ZObject } from "zapier-platform-core";
import { KontentBundle } from "../../types/kontentBundle";
import { OutputField } from "../../fields/output/outputField";
import { OutputFromOutputFields } from "../../fields/output/outputFromOutputFields";
import { PollingTrigger } from "../../types/trigger";

const execute = (z: ZObject, bundle: KontentBundle<{}>): Output => {
return bundle.authData.adminOf
Expand All @@ -26,7 +27,7 @@ const outputFields = [

type Output = ReadonlyArray<OutputFromOutputFields<typeof outputFields>>;

export default {
const trigger: PollingTrigger = {
key: "get_subdomains",
noun: "Subdomain choice",
display: {
Expand All @@ -43,4 +44,5 @@ export default {
},
outputFields,
},
} as const;
};
export default trigger;
6 changes: 4 additions & 2 deletions src/triggers/triggerBookingCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { bookingSample } from "../utils/samples";
import { BookingOutput } from "../types/outputs";
import { apiResponseToBookingOutput } from "../utils/api-to-output";
import { BookingApiResponse } from "../types/api-responses";
import { HookTrigger } from "../types/trigger";

const hookLabel = "Booking Created";
const event = "created_booking";
Expand Down Expand Up @@ -49,7 +50,7 @@ async function parsePayload(
}
}

export default {
const trigger: HookTrigger = {
key: event,
noun: hookLabel,
display: {
Expand All @@ -74,4 +75,5 @@ export default {
},
sample: bookingSample,
},
} as const;
};
export default trigger;
6 changes: 4 additions & 2 deletions src/triggers/triggerBookingWillBegin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { apiResponseToBookingOutput } from "../utils/api-to-output";
import { BookingOutput } from "../types/outputs";
import { bookingSample } from "../utils/samples";
import { BookingApiResponse } from "../types/api-responses";
import { HookTrigger, Trigger } from "../types/trigger";

const hookLabel = "Booking Will Begin";
const event = "booking_will_begin";
Expand Down Expand Up @@ -49,7 +50,7 @@ async function parsePayload(
}
}

export default {
const trigger: HookTrigger = {
key: event,
noun: hookLabel,
display: {
Expand All @@ -74,4 +75,5 @@ export default {
},
sample: bookingSample,
},
} as const;
};
export default trigger;
Loading

0 comments on commit 5a386a7

Please sign in to comment.