mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-08-03 12:40:00 +00:00
Updated challenge service to user apiv3 and update challenge ctrl (#7111)
* Updated challenge service to user apiv3 and update challenge ctrl * Removed extra code. Added challenge update. Fixed group qurey
This commit is contained in:
parent
2619b34c65
commit
570d5c7fd9
4 changed files with 266 additions and 84 deletions
88
test/spec/services/challengeServicesSpec.js
Normal file
88
test/spec/services/challengeServicesSpec.js
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
'use strict';
|
||||
|
||||
describe('challengeServices', function() {
|
||||
var $httpBackend, $http, challenges, user;
|
||||
var apiV3Prefix = '/api/v3';
|
||||
|
||||
beforeEach(function() {
|
||||
module(function($provide) {
|
||||
$provide.value('User', {user:user});
|
||||
});
|
||||
|
||||
inject(function(_$httpBackend_, Challenges, User) {
|
||||
$httpBackend = _$httpBackend_;
|
||||
challenges = Challenges;
|
||||
user = User;
|
||||
user.sync = function(){};
|
||||
});
|
||||
});
|
||||
|
||||
it('calls create challenge endpoint', function() {
|
||||
$httpBackend.expectPOST(apiV3Prefix + '/challenges').respond({});
|
||||
challenges.createChallenge();
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls join challenge endpoint', function() {
|
||||
var challengeId = 1;
|
||||
$httpBackend.expectPOST(apiV3Prefix + '/challenges/' + challengeId + '/join').respond({});
|
||||
challenges.joinChallenge(challengeId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls leave challenge endpoint', function() {
|
||||
var challengeId = 1;
|
||||
$httpBackend.expectPOST(apiV3Prefix + '/challenges/' + challengeId + '/leave').respond({});
|
||||
challenges.leaveChallenge(challengeId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls get user challenges endpoint', function() {
|
||||
$httpBackend.expectGET(apiV3Prefix + '/challenges/user').respond({});
|
||||
challenges.getUserChallenges();
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls get group challenges endpoint', function() {
|
||||
var groupId = 1;
|
||||
$httpBackend.expectGET(apiV3Prefix + '/challenges/groups/' + groupId).respond({});
|
||||
challenges.getGroupChallenges(groupId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls get challenge endpoint', function() {
|
||||
var challengeId = 1;
|
||||
$httpBackend.expectGET(apiV3Prefix + '/challenges/' + challengeId).respond({});
|
||||
challenges.getChallenge(challengeId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls export challenge to csv endpoint', function() {
|
||||
var challengeId = 1;
|
||||
$httpBackend.expectGET(apiV3Prefix + '/challenges/' + challengeId + '/export/csv').respond({});
|
||||
challenges.exportChallengeCsv(challengeId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls update challenge endpoint', function() {
|
||||
var challengeId = 1;
|
||||
$httpBackend.expectPUT(apiV3Prefix + '/challenges/' + challengeId).respond({});
|
||||
challenges.updateChallenge(challengeId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls delete challenge endpoint', function() {
|
||||
var challengeId = 1;
|
||||
$httpBackend.expectDELETE(apiV3Prefix + '/challenges/' + challengeId).respond({});
|
||||
challenges.deleteChallenge(challengeId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
|
||||
it('calls select challenge winner endpoint', function() {
|
||||
var challengeId = 1;
|
||||
var winnerId = 2;
|
||||
$httpBackend.expectPOST(apiV3Prefix + '/challenges/' + challengeId + 'selectWinner/' + winnerId).respond({});
|
||||
challenges.selectChallengeWinner(challengeId, winnerId);
|
||||
$httpBackend.flush();
|
||||
});
|
||||
});
|
||||
|
|
@ -173,10 +173,12 @@ window.habitrpg = angular.module('habitrpg',
|
|||
templateUrl: 'partials/options.social.challenges.detail.html',
|
||||
title: env.t('titleChallenges'),
|
||||
controller: ['$scope', 'Challenges', '$stateParams',
|
||||
function($scope, Challenges, $stateParams){
|
||||
$scope.obj = $scope.challenge = Challenges.Challenge.get({cid:$stateParams.cid}, function(){
|
||||
$scope.challenge._locked = true;
|
||||
});
|
||||
function ($scope, Challenges, $stateParams) {
|
||||
Challenges.getChallenge($stateParams.cid)
|
||||
.then(function (response) {
|
||||
$scope.obj = $scope.challenge = response.data.data;
|
||||
$scope.challenge._locked = true;
|
||||
});
|
||||
}]
|
||||
})
|
||||
.state('options.social.challenges.edit', {
|
||||
|
|
@ -184,10 +186,12 @@ window.habitrpg = angular.module('habitrpg',
|
|||
templateUrl: 'partials/options.social.challenges.detail.html',
|
||||
title: env.t('titleChallenges'),
|
||||
controller: ['$scope', 'Challenges', '$stateParams',
|
||||
function($scope, Challenges, $stateParams){
|
||||
$scope.obj = $scope.challenge = Challenges.Challenge.get({cid:$stateParams.cid}, function(){
|
||||
$scope.challenge._locked = false;
|
||||
});
|
||||
function ($scope, Challenges, $stateParams) {
|
||||
Challenges.getChallenge($stateParams.cid)
|
||||
.then(function (response) {
|
||||
$scope.obj = $scope.challenge = response.data.data;
|
||||
$scope.challenge._locked = false;
|
||||
});
|
||||
}]
|
||||
})
|
||||
.state('options.social.challenges.detail.member', {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,11 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
_getChallenges();
|
||||
|
||||
// FIXME $scope.challenges needs to be resolved first (see app.js)
|
||||
$scope.groups = Groups.Group.query({type:'party,guilds,tavern'});
|
||||
$scope.groups = [];
|
||||
Groups.Group.getGroups('party,publicGuilds,privateGuilds,habitrpg')
|
||||
.then(function (response) {
|
||||
$scope.groups = response.data.data;
|
||||
});
|
||||
|
||||
// override score() for tasks listed in challenges-editing pages, so that nothing happens
|
||||
$scope.score = function(){}
|
||||
|
|
@ -53,7 +57,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
|
||||
if(!defaultGroup) defaultGroup = 'habitrpg';
|
||||
|
||||
$scope.obj = $scope.newChallenge = new Challenges.Challenge({
|
||||
$scope.obj = $scope.newChallenge = {
|
||||
name: '',
|
||||
description: '',
|
||||
habits: [],
|
||||
|
|
@ -65,7 +69,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
timestamp: +(new Date),
|
||||
members: [],
|
||||
official: false
|
||||
});
|
||||
};
|
||||
|
||||
_calculateMaxPrize(defaultGroup);
|
||||
};
|
||||
|
|
@ -82,10 +86,12 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
};
|
||||
|
||||
_(clonedTasks).each(function(val, type) {
|
||||
challenge[type + 's'].forEach(_cloneTaskAndPush);
|
||||
if (challenge[type + 's']) {
|
||||
challenge[type + 's'].forEach(_cloneTaskAndPush);
|
||||
}
|
||||
}).value();
|
||||
|
||||
$scope.obj = $scope.newChallenge = new Challenges.Challenge({
|
||||
$scope.obj = $scope.newChallenge = {
|
||||
name: challenge.name,
|
||||
shortName: challenge.shortName,
|
||||
description: challenge.description,
|
||||
|
|
@ -97,7 +103,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
group: challenge.group._id,
|
||||
official: challenge.official,
|
||||
prize: challenge.prize
|
||||
});
|
||||
};
|
||||
|
||||
function _cloneTaskAndPush(taskToClone) {
|
||||
var task = Tasks.cloneTask(taskToClone);
|
||||
|
|
@ -117,16 +123,25 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
return alert(window.env.t('challengeNotEnoughGems'));
|
||||
}
|
||||
|
||||
challenge.$save(function(_challenge){
|
||||
if (isNew) {
|
||||
Notification.text(window.env.t('challengeCreated'));
|
||||
User.sync();
|
||||
}
|
||||
|
||||
$state.transitionTo('options.social.challenges.detail', { cid: _challenge._id }, {
|
||||
reload: true, inherit: false, notify: true
|
||||
});
|
||||
});
|
||||
if (isNew) {
|
||||
Challenges.createChallenge(challenge)
|
||||
.then(function (response) {
|
||||
var _challenge = response.data.data;
|
||||
Notification.text(window.env.t('challengeCreated'));
|
||||
User.sync();
|
||||
$state.transitionTo('options.social.challenges.detail', { cid: _challenge._id }, {
|
||||
reload: true, inherit: false, notify: true
|
||||
});
|
||||
});
|
||||
} else {
|
||||
Challenges.updateChallenge(challenge)
|
||||
.then(function (response) {
|
||||
var _challenge = response.data.data;
|
||||
$state.transitionTo('options.social.challenges.detail', { cid: _challenge._id }, {
|
||||
reload: true, inherit: false, notify: true
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -136,7 +151,6 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
$scope.newChallenge = null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Close Challenge
|
||||
* ------------------
|
||||
|
|
@ -150,25 +164,31 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
|
||||
$scope["delete"] = function(challenge) {
|
||||
var warningMsg;
|
||||
|
||||
if(challenge.group._id == 'habitrpg') {
|
||||
warningMsg = window.env.t('sureDelChaTavern');
|
||||
} else {
|
||||
warningMsg = window.env.t('sureDelCha');
|
||||
}
|
||||
|
||||
if (!confirm(warningMsg)) return;
|
||||
challenge.$delete(function(){
|
||||
$scope.popoverEl.popover('destroy');
|
||||
_backToChallenges();
|
||||
});
|
||||
|
||||
Challenges.deleteChallenge(challenge._id)
|
||||
.then(function (response) {
|
||||
$scope.popoverEl.popover('destroy');
|
||||
_backToChallenges();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.selectWinner = function(challenge) {
|
||||
if (!challenge.winner) return;
|
||||
if (!confirm(window.env.t('youSure'))) return;
|
||||
challenge.$close({uid:challenge.winner}, function(){
|
||||
$scope.popoverEl.popover('destroy');
|
||||
_backToChallenges();
|
||||
})
|
||||
|
||||
Challenges.selectWinner(challenge._id, challenge.winner)
|
||||
.then(function (response) {
|
||||
$scope.popoverEl.popover('destroy');
|
||||
_backToChallenges();
|
||||
});
|
||||
}
|
||||
|
||||
$scope.close = function(challenge, $event) {
|
||||
|
|
@ -229,22 +249,21 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
--------------------------
|
||||
*/
|
||||
|
||||
$scope.join = function(challenge){
|
||||
challenge.$join(function(){
|
||||
_getChallenges()
|
||||
User.log({});
|
||||
});
|
||||
|
||||
$scope.join = function (challenge) {
|
||||
Challenges.joinChallenge(challenge._id)
|
||||
.then(function (response) {
|
||||
_getChallenges()
|
||||
});
|
||||
}
|
||||
|
||||
$scope.leave = function(keep) {
|
||||
if (keep == 'cancel') {
|
||||
$scope.selectedChal = undefined;
|
||||
} else {
|
||||
$scope.selectedChal.$leave({keep:keep}, function(){
|
||||
_getChallenges()
|
||||
User.log({});
|
||||
});
|
||||
Challenges.leaveChallenge(challenge._id, keep)
|
||||
.then(function (response) {
|
||||
_getChallenges()
|
||||
});
|
||||
}
|
||||
$scope.popoverEl.popover('destroy');
|
||||
}
|
||||
|
|
@ -316,21 +335,24 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
}
|
||||
|
||||
$scope.sendMessageToChallengeParticipant = function(uid) {
|
||||
Members.selectMember(uid, function(){
|
||||
$rootScope.openModal('private-message',{controller:'MemberModalCtrl'});
|
||||
});
|
||||
Members.selectMember(uid)
|
||||
.then(function () {
|
||||
$rootScope.openModal('private-message', {controller:'MemberModalCtrl'});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.sendGiftToChallengeParticipant = function(uid) {
|
||||
Members.selectMember(uid, function(){
|
||||
$rootScope.openModal('send-gift',{controller:'MemberModalCtrl'})
|
||||
});
|
||||
Members.selectMember(uid)
|
||||
.then(function () {
|
||||
$rootScope.openModal('send-gift', {controller:'MemberModalCtrl'});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.filterInitialChallenges = function() {
|
||||
$scope.groupsFilter = _.uniq(_.pluck($scope.challenges, 'group'), function(g){return g._id});
|
||||
$scope.groupsFilter = _.uniq(_.pluck($scope.challenges, 'group'), function(g) {return g._id});
|
||||
|
||||
$scope.search = {
|
||||
group: _.transform($scope.groups, function(m,g){m[g._id]=true;}),
|
||||
group: _.transform($scope.groups, function(m,g){ m[g._id] = true;}),
|
||||
_isMember: "either",
|
||||
_isOwner: "either"
|
||||
};
|
||||
|
|
@ -361,7 +383,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
return groupBalance;
|
||||
}
|
||||
|
||||
function _shouldShowChallenge(chal) {
|
||||
function _shouldShowChallenge (chal) {
|
||||
// Have to check that the leader object exists first in the
|
||||
// case where a challenge's leader deletes their account
|
||||
var userIsOwner = (chal.leader && chal.leader._id) === User.user.id;
|
||||
|
|
@ -377,24 +399,23 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||
$scope.popoverEl.popover('destroy');
|
||||
$scope.cid = null;
|
||||
$state.go('options.social.challenges');
|
||||
$scope.challenges = Challenges.Challenge.query();
|
||||
User.log({});
|
||||
_getChallenges();
|
||||
}
|
||||
|
||||
|
||||
// Fetch single challenge if a cid is present; fetch multiple challenges
|
||||
// otherwise
|
||||
function _getChallenges() {
|
||||
if ($scope.cid) {
|
||||
Challenges.Challenge.get({cid: $scope.cid}, function(challenge) {
|
||||
$scope.challenges = [challenge];
|
||||
});
|
||||
Challenges.getChallenge($scope.cid)
|
||||
.then(function (challenge) {
|
||||
$scope.challenges = [challenge];
|
||||
});
|
||||
} else {
|
||||
Challenges.Challenge.query(function(challenges){
|
||||
$scope.challenges = challenges;
|
||||
$scope.filterInitialChallenges();
|
||||
});
|
||||
Challenges.getUserChallenges()
|
||||
.then(function(response){
|
||||
$scope.challenges = response.data.data;
|
||||
$scope.filterInitialChallenges();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
}]);
|
||||
|
|
|
|||
|
|
@ -1,26 +1,95 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Services that persists and retrieves user from localStorage.
|
||||
*/
|
||||
angular.module('habitrpg')
|
||||
.factory('Challenges', ['ApiUrl', '$resource', '$http',
|
||||
function(ApiUrl, $resource, $http) {
|
||||
var apiV3Prefix = '/api/v3';
|
||||
|
||||
angular.module('habitrpg').factory('Challenges',
|
||||
['ApiUrl', '$resource',
|
||||
function(ApiUrl, $resource) {
|
||||
var Challenge = $resource(ApiUrl.get() + '/api/v2/challenges/:cid',
|
||||
{cid:'@_id'},
|
||||
{
|
||||
//'query': {method: "GET", isArray:false}
|
||||
join: {method: "POST", url: ApiUrl.get() + '/api/v2/challenges/:cid/join'},
|
||||
leave: {method: "POST", url: ApiUrl.get() + '/api/v2/challenges/:cid/leave'},
|
||||
close: {method: "POST", params: {uid:''}, url: ApiUrl.get() + '/api/v2/challenges/:cid/close'},
|
||||
getMember: {method: "GET", url: ApiUrl.get() + '/api/v2/challenges/:cid/member/:uid'}
|
||||
});
|
||||
function createChallenge (challengeData) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: apiV3Prefix + '/challenges',
|
||||
data: challengeData,
|
||||
});
|
||||
}
|
||||
|
||||
//var challenges = [];
|
||||
function joinChallenge (challengeId) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId + '/join',
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
Challenge: Challenge
|
||||
//challenges: challenges
|
||||
}
|
||||
}]);
|
||||
function leaveChallenge (challengeId, keep) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId + '/leave',
|
||||
data: {
|
||||
keep: keep,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getUserChallenges () {
|
||||
return $http({
|
||||
method: 'GET',
|
||||
url: apiV3Prefix + '/challenges/user',
|
||||
});
|
||||
}
|
||||
|
||||
function getGroupChallenges (groupId) {
|
||||
return $http({
|
||||
method: 'GET',
|
||||
url: apiV3Prefix + '/challenges/groups/' + groupId,
|
||||
});
|
||||
}
|
||||
|
||||
function getChallenge (challengeId) {
|
||||
return $http({
|
||||
method: 'GET',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId,
|
||||
});
|
||||
}
|
||||
|
||||
function exportChallengeCsv (challengeId) {
|
||||
return $http({
|
||||
method: 'GET',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId + '/export/csv',
|
||||
});
|
||||
}
|
||||
|
||||
function updateChallenge (challengeId, updateData) {
|
||||
return $http({
|
||||
method: 'PUT',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId,
|
||||
data: updateData,
|
||||
});
|
||||
}
|
||||
|
||||
function deleteChallenge (challengeId) {
|
||||
return $http({
|
||||
method: 'DELETE',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId,
|
||||
});
|
||||
}
|
||||
|
||||
function selectChallengeWinner (challengeId, winnerId) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: apiV3Prefix + '/challenges/' + challengeId + 'selectWinner/' + winnerId,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
createChallenge: createChallenge,
|
||||
joinChallenge: joinChallenge,
|
||||
leaveChallenge: leaveChallenge,
|
||||
getUserChallenges: getUserChallenges,
|
||||
getGroupChallenges: getGroupChallenges,
|
||||
getChallenge: getChallenge,
|
||||
exportChallengeCsv: exportChallengeCsv,
|
||||
updateChallenge: updateChallenge,
|
||||
deleteChallenge: deleteChallenge,
|
||||
selectChallengeWinner: selectChallengeWinner,
|
||||
}
|
||||
}]);
|
||||
|
|
|
|||
Loading…
Reference in a new issue