@@ -8,61 +8,41 @@ import _ from 'lodash';
8
8
import { config } from 'topcoder-react-utils' ;
9
9
10
10
import { getService as getCommunityService } from './communities' ;
11
+ import { getService as getChallengeService } from './challenges' ;
11
12
import { getApi } from './api' ;
12
13
13
14
/**
14
15
* Service class.
15
16
*/
16
17
class TermsService {
17
18
/**
18
- * @param {String } tokenV2 Optional. Auth token for Topcoder API v2 .
19
+ * @param {String } tokenV3 Optional. Auth token for Topcoder API v3 .
19
20
*/
20
- constructor ( tokenV2 ) {
21
+ constructor ( tokenV3 ) {
21
22
this . private = {
22
- api : getApi ( 'V2 ' , tokenV2 ) ,
23
- tokenV2 ,
23
+ api : getApi ( 'V5 ' , tokenV3 ) ,
24
+ tokenV3 ,
24
25
} ;
25
26
}
26
27
27
28
/**
28
29
* get all terms of specified challenge
29
- * @param {Number| String} challengeId id of the challenge
30
+ * @param {Array< String> } terms terms of the challenge
30
31
* @return {Promise } promise of the request result
31
32
*/
32
- getChallengeTerms ( challengeId ) {
33
- if ( this . private . tokenV2 ) {
34
- let registered = false ;
35
- return this . private . api . get ( `/terms/${ challengeId } ?role=Submitter` )
36
- . then ( res => res . json ( ) )
37
- . then ( ( res ) => {
38
- if ( res . error ) {
39
- if ( res . error . details === 'You are already registered for this challenge.' ) {
40
- registered = true ;
41
- }
42
- return this . private . api . get ( `/terms/${ challengeId } ?role=Submitter&noauth=true` )
43
- . then ( ( resp ) => {
44
- if ( resp . ok ) {
45
- return resp . json ( ) . then ( ( result ) => {
46
- if ( registered ) {
47
- // eslint-disable-next-line no-param-reassign
48
- _ . forEach ( result . terms , ( t ) => { t . agreed = true ; } ) ;
49
- }
50
- return result ;
51
- } ) ;
52
- }
53
- return new Error ( resp . statusText ) ;
54
- } ) ;
55
- }
56
- return res ;
57
- } ) ;
33
+ async getChallengeTerms ( terms ) {
34
+ if ( this . private . tokenV3 ) {
35
+ const challengeService = getChallengeService ( this . private . tokenV3 ) ;
36
+ const roleId = await challengeService . getRoleId ( 'Submitter' ) ;
37
+ const registerTerms = _ . filter ( terms , t => t . roleId === roleId ) ;
38
+
39
+ return Promise . all ( _ . map ( registerTerms , term => this . getTermDetails ( term . id ) ) )
40
+ . then ( challengeTerms => (
41
+ _ . map ( challengeTerms , term => _ . pick ( term , 'id' , 'title' , 'agreed' ) )
42
+ ) ) ;
58
43
}
59
- return this . private . api . get ( `/terms/${ challengeId } ?role=Submitter&noauth=true` )
60
- . then ( ( resp ) => {
61
- if ( resp . ok ) {
62
- return resp . json ( ) ;
63
- }
64
- throw new Error ( resp . statusText ) ;
65
- } ) ;
44
+
45
+ return [ ] ;
66
46
}
67
47
68
48
/**
@@ -110,7 +90,7 @@ class TermsService {
110
90
return Promise . resolve ( term ) ;
111
91
}
112
92
// Otherwise grab new details from terms api
113
- return this . getTermDetails ( term . termsOfUseId ) . then ( res => _ . pick ( res , [ 'termsOfUseId ' , 'agreed' , 'title' ] ) ) ;
93
+ return this . getTermDetails ( term . id ) . then ( res => _ . pick ( res , [ 'id ' , 'agreed' , 'title' ] ) ) ;
114
94
} ) ;
115
95
116
96
return Promise . all ( promises ) . then ( terms => ( { terms } ) ) ;
@@ -123,8 +103,7 @@ class TermsService {
123
103
*/
124
104
getTermDetails ( termId ) {
125
105
// looks like server cache responses, to prevent it we add nocache param with always new value
126
- const nocache = ( new Date ( ) ) . getTime ( ) ;
127
- return this . private . api . get ( `/terms/detail/${ termId } ?nocache=${ nocache } ` )
106
+ return this . private . api . get ( `/terms/${ termId } ` )
128
107
. then ( res => ( res . ok ? res . json ( ) : Promise . reject ( res . json ( ) ) ) ) ;
129
108
}
130
109
@@ -135,7 +114,11 @@ class TermsService {
135
114
* @return {Promise } promise of the request result
136
115
*/
137
116
getDocuSignUrl ( templateId , returnUrl ) {
138
- return this . private . api . post ( `/terms/docusign/viewURL?templateId=${ templateId } &returnUrl=${ returnUrl } ` )
117
+ const params = {
118
+ templateId,
119
+ returnUrl,
120
+ } ;
121
+ return this . private . api . postJson ( '/terms/docusignViewURL' , params )
139
122
. then ( res => ( res . ok ? res . json ( ) : Promise . reject ( res . json ( ) ) ) ) ;
140
123
}
141
124
@@ -153,20 +136,20 @@ class TermsService {
153
136
let lastInstance = null ;
154
137
/**
155
138
* Returns a new or existing terms service.
156
- * @param {String } tokenV2 Optional. Auth token for Topcoder API v2 .
139
+ * @param {String } tokenV3 Optional. Auth token for Topcoder API v3 .
157
140
* @return {TermsService } Terms service object
158
141
*/
159
- export function getService ( tokenV2 ) {
142
+ export function getService ( tokenV3 ) {
160
143
/* Because of Topcoder backend restrictions, it is not straightforward to test
161
144
* terms-related functionality in any other way than just providing an option
162
145
* to run the app against mock terms service. */
163
146
if ( config . MOCK_TERMS_SERVICE ) {
164
147
/* eslint-disable global-require */
165
- return require ( './__mocks__/terms' ) . getService ( tokenV2 ) ;
148
+ return require ( './__mocks__/terms' ) . getService ( tokenV3 ) ;
166
149
/* eslint-enable global-require */
167
150
}
168
- if ( ! lastInstance || ( tokenV2 && lastInstance . private . tokenV2 !== tokenV2 ) ) {
169
- lastInstance = new TermsService ( tokenV2 ) ;
151
+ if ( ! lastInstance || ( tokenV3 && lastInstance . private . tokenV3 !== tokenV3 ) ) {
152
+ lastInstance = new TermsService ( tokenV3 ) ;
170
153
}
171
154
return lastInstance ;
172
155
}
0 commit comments