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

140 lines
3.1 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
2022-12-26 21:10:50 +01:00
2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
values: {
type: 'ref',
required: true,
},
project: {
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
if (sails.helpers.lists.isFinite(values.list)) {
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
const cards = await Card.qm.getByListId(values.list.id);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
values.position,
cards,
);
values.position = position;
if (repositions.length > 0) {
// eslint-disable-next-line no-restricted-syntax
for (const reposition of repositions) {
// eslint-disable-next-line no-await-in-loop
await Card.qm.updateOne(
{
id: reposition.record.id,
listId: reposition.record.listId,
},
{
position: reposition.position,
},
);
sails.sockets.broadcast(`board:${values.board.id}`, 'cardUpdate', {
item: {
id: reposition.record.id,
position: reposition.position,
},
});
// TODO: send webhooks
}
2024-08-12 23:17:17 +02:00
}
} else {
delete values.position;
2024-08-12 23:17:17 +02:00
}
const card = await Card.qm.createOne({
...values,
boardId: values.board.id,
2022-12-26 21:10:50 +01:00
listId: values.list.id,
creatorUserId: values.creatorUser.id,
listChangedAt: new Date().toISOString(),
});
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',
buildData: () => ({
item: card,
included: {
projects: [inputs.project],
boards: [values.board],
lists: [values.list],
},
}),
user: values.creatorUser,
});
2022-12-26 21:10:50 +01:00
if (values.creatorUser.subscribeToOwnCards) {
try {
await CardSubscription.qm.createOne({
cardId: card.id,
userId: card.creatorUserId,
});
} catch (error) {
if (error.code !== 'E_UNIQUE') {
throw error;
}
}
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: {
2025-05-22 23:14:46 +02:00
card: _.pick(card, ['name']),
list: _.pick(values.list, ['id', 'type', 'name']),
},
2022-12-26 21:10:50 +01:00
user: values.creatorUser,
2019-08-31 04:07:25 +05:00
},
project: inputs.project,
board: values.board,
list: values.list,
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
};