1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00
planka/server/api/helpers/create-card.js

127 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
board: {
2019-08-31 04:07:25 +05:00
type: 'ref',
required: true,
2019-08-31 04:07:25 +05:00
},
list: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
values: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
return true;
},
required: true,
2019-08-31 04:07:25 +05:00
},
user: {
type: 'ref',
required: true,
2019-08-31 04:07:25 +05:00
},
request: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
},
exits: {
listMustBePresent: {},
listMustBelongToBoard: {},
positionMustBeInValues: {},
},
async fn(inputs, exits) {
const { values } = inputs;
2019-08-31 04:07:25 +05:00
values.boardId = inputs.board.id;
2019-08-31 04:07:25 +05:00
if (inputs.board.type === 'kanban') {
if (!inputs.list) {
throw 'listMustBePresent';
}
if (inputs.list.boardId !== inputs.board.id) {
throw 'listMustBelongToBoard';
}
2019-08-31 04:07:25 +05:00
values.listId = inputs.list.id;
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
const cards = await sails.helpers.getCardsForList(inputs.list.id);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
cards,
);
repositions.forEach(async ({ id, position: nextPosition }) => {
await Card.update({
2019-08-31 04:07:25 +05:00
id,
listId: inputs.list.id,
}).set({
position: nextPosition,
});
sails.sockets.broadcast(`board:${inputs.board.id}`, 'cardUpdate', {
item: {
id,
position: nextPosition,
},
});
2019-08-31 04:07:25 +05:00
});
values.position = position;
} else if (inputs.board.type === 'collection') {
delete values.position;
}
const card = await Card.create(values).fetch();
2019-08-31 04:07:25 +05:00
if (inputs.user.subscribeToOwnCards) {
await CardSubscription.create({
cardId: card.id,
userId: inputs.user.id,
}).tolerate('E_UNIQUE');
card.isSubscribed = true;
} else {
card.isSubscribed = false;
}
// FIXME: broadcast subscription separately
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardCreate',
{
item: card,
included: {
cardMemberships: [],
cardLabels: [],
tasks: [],
attachments: [],
},
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
await sails.helpers.createAction(card, inputs.user, {
2019-08-31 04:07:25 +05:00
type: 'createCard',
data: {
list: _.pick(inputs.list, ['id', 'name']),
},
});
2019-08-31 04:07:25 +05:00
return exits.success(card);
},
2019-08-31 04:07:25 +05:00
};