Skip to content

Commit 8c4c753

Browse files
authored
Merge pull request #136 from zendesk/victor.piolin/zis-jobspec
feat: Add possibility to deal with Zis Job spec inside the zendesk ap…
2 parents b730bc5 + 3e8d969 commit 8c4c753

6 files changed

Lines changed: 205 additions & 10 deletions

File tree

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

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,107 @@ describe("ZendeskService", () => {
594594
expect(result).toEqual(zisIntegration);
595595
});
596596
});
597+
598+
describe("jobSpecs", () => {
599+
const jobSpec = {
600+
description: "Test Job",
601+
event_source: "source",
602+
event_type: "type",
603+
flow_name: "flow",
604+
installed: true,
605+
integration: "integration",
606+
name: "jobName",
607+
uuid: "uuid"
608+
};
609+
610+
it("should fetchs jobs and call Zendesk API multiple times if has more is true", async () => {
611+
requestMock
612+
.mockResolvedValueOnce({
613+
job_specs: [jobSpec],
614+
meta: {
615+
has_more: true,
616+
after: "1"
617+
}
618+
})
619+
.mockResolvedValueOnce({
620+
job_specs: [jobSpec],
621+
meta: {
622+
has_more: false
623+
}
624+
});
625+
626+
const data = await service.fetchZisJobSpecs("integrationName");
627+
628+
expect(requestMock).toHaveBeenNthCalledWith(1, {
629+
url: "/api/services/zis/registry/integrationName/job_specs",
630+
type: "GET",
631+
data: {
632+
page: {
633+
size: "100"
634+
}
635+
}
636+
});
637+
expect(requestMock).toHaveBeenNthCalledWith(2, {
638+
url: "/api/services/zis/registry/integrationName/job_specs",
639+
type: "GET",
640+
data: {
641+
page: {
642+
after: "1",
643+
size: "100"
644+
}
645+
}
646+
});
647+
expect(requestMock).toHaveBeenCalledTimes(2);
648+
expect(data).toHaveLength(2);
649+
});
650+
651+
it("should fetchs jobs with correct data", async () => {
652+
requestMock.mockResolvedValueOnce({
653+
job_specs: [jobSpec],
654+
meta: {
655+
has_more: false
656+
}
657+
});
658+
659+
const data = await service.fetchZisJobSpecs("integrationName", {
660+
page: {
661+
size: "5"
662+
}
663+
});
664+
665+
expect(requestMock).toHaveBeenNthCalledWith(1, {
666+
url: "/api/services/zis/registry/integrationName/job_specs",
667+
type: "GET",
668+
data: {
669+
page: {
670+
size: "5"
671+
}
672+
}
673+
});
674+
expect(requestMock).toHaveBeenCalledTimes(1);
675+
expect(data).toHaveLength(1);
676+
});
677+
678+
it("should create a job spec with correct data", async () => {
679+
await service.createZisJobSpec("job_name_test");
680+
681+
expect(requestMock).toHaveBeenNthCalledWith(1, {
682+
url: "/api/services/zis/registry/job_specs/install?job_spec_name=job_name_test",
683+
type: "POST",
684+
contentType: "application/json"
685+
});
686+
expect(requestMock).toHaveBeenCalledTimes(1);
687+
});
688+
689+
it("should delete a job spec with correct data", async () => {
690+
await service.deleteZisJobSpec("job_name_test");
691+
692+
expect(requestMock).toHaveBeenNthCalledWith(1, {
693+
url: "/api/services/zis/registry/job_specs/install?job_spec_name=job_name_test",
694+
type: "DELETE",
695+
contentType: "application/json"
696+
});
697+
expect(requestMock).toHaveBeenCalledTimes(1);
698+
});
699+
});
597700
});

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": "0.6.0",
3+
"version": "0.7.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/custom-objects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export enum ListCutomObjectRecordsSortingOptions {
110110
MINUS_UPDATED_AT = "-updated_at"
111111
}
112112

