1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 12:49:43 +02:00
planka/server/api/helpers/labels/update-one.js
Maksim Eltyshev a741e26ccb feat: Labels reordering
Closes #289
2023-01-09 12:17:06 +01:00

74 lines
1.5 KiB
JavaScript

const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
return true;
};
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
values: {
type: 'json',
custom: valuesValidator,
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
const { values } = inputs;
if (!_.isUndefined(values.position)) {
const labels = await sails.helpers.boards.getLabels(inputs.record.boardId, inputs.record.id);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
values.position,
labels,
);
values.position = position;
repositions.forEach(async ({ id, position: nextPosition }) => {
await Label.update({
id,
boardId: inputs.record.boardId,
}).set({
position: nextPosition,
});
sails.sockets.broadcast(`board:${inputs.record.boardId}`, 'labelUpdate', {
item: {
id,
position: nextPosition,
},
});
});
}
const label = await Label.updateOne(inputs.record.id).set({ ...values });
if (label) {
sails.sockets.broadcast(
`board:${label.boardId}`,
'labelUpdate',
{
item: label,
},
inputs.request,
);
}
return label;
},
};