Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add triggers ids support ENG-2903 #1038

Closed
wants to merge 12 commits into from
22 changes: 20 additions & 2 deletions js/src/sdk/models/triggers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Triggers } from "./triggers";
import { ConnectedAccounts } from "./connectedAccounts";
import { Entity } from "./Entity";
import { Actions } from "./actions";
import { SDK_ERROR_CODES } from "../utils/errors/src/constants";

describe("Apps class tests", () => {
let backendClient;
Expand All @@ -30,12 +31,13 @@ describe("Apps class tests", () => {
});

it("should retrieve a list of triggers for a specific app", async () => {
const appName = "github";
const triggerList = await triggers.list({
appNames: "github",
appNames: [appName],
});
// this is breaking for now
expect(triggerList.length).toBeGreaterThan(0);
expect(triggerList[0].appName).toBe("github");
expect(triggerList[0].appName).toBe(appName);
});
});

Expand Down Expand Up @@ -94,6 +96,22 @@ describe("Apps class tests subscribe", () => {
expect(trigger.status).toBe("success");
});

it("should retrieve a list of triggers for a specific triggerId", async () => {
const triggerList = await triggers.list({
triggerIds: ["GMAIL_NEW_GMAIL_MESSAGE"],
});
expect(triggerList.length).toBeGreaterThan(0);
expect(triggerList[0].name).toBe("GMAIL_NEW_GMAIL_MESSAGE");
});

it("try to subscribe to a trigger that doesn't exist", async () => {
await expect(
triggers.subscribe(async () => {}, {
triggerId: "GMAIL_NEW_GMAIL_MESSAGE_2",
})
).rejects.toThrow("No triggers found for the given filters");
});

// it("should subscribe to a trigger and receive a trigger", async () => {
// function waitForTriggerReceived() {
// return new Promise((resolve) => {
Expand Down
54 changes: 49 additions & 5 deletions js/src/sdk/models/triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@ import logger from "../../utils/logger";
import { BackendClient } from "./backendClient";

import apiClient from "../client/client";

import z from "zod";
import { CEG } from "../utils/error";
import { ListTriggersData } from "../client";
import { TELEMETRY_LOGGER } from "../utils/telemetry";
import { TELEMETRY_EVENTS } from "../utils/telemetry/events";
import { SDK_ERROR_CODES } from "../utils/errors/src/constants";

type RequiredQuery = ListTriggersData["query"];

const ZTriggerQuery = z.object({
triggerIds: z.array(z.string()).optional(),
appNames: z.array(z.string()).optional(),
connectedAccountsIds: z.array(z.string()).optional(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in connectedAccountsIds. It should be connectedAccountIds to match the expected query parameter in the API call.

integrationIds: z.array(z.string()).optional(),
showEnabledOnly: z.boolean().optional(),
});

type TTriggerQuery = z.infer<typeof ZTriggerQuery>;

export class Triggers {
trigger_to_client_event = "trigger_to_client";

Expand All @@ -29,18 +40,32 @@ export class Triggers {
* @returns {CancelablePromise<ListTriggersResponse>} A promise that resolves to the list of all triggers.
* @throws {ApiError} If the request fails.
*/
async list(data: RequiredQuery = {}) {
async list(data: TTriggerQuery = {}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc comment for the list method should be updated to reflect the new parameters and validation requirements. Consider adding:

/**
 * Retrieves a list of triggers based on provided filters.
 * @param {TTriggerQuery} data Query parameters for filtering triggers
 * @param {string[]} [data.triggerIds] - Filter by specific trigger IDs
 * @param {string[]} [data.appNames] - Filter by app names
 * @param {string[]} [data.connectedAccountsIds] - Filter by connected account IDs
 * @param {string[]} [data.integrationIds] - Filter by integration IDs
 * @param {boolean} [data.showEnabledOnly] - Show only enabled triggers
 * @throws {CustomError} When no valid filter parameters are provided
 * @returns {Promise<Array>} List of matching triggers
 */

TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
method: "list",
file: this.fileName,
params: { data },
});

try {
const {
appNames,
triggerIds,
connectedAccountsIds,
integrationIds,
showEnabledOnly,
} = ZTriggerQuery.parse(data);

const { data: response } = await apiClient.triggers.listTriggers({
query: {
appNames: data?.appNames,
appNames: appNames?.join(","),
triggerIds: triggerIds?.join(","),
connectedAccountIds: connectedAccountsIds?.join(","),
integrationIds: integrationIds?.join(","),
showEnabledOnly: showEnabledOnly,
},
});

return response || [];
} catch (error) {
throw CEG.handleAllError(error);
Expand Down Expand Up @@ -162,9 +187,28 @@ export class Triggers {
params: { filters },
});
if (!fn) throw new Error("Function is required for trigger subscription");
//@ts-ignore
const clientId = await this.backendClient.getClientId();
//@ts-ignore

const availableTriggers = await this.list({
appNames: filters.appName ? [filters.appName] : undefined,
triggerIds: filters.triggerId ? [filters.triggerId] : undefined,
connectedAccountsIds: filters.connectionId
? [filters.connectionId]
: undefined,
integrationIds: filters.integrationId
? [filters.integrationId]
: undefined,
});

if (availableTriggers.length === 0) {
throw CEG.getCustomError(SDK_ERROR_CODES.COMMON.INVALID_PARAMS_PASSED, {
message: "No triggers match the specified filters",
description: "The provided filters did not return any triggers.",
possibleFix:
"Verify the filters and ensure a trigger is set up with the specified criteria.",
});
}

await PusherUtils.getPusherClient(
this.backendClient.baseUrl,
this.backendClient.apiKey
Expand Down
Loading