2025-05-10 02:09:06 +02:00
|
|
|
/*!
|
|
|
|
* 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
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const { POSITION_GAP } = require('../../../constants');
|
2022-12-26 21:10:50 +01:00
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
record: {
|
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2020-08-04 01:32:46 +05:00
|
|
|
values: {
|
2025-05-10 02:09:06 +02:00
|
|
|
type: 'json',
|
2022-12-26 21:10:50 +01:00
|
|
|
required: true,
|
2020-05-09 05:30:52 +05:00
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
project: {
|
2019-08-31 04:07:25 +05:00
|
|
|
type: 'ref',
|
2024-06-12 00:51:36 +02:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2021-06-24 01:05:22 +05:00
|
|
|
board: {
|
|
|
|
type: 'ref',
|
2024-06-12 00:51:36 +02:00
|
|
|
required: true,
|
2021-06-24 01:05:22 +05:00
|
|
|
},
|
|
|
|
list: {
|
|
|
|
type: 'ref',
|
2024-06-12 00:51:36 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
actorUser: {
|
|
|
|
type: 'ref',
|
|
|
|
required: true,
|
2021-06-24 01:05:22 +05:00
|
|
|
},
|
2025-07-04 22:04:11 +02:00
|
|
|
webhooks: {
|
|
|
|
type: 'ref',
|
|
|
|
},
|
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-04-23 03:02:53 +05:00
|
|
|
exits: {
|
2020-08-04 01:32:46 +05:00
|
|
|
positionMustBeInValues: {},
|
2024-06-12 00:51:36 +02:00
|
|
|
boardInValuesMustBelongToProject: {},
|
2022-12-26 21:10:50 +01:00
|
|
|
listMustBeInValues: {},
|
|
|
|
listInValuesMustBelongToBoard: {},
|
2025-05-10 02:09:06 +02:00
|
|
|
coverAttachmentInValuesMustContainImage: {},
|
2020-04-23 03:02:53 +05:00
|
|
|
},
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
// TODO: use normalizeValues and refactor
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
2019-08-31 04:07:25 +05:00
|
|
|
const { isSubscribed, ...values } = inputs.values;
|
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
if (values.project && values.project.id === inputs.project.id) {
|
|
|
|
delete values.project;
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
const project = values.project || inputs.project;
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
if (values.board) {
|
|
|
|
if (values.board.projectId !== project.id) {
|
|
|
|
throw 'boardInValuesMustBelongToProject';
|
|
|
|
}
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
if (values.board.id === inputs.board.id) {
|
|
|
|
delete values.board;
|
|
|
|
} else {
|
|
|
|
values.boardId = values.board.id;
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
const board = values.board || inputs.board;
|
2020-08-04 01:32:46 +05:00
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
if (values.list) {
|
|
|
|
if (values.list.boardId !== board.id) {
|
|
|
|
throw 'listInValuesMustBelongToBoard';
|
2020-08-04 01:32:46 +05:00
|
|
|
}
|
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
if (values.list.id === inputs.list.id) {
|
|
|
|
delete values.list;
|
|
|
|
} else {
|
|
|
|
values.listId = values.list.id;
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
2024-06-12 00:51:36 +02:00
|
|
|
} else if (values.board) {
|
|
|
|
throw 'listMustBeInValues';
|
2020-04-23 03:02:53 +05:00
|
|
|
}
|
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
const list = values.list || inputs.list;
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (sails.helpers.lists.isFinite(list)) {
|
|
|
|
if (values.list && _.isUndefined(values.position)) {
|
|
|
|
throw 'positionMustBeInValues';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
values.position = null;
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (values.coverAttachment) {
|
|
|
|
if (!values.coverAttachment.data.image) {
|
|
|
|
throw 'coverAttachmentInValuesMustContainImage';
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
values.coverAttachmentId = values.coverAttachment.id;
|
|
|
|
}
|
2022-12-26 21:10:50 +01:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
let card;
|
|
|
|
if (_.isEmpty(values)) {
|
|
|
|
card = inputs.record;
|
|
|
|
} else {
|
2025-07-04 22:04:11 +02:00
|
|
|
const { webhooks = await Webhook.qm.getAll() } = inputs;
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (!_.isNil(values.position)) {
|
|
|
|
const cards = await Card.qm.getByListId(list.id, {
|
|
|
|
exceptIdOrIds: inputs.record.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const { position, repositions } = sails.helpers.utils.insertToPositionables(
|
|
|
|
values.position,
|
|
|
|
cards,
|
|
|
|
);
|
2024-06-12 00:51:36 +02:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
values.position = position;
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
);
|
2024-08-12 23:17:17 +02:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
sails.sockets.broadcast(`board:${board.id}`, 'cardUpdate', {
|
|
|
|
item: {
|
|
|
|
id: reposition.record.id,
|
|
|
|
position: reposition.position,
|
|
|
|
},
|
|
|
|
});
|
2024-08-12 23:17:17 +02:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
// TODO: send webhooks
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 23:17:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 05:30:52 +05:00
|
|
|
let prevLabels;
|
2022-12-26 21:10:50 +01:00
|
|
|
if (values.board) {
|
2025-05-10 02:09:06 +02:00
|
|
|
prevLabels = await sails.helpers.cards.getLabels(inputs.record.id);
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(values.board.id);
|
2020-05-09 05:30:52 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
await CardSubscription.qm.delete({
|
2022-12-26 21:10:50 +01:00
|
|
|
cardId: inputs.record.id,
|
|
|
|
userId: {
|
|
|
|
'!=': boardMemberUserIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
await CardMembership.qm.delete({
|
2022-12-26 21:10:50 +01:00
|
|
|
cardId: inputs.record.id,
|
|
|
|
userId: {
|
|
|
|
'!=': boardMemberUserIds,
|
|
|
|
},
|
|
|
|
});
|
2020-05-05 03:33:29 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
await CardLabel.qm.delete({
|
2020-05-05 03:33:29 +05:00
|
|
|
cardId: inputs.record.id,
|
|
|
|
});
|
2025-05-10 02:09:06 +02:00
|
|
|
|
|
|
|
const taskLists = await TaskList.qm.getByCardId(inputs.record.id);
|
|
|
|
const taskListIds = sails.helpers.utils.mapRecords(taskLists);
|
|
|
|
|
|
|
|
await Task.qm.update(
|
|
|
|
{
|
|
|
|
taskListId: taskListIds,
|
|
|
|
assigneeUserId: {
|
|
|
|
'!=': boardMemberUserIds,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
assigneeUserId: null,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const boardCustomFieldGroups = await CustomFieldGroup.qm.getByBoardId(inputs.board.id);
|
|
|
|
const boardCustomFieldGroupIds = sails.helpers.utils.mapRecords(boardCustomFieldGroups);
|
|
|
|
|
|
|
|
const boardCustomFields =
|
|
|
|
await CustomField.qm.getByCustomFieldGroupIds(boardCustomFieldGroupIds);
|
|
|
|
|
|
|
|
const cardCustomFieldGroups = await CustomFieldGroup.qm.getByCardId(inputs.record.id);
|
|
|
|
|
|
|
|
let basedCardCustomFieldGroups;
|
|
|
|
let basedCustomFieldGroups;
|
|
|
|
let baseCustomFieldGroupById;
|
|
|
|
let customFieldsByBaseCustomFieldGroupId;
|
|
|
|
|
|
|
|
if (values.project) {
|
|
|
|
const basedBoardCustomFieldGroups = boardCustomFieldGroups.filter(
|
|
|
|
({ baseCustomFieldGroupId }) => baseCustomFieldGroupId,
|
|
|
|
);
|
|
|
|
|
|
|
|
basedCardCustomFieldGroups = cardCustomFieldGroups.filter(
|
|
|
|
({ baseCustomFieldGroupId }) => baseCustomFieldGroupId,
|
|
|
|
);
|
|
|
|
|
|
|
|
basedCustomFieldGroups = [...basedBoardCustomFieldGroups, ...basedCardCustomFieldGroups];
|
|
|
|
|
|
|
|
const baseCustomFieldGroupIds = sails.helpers.utils.mapRecords(
|
|
|
|
basedCustomFieldGroups,
|
|
|
|
'baseCustomFieldGroupId',
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
const baseCustomFieldGroups =
|
|
|
|
await BaseCustomFieldGroup.qm.getByIds(baseCustomFieldGroupIds);
|
|
|
|
|
|
|
|
baseCustomFieldGroupById = _.keyBy(baseCustomFieldGroups, 'id');
|
|
|
|
|
|
|
|
const baseCustomFields = await CustomField.qm.getByBaseCustomFieldGroupIds(
|
|
|
|
Object.keys(baseCustomFieldGroupById),
|
|
|
|
);
|
|
|
|
|
|
|
|
customFieldsByBaseCustomFieldGroupId = _.groupBy(
|
|
|
|
baseCustomFields,
|
|
|
|
'baseCustomFieldGroupId',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let idsTotal = boardCustomFieldGroups.length + boardCustomFields.length;
|
|
|
|
|
|
|
|
if (values.project) {
|
|
|
|
idsTotal += basedCustomFieldGroups.reduce((result, customFieldGroup) => {
|
|
|
|
const customFieldsItem =
|
|
|
|
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
|
|
|
|
|
|
|
return result + (customFieldsItem ? customFieldsItem.length : 0);
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ids = await sails.helpers.utils.generateIds(idsTotal);
|
|
|
|
|
|
|
|
const nextCustomFieldGroupIdByCustomFieldGroupId = {};
|
|
|
|
const nextCustomFieldGroupsValues = boardCustomFieldGroups.map(
|
|
|
|
(customFieldGroup, index) => {
|
|
|
|
const id = ids.shift();
|
|
|
|
nextCustomFieldGroupIdByCustomFieldGroupId[customFieldGroup.id] = id;
|
|
|
|
|
|
|
|
const nextValues = {
|
|
|
|
..._.pick(customFieldGroup, ['baseCustomFieldGroupId', 'name']),
|
|
|
|
id,
|
|
|
|
cardId: inputs.record.id,
|
|
|
|
position: POSITION_GAP * (index + 1),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (values.project && customFieldGroup.baseCustomFieldGroupId) {
|
|
|
|
nextValues.baseCustomFieldGroupId = null;
|
|
|
|
|
|
|
|
if (!customFieldGroup.name) {
|
|
|
|
nextValues.name =
|
|
|
|
baseCustomFieldGroupById[customFieldGroup.baseCustomFieldGroupId].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextValues;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (nextCustomFieldGroupsValues.length > 0) {
|
|
|
|
const { position } = nextCustomFieldGroupsValues[nextCustomFieldGroupsValues.length - 1];
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
cardCustomFieldGroups.map((customFieldGroup) =>
|
|
|
|
CustomFieldGroup.qm.updateOne(customFieldGroup.id, {
|
|
|
|
position: customFieldGroup.position + position,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await CustomFieldGroup.qm.create(nextCustomFieldGroupsValues);
|
|
|
|
|
|
|
|
if (values.project) {
|
|
|
|
await CustomFieldGroup.qm.update(
|
|
|
|
{
|
|
|
|
cardId: inputs.record.id,
|
|
|
|
baseCustomFieldGroupId: {
|
|
|
|
'!=': null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
baseCustomFieldGroupId: null,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const unnamedCustomFieldGroups = basedCardCustomFieldGroups.filter(({ name }) => !name);
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
unnamedCustomFieldGroups.map((customFieldGroup) =>
|
|
|
|
CustomFieldGroup.qm.updateOne(customFieldGroup.id, {
|
|
|
|
name: baseCustomFieldGroupById[customFieldGroup.baseCustomFieldGroupId].name,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextCustomFieldIdByCustomFieldId = {};
|
|
|
|
const nextCustomFieldsValues = boardCustomFields.map((customField) => {
|
|
|
|
const id = ids.shift();
|
|
|
|
nextCustomFieldIdByCustomFieldId[customField.id] = id;
|
|
|
|
|
|
|
|
return {
|
|
|
|
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
|
|
|
id,
|
|
|
|
customFieldGroupId:
|
|
|
|
nextCustomFieldGroupIdByCustomFieldGroupId[customField.customFieldGroupId],
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
if (values.project) {
|
|
|
|
basedCustomFieldGroups.forEach((customFieldGroup) => {
|
|
|
|
const customFieldsItem =
|
|
|
|
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
|
|
|
|
|
|
|
if (!customFieldsItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
customFieldsItem.forEach((customField) => {
|
|
|
|
const id = ids.shift();
|
|
|
|
nextCustomFieldIdByCustomFieldId[`${customFieldGroup.id}:${customField.id}`] = id;
|
|
|
|
|
|
|
|
nextCustomFieldsValues.push({
|
|
|
|
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
|
|
|
id,
|
|
|
|
customFieldGroupId:
|
|
|
|
nextCustomFieldGroupIdByCustomFieldGroupId[customFieldGroup.id] ||
|
|
|
|
customFieldGroup.id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await CustomField.qm.create(nextCustomFieldsValues);
|
|
|
|
|
|
|
|
const customFieldGroupIds = boardCustomFieldGroupIds;
|
|
|
|
if (values.project) {
|
|
|
|
customFieldGroupIds.push(...sails.helpers.utils.mapRecords(basedCardCustomFieldGroups));
|
|
|
|
}
|
|
|
|
|
|
|
|
const customFieldValues = await CustomFieldValue.qm.getByCardId(inputs.record.id, {
|
|
|
|
customFieldGroupIdOrIds: customFieldGroupIds,
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
customFieldValues.map((customFieldValue) => {
|
|
|
|
const updateValues = {
|
|
|
|
customFieldGroupId:
|
|
|
|
nextCustomFieldGroupIdByCustomFieldGroupId[customFieldValue.customFieldGroupId],
|
|
|
|
};
|
|
|
|
|
|
|
|
const nextCustomFieldId =
|
|
|
|
nextCustomFieldIdByCustomFieldId[
|
|
|
|
`${customFieldValue.customFieldGroupId}:${customFieldValue.customFieldId}`
|
|
|
|
] || nextCustomFieldIdByCustomFieldId[customFieldValue.customFieldId];
|
|
|
|
|
|
|
|
if (nextCustomFieldId) {
|
|
|
|
updateValues.customFieldId = nextCustomFieldId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CustomFieldValue.qm.updateOne(customFieldValue.id, updateValues);
|
|
|
|
}),
|
|
|
|
);
|
2020-05-05 03:33:29 +05:00
|
|
|
}
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (values.list) {
|
|
|
|
if (values.board || inputs.list.type === List.Types.TRASH) {
|
|
|
|
values.prevListId = null;
|
|
|
|
} else if (sails.helpers.lists.isArchiveOrTrash(values.list)) {
|
|
|
|
values.prevListId = inputs.list.id;
|
|
|
|
} else if (inputs.list.type === List.Types.ARCHIVE) {
|
|
|
|
values.prevListId = null;
|
|
|
|
}
|
2025-07-09 17:45:47 +02:00
|
|
|
|
2025-07-14 14:54:06 +02:00
|
|
|
const typeState = List.TYPE_STATE_BY_TYPE[values.list.type];
|
|
|
|
|
2025-07-09 17:45:47 +02:00
|
|
|
if (inputs.record.isClosed) {
|
2025-07-14 14:54:06 +02:00
|
|
|
if (typeState === List.TypeStates.OPENED) {
|
2025-07-09 17:45:47 +02:00
|
|
|
values.isClosed = false;
|
|
|
|
}
|
2025-07-14 14:54:06 +02:00
|
|
|
} else if (typeState === List.TypeStates.CLOSED) {
|
2025-07-09 17:45:47 +02:00
|
|
|
values.isClosed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
values.listChangedAt = new Date().toISOString();
|
2025-05-10 02:09:06 +02:00
|
|
|
}
|
|
|
|
|
2025-07-11 01:04:02 +02:00
|
|
|
const updateResult = await Card.qm.updateOne(inputs.record.id, values);
|
|
|
|
|
|
|
|
({ card } = updateResult);
|
|
|
|
const { tasks } = updateResult;
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!card) {
|
2021-06-24 01:05:22 +05:00
|
|
|
return card;
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
if (values.board) {
|
2025-05-10 02:09:06 +02:00
|
|
|
const labels = await Label.qm.getByBoardId(card.boardId);
|
2022-12-26 21:10:50 +01:00
|
|
|
const labelByName = _.keyBy(labels, 'name');
|
2020-05-09 05:30:52 +05:00
|
|
|
|
|
|
|
const labelIds = await Promise.all(
|
2022-12-26 21:10:50 +01:00
|
|
|
prevLabels.map(async (label) => {
|
|
|
|
if (labelByName[label.name]) {
|
|
|
|
return labelByName[label.name].id;
|
2020-05-09 05:30:52 +05:00
|
|
|
}
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
const { id } = await sails.helpers.labels.createOne.with({
|
2024-06-12 00:51:36 +02:00
|
|
|
project,
|
2025-07-04 22:04:11 +02:00
|
|
|
webhooks,
|
2022-12-26 21:10:50 +01:00
|
|
|
values: {
|
2025-05-10 02:09:06 +02:00
|
|
|
..._.omit(label, ['id', 'boardId', 'createdAt', 'updatedAt']),
|
|
|
|
board,
|
2022-12-26 21:10:50 +01:00
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
actorUser: inputs.actorUser,
|
2022-12-26 21:10:50 +01:00
|
|
|
});
|
2020-05-09 05:30:52 +05:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-05-10 02:06:14 +05:00
|
|
|
await Promise.all(
|
2025-05-10 02:09:06 +02:00
|
|
|
labelIds.map((labelId) => {
|
|
|
|
try {
|
|
|
|
return CardLabel.qm.createOne({
|
|
|
|
labelId,
|
|
|
|
cardId: card.id,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'E_UNIQUE') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}),
|
2020-05-10 02:06:14 +05:00
|
|
|
);
|
2020-05-05 03:33:29 +05:00
|
|
|
|
2024-07-15 14:01:06 +02:00
|
|
|
sails.sockets.broadcast(
|
2025-05-10 02:09:06 +02:00
|
|
|
`board:${inputs.board.id}`,
|
|
|
|
'cardUpdate',
|
2024-07-15 14:01:06 +02:00
|
|
|
{
|
2025-05-10 02:09:06 +02:00
|
|
|
item: {
|
|
|
|
id: card.id,
|
|
|
|
boardId: null,
|
|
|
|
},
|
2024-07-15 14:01:06 +02:00
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
sails.sockets.broadcast(`board:${card.boardId}`, 'cardUpdate', {
|
2020-05-10 02:06:14 +05:00
|
|
|
item: card,
|
|
|
|
});
|
2020-05-09 05:30:52 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
// TODO: add transfer action
|
2020-05-09 05:30:52 +05:00
|
|
|
} else {
|
|
|
|
sails.sockets.broadcast(
|
|
|
|
`board:${card.boardId}`,
|
|
|
|
'cardUpdate',
|
|
|
|
{
|
|
|
|
item: card,
|
|
|
|
},
|
|
|
|
inputs.request,
|
|
|
|
);
|
2025-05-10 02:09:06 +02:00
|
|
|
|
|
|
|
if (values.list) {
|
|
|
|
await sails.helpers.actions.createOne.with({
|
2025-07-04 22:04:11 +02:00
|
|
|
webhooks,
|
2025-05-10 02:09:06 +02:00
|
|
|
values: {
|
|
|
|
card,
|
|
|
|
type: Action.Types.MOVE_CARD,
|
|
|
|
data: {
|
2025-05-22 23:14:46 +02:00
|
|
|
card: _.pick(card, ['name']),
|
2025-05-10 02:09:06 +02:00
|
|
|
fromList: _.pick(inputs.list, ['id', 'type', 'name']),
|
|
|
|
toList: _.pick(values.list, ['id', 'type', 'name']),
|
|
|
|
},
|
|
|
|
user: inputs.actorUser,
|
|
|
|
},
|
|
|
|
project: inputs.project,
|
|
|
|
board: inputs.board,
|
|
|
|
list: values.list,
|
|
|
|
});
|
|
|
|
}
|
2020-05-09 05:30:52 +05:00
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2025-07-11 01:04:02 +02:00
|
|
|
if (tasks) {
|
|
|
|
const taskListIds = sails.helpers.utils.mapRecords(tasks, 'taskListId', true);
|
|
|
|
const taskLists = await TaskList.qm.getByIds(taskListIds);
|
|
|
|
const taskListById = _.keyBy(taskLists, 'id');
|
|
|
|
|
|
|
|
const cardIds = sails.helpers.utils.mapRecords(taskLists, 'cardId', true);
|
|
|
|
const cards = await Card.qm.getByIds(cardIds);
|
|
|
|
const cardById = _.keyBy(cards, 'id');
|
|
|
|
|
|
|
|
const boardIdByTaskId = tasks.reduce(
|
|
|
|
(result, task) => ({
|
|
|
|
...result,
|
|
|
|
[task.id]: cardById[taskListById[task.taskListId].cardId].boardId,
|
|
|
|
}),
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
|
|
|
|
tasks.forEach((task) => {
|
|
|
|
sails.sockets.broadcast(`board:${boardIdByTaskId[task.id]}`, 'taskUpdate', {
|
|
|
|
item: task,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: send webhooks
|
|
|
|
}
|
|
|
|
|
2024-06-12 00:51:36 +02:00
|
|
|
sails.helpers.utils.sendWebhooks.with({
|
2025-07-04 22:04:11 +02:00
|
|
|
webhooks,
|
|
|
|
event: Webhook.Events.CARD_UPDATE,
|
2025-05-10 02:09:06 +02:00
|
|
|
buildData: () => ({
|
2024-06-12 00:51:36 +02:00
|
|
|
item: card,
|
|
|
|
included: {
|
|
|
|
projects: [project],
|
|
|
|
boards: [board],
|
|
|
|
lists: [list],
|
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
}),
|
|
|
|
buildPrevData: () => ({
|
2024-10-31 00:48:49 +01:00
|
|
|
item: inputs.record,
|
2025-05-10 02:09:06 +02:00
|
|
|
included: {
|
|
|
|
projects: [inputs.project],
|
|
|
|
boards: [inputs.board],
|
|
|
|
lists: [inputs.list],
|
|
|
|
},
|
|
|
|
}),
|
2024-06-12 00:51:36 +02:00
|
|
|
user: inputs.actorUser,
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(isSubscribed)) {
|
2025-05-10 02:09:06 +02:00
|
|
|
const wasSubscribed = await sails.helpers.users.isCardSubscriber(
|
2024-06-12 00:51:36 +02:00
|
|
|
inputs.actorUser.id,
|
|
|
|
card.id,
|
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (isSubscribed !== wasSubscribed) {
|
2019-08-31 04:07:25 +05:00
|
|
|
if (isSubscribed) {
|
2025-05-10 02:09:06 +02:00
|
|
|
try {
|
|
|
|
await CardSubscription.qm.createOne({
|
|
|
|
cardId: card.id,
|
|
|
|
userId: inputs.actorUser.id,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'E_UNIQUE') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
} else {
|
2025-05-10 02:09:06 +02:00
|
|
|
await CardSubscription.qm.deleteOne({
|
2019-08-31 04:07:25 +05:00
|
|
|
cardId: card.id,
|
2024-06-12 00:51:36 +02:00
|
|
|
userId: inputs.actorUser.id,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sails.sockets.broadcast(
|
2024-06-12 00:51:36 +02:00
|
|
|
`user:${inputs.actorUser.id}`,
|
2019-08-31 04:07:25 +05:00
|
|
|
'cardUpdate',
|
|
|
|
{
|
|
|
|
item: {
|
|
|
|
isSubscribed,
|
2019-11-05 18:01:42 +05:00
|
|
|
id: card.id,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
inputs.request,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
2024-06-12 00:51:36 +02:00
|
|
|
|
|
|
|
// TODO: send webhooks
|
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
|
|
|
};
|