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/cards/create-one.js

141 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
2023-01-08 22:10:41 +01:00
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
2022-12-26 21:10:50 +01:00
return false;
}
if (!_.isPlainObject(value.list)) {
return false;
}
if (!_.isPlainObject(value.creatorUser)) {
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,
},
board: {
type: 'ref',
required: true,
},
2019-08-31 04:07:25 +05:00
request: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
},
exits: {
positionMustBeInValues: {},
},
async fn(inputs) {
const { values } = inputs;
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
2022-12-26 21:10:50 +01:00
const cards = await sails.helpers.lists.getCards(values.list.id);
2022-12-26 21:10:50 +01:00
const { position, repositions } = sails.helpers.utils.insertToPositionables(
values.position,
cards,
);
2022-12-26 21:10:50 +01:00
repositions.forEach(async ({ id, position: nextPosition }) => {
await Card.update({
id,
listId: values.list.id,
}).set({
position: nextPosition,
});
2022-12-26 21:10:50 +01:00
sails.sockets.broadcast(`board:${values.list.boardId}`, 'cardUpdate', {
item: {
2019-08-31 04:07:25 +05:00
id,
position: nextPosition,
2022-12-26 21:10:50 +01:00
},
2019-08-31 04:07:25 +05:00
});
// TODO: send webhooks
2022-12-26 21:10:50 +01:00
});
const card = await Card.create({
...values,
2022-12-26 21:10:50 +01:00
position,
boardId: values.list.boardId,
listId: values.list.id,
creatorUserId: values.creatorUser.id,
}).fetch();
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardCreate',
{
item: card,
},
inputs.request,
);
2019-08-31 04:07:25 +05:00
sails.helpers.utils.sendWebhooks.with({
event: 'cardCreate',
data: {
item: card,
included: {
projects: [inputs.project],
boards: [inputs.board],
lists: [values.list],
},
},
user: values.creatorUser,
});
2022-12-26 21:10:50 +01:00
if (values.creatorUser.subscribeToOwnCards) {
await CardSubscription.create({
cardId: card.id,
2022-12-26 21:10:50 +01:00
userId: card.creatorUserId,
}).tolerate('E_UNIQUE');
2022-12-26 21:10:50 +01:00
sails.sockets.broadcast(`user:${card.creatorUserId}`, 'cardUpdate', {
item: {
id: card.id,
isSubscribed: true,
},
});
// TODO: send webhooks
}
2022-12-26 21:10:50 +01:00
await sails.helpers.actions.createOne.with({
values: {
card,
type: Action.Types.CREATE_CARD,
data: {
2022-12-26 21:10:50 +01:00
list: _.pick(values.list, ['id', 'name']),
},
2022-12-26 21:10:50 +01:00
user: values.creatorUser,
2019-08-31 04:07:25 +05:00
},
project: inputs.project,
board: inputs.board,
list: values.list,
request: inputs.request,
2022-12-26 21:10:50 +01:00
});
2019-08-31 04:07:25 +05:00
return card;
},
2019-08-31 04:07:25 +05:00
};