Skip to content

Commit b9a4b20

Browse files
committed
new components
1 parent 5cf8f2a commit b9a4b20

File tree

10 files changed

+477
-9
lines changed

10 files changed

+477
-9
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import zendesk from "../../zendesk.app.mjs";
2+
3+
export default {
4+
key: "zendesk-get-user-info",
5+
name: "Get User Info",
6+
description: "Retrieves information about a specific user. [See the documentation](https://developer.zendesk.com/api-reference/ticketing/users/users/#show-user).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
zendesk,
11+
userId: {
12+
propDefinition: [
13+
zendesk,
14+
"userId",
15+
],
16+
},
17+
},
18+
async run({ $: step }) {
19+
const response = await this.zendesk.getUserInfo({
20+
step,
21+
userId: this.userId,
22+
});
23+
24+
step.export("$summary", `Successfully retrieved user info for ${response.user.name}`);
25+
26+
return response;
27+
},
28+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import zendesk from "../../zendesk.app.mjs";
2+
3+
export default {
4+
key: "zendesk-list-locales",
5+
name: "List Locales",
6+
description: "Retrieves all locales. [See the documentation](https://developer.zendesk.com/api-reference/ticketing/account-configuration/locales/).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
zendesk,
11+
},
12+
async run({ $: step }) {
13+
const { locales } = await this.zendesk.listLocales();
14+
15+
step.export("$summary", `Successfully retrieved ${locales.length} locale${locales.length === 1
16+
? ""
17+
: "s"}`);
18+
19+
return locales;
20+
},
21+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import zendesk from "../../zendesk.app.mjs";
2+
3+
export default {
4+
key: "zendesk-list-macros",
5+
name: "List Macros",
6+
description: "Retrieves all macros. [See the documentation](https://developer.zendesk.com/api-reference/ticketing/business-rules/macros/#list-macros).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
zendesk,
11+
access: {
12+
type: "string",
13+
label: "Access",
14+
description: "The access level of the macros to return",
15+
options: [
16+
"personal",
17+
"agents",
18+
"shared",
19+
"account",
20+
],
21+
optional: true,
22+
},
23+
active: {
24+
type: "boolean",
25+
label: "Active",
26+
description: "Filter by active macros if `true` or inactive macros if `false`",
27+
optional: true,
28+
},
29+
macroCategory: {
30+
propDefinition: [
31+
zendesk,
32+
"macroCategory",
33+
],
34+
},
35+
groupId: {
36+
propDefinition: [
37+
zendesk,
38+
"groupId",
39+
],
40+
},
41+
sortBy: {
42+
type: "string",
43+
label: "Sort By",
44+
description: "The field to sort the results by",
45+
options: [
46+
"alphabetical",
47+
"created_at",
48+
"updated_at",
49+
"usage_1h",
50+
"usage_24h",
51+
"usage_7d",
52+
"usage_30d",
53+
],
54+
optional: true,
55+
},
56+
sortOrder: {
57+
propDefinition: [
58+
zendesk,
59+
"sortOrder",
60+
],
61+
},
62+
limit: {
63+
propDefinition: [
64+
zendesk,
65+
"limit",
66+
],
67+
description: "Maximum number of macros to return",
68+
},
69+
},
70+
async run({ $: step }) {
71+
const results = this.zendesk.paginate({
72+
fn: this.zendesk.listMacros,
73+
args: {
74+
step,
75+
params: {
76+
access: this.access,
77+
active: this.active,
78+
category: this.macroCategory,
79+
group_id: this.groupId,
80+
sort_by: this.sortBy,
81+
sort_order: this.sortOrder,
82+
},
83+
},
84+
resourceKey: "macros",
85+
max: this.limit,
86+
});
87+
88+
const macros = [];
89+
for await (const macro of results) {
90+
macros.push(macro);
91+
}
92+
93+
step.export("$summary", `Successfully retrieved ${macros.length} macro${macros.length === 1
94+
? ""
95+
: "s"}`);
96+
97+
return macros;
98+
},
99+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import zendesk from "../../zendesk.app.mjs";
2+
3+
export default {
4+
key: "zendesk-list-ticket-comments",
5+
name: "List Ticket Comments",
6+
description: "Retrieves all comments for a specific ticket. [See the documentation](https://developer.zendesk.com/api-reference/ticketing/tickets/ticket_comments/#list-comments).",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
zendesk,
11+
ticketId: {
12+
propDefinition: [
13+
zendesk,
14+
"ticketId",
15+
],
16+
},
17+
sortOrder: {
18+
propDefinition: [
19+
zendesk,
20+
"sortOrder",
21+
],
22+
},
23+
limit: {
24+
propDefinition: [
25+
zendesk,
26+
"limit",
27+
],
28+
description: "Maximum number of comments to return",
29+
},
30+
},
31+
async run({ $: step }) {
32+
const results = this.zendesk.paginate({
33+
fn: this.zendesk.listTicketComments,
34+
args: {
35+
step,
36+
ticketId: this.ticketId,
37+
params: {
38+
sort_order: this.sortOrder,
39+
},
40+
},
41+
resourceKey: "comments",
42+
max: this.limit,
43+
});
44+
45+
const comments = [];
46+
for await (const comment of results) {
47+
comments.push(comment);
48+
}
49+
50+
step.export("$summary", `Successfully retrieved ${comments.length} comment${comments.length === 1
51+
? ""
52+
: "s"}`);
53+
54+
return comments;
55+
},
56+
};

