|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | + |
1 | 3 | export default {
|
2 | 4 | type: "app",
|
3 | 5 | app: "easypromos",
|
4 |
| - propDefinitions: {}, |
| 6 | + propDefinitions: { |
| 7 | + userId: { |
| 8 | + type: "integer", |
| 9 | + label: "User ID", |
| 10 | + description: "The ID of the user", |
| 11 | + async options({ |
| 12 | + promotionId, prevContext, |
| 13 | + }) { |
| 14 | + const { |
| 15 | + items, paging, |
| 16 | + } = await this.getUsers({ |
| 17 | + promotionId, |
| 18 | + params: { |
| 19 | + next_cursor: prevContext.nextCursor, |
| 20 | + }, |
| 21 | + }); |
| 22 | + return { |
| 23 | + options: items.map(({ |
| 24 | + id: value, email: label, |
| 25 | + }) => ({ |
| 26 | + label, |
| 27 | + value, |
| 28 | + })), |
| 29 | + context: { |
| 30 | + nextCursor: paging.next_cursor, |
| 31 | + }, |
| 32 | + }; |
| 33 | + }, |
| 34 | + }, |
| 35 | + promotionId: { |
| 36 | + type: "integer", |
| 37 | + label: "Promotion ID", |
| 38 | + description: "The ID of the promotion", |
| 39 | + async options({ prevContext }) { |
| 40 | + const { |
| 41 | + items, paging, |
| 42 | + } = await this.getPromotions({ |
| 43 | + params: { |
| 44 | + next_cursor: prevContext.nextCursor, |
| 45 | + }, |
| 46 | + }); |
| 47 | + return { |
| 48 | + options: items.map(({ |
| 49 | + id, title, internal_ref: ref, |
| 50 | + }) => ({ |
| 51 | + label: ref || title, |
| 52 | + value: parseInt(id), |
| 53 | + })), |
| 54 | + context: { |
| 55 | + nextCursor: paging.next_cursor, |
| 56 | + }, |
| 57 | + }; |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
5 | 61 | methods: {
|
6 |
| - // this.$auth contains connected account data |
7 |
| - authKeys() { |
8 |
| - console.log(Object.keys(this.$auth)); |
| 62 | + _baseUrl() { |
| 63 | + return "https://api.easypromosapp.com/v2"; |
| 64 | + }, |
| 65 | + _headers() { |
| 66 | + return { |
| 67 | + Authorization: `Bearer ${this.$auth.api_key}`, |
| 68 | + }; |
| 69 | + }, |
| 70 | + _makeRequest({ |
| 71 | + $ = this, path, ...opts |
| 72 | + }) { |
| 73 | + return axios($, { |
| 74 | + url: this._baseUrl() + path, |
| 75 | + headers: this._headers(), |
| 76 | + ...opts, |
| 77 | + }); |
| 78 | + }, |
| 79 | + getCoinTransactions({ |
| 80 | + promotionId, ...opts |
| 81 | + }) { |
| 82 | + return this._makeRequest({ |
| 83 | + path: `/coin_transactions/${promotionId}`, |
| 84 | + ...opts, |
| 85 | + }); |
| 86 | + }, |
| 87 | + getUsers({ |
| 88 | + promotionId, ...opts |
| 89 | + }) { |
| 90 | + return this._makeRequest({ |
| 91 | + path: `/users/${promotionId}`, |
| 92 | + ...opts, |
| 93 | + }); |
| 94 | + }, |
| 95 | + getParticipations({ |
| 96 | + promotionId, ...opts |
| 97 | + }) { |
| 98 | + return this._makeRequest({ |
| 99 | + path: `/participations/${promotionId}`, |
| 100 | + ...opts, |
| 101 | + }); |
| 102 | + }, |
| 103 | + getPromotions(opts = {}) { |
| 104 | + return this._makeRequest({ |
| 105 | + path: "/promotions", |
| 106 | + ...opts, |
| 107 | + }); |
| 108 | + }, |
| 109 | + async *paginate({ |
| 110 | + fn, params = {}, maxResults = null, ...opts |
| 111 | + }) { |
| 112 | + let hasMore = false; |
| 113 | + let count = 0; |
| 114 | + let nextCursor = null; |
| 115 | + |
| 116 | + do { |
| 117 | + params.next_cursor = nextCursor; |
| 118 | + const { |
| 119 | + items, |
| 120 | + paging: { next_cursor }, |
| 121 | + } = await fn({ |
| 122 | + params, |
| 123 | + ...opts, |
| 124 | + }); |
| 125 | + for (const d of items) { |
| 126 | + yield d; |
| 127 | + |
| 128 | + if (maxResults && ++count === maxResults) { |
| 129 | + return count; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + nextCursor = next_cursor; |
| 134 | + hasMore = nextCursor; |
| 135 | + |
| 136 | + } while (hasMore); |
9 | 137 | },
|
10 | 138 | },
|
11 | 139 | };
|
0 commit comments