|
| 1 | +import { ConfigurationError } from "@pipedream/platform"; |
| 2 | +import zendesk from "../../zendesk.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "zendesk-list-articles", |
| 6 | + name: "List Articles", |
| 7 | + description: "Retrieves a list of articles. [See the documentation](https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/#list-articles).", |
| 8 | + type: "action", |
| 9 | + version: "0.0.1", |
| 10 | + annotations: { |
| 11 | + destructiveHint: false, |
| 12 | + openWorldHint: true, |
| 13 | + readOnlyHint: true, |
| 14 | + }, |
| 15 | + props: { |
| 16 | + zendesk, |
| 17 | + locale: { |
| 18 | + propDefinition: [ |
| 19 | + zendesk, |
| 20 | + "locale", |
| 21 | + ], |
| 22 | + optional: true, |
| 23 | + }, |
| 24 | + categoryId: { |
| 25 | + propDefinition: [ |
| 26 | + zendesk, |
| 27 | + "articleCategoryId", |
| 28 | + ({ locale }) => ({ |
| 29 | + locale, |
| 30 | + }), |
| 31 | + ], |
| 32 | + optional: true, |
| 33 | + }, |
| 34 | + sectionId: { |
| 35 | + propDefinition: [ |
| 36 | + zendesk, |
| 37 | + "sectionId", |
| 38 | + ({ |
| 39 | + locale, categoryId, |
| 40 | + }) => ({ |
| 41 | + locale, |
| 42 | + categoryId, |
| 43 | + }), |
| 44 | + ], |
| 45 | + optional: true, |
| 46 | + }, |
| 47 | + userId: { |
| 48 | + propDefinition: [ |
| 49 | + zendesk, |
| 50 | + "userId", |
| 51 | + ], |
| 52 | + optional: true, |
| 53 | + reloadProps: true, |
| 54 | + }, |
| 55 | + limit: { |
| 56 | + propDefinition: [ |
| 57 | + zendesk, |
| 58 | + "limit", |
| 59 | + ], |
| 60 | + description: "Maximum number of articles to return", |
| 61 | + }, |
| 62 | + }, |
| 63 | + async additionalProps(props) { |
| 64 | + props.locale.hidden = false; |
| 65 | + props.categoryId.hidden = false; |
| 66 | + props.sectionId.hidden = false; |
| 67 | + if (this.userId) { |
| 68 | + props.locale.hidden = true; |
| 69 | + props.categoryId.hidden = true; |
| 70 | + props.sectionId.hidden = true; |
| 71 | + } |
| 72 | + return {}; |
| 73 | + }, |
| 74 | + async run({ $ }) { |
| 75 | + if ((this.categoryId && this.userId) || (this.sectionId && this.userId)) { |
| 76 | + throw new ConfigurationError("Providing a User ID, you cannot provide a Category ID or Section ID."); |
| 77 | + } |
| 78 | + |
| 79 | + const results = this.zendesk.paginate({ |
| 80 | + fn: this.zendesk.listArticles, |
| 81 | + args: { |
| 82 | + $, |
| 83 | + categoryId: this.categoryId, |
| 84 | + sectionId: this.sectionId, |
| 85 | + userId: this.userId, |
| 86 | + locale: this.userId |
| 87 | + ? null |
| 88 | + : this.locale, |
| 89 | + }, |
| 90 | + resourceKey: "articles", |
| 91 | + max: this.limit, |
| 92 | + }); |
| 93 | + |
| 94 | + const articles = []; |
| 95 | + for await (const article of results) { |
| 96 | + articles.push(article); |
| 97 | + } |
| 98 | + |
| 99 | + $.export("$summary", `Successfully retrieved ${articles.length} article${articles.length === 1 |
| 100 | + ? "" |
| 101 | + : "s"}`); |
| 102 | + |
| 103 | + return articles; |
| 104 | + }, |
| 105 | +}; |
0 commit comments