Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit e1d6c29

Browse files
author
Nick Litwin
committedJun 29, 2015
Move services from peer review to topcoder-app
1 parent d68f790 commit e1d6c29

File tree

6 files changed

+281
-0
lines changed

6 files changed

+281
-0
lines changed
 

‎app/services/api.service.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('topcoder').factory('api', api);
5+
6+
api.$inject = ['$http'];
7+
8+
function api() {
9+
var service = {
10+
requestHandler: requestHandler
11+
};
12+
return service;
13+
14+
///////////////
15+
16+
function requestHandler(method, url, data) {
17+
var options = {
18+
method : method,
19+
url : url,
20+
headers: {}
21+
};
22+
23+
if(data) {
24+
options.data = data;
25+
}
26+
27+
$http(options);
28+
}
29+
}
30+
31+
})();

‎app/services/auth.service.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('topcoder').factory('auth', auth);
5+
6+
auth.$inject = ['CONSTANTS', '$window', 'AuthToken', '$state', '$stateParams',' ApiService'];
7+
8+
function auth() {
9+
var auth0 = new Auth0({
10+
domain: CONSTANTS.auth0Domain,
11+
clientID: CONSTANTS.clientId,
12+
callbackURL: CONSTANTS.auth0Callback
13+
});
14+
15+
var service = {
16+
login: login,
17+
logout: logout,
18+
register: register,
19+
checkLogin: checkLogin,
20+
isAuthenticated: isAuthenticated
21+
};
22+
return service;
23+
24+
///////////////
25+
26+
function login(username, password, successCallback, errorCallback) {
27+
var options = {
28+
connection: 'LDAP',
29+
scope: 'openid profile',
30+
username: username,
31+
password: password
32+
};
33+
34+
auth0.signin(options, function(err, profile, id_token, access_token, state) {
35+
if (err) {
36+
errorCallback(err)
37+
} else {
38+
AuthToken.setToken(id_token);
39+
successCallback( )profile, id_token, access_token, state);
40+
}
41+
});
42+
}
43+
44+
function logout(successCallback) {
45+
AuthToken.removeToken();
46+
47+
if (typeof(successCallback) === 'function') {
48+
successCallback();
49+
} else {
50+
$state.transitionTo($state.current, $stateParams, {
51+
reload: true,
52+
inherit: false,
53+
notify: true
54+
}
55+
}
56+
}
57+
58+
function register(reg) {
59+
// api params
60+
// required: ["firstName", "lastName", "handle", "country", "email"],
61+
// optional: ["password", "socialProviderId", "socialUserName", "socialEmail", "socialEmailVerified", "regSource", "socialUserId", "utm_source", "utm_medium", "utm_campaign"]
62+
var url = CONSTANTS.API_URL_V2 + '/users/';
63+
ApiService.requestHandler('POST', url, JSON.stringify(reg));
64+
}
65+
66+
function checkLogin() {
67+
if (!this.isAuthenticated) {
68+
$window.location = '/login';
69+
}
70+
}
71+
72+
function isAuthenticated() {
73+
return !!AuthToken.getToken();
74+
}
75+
}
76+
})();

‎app/services/authtoken.service.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('topcoder').factory('authtoken', authtoken);
5+
6+
authtoken.$inject = ['CONSTANTS', '$window', '$cookies'];
7+
8+
function authtoken() {
9+
var tokenKey = 'tcjwt';
10+
11+
var service = {
12+
setToken: setToken,
13+
getToken: getToken,
14+
removeToken: removeToken
15+
};
16+
return service;
17+
18+
///////////////
19+
20+
function setToken() {
21+
$window.document.cookie = tokenKey + '=' + token + '; path=/; domain=.' + CONSTANTS.domain + '; expires=' + new Date(new Date().getTime() + 12096e5);
22+
}
23+
24+
function getToken() {
25+
return $cookies.get(tokenKey);
26+
}
27+
28+
function removeToken() {
29+
$window.document.cookie = tokenKey + '=; path=/; domain=.' + CONSTANTS.domain + '; expires=' + (new Date(0)).toUTCString();
30+
}
31+
32+
}
33+
34+
})();

