Skip to content

Commit 87e8aad

Browse files
Updated terms to use V5 API
1 parent 03713ef commit 87e8aad

File tree

4 files changed

+47
-64
lines changed

4 files changed

+47
-64
lines changed

src/actions/terms.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function getTermsInit(arg) {
3232
* @return {Action}
3333
*/
3434
function getTermsDone(entity, tokens, mockAgreed) {
35-
const service = getService(tokens.tokenV2);
35+
const service = getService(tokens.tokenV3);
3636
let termsPromise;
3737

3838
// if mockAgreed=true passed, then we create an array of 10 true which we pass to the
@@ -44,7 +44,7 @@ function getTermsDone(entity, tokens, mockAgreed) {
4444

4545
switch (entity.type) {
4646
case 'challenge': {
47-
termsPromise = service.getChallengeTerms(entity.id, mockAgreedArray);
47+
termsPromise = service.getChallengeTerms(entity.terms, mockAgreedArray);
4848
break;
4949
}
5050
case 'community': {
@@ -59,7 +59,7 @@ function getTermsDone(entity, tokens, mockAgreed) {
5959
throw new Error(`Entity type '${entity.type}' is not supported by getTermsDone.`);
6060
}
6161

62-
return termsPromise.then(res => ({ entity, terms: res.terms }));
62+
return termsPromise.then(res => ({ entity, terms: res }));
6363
}
6464

6565
/**
@@ -123,14 +123,14 @@ function checkStatusDone(entity, tokens) {
123123
* @return {Promise} resolves to the list of term objects
124124
*/
125125
const checkStatus = maxAttempts => getTermsDone(entity, tokens, mockAgreed).then((res) => {
126-
const allAgreed = _.every(res.terms, 'agreed');
126+
const allAgreed = _.every(res, 'agreed');
127127

128128
// if not all terms are agreed and we still have some attempts to try
129129
if (!allAgreed && maxAttempts > 1) {
130130
return delay(TIME_OUT).then(() => checkStatus(maxAttempts - 1));
131131
}
132132

133-
return res.terms;
133+
return res;
134134
});
135135

136136
return checkStatus(MAX_ATTEMPTS);
@@ -152,11 +152,11 @@ function getTermDetailsInit(termId) {
152152
* @static
153153
* @desc Creates an action that fetches details of the specified term.
154154
* @param {Number|String} termId
155-
* @param {String} tokenV2
155+
* @param {String} tokenV3
156156
* @return {Action}
157157
*/
158-
function getTermDetailsDone(termId, tokenV2) {
159-
const service = getService(tokenV2);
158+
function getTermDetailsDone(termId, tokenV3) {
159+
const service = getService(tokenV3);
160160
return service.getTermDetails(termId).then(details => ({ termId, details }));
161161
}
162162

@@ -175,11 +175,11 @@ function getDocuSignUrlInit(templateId) {
175175
* @desc Creates an action that generates the url of DoduSign term
176176
* @param {Number|String} templateId id of document template to sign
177177
* @param {String} returnUrl callback url after finishing singing
178-
* @param {String} tokenV2 auth token
178+
* @param {String} tokenV3 auth token
179179
* @return {Action}
180180
*/
181-
function getDocuSignUrlDone(templateId, returnUrl, tokenV2) {
182-
const service = getService(tokenV2);
181+
function getDocuSignUrlDone(templateId, returnUrl, tokenV3) {
182+
const service = getService(tokenV3);
183183
return service.getDocuSignUrl(templateId, returnUrl)
184184
.then(resp => ({ templateId, docuSignUrl: resp.recipientViewUrl }));
185185
}
@@ -198,11 +198,11 @@ function agreeTermInit(termId) {
198198
* @static
199199
* @desc Creates an action that agrees to a term.
200200
* @param {Number|String} termId id of term
201-
* @param {String} tokenV2 auth token
201+
* @param {String} tokenV3 auth token
202202
* @return {Action}
203203
*/
204-
function agreeTermDone(termId, tokenV2) {
205-
const service = getService(tokenV2);
204+
function agreeTermDone(termId, tokenV3) {
205+
const service = getService(tokenV3);
206206
return service.agreeTerm(termId).then(resp => ({ termId, success: resp.success }));
207207
}
208208

src/reducers/reviewOpportunity.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function buildRequiredTermsList(details) {
2323
// Sometimes roles such as Primary Reviewer have no directly equal
2424
// terms entry. Include the plain Reviewer terms when present as a back-up.
2525
.filter(term => term.role === 'Reviewer' || _.includes(roles, term.role))
26-
.map(term => _.pick(term, ['termsOfUseId', 'agreed', 'title'])),
27-
term => term.termsOfUseId,
26+
.map(term => _.pick(term, ['id', 'agreed', 'title'])),
27+
term => term.id,
2828
);
2929

3030
return requiredTerms || [];

src/reducers/terms.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function onAgreeTermDone(state, action) {
181181
}
182182
if (action.payload.success) {
183183
const terms = _.cloneDeep(state.terms);
184-
const term = _.find(terms, ['termsOfUseId', action.payload.termId]);
184+
const term = _.find(terms, ['id', action.payload.termId]);
185185
term.agreed = true;
186186
const selectedTerm = _.find(terms, t => !t.agreed);
187187
return {

src/services/terms.js

+30-47
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,41 @@ import _ from 'lodash';
88
import { config } from 'topcoder-react-utils';
99

1010
import { getService as getCommunityService } from './communities';
11+
import { getService as getChallengeService } from './challenges';
1112
import { getApi } from './api';
1213

1314
/**
1415
* Service class.
1516
*/
1617
class TermsService {
1718
/**
18-
* @param {String} tokenV2 Optional. Auth token for Topcoder API v2.
19+
* @param {String} tokenV3 Optional. Auth token for Topcoder API v3.
1920
*/
20-
constructor(tokenV2) {
21+
constructor(tokenV3) {
2122
this.private = {
22-
api: getApi('V2', tokenV2),
23-
tokenV2,
23+
api: getApi('V5', tokenV3),
24+
tokenV3,
2425
};
2526
}
2627

2728
/**
2829
* get all terms of specified challenge
29-
* @param {Number|String} challengeId id of the challenge
30+
* @param {Array<String>} terms terms of the challenge
3031
* @return {Promise} promise of the request result
3132
*/
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+
));
5843
}
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 [];
6646
}
6747

6848
/**
@@ -110,7 +90,7 @@ class TermsService {
11090
return Promise.resolve(term);
11191
}
11292
// 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']));
11494
});
11595

11696
return Promise.all(promises).then(terms => ({ terms }));
@@ -123,8 +103,7 @@ class TermsService {
123103
*/
124104
getTermDetails(termId) {
125105
// 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}`)
128107
.then(res => (res.ok ? res.json() : Promise.reject(res.json())));
129108
}
130109

@@ -135,7 +114,11 @@ class TermsService {
135114
* @return {Promise} promise of the request result
136115
*/
137116
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)
139122
.then(res => (res.ok ? res.json() : Promise.reject(res.json())));
140123
}
141124

@@ -153,20 +136,20 @@ class TermsService {
153136
let lastInstance = null;
154137
/**
155138
* 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.
157140
* @return {TermsService} Terms service object
158141
*/
159-
export function getService(tokenV2) {
142+
export function getService(tokenV3) {
160143
/* Because of Topcoder backend restrictions, it is not straightforward to test
161144
* terms-related functionality in any other way than just providing an option
162145
* to run the app against mock terms service. */
163146
if (config.MOCK_TERMS_SERVICE) {
164147
/* eslint-disable global-require */
165-
return require('./__mocks__/terms').getService(tokenV2);
148+
return require('./__mocks__/terms').getService(tokenV3);
166149
/* eslint-enable global-require */
167150
}
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);
170153
}
171154
return lastInstance;
172155
}

0 commit comments

Comments
 (0)