Skip to content

Commit 45e5a20

Browse files
committed
[Components] easypromos #15197
Sources - New Coin Transaction - New Participation - New User
1 parent b79de9b commit 45e5a20

File tree

9 files changed

+341
-351
lines changed

9 files changed

+341
-351
lines changed

components/easypromos/easypromos.app.mjs

Lines changed: 98 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,120 +3,137 @@ import { axios } from "@pipedream/platform";
33
export default {
44
type: "app",
55
app: "easypromos",
6-
version: "0.0.{{ts}}",
76
propDefinitions: {
8-
userid: {
7+
userId: {
98
type: "integer",
109
label: "User ID",
1110
description: "The ID of the user",
12-
async options() {
13-
const users = await this.getUsers();
14-
return users.map((user) => ({
15-
label: user.name,
16-
value: user.id,
17-
}));
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+
};
1833
},
1934
},
20-
promotionid: {
35+
promotionId: {
2136
type: "integer",
2237
label: "Promotion ID",
2338
description: "The ID of the promotion",
24-
async options() {
25-
const promotions = await this.getPromotions();
26-
return promotions.map((promotion) => ({
27-
label: promotion.name,
28-
value: promotion.id,
29-
}));
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: value, title, internal_ref: ref,
50+
}) => ({
51+
label: ref || title,
52+
value,
53+
})),
54+
context: {
55+
nextCursor: paging.next_cursor,
56+
},
57+
};
3058
},
3159
},
3260
},
3361
methods: {
34-
// Existing method
35-
authKeys() {
36-
console.log(Object.keys(this.$auth));
37-
},
38-
// Base URL for the Easypromos API
3962
_baseUrl() {
40-
return "https://api.easypromos.com";
63+
return "https://api.easypromosapp.com/v2";
4164
},
42-
// Helper method to make HTTP requests
43-
async _makeRequest(opts = {}) {
44-
const {
45-
$ = this,
46-
method = "GET",
47-
path = "/",
48-
data,
49-
params,
50-
headers = {},
51-
...otherOpts
52-
} = opts;
53-
54-
const requestHeaders = {
55-
...headers,
56-
Authorization: `Bearer ${this.$auth.access_token}`,
65+
_headers() {
66+
return {
67+
Authorization: `Bearer ${this.$auth.api_key}`,
5768
};
58-
59-
if (data) {
60-
requestHeaders["Content-Type"] = "application/json";
61-
}
62-
69+
},
70+
_makeRequest({
71+
$ = this, path, ...opts
72+
}) {
6373
return axios($, {
64-
method,
6574
url: this._baseUrl() + path,
66-
headers: requestHeaders,
67-
data,
68-
params,
69-
...otherOpts,
75+
headers: this._headers(),
76+
...opts,
7077
});
7178
},
72-
// Method to fetch users
73-
async getUsers(opts = {}) {
79+
getCoinTransactions({
80+
promotionId, ...opts
81+
}) {
7482
return this._makeRequest({
75-
method: "GET",
76-
path: "/users",
77-
params: opts.params,
83+
path: `/coin_transactions/${promotionId}`,
84+
...opts,
7885
});
7986
},
80-
// Method to fetch promotions
81-
async getPromotions(opts = {}) {
87+
getUsers({
88+
promotionId, ...opts
89+
}) {
8290
return this._makeRequest({
83-
method: "GET",
84-
path: "/promotions",
85-
params: opts.params,
91+
path: `/users/${promotionId}`,
92+
...opts,
8693
});
8794
},
88-
// Emit event when a user earns or spends coins
89-
async emitCoinTransaction({
90-
userid, promotionid,
95+
getParticipations({
96+
promotionId, ...opts
9197
}) {
9298
return this._makeRequest({
93-
method: "POST",
94-
path: "/coin-transactions",
95-
data: {
96-
user_id: userid,
97-
promotion_id: promotionid,
98-
},
99+
path: `/participations/${promotionId}`,
100+
...opts,
99101
});
100102
},
101-
// Emit event when a registered user submits participation
102-
async emitParticipationSubmission({ promotionid }) {
103+
getPromotions(opts = {}) {
103104
return this._makeRequest({
104-
method: "POST",
105-
path: "/participations",
106-
data: {
107-
promotion_id: promotionid,
108-
},
105+
path: "/promotions",
106+
...opts,
109107
});
110108
},
111-
// Emit event when a user registers in the promotion
112-
async emitUserRegistration({ promotionid }) {
113-
return this._makeRequest({
114-
method: "POST",
115-
path: "/registrations",
116-
data: {
117-
promotion_id: promotionid,
118-
},
119-
});
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);
120137
},
121138
},
122139
};

components/easypromos/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/easypromos",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Easypromos Components",
55
"main": "easypromos.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
1518
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import easypromos from "../../easypromos.app.mjs";
3+
4+
export default {
5+
props: {
6+
easypromos,
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+
_getLastId() {
17+
return this.db.get("lastId") || 0;
18+
},
19+
_setLastId(lastId) {
20+
this.db.set("lastId", lastId);
21+
},
22+
getOpts() {
23+
return {};
24+
},
25+
async emitEvent(maxResults = false) {
26+
const lastId = this._getLastId();
27+
28+
const response = this.easypromos.paginate({
29+
fn: this.getFunction(),
30+
...this.getOpts(),
31+
params: {
32+
order: "created_desc",
33+
},
34+
});
35+
36+
let responseArray = [];
37+
for await (const item of response) {
38+
if (item.id <= lastId) break;
39+
responseArray.push(item);
40+
}
41+
42+
if (responseArray.length) {
43+
if (maxResults && (responseArray.length > maxResults)) {
44+
responseArray.length = maxResults;
45+
}
46+
this._setLastId(responseArray[0].id);
47+
}
48+
49+
for (const item of responseArray.reverse()) {
50+
this.$emit(item, {
51+
id: item.id || item.transaction.id,
52+
summary: this.getSummary(item),
53+
ts: Date.parse(item.created || new Date()),
54+
});
55+
}
56+
},
57+
},
58+
hooks: {
59+
async deploy() {
60+
await this.emitEvent(25);
61+
},
62+
},
63+
async run() {
64+
await this.emitEvent();
65+
},
66+
};

0 commit comments

Comments
 (0)