@@ -3,120 +3,137 @@ import { axios } from "@pipedream/platform";
33export 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} ;
0 commit comments