1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/helpers/tasks/create-one.js

73 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isFinite(value.position)) {
return false;
}
if (!_.isPlainObject(value.card)) {
return false;
}
return true;
};
2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
values: {
type: 'ref',
2022-12-26 21:10:50 +01:00
custom: valuesValidator,
required: true,
},
2019-08-31 04:07:25 +05:00
request: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2022-12-26 21:10:50 +01:00
const { values } = inputs;
const tasks = await sails.helpers.cards.getTasks(values.card.id);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
2022-12-26 21:10:50 +01:00
values.position,
tasks,
);
repositions.forEach(async ({ id, position: nextPosition }) => {
await Task.update({
id,
2022-12-26 21:10:50 +01:00
cardId: values.card.id,
}).set({
position: nextPosition,
});
2022-12-26 21:10:50 +01:00
sails.sockets.broadcast(`board:${values.card.boardId}`, 'taskUpdate', {
item: {
id,
position: nextPosition,
},
});
});
2019-08-31 04:07:25 +05:00
const task = await Task.create({
2022-12-26 21:10:50 +01:00
...values,
position,
2022-12-26 21:10:50 +01:00
cardId: values.card.id,
2019-08-31 04:07:25 +05:00
}).fetch();
sails.sockets.broadcast(
2022-12-26 21:10:50 +01:00
`board:${values.card.boardId}`,
2019-08-31 04:07:25 +05:00
'taskCreate',
{
item: task,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
return task;
},
2019-08-31 04:07:25 +05:00
};