2022-12-26 21:10:50 +01:00
|
|
|
const valuesValidator = (value) => {
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isPlainObject(value.card)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isPlainObject(value.user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-09-04 15:33:43 +02:00
|
|
|
const buildAndSendMessage = async (card, action, actorUser, send) => {
|
2024-04-08 00:33:29 +02:00
|
|
|
const cardLink = `<${sails.config.custom.baseUrl}/cards/${card.id}|${card.name}>`;
|
|
|
|
|
|
|
|
let markdown;
|
|
|
|
switch (action.type) {
|
|
|
|
case Action.Types.CREATE_CARD:
|
2024-06-12 00:51:36 +02:00
|
|
|
markdown = `${cardLink} was created by ${actorUser.name} in *${action.data.list.name}*`;
|
2024-04-08 00:33:29 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
case Action.Types.MOVE_CARD:
|
2024-06-12 00:51:36 +02:00
|
|
|
markdown = `${cardLink} was moved by ${actorUser.name} to *${action.data.toList.name}*`;
|
2024-04-08 00:33:29 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
case Action.Types.COMMENT_CARD:
|
2024-06-12 00:51:36 +02:00
|
|
|
markdown = `*${actorUser.name}* commented on ${cardLink}:\n>${action.data.text}`;
|
2024-04-08 00:33:29 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-04 15:33:43 +02:00
|
|
|
await send(markdown);
|
2024-04-08 00:33:29 +02:00
|
|
|
};
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
values: {
|
|
|
|
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,
|
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
list: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
|
|
|
},
|
2021-06-24 01:05:22 +05:00
|
|
|
request: {
|
|
|
|
type: 'ref',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
async fn(inputs) {
|
2022-12-26 21:10:50 +01:00
|
|
|
const { values } = inputs;
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const action = await Action.create({
|
2022-12-26 21:10:50 +01:00
|
|
|
...values,
|
|
|
|
cardId: values.card.id,
|
|
|
|
userId: values.user.id,
|
2021-06-24 01:05:22 +05:00
|
|
|
}).fetch();
|
|
|
|
|
|
|
|
sails.sockets.broadcast(
|
2022-12-26 21:10:50 +01:00
|
|
|
`board:${values.card.boardId}`,
|
2021-06-24 01:05:22 +05:00
|
|
|
'actionCreate',
|
|
|
|
{
|
|
|
|
item: action,
|
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
sails.helpers.utils.sendWebhooks.with({
|
|
|
|
event: 'actionCreate',
|
|
|
|
data: {
|
|
|
|
item: action,
|
|
|
|
included: {
|
|
|
|
projects: [inputs.project],
|
|
|
|
boards: [inputs.board],
|
|
|
|
lists: [inputs.list],
|
|
|
|
cards: [values.card],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
user: values.user,
|
|
|
|
});
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const subscriptionUserIds = await sails.helpers.cards.getSubscriptionUserIds(
|
|
|
|
action.cardId,
|
|
|
|
action.userId,
|
|
|
|
);
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
await Promise.all(
|
|
|
|
subscriptionUserIds.map(async (userId) =>
|
|
|
|
sails.helpers.notifications.createOne.with({
|
|
|
|
values: {
|
|
|
|
userId,
|
|
|
|
action,
|
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
project: inputs.project,
|
2024-03-22 00:14:09 +01:00
|
|
|
board: inputs.board,
|
2024-06-12 00:51:36 +02:00
|
|
|
list: inputs.list,
|
2024-03-22 00:14:09 +01:00
|
|
|
card: values.card,
|
2024-06-12 00:51:36 +02:00
|
|
|
actorUser: values.user,
|
2022-12-26 21:10:50 +01:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
2021-06-24 01:05:22 +05:00
|
|
|
|
2024-09-04 15:33:43 +02:00
|
|
|
if (sails.config.custom.slackBotToken) {
|
|
|
|
buildAndSendMessage(values.card, action, values.user, sails.helpers.utils.sendSlackMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sails.config.custom.googleChatWebhookUrl) {
|
|
|
|
buildAndSendMessage(
|
|
|
|
values.card,
|
|
|
|
action,
|
|
|
|
values.user,
|
|
|
|
sails.helpers.utils.sendGoogleChatMessage,
|
|
|
|
);
|
|
|
|
}
|
2021-06-24 01:05:22 +05:00
|
|
|
return action;
|
|
|
|
},
|
|
|
|
};
|