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: {
|
2021-06-24 01:05:22 +05:00
|
|
|
type: 'ref',
|
2022-12-26 21:10:50 +01:00
|
|
|
custom: valuesValidator,
|
2021-06-24 01:05:22 +05:00
|
|
|
required: true,
|
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
project: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
|
|
|
},
|
2024-03-22 00:14:09 +01:00
|
|
|
board: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2020-08-04 01:32:46 +05:00
|
|
|
exits: {
|
|
|
|
positionMustBeInValues: {},
|
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
2020-08-04 01:32:46 +05:00
|
|
|
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';
|
|
|
|
}
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
const cards = await sails.helpers.lists.getCards(values.list.id);
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
const { position, repositions } = sails.helpers.utils.insertToPositionables(
|
|
|
|
values.position,
|
|
|
|
cards,
|
|
|
|
);
|
2020-08-04 01:32:46 +05:00
|
|
|
|
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,
|
|
|
|
});
|
2020-08-04 01:32:46 +05:00
|
|
|
|
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,
|
2019-11-05 18:01:42 +05:00
|
|
|
position: nextPosition,
|
2022-12-26 21:10:50 +01:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
2024-06-12 00:51:36 +02:00
|
|
|
|
|
|
|
// TODO: send webhooks
|
2022-12-26 21:10:50 +01:00
|
|
|
});
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2021-06-24 01:05:22 +05: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,
|
2021-06-24 01:05:22 +05:00
|
|
|
}).fetch();
|
|
|
|
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${card.boardId}`,
|
|
|
|
'cardCreate',
|
|
|
|
{
|
|
|
|
item: card,
|
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2024-06-12 00:51:36 +02: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) {
|
2020-04-10 00:11:34 +05:00
|
|
|
await CardSubscription.create({
|
|
|
|
cardId: card.id,
|
2022-12-26 21:10:50 +01:00
|
|
|
userId: card.creatorUserId,
|
2020-04-10 00:11:34 +05:00
|
|
|
}).tolerate('E_UNIQUE');
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
sails.sockets.broadcast(`user:${card.creatorUserId}`, 'cardUpdate', {
|
2021-06-24 01:05:22 +05:00
|
|
|
item: {
|
|
|
|
id: card.id,
|
|
|
|
isSubscribed: true,
|
|
|
|
},
|
|
|
|
});
|
2024-06-12 00:51:36 +02:00
|
|
|
|
|
|
|
// TODO: send webhooks
|
2020-04-10 00:11:34 +05:00
|
|
|
}
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
await sails.helpers.actions.createOne.with({
|
|
|
|
values: {
|
|
|
|
card,
|
2021-06-24 01:05:22 +05:00
|
|
|
type: Action.Types.CREATE_CARD,
|
|
|
|
data: {
|
2022-12-26 21:10:50 +01:00
|
|
|
list: _.pick(values.list, ['id', 'name']),
|
2020-05-09 05:30:52 +05:00
|
|
|
},
|
2022-12-26 21:10:50 +01:00
|
|
|
user: values.creatorUser,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
project: inputs.project,
|
2024-06-06 20:22:14 +02:00
|
|
|
board: inputs.board,
|
|
|
|
list: values.list,
|
2024-06-12 00:51:36 +02:00
|
|
|
request: inputs.request,
|
2022-12-26 21:10:50 +01:00
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return card;
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|