mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-08-03 12:40:00 +00:00
Added tags update route. Added sort to user service (#7381)
* Added tags update route. Added sort to user service * Change update tasks route to reorder tasks * Fixed linting issue * Changed params for reorder tags route * Fixed not found tag and added test
This commit is contained in:
parent
81a30d1f20
commit
e3c79fbdfa
7 changed files with 99 additions and 2 deletions
|
|
@ -73,6 +73,7 @@
|
|||
"clearTags": "Clear",
|
||||
"hideTags": "Hide",
|
||||
"showTags": "Show",
|
||||
"toRequired": "You must supply a to value",
|
||||
"startDate": "Start Date",
|
||||
"startDateHelpTitle": "When should this task start?",
|
||||
"startDateHelp": "Set the date for which this task takes effect. Will not be due on earlier days.",
|
||||
|
|
|
|||
|
|
@ -7,7 +7,10 @@ module.exports = function sortTag (user, req = {}) {
|
|||
let to = _.get(req, 'query.to');
|
||||
let fromParam = _.get(req, 'query.from');
|
||||
|
||||
if (!to || !fromParam) {
|
||||
let invalidTo = !to && to !== 0;
|
||||
let invalidFrom = !fromParam && fromParam !== 0;
|
||||
|
||||
if (invalidTo || invalidFrom) {
|
||||
throw new BadRequest('?to=__&from=__ are required');
|
||||
}
|
||||
|
||||
|
|
|
|||
44
test/api/v3/integration/tags/POST-tag-reorder.test.js
Normal file
44
test/api/v3/integration/tags/POST-tag-reorder.test.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import {
|
||||
generateUser,
|
||||
translate as t,
|
||||
} from '../../../../helpers/api-integration/v3';
|
||||
|
||||
describe('POST /reorder-tags', () => {
|
||||
let user;
|
||||
|
||||
before(async () => {
|
||||
user = await generateUser();
|
||||
});
|
||||
|
||||
it('returns error when no parameters are provided', async () => {
|
||||
await expect(user.post('/reorder-tags'))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 400,
|
||||
error: 'BadRequest',
|
||||
message: 'Invalid request parameters.',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when tag is not found', async () => {
|
||||
await expect(user.post('/reorder-tags', {tagId: 'fake-id', to: 3}))
|
||||
.to.eventually.be.rejected.and.eql({
|
||||
code: 404,
|
||||
error: 'NotFound',
|
||||
message: t('tagNotFound'),
|
||||
});
|
||||
});
|
||||
|
||||
it('updates tags', async () => {
|
||||
let tag1Name = 'Tag 1';
|
||||
let tag2Name = 'Tag 2';
|
||||
await user.post('/tags', {name: tag1Name});
|
||||
await user.post('/tags', {name: tag2Name});
|
||||
await user.sync();
|
||||
|
||||
await user.post('/reorder-tags', {tagId: user.tags[4].id, to: 3});
|
||||
await user.sync();
|
||||
|
||||
expect(user.tags[3].name).to.equal(tag2Name);
|
||||
expect(user.tags[4].name).to.equal(tag1Name);
|
||||
});
|
||||
});
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
User.sortTag({
|
||||
query: {
|
||||
from: ui.item.data('startIndex'),
|
||||
to:ui.item.index()
|
||||
to: ui.item.index()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,14 @@ angular.module('habitrpg')
|
|||
});
|
||||
};
|
||||
|
||||
function sortTag (tagId, to) {
|
||||
return $http({
|
||||
method: 'POST',
|
||||
url: 'api/v3/reorder-tags',
|
||||
data: {tagId: tagId, to: to},
|
||||
});
|
||||
};
|
||||
|
||||
function deleteTag (tagId) {
|
||||
return $http({
|
||||
method: 'DELETE',
|
||||
|
|
@ -46,6 +54,7 @@ angular.module('habitrpg')
|
|||
createTag: createTag,
|
||||
getTag: getTag,
|
||||
updateTag: updateTag,
|
||||
sortTag: sortTag,
|
||||
deleteTag: deleteTag,
|
||||
};
|
||||
}]);
|
||||
|
|
|
|||
|
|
@ -267,6 +267,11 @@ angular.module('habitrpg')
|
|||
Tags.updateTag(data.params.id, data.body);
|
||||
},
|
||||
|
||||
sortTag: function (data) {
|
||||
user.ops.sortTag(data);
|
||||
Tags.sortTag(user.tags[data.query.from].id, data.query.to);
|
||||
},
|
||||
|
||||
deleteTag: function(data) {
|
||||
user.ops.deleteTag(data);
|
||||
save();
|
||||
|
|
|
|||
|
|
@ -113,6 +113,41 @@ api.updateTag = {
|
|||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /api/v3/reorder-tags Reorder a tag
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName ReorderTags
|
||||
* @apiGroup Tag
|
||||
*
|
||||
* @apiParam {tagId} UUID Id of the tag to move
|
||||
* @apiParam {to} number Position the tag is moving to
|
||||
*
|
||||
* @apiSuccess {object} data An empty object
|
||||
*/
|
||||
api.reorderTags = {
|
||||
method: 'POST',
|
||||
url: '/reorder-tags',
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
|
||||
req.checkBody('to', res.t('toRequired')).notEmpty();
|
||||
req.checkBody('tagId', res.t('tagIdRequired')).notEmpty();
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
let tagIndex = _.findIndex(user.tags, function findTag (tag) {
|
||||
return tag.id === req.body.tagId;
|
||||
});
|
||||
if (tagIndex === -1) throw new NotFound(res.t('tagNotFound'));
|
||||
user.tags.splice(req.body.to, 0, user.tags.splice(tagIndex, 1)[0]);
|
||||
|
||||
await user.save();
|
||||
res.respond(200, {});
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {delete} /api/v3/tag/:tagId Delete a user tag given its id
|
||||
* @apiVersion 3.0.0
|
||||
|
|
|
|||
Loading…
Reference in a new issue