Skip to content

Commit 7f1122a

Browse files
authored
Merge pull request #279 from zendesk/victor.piolin/brands
feat: implement fetch all brands api
2 parents 130d7ba + cca857f commit 7f1122a

5 files changed

Lines changed: 127 additions & 2 deletions

File tree

__tests__/services/zendesk-api-service.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,4 +1167,51 @@ describe("ZendeskService", () => {
11671167
expect(result).toEqual([viewSample]);
11681168
});
11691169
});
1170+
1171+
describe("getBrands", () => {
1172+
const brandSample = {
1173+
active: true,
1174+
brand_url: "https://example.zendesk.com/api/v2/brands/123.json",
1175+
created_at: "2023-01-01T00:00:00Z",
1176+
default: false,
1177+
has_help_center: true,
1178+
help_center_state: "enabled"
1179+
};
1180+
1181+
it("should fetch brands with the correct data", async () => {
1182+
requestMock.mockResolvedValueOnce({ brands: [brandSample] });
1183+
1184+
const result = await service.getBrands();
1185+
1186+
expect(requestMock).toHaveBeenCalledWith({
1187+
url: `/api/v2/brands`
1188+
});
1189+
expect(result).toEqual([brandSample]);
1190+
});
1191+
1192+
it("should continue calling the API until next_page disappears", async () => {
1193+
requestMock
1194+
.mockResolvedValueOnce({ brands: [brandSample], next_page: "next_page" })
1195+
.mockResolvedValueOnce({ brands: [] });
1196+
1197+
const result = await service.getBrands();
1198+
1199+
expect(requestMock).toHaveBeenCalledTimes(2);
1200+
expect(requestMock).toHaveBeenNthCalledWith(1, {
1201+
url: `/api/v2/brands`
1202+
});
1203+
expect(requestMock).toHaveBeenNthCalledWith(2, {
1204+
url: "next_page"
1205+
});
1206+
expect(result).toEqual([brandSample]);
1207+
});
1208+
1209+
it("should only call the API one time with fetchAllBrands set to false", async () => {
1210+
requestMock.mockResolvedValueOnce({ brands: [brandSample], next_page: "next_page" });
1211+
1212+
await service.getBrands(false);
1213+
1214+
expect(requestMock).toHaveBeenCalledTimes(1);
1215+
});
1216+
});
11701217
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zendesk/zaf-toolbox",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs",
55
"main": "lib/src/index.js",
66
"types": "lib/src/index.d.ts",

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export * from "@models/whats-app-template";
99
export * from "@models/zendesk-user";
1010
export * from "@models/custom-objects";
1111
export * from "@models/zendesk-api";
12+
export * from "@models/zendesk-brands";
1213
export * from "@models/zendesk-view";

src/models/zendesk-brands.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { IZendeskResponse } from "./zendesk-api";
2+
3+
export type HelpCenterState = "enabled" | "disabled" | "restricted";
4+
5+
export interface IBrandLogo {
6+
content_type?: string;
7+
content_url?: string;
8+
deleted?: boolean;
9+
file_name?: string;
10+
height?: string;
11+
id?: number;
12+
inline?: boolean;
13+
mapped_content_url?: string;
14+
size?: number;
15+
thumbnails?: {
16+
content_type: string;
17+
content_url: string;
18+
deleted: boolean;
19+
file_name: string;
20+
height: string;
21+
id: number;
22+
inline: boolean;
23+
mapped_content_url: string;
24+
size: number;
25+
url: string;
26+
width: string;
27+
}[];
28+
url?: string;
29+
width?: string;
30+
}
31+
32+
export interface IZendeskBrand {
33+
active: boolean;
34+
brand_url: string;
35+
created_at: string;
36+
default: boolean;
37+
has_help_center: boolean;
38+
help_center_state: HelpCenterState;
39+
host_mapping: string;
40+
id: number;
41+
is_deleted: boolean;
42+
logo: IBrandLogo | null;
43+
name: string;
44+
signature_template: string;
45+
subdomain: string;
46+
ticket_form_ids: number[];
47+
updated_at: string;
48+
url: string;
49+
}
50+
51+
export interface IBrandsResponse extends IZendeskResponse {
52+
brands: IZendeskBrand[];
53+
}
54+
55+
export interface IBrandResponse extends IZendeskResponse {
56+
brand: IZendeskBrand;
57+
}

src/services/zendesk-api-service.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import {
3232
IBulkJobResponse,
3333
ITicketsResults,
3434
IZendeskView,
35-
IViewsResponse
35+
IViewsResponse,
36+
IZendeskBrand,
37+
IBrandsResponse
3638
} from "@models/index";
3739
import { buildUrlParams } from "@utils/build-url-params";
3840
import {
@@ -723,4 +725,22 @@ export class ZendeskApiService {
723725
(response) => response.views
724726
);
725727
}
728+
729+
/**
730+
* BRANDS SECTION
731+
*/
732+
733+
/**
734+
* Fetch all brands
735+
*
736+
* @param fetchAllBrands Whether to fetch all pages or just the first. Defaults to true
737+
* @returns Promise resolving to array of brands
738+
*/
739+
public async getBrands(fetchAllBrands = true): Promise<IZendeskBrand[]> {
740+
return this.fetchAllPaginatedResults<IBrandsResponse, IZendeskBrand>(
741+
"/api/v2/brands",
742+
fetchAllBrands,
743+
(response) => response.brands
744+
);
745+
}
726746
}

0 commit comments

Comments
 (0)