components/zendesk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/zendesk",
3-
"version": "0.7.2",
3+
"version": "0.8.0",
44
"description": "Pipedream Zendesk Components",
55
"main": "zendesk.app.mjs",
66
"keywords": [
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import zendesk from "../../zendesk.app.mjs";
2+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
3+
4+
export default {
5+
props: {
6+
zendesk,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getLastTs() {
17+
return this.db.get("lastTs") || 0;
18+
},
19+
_setLastTs(ts) {
20+
this.db.set("lastTs", ts);
21+
},
22+
},
23+
};

components/zendesk/sources/common/webhook.mjs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ export default {
212212
isRelevant() {
213213
return true;
214214
},
215+
emitEvent(payload) {
216+
const ts = Date.parse(payload.updatedAt);
217+
const id = `${payload.ticketId}-${ts}`;
218+
this.$emit(payload, {
219+
id,
220+
summary: payload.title || payload.ticketId,
221+
ts,
222+
});
223+
},
215224
},
216225
async run(event) {
217226
const {
@@ -241,13 +250,6 @@ export default {
241250
return;
242251
}
243252

244-
const ts = Date.parse(payload.updatedAt);
245-
const id = `${payload.ticketId}-${ts}`;
246-
247-
this.$emit(payload, {
248-
id,
249-
summary: payload.title || payload.ticketId,
250-
ts,
251-
});
253+
this.emitEvent(payload);
252254
},
253255
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import common from "../common/polling.mjs";
2+
3+
export default {
4+
...common,
5+
key: "zendesk-locale-updated",
6+
name: "Locale Updated",
7+
type: "source",
8+
description: "Emit new event when a locale has been updated",
9+
version: "0.0.1",
10+
dedupe: "unique",
11+
async run() {
12+
const lastTs = this._getLastTs();
13+
let maxTs = lastTs;
14+
15+
const { locales } = await this.zendesk.listLocales();
16+
for (const locale of locales) {
17+
const ts = Date.parse(locale.updated_at);
18+
if (ts > lastTs) {
19+
this.$emit(locale, {
20+
id: `${locale.id}-${ts}`,
21+
summary: locale.name,
22+
ts,
23+
});
24+
maxTs = Math.max(maxTs, ts);
25+
}
26+
}
27+
this._setLastTs(maxTs);
28+
},
29+
};
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import common from "../common/ticket.mjs";
2+
3+
export default {
4+
...common,
5+
name: "New Ticket Comment Added (Instant)",
6+
key: "zendesk-new-ticket-comment-added",
7+
type: "source",
8+
description: "Emit new event when a ticket comment has been added",
9+
version: "0.0.1",
10+
dedupe: "unique",
11+
methods: {
12+
...common.methods,
13+
_getLastTs() {
14+
return this.db.get("lastTs");
15+
},
16+
_setLastTs(ts) {
17+
this.db.set("lastTs", ts);
18+
},
19+
getWebhookName() {
20+
return "Ticket Comment Added Webhook";
21+
},
22+
getTriggerTitle() {
23+
return "Ticket Comment Added Trigger";
24+
},
25+
getTriggerConditions() {
26+
return {
27+
all: [
28+
{
29+
field: "update_type",
30+
value: "Change",
31+
},
32+
],
33+
};
34+
},
35+
getTriggerPayload() {
36+
const payload = common.methods.getTriggerPayload.call(this);
37+
return {
38+
...payload,
39+
ticketComments: "{{ticket.comments}}",
40+
};
41+
},
42+
convertCommentsToJson(raw) {
43+
return [
44+
...raw.matchAll(/#<Comment (.*?)>/g),
45+
].map((match) => {
46+
const fields = match[1]
47+
.split(",")
48+
.map((part) => part.trim())
49+
.map((pair) => {
50+
const [
51+
key,
52+
value,
53+
] = pair.split(/:\s+/);
54+
// Clean up values: remove extra quotes or cast to appropriate types
55+
let cleaned = value;
56+
if (cleaned === "nil") cleaned = null;
57+
else if (cleaned === "true") cleaned = true;
58+
else if (cleaned === "false") cleaned = false;
59+
else if (/^\d+$/.test(cleaned)) cleaned = parseInt(cleaned, 10);
60+
else if (/^".*"$/.test(cleaned)) cleaned = cleaned.slice(1, -1);
61+
return [
62+
key,
63+
cleaned,
64+
];
65+
});
66+
return Object.fromEntries(fields);
67+
});
68+
},
69+
isRelevant(payload) {
70+
const lastTs = this._getLastTs() || 0;
71+
let maxTs = lastTs;
72+
const comments = this.convertCommentsToJson(payload.ticketComments);
73+
for (const comment of comments) {
74+
const ts = Date.parse(comment.created_at);
75+
maxTs = Math.max(maxTs, ts);
76+
}
77+
this._setLastTs(maxTs);
78+
return comments.length > 0 && maxTs > lastTs;
79+
},
80+
emitEvent(payload) {
81+
payload.ticketComments = this.convertCommentsToJson(payload.ticketComments);
82+
for (const comment of payload.ticketComments) {
83+
const ts = Date.parse(comment.created_at);
84+
const id = `${payload.ticketId}-${ts}`;
85+
this.$emit(comment, {
86+
id,
87+
summary: comment.value,
88+
ts,
89+
});
90+
}
91+
},
92+
},
93+
};

0 commit comments

Comments
 (0)