1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00
planka/server/api/helpers/lists/create-one.js

95 lines
1.7 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.board)) {
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,
},
project: {
type: 'ref',
required: true,
},
actorUser: {
type: 'ref',
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 lists = await sails.helpers.boards.getLists(values.board.id);
2019-08-31 04:07:25 +05:00
const { position, repositions } = sails.helpers.utils.insertToPositionables(
2022-12-26 21:10:50 +01:00
values.position,
lists,
2019-08-31 04:07:25 +05:00
);
repositions.forEach(async ({ id, position: nextPosition }) => {
2019-08-31 04:07:25 +05:00
await List.update({
id,
2022-12-26 21:10:50 +01:00
boardId: values.board.id,
2019-08-31 04:07:25 +05:00
}).set({
position: nextPosition,
2019-08-31 04:07:25 +05:00
});
2022-12-26 21:10:50 +01:00
sails.sockets.broadcast(`board:${values.board.id}`, 'listUpdate', {
2019-08-31 04:07:25 +05:00
item: {
id,
position: nextPosition,
},
2019-08-31 04:07:25 +05:00
});
// TODO: send webhooks
2019-08-31 04:07:25 +05:00
});
const list = await List.create({
2022-12-26 21:10:50 +01:00
...values,
2019-08-31 04:07:25 +05:00
position,
2022-12-26 21:10:50 +01:00
boardId: values.board.id,
2019-08-31 04:07:25 +05:00
}).fetch();
sails.sockets.broadcast(
`board:${list.boardId}`,
'listCreate',
{
item: list,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
sails.helpers.utils.sendWebhooks.with({
event: 'listCreate',
data: {
item: list,
included: {
projects: [inputs.project],
boards: [values.board],
},
},
user: inputs.actorUser,
});
return list;
},
2019-08-31 04:07:25 +05:00
};