Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
natperpepaj committed Apr 1, 2024
1 parent a0033d2 commit 941b9de
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/resources/objects/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export interface ListObjectOptions extends PaginationOptions {
}

export interface ListObjectSubscriptionsOptions extends PaginationOptions {
recipients?: Map<string, Recipient>;
recipients?: {[key: string]: Recipient};
}

export interface GetObjectSubscriptionsOptions extends PaginationOptions {
objects?: Map<string, ObjectRef>;
objects?: {[key: string]: ObjectRef};
}

export interface ObjectSubscription<T = CommonMetadata> {
Expand Down
4 changes: 2 additions & 2 deletions src/resources/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
WorkflowPreferenceSetting,
} from "../preferences/interfaces";
import { ListMessagesOptions, Message } from "../messages/interfaces";
import { ListSchedulesProps, Schedule } from "../workflows/interfaces";
import { ListUserSchedulesProps, Schedule } from "../workflows/interfaces";
import {
DEFAULT_PREFERENCE_SET_ID,
buildUpdateParam,
Expand Down Expand Up @@ -354,7 +354,7 @@ export class Users {

async getSchedules(
userId: string,
filteringOptions: ListSchedulesProps = {},
filteringOptions: ListUserSchedulesProps = {},
): Promise<PaginatedEntriesResponse<Schedule>> {
if (!userId) {
throw new Error(`Incomplete arguments. You must provide a 'userId'`);
Expand Down
2 changes: 1 addition & 1 deletion src/resources/users/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ export interface ListUserOptions extends PaginationOptions {
}

export interface ListSubscriptionsOptions extends PaginationOptions {
objects?: Map<string, ObjectRef>;
objects?: {[key: string]: ObjectRef};
}
7 changes: 6 additions & 1 deletion src/resources/workflows/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ export interface UpdateSchedulesProps
schedule_ids: string[];
}

export interface ListUserSchedulesProps extends PaginationOptions {
recipients?: {[key: string]: Recipient};
tenant?: string;
}

export interface ListSchedulesProps extends PaginationOptions {
recipients?: Map<string, Recipient>;
recipients?: Recipient[];
tenant?: string;
}

Expand Down
39 changes: 33 additions & 6 deletions test/resources/workflows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterAll, afterEach, beforeAll, describe, test, expect } from "vitest";
import { setupServer } from "msw/node";
import { HttpResponse, http } from "msw";
import { Knock } from "../../src/knock";
import { ListSchedulesProps } from "../../src/resources/workflows/interfaces";
import { ListSchedulesProps, ListUserSchedulesProps } from "../../src/resources/workflows/interfaces";

const restHandlers = [
http.get("http://api.knock.test/v1/schedules", () => {
Expand All @@ -17,6 +17,18 @@ const restHandlers = [
},
});
}),
http.get("http://api.knock.test/v1/users/:userID/schedules", () => {
return HttpResponse.json({
entries: [],
page_info: {
__typename: "PageInfo",
after: null,
before: null,
page_size: 50,
total_count: 0,
},
});
}),
];

const server = setupServer(...restHandlers);
Expand All @@ -34,6 +46,22 @@ afterEach(() => server.resetHandlers());
describe("schedules", () => {
test("it can list schedules", async () => {
const params: ListSchedulesProps = {
recipients: ["1", "2", "3"],
};
const { url } = await knock.get("/v1/schedules", {
...params,
workflow: "test-workflow",
});

// Formats recipients list as a URL-encoded array
// 'http://api.knock.test/v1/schedules?recipients[]=1&recipients[]=2&recipients[]=3&workflow=test-workflow'
expect(url).toBe(
"http://api.knock.test/v1/schedules?recipients%5B%5D=1&recipients%5B%5D=2&recipients%5B%5D=3&workflow=test-workflow",
);
});

test("it can list schedules for a given user", async () => {
const params: ListUserSchedulesProps = {
recipients: {
"0": "user_id",
"1": {
Expand All @@ -42,15 +70,14 @@ describe("schedules", () => {
}
},
};
const { url } = await knock.get("/v1/schedules", {
const { url } = await knock.get("/v1/users/user_id/schedules", {
...params,
workflow: "test-workflow",
});

// Formats recipients as a URL-encoded map
// http://api.knock.test/v1/schedules?recipients[0]=user_id&recipients[1][collection]=object_collection&recipients[1][id]=object_id&workflow=test-workflow
// Formats recipients list as a URL-encoded array
// 'http://api.knock.test/v1/users/user_id/schedules?recipients[0]=user_id&recipients[1][collection]=object_collection&recipients[1][id]=object_id'
expect(url).toBe(
"http://api.knock.test/v1/schedules?recipients%5B0%5D=user_id&recipients%5B1%5D%5Bcollection%5D=object_collection&recipients%5B1%5D%5Bid%5D=object_id&workflow=test-workflow",
"http://api.knock.test/v1/users/user_id/schedules?recipients%5B0%5D=user_id&recipients%5B1%5D%5Bcollection%5D=object_collection&recipients%5B1%5D%5Bid%5D=object_id",
);
});
});

0 comments on commit 941b9de

Please sign in to comment.