113-
export interface IListCustomObjectRecordsFilter {
113+
export interface IListFilter {
114114
filter?: {
115115
/**
116116
* Comma separated list of external ids of records
@@ -141,7 +141,7 @@ export interface IListCustomObjectRecordsFilter {
141141
sort?: ListCutomObjectRecordsSortingOptions;
142142
}
143143

144-
export interface ISearchCustomObjectRecordsFilter extends IListCustomObjectRecordsFilter {
144+
export interface ISearchCustomObjectRecordsFilter extends IListFilter {
145145
query: string;
146146
}
147147

src/models/zendesk-integration-services.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,22 @@ export interface IZisIntegration {
1414
export interface IZisIntegrationResponse {
1515
integrations: IZisIntegration[];
1616
}
17+
18+
export interface IZisJobspec {
19+
description: string;
20+
event_source: string;
21+
event_type: string;
22+
flow_name: string;
23+
installed: boolean;
24+
integration: string;
25+
name: string;
26+
uuid: string;
27+
}
28+
export interface IZisJobspecsResponse {
29+
job_specs: IZisJobspec[];
30+
meta: {
31+
after: string;
32+
before: string;
33+
has_more: boolean;
34+
};
35+
}

src/services/custom-object-service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
IGetCustomObjectRecordsResponse,
88
IListCustomObjectsResponse,
99
ICreateCustomObjectRecordBody,
10-
IListCustomObjectRecordsFilter,
10+
IListFilter,
1111
IListCustomObjectRecordsResponse,
1212
ICustomObjectRecord,
1313
ListCutomObjectRecordsSortingOptions,
@@ -124,7 +124,7 @@ export class CustomObjectService {
124124
*/
125125
public async listCustomObjectRecords<T extends ICustomObjectRecordField>(
126126
key: string,
127-
data?: IListCustomObjectRecordsFilter
127+
data?: IListFilter
128128
): Promise<ICustomObjectRecord<T>[]> {
129129
const { custom_object_records } = await this.client.request<any, IListCustomObjectRecordsResponse<T>>({
130130
url: `/api/v2/custom_objects/${key}/records`,
@@ -145,7 +145,7 @@ export class CustomObjectService {
145145
): Promise<ICustomObjectRecord<T>[]> {
146146
return this.fetchAllPaginatedRecords<T>(`/api/v2/custom_objects/${key}/records`, {
147147
sort: sortOptions?.sort
148-
} as IListCustomObjectRecordsFilter);
148+
} as IListFilter);
149149
}
150150

151151
/**
@@ -263,7 +263,7 @@ export class CustomObjectService {
263263
*/
264264
private async fetchAllPaginatedRecords<T extends ICustomObjectRecordField>(
265265
url: string,
266-
initialData: IListCustomObjectRecordsFilter
266+
initialData: IListFilter
267267
): Promise<ICustomObjectRecord<T>[]> {
268268
let hasMore = true;
269269
let data = initialData;
@@ -290,7 +290,7 @@ export class CustomObjectService {
290290
sort: initialData.sort
291291
}
292292
: undefined)
293-
} as IListCustomObjectRecordsFilter;
293+
} as IListFilter;
294294
}
295295
} while (hasMore);
296296

src/services/zendesk-api-service.ts

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ import {
2525
IZendeskResponse,
2626
Line,
2727
IMessage,
28-
IMessagesResults
28+
IMessagesResults,
29+
IListFilter
2930
} from "@models/index";
30-
import { IZisIntegration, IZisIntegrationResponse } from "@models/zendesk-integration-services";
31+
import {
32+
IZisIntegration,
33+
IZisIntegrationResponse,
34+
IZisJobspec,
35+
IZisJobspecsResponse
36+
} from "@models/zendesk-integration-services";
3137
import { convertContentMessageToHtml } from "@utils/convert-content-message-to-html";
3238
import { getFromClient } from "@utils/get-from-client";
3339
import { Client } from "@zendesk/sell-zaf-app-toolbox";
@@ -325,4 +331,71 @@ export class ZendeskApiService {
325331
})
326332
});
327333
}
334+
335+
/**
336+
* Fetch Zis job specs from an integration
337+
*
338+
* @param {string} integrationName Name of the Zis integration
339+
* @returns {Promise<unknown>} List of Zis job specs
340+
*/
341+
public async fetchZisJobSpecs(integrationName: string, filterTheList?: IListFilter): Promise<IZisJobspec[]> {
342+
let hasMore = true;
343+
const numberOfJobSpecs = "100"; // Maximum number of job specs per page
344+
let jobspecs: IZisJobspec[] = [];
345+
let data: IListFilter = filterTheList ?? {
346+
page: {
347+
size: numberOfJobSpecs
348+
}
349+
};
350+
351+
do {
352+
const response = await this.client.request<unknown, IZisJobspecsResponse>({
353+
url: `/api/services/zis/registry/${integrationName}/job_specs`,
354+
type: "GET",
355+
data
356+
});
357+
358+
jobspecs = [...jobspecs, ...response.job_specs];
359+
360+
hasMore = response.meta.has_more;
361+
if (hasMore) {
362+
data = {
363+
page: {
364+
after: response.meta.after,
365+
size: numberOfJobSpecs
366+
}
367+
};
368+
}
369+
} while (hasMore);
370+
371+
return jobspecs;
372+
}
373+
374+
/**
375+
* Install a Zis job spec for an integration
376+
*
377+
* @param {string} jobSpecName Name of the Zis job spec to install
378+
* @returns {Promise<void>} Resolves when the job spec is installed
379+
*/
380+
public async createZisJobSpec(jobSpecName: string): Promise<void> {
381+
return await this.client.request({
382+
url: `/api/services/zis/registry/job_specs/install?job_spec_name=${encodeURIComponent(jobSpecName)}`,
383+
contentType: "application/json",
384+
type: "POST"
385+
});
386+
}
387+
388+
/**
389+
* Delete a Zis job spec for an integration
390+
*
391+
* @param {string} jobSpecName Name of the Zis job spec to delete
392+
* @returns {Promise<void>} Resolves when the job spec is deleted
393+
*/
394+
public async deleteZisJobSpec(jobSpecName: string): Promise<void> {
395+
return await this.client.request({
396+
url: `/api/services/zis/registry/job_specs/install?job_spec_name=${encodeURIComponent(jobSpecName)}`,
397+
contentType: "application/json",
398+
type: "DELETE"
399+
});
400+
}
328401
}

0 commit comments

Comments
 (0)