mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
change score task route
This commit is contained in:
@@ -4,8 +4,9 @@ import {
|
|||||||
translate as t,
|
translate as t,
|
||||||
} from '../../../../helpers/api-integration.helper';
|
} from '../../../../helpers/api-integration.helper';
|
||||||
import { v4 as generateUUID } from 'uuid';
|
import { v4 as generateUUID } from 'uuid';
|
||||||
|
import Q from 'q';
|
||||||
|
|
||||||
describe('POST /tasks/score/:id/:direction', () => {
|
describe('POST /tasks/:id/score/:direction', () => {
|
||||||
let user, api;
|
let user, api;
|
||||||
|
|
||||||
before(() => {
|
before(() => {
|
||||||
@@ -17,7 +18,7 @@ describe('POST /tasks/score/:id/:direction', () => {
|
|||||||
|
|
||||||
context('all', () => {
|
context('all', () => {
|
||||||
it('requires a task id', () => {
|
it('requires a task id', () => {
|
||||||
return expect(api.post('/tasks/score/123/up')).to.eventually.be.rejected.and.eql({
|
return expect(api.post('/tasks/123/score/up')).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
error: 'BadRequest',
|
error: 'BadRequest',
|
||||||
message: t('invalidReqParams'),
|
message: t('invalidReqParams'),
|
||||||
@@ -25,7 +26,7 @@ describe('POST /tasks/score/:id/:direction', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('requires a task direction', () => {
|
it('requires a task direction', () => {
|
||||||
return expect(api.post(`/tasks/score/${generateUUID()}/tt`)).to.eventually.be.rejected.and.eql({
|
return expect(api.post(`/tasks/${generateUUID()}/score/tt`)).to.eventually.be.rejected.and.eql({
|
||||||
code: 400,
|
code: 400,
|
||||||
error: 'BadRequest',
|
error: 'BadRequest',
|
||||||
message: t('invalidReqParams'),
|
message: t('invalidReqParams'),
|
||||||
@@ -37,7 +38,12 @@ describe('POST /tasks/score/:id/:direction', () => {
|
|||||||
let todo;
|
let todo;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// todo = createdTodo
|
return api.post('/tasks', {
|
||||||
|
text: 'test todo',
|
||||||
|
type: 'todo',
|
||||||
|
}).then((task) => {
|
||||||
|
todo = task;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('completes todo when direction is up');
|
it('completes todo when direction is up');
|
||||||
@@ -65,7 +71,12 @@ describe('POST /tasks/score/:id/:direction', () => {
|
|||||||
let daily;
|
let daily;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// daily = createdDaily
|
return api.post('/tasks', {
|
||||||
|
text: 'test daily',
|
||||||
|
type: 'daily',
|
||||||
|
}).then((task) => {
|
||||||
|
daily = task;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('completes daily when direction is up');
|
it('completes daily when direction is up');
|
||||||
@@ -93,10 +104,33 @@ describe('POST /tasks/score/:id/:direction', () => {
|
|||||||
let habit, minusHabit, plusHabit, neitherHabit;
|
let habit, minusHabit, plusHabit, neitherHabit;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// habit = createdHabit
|
return Q.all([
|
||||||
// plusHabit = createdPlusHabit
|
api.post('/tasks', {
|
||||||
// minusHabit = createdMinusHabit
|
text: 'test habit',
|
||||||
// neitherHabit = createdNeitherHabit
|
type: 'habit',
|
||||||
|
}),
|
||||||
|
api.post('/tasks', {
|
||||||
|
text: 'test min habit',
|
||||||
|
type: 'habit',
|
||||||
|
up: false,
|
||||||
|
}),
|
||||||
|
api.post('/tasks', {
|
||||||
|
text: 'test plus habit',
|
||||||
|
type: 'habit',
|
||||||
|
down: false,
|
||||||
|
}),
|
||||||
|
api.post('/tasks', {
|
||||||
|
text: 'test neither habit',
|
||||||
|
type: 'habit',
|
||||||
|
up: false,
|
||||||
|
down: false,
|
||||||
|
}),
|
||||||
|
]).then(tasks => {
|
||||||
|
habit = tasks[0];
|
||||||
|
minusHabit = tasks[1];
|
||||||
|
plusHabit = tasks[2];
|
||||||
|
neitherHabit = tasks[3];
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('prevents plus only habit from scoring down'); // Yes?
|
it('prevents plus only habit from scoring down'); // Yes?
|
||||||
@@ -120,7 +154,12 @@ describe('POST /tasks/score/:id/:direction', () => {
|
|||||||
let reward;
|
let reward;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// reward = createdReward
|
return api.post('/tasks', {
|
||||||
|
text: 'test reward',
|
||||||
|
type: 'reward',
|
||||||
|
}).then((task) => {
|
||||||
|
reward = task;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('purchases reward');
|
it('purchases reward');
|
||||||
@@ -223,7 +223,7 @@ function _generateWebhookTaskData (task, direction, delta, stats, user) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @api {put} /tasks/score/:taskId/:direction Score a task
|
* @api {put} /tasks/:taskId/score/:direction Score a task
|
||||||
* @apiVersion 3.0.0
|
* @apiVersion 3.0.0
|
||||||
* @apiName ScoreTask
|
* @apiName ScoreTask
|
||||||
* @apiGroup Task
|
* @apiGroup Task
|
||||||
@@ -235,11 +235,11 @@ function _generateWebhookTaskData (task, direction, delta, stats, user) {
|
|||||||
*/
|
*/
|
||||||
api.scoreTask = {
|
api.scoreTask = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/tasks/score/:taskId/:direction',
|
url: '/tasks/:taskId/score/:direction',
|
||||||
middlewares: [authWithHeaders()],
|
middlewares: [authWithHeaders()],
|
||||||
handler (req, res, next) {
|
handler (req, res, next) {
|
||||||
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
||||||
req.checkParams('direction', res.t('directionUpDown')).notEmpty().isIn(['up', 'down']);
|
req.checkParams('direction', res.t('directionUpDown')).notEmpty().isIn(['up', 'down']); // TODO what about rewards? maybe separate route?
|
||||||
|
|
||||||
let validationErrors = req.validationErrors();
|
let validationErrors = req.validationErrors();
|
||||||
if (validationErrors) return next(validationErrors);
|
if (validationErrors) return next(validationErrors);
|
||||||
@@ -255,7 +255,7 @@ api.scoreTask = {
|
|||||||
if (!task) throw new NotFound(res.t('taskNotFound'));
|
if (!task) throw new NotFound(res.t('taskNotFound'));
|
||||||
|
|
||||||
if (task.type === 'daily' || task.type === 'todo') {
|
if (task.type === 'daily' || task.type === 'todo') {
|
||||||
task.completed = direction === 'up';
|
task.completed = direction === 'up'; // TODO move into scoreTask
|
||||||
}
|
}
|
||||||
|
|
||||||
let delta = scoreTask({task, user, direction}, req);
|
let delta = scoreTask({task, user, direction}, req);
|
||||||
|
|||||||
Reference in New Issue
Block a user