‎app/services/helpers.service.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('topcoder').factory('Helpers', Helpers);
5+
6+
Helpers.$inject = ['$window', '$location'];
7+
8+
function Helpers($window, $location) {
9+
// TODO: Separate helpers by submodule
10+
11+
var service = {
12+
storeById: storeById,
13+
parseQuestions: parseQuestions,
14+
parseAnswers: parseAnswers,
15+
compileReviewItems: compileReviewItems
16+
};
17+
return service;
18+
19+
/////////////////////
20+
21+
function storeById(object, questions) {
22+
angular.forEach(questions, function(question) {
23+
object[question.id] = question;
24+
});
25+
}
26+
27+
function parseQuestions(questions) {
28+
angular.forEach(questions, function(question) {
29+
if (question.questionTypeId === 5) {
30+
question.description = question.description;
31+
question.guidelines = question.guideline.split('\n');
32+
}
33+
});
34+
}
35+
36+
function parseAnswers(questions, answers) {
37+
var saved = false;
38+
angular.forEach(answers, function(answerObject) {
39+
var questionId = answerObject.scorecardQuestionId;
40+
41+
questions[questionId].answer = answerObject.answer;
42+
questions[questionId].reviewItemId = answerObject.id;
43+
44+
if (answerObject.answer !== '') {
45+
saved = true;
46+
}
47+
});
48+
49+
return saved;
50+
}
51+
52+
function compileReviewItems(questions, review, updating) {
53+
var reviewItems = [];
54+
55+
for (var qId in questions) {
56+
var q = questions[qId];
57+
58+
var reviewItem = {
59+
reviewId: review.id,
60+
scorecardQuestionId: parseInt(qId),
61+
uploadId: review.uploadId,
62+
answer: '' + q.answer
63+
};
64+
65+
if (updating) {
66+
reviewItem.id = q.reviewItemId;
67+
}
68+
reviewItems.push(reviewItem);
69+
}
70+
return reviewItems;
71+
}
72+
73+
function countCompleted(reviews) {
74+
return reviews.reduce(function(numCompleted, review) {
75+
if (review.committed === 1) {
76+
return numCompleted + 1;
77+
}
78+
return numCompleted;
79+
}, 0);
80+
}
81+
82+
function getParameterByName(name, url) {
83+
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
84+
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
85+
var results = regex.exec(url);
86+
if (results === null) {
87+
results = '';
88+
} else {
89+
results = $window.decodeURIComponent(results[1].replace(/\+/g, ' '));
90+
}
91+
return results;
92+
}
93+
}
94+
})();

‎app/services/profile.service.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('topcoder').factory('profile', profile);
5+
6+
profile.$inject = ['CONSTANTS', 'ApiService', 'UserService'];
7+
8+
function profile() {
9+
var service = {
10+
getUserProfile: getUserProfile
11+
};
12+
return service;
13+
14+
///////////////
15+
16+
function getUserProfile() {
17+
UserService.getUsername()
18+
.then(function(response) {
19+
ApiService.requestHandler('GET', CONSTANTS.API_URL_V2 + '/users/' + response.data.handle);
20+
});
21+
}
22+
}
23+
24+
})();

‎app/services/user.service.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(function() {
2+
'use strict';
3+
4+
angular.module('topcoder').factory('user', user);
5+
6+
user.$inject = ['CONSTANTS', 'ApiService', 'jwtHelper', 'AuthToken'];
7+
8+
function user() {
9+
var service = {
10+
getUsername: getUsername
11+
};
12+
return service;
13+
14+
///////////////
15+
16+
function getUsername() {
17+
url = CONSTANTS.API_URL_V2 + '/user/identity';
18+
ApiService.requestHandler('GET', url);
19+
}
20+
}
21+
22+
})();

0 commit comments

Comments
 (0)
This repository has been archived.