1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

Project managers, board members, auto-update after reconnection, refactoring

This commit is contained in:
Maksim Eltyshev 2021-06-24 01:05:22 +05:00
parent 7956503a46
commit fe91b5241e
478 changed files with 21226 additions and 19495 deletions

View file

@ -0,0 +1,128 @@
module.exports = {
inputs: {
values: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
return true;
},
required: true,
},
user: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
list: {
type: 'ref',
},
request: {
type: 'ref',
},
},
exits: {
listMustBePresent: {},
listMustBelongToBoard: {},
positionMustBeInValues: {},
},
async fn(inputs) {
const { values } = inputs;
if (inputs.board.type === Board.Types.KANBAN) {
if (!inputs.list) {
throw 'listMustBePresent';
}
if (inputs.list.boardId !== inputs.board.id) {
throw 'listMustBelongToBoard';
}
values.listId = inputs.list.id;
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
const cards = await sails.helpers.lists.getCards(inputs.list.id);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
inputs.values.position,
cards,
);
repositions.forEach(async ({ id, position: nextPosition }) => {
await Card.update({
id,
listId: inputs.list.id,
}).set({
position: nextPosition,
});
sails.sockets.broadcast(`board:${inputs.board.id}`, 'cardUpdate', {
item: {
id,
position: nextPosition,
},
});
});
values.position = position;
} else if (inputs.board.type === Board.Types.COLLECTION) {
delete values.position;
}
const card = await Card.create({
...values,
boardId: inputs.board.id,
creatorUserId: inputs.user.id,
}).fetch();
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardCreate',
{
item: card,
},
inputs.request,
);
if (inputs.user.subscribeToOwnCards) {
await CardSubscription.create({
cardId: card.id,
userId: inputs.user.id,
}).tolerate('E_UNIQUE');
sails.sockets.broadcast(`user:${inputs.user.id}`, 'cardUpdate', {
item: {
id: card.id,
isSubscribed: true,
},
});
}
await sails.helpers.actions.createOne(
{
type: Action.Types.CREATE_CARD,
data: {
list: _.pick(inputs.list, ['id', 'name']),
},
},
inputs.user,
card,
);
return card;
},
};

View file

@ -0,0 +1,28 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
const card = await Card.archiveOne(inputs.record.id);
if (card) {
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardDelete',
{
item: card,
},
inputs.request,
);
}
return card;
},
};

View file

@ -0,0 +1,28 @@
const LIMIT = 10;
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
beforeId: {
type: 'string',
},
},
async fn(inputs) {
const criteria = {
cardId: inputs.idOrIds,
};
if (!_.isUndefined(inputs.beforeId)) {
criteria.id = {
'<': inputs.beforeId,
};
}
return sails.helpers.actions.getMany(criteria, LIMIT);
},
};

View file

@ -0,0 +1,15 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
},
async fn(inputs) {
return sails.helpers.attachments.getMany({
cardId: inputs.idOrIds,
});
},
};

View file

@ -0,0 +1,15 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
},
async fn(inputs) {
return sails.helpers.cardLabels.getMany({
cardId: inputs.idOrIds,
});
},
};

View file

@ -0,0 +1,15 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
},
async fn(inputs) {
return sails.helpers.cardMemberships.getMany({
cardId: inputs.idOrIds,
});
},
};

View file

@ -0,0 +1,27 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
exceptUserIdOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
},
},
async fn(inputs) {
const criteria = {
cardId: inputs.idOrIds,
};
if (!_.isUndefined(inputs.exceptUserIdOrIds)) {
criteria.userId = {
'!=': inputs.exceptUserIdOrIds,
};
}
return sails.helpers.cardSubscriptions.getMany(criteria);
},
};

View file

@ -0,0 +1,15 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
},
async fn(inputs) {
const cardLabels = await sails.helpers.cards.getCardLabels(inputs.idOrIds);
return sails.helpers.utils.mapRecords(cardLabels, 'labelId', _.isArray(inputs.idOrIds));
},
};

View file

@ -0,0 +1,15 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
},
async fn(inputs) {
const labelIds = await sails.helpers.cards.getLabelIds(inputs.idOrIds);
return sails.helpers.labels.getMany(labelIds);
},
};

View file

@ -0,0 +1,19 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: (value) => _.isArray(value) || _.isPlainObject(value),
},
sort: {
type: 'json',
defaultsTo: 'id DESC',
},
limit: {
type: 'number',
},
},
async fn(inputs) {
return Card.find(inputs.criteria).sort(inputs.sort).limit(inputs.limit);
},
};

View file

@ -0,0 +1,46 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true,
},
},
exits: {
pathNotFound: {},
},
async fn(inputs) {
const card = await Card.findOne(inputs.criteria);
if (!card) {
throw 'pathNotFound';
}
let path;
if (card.listId) {
path = await sails.helpers.lists
.getProjectPath(card.listId)
.intercept('pathNotFound', (nodes) => ({
pathNotFound: {
card,
...nodes,
},
}));
} else {
path = await sails.helpers.boards
.getProjectPath(card.boardId)
.intercept('pathNotFound', (nodes) => ({
pathNotFound: {
card,
...nodes,
},
}));
}
return {
card,
...path,
};
},
};

View file

@ -0,0 +1,22 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
exceptUserIdOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
},
},
async fn(inputs) {
const cardSubscriptions = await sails.helpers.cards.getCardSubscriptions(
inputs.idOrIds,
inputs.exceptUserIdOrIds,
);
return sails.helpers.utils.mapRecords(cardSubscriptions, 'userId', _.isArray(inputs.idOrIds));
},
};

View file

@ -0,0 +1,15 @@
module.exports = {
inputs: {
idOrIds: {
type: 'json',
custom: (value) => _.isString(value) || _.every(value, _.isString),
required: true,
},
},
async fn(inputs) {
return sails.helpers.tasks.getMany({
cardId: inputs.idOrIds,
});
},
};

View file

@ -0,0 +1,281 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
values: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.position) && !_.isFinite(value.position)) {
return false;
}
return true;
},
defaultsTo: {},
},
nextBoard: {
type: 'ref',
},
nextList: {
type: 'ref',
},
user: {
type: 'ref',
},
board: {
type: 'ref',
},
list: {
type: 'ref',
},
request: {
type: 'ref',
},
},
exits: {
boardMustBePresent: {},
listMustBePresent: {},
nextListMustBelongToBoard: {},
nextListMustBePresent: {},
positionMustBeInValues: {},
userMustBePresent: {},
},
async fn(inputs) {
const { isSubscribed, ...values } = inputs.values;
if (inputs.nextBoard || inputs.nextList || !_.isUndefined(values.position)) {
if (!inputs.board) {
throw 'boardMustBePresent';
}
if (inputs.nextBoard) {
if (inputs.nextBoard.id === inputs.board.id) {
delete inputs.nextBoard; // eslint-disable-line no-param-reassign
} else {
values.boardId = inputs.nextBoard.id;
}
}
const board = inputs.nextBoard || inputs.board;
if (inputs.nextList) {
if (inputs.board.type === Board.Types.KANBAN && !inputs.list) {
throw 'listMustBePresent';
}
if (inputs.nextList.boardId !== board.id) {
throw 'nextListMustBelongToBoard';
}
if (
board.type === Board.Types.COLLECTION ||
(inputs.board.type === Board.Types.KANBAN && inputs.nextList.id === inputs.list.id)
) {
delete inputs.nextList; // eslint-disable-line no-param-reassign
} else {
values.listId = inputs.nextList.id;
}
}
if (inputs.nextList) {
if (_.isUndefined(values.position)) {
throw 'positionMustBeInValues';
}
} else if (inputs.nextBoard) {
if (inputs.nextBoard.type === Board.Types.KANBAN) {
throw 'nextListMustBePresent';
}
if (inputs.board.type === Board.Types.KANBAN) {
values.listId = null;
values.position = null;
}
}
}
if ((!_.isUndefined(isSubscribed) || inputs.nextBoard || inputs.nextList) && !inputs.user) {
throw 'userMustBePresent';
}
if (!_.isNil(values.position)) {
const boardId = values.boardId || inputs.record.boardId;
const listId = values.listId || inputs.record.listId;
const cards = await sails.helpers.lists.getCards(listId, inputs.record.id);
const { position, repositions } = sails.helpers.utils.insertToPositionables(
values.position,
cards,
);
repositions.forEach(async ({ id, position: nextPosition }) => {
await Card.update({
id,
listId,
}).set({
position: nextPosition,
});
sails.sockets.broadcast(`board:${boardId}`, 'cardUpdate', {
item: {
id,
position: nextPosition,
},
});
});
values.position = position;
}
let card;
if (!_.isEmpty(values)) {
let prevLabels;
if (inputs.nextBoard) {
if (inputs.nextBoard.projectId !== inputs.board.projectId) {
const memberUserIds = await sails.helpers.boards.getMemberUserIds(inputs.nextBoard.id);
await CardSubscription.destroy({
cardId: inputs.record.id,
userId: {
'!=': memberUserIds,
},
});
await CardMembership.destroy({
cardId: inputs.record.id,
userId: {
'!=': memberUserIds,
},
});
}
prevLabels = await sails.helpers.cards.getLabels(inputs.record.id);
await CardLabel.destroy({
cardId: inputs.record.id,
});
}
card = await Card.updateOne(inputs.record.id).set(values);
if (!card) {
return card;
}
if (inputs.nextBoard) {
const labels = await sails.helpers.boards.getLabels(card.boardId);
const labelByNameMap = _.keyBy(labels, 'name');
const labelIds = await Promise.all(
prevLabels.map(async (prevLabel) => {
if (labelByNameMap[prevLabel.name]) {
return labelByNameMap[prevLabel.name].id;
}
const { id } = await sails.helpers.labels.createOne(
_.omit(prevLabel, ['id', 'boardId']),
inputs.nextBoard,
);
return id;
}),
);
await Promise.all(
labelIds.map(async (labelId) =>
CardLabel.create({
labelId,
cardId: card.id,
})
.tolerate('E_UNIQUE')
.fetch(),
),
);
sails.sockets.broadcast(`board:${card.boardId}`, 'cardUpdate', {
item: card,
});
const subscriptionUserIds = await sails.helpers.cards.getSubscriptionUserIds(card.id);
subscriptionUserIds.forEach((userId) => {
sails.sockets.broadcast(`user:${userId}`, 'cardUpdate', {
item: {
id: card.id,
isSubscribed: true,
},
});
});
} else {
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardUpdate',
{
item: card,
},
inputs.request,
);
}
if (!inputs.nextBoard && inputs.nextList) {
// TODO: add transfer action
await sails.helpers.actions.createOne(
{
type: Action.Types.MOVE_CARD,
data: {
fromList: _.pick(inputs.list, ['id', 'name']),
toList: _.pick(inputs.nextList, ['id', 'name']),
},
},
inputs.user,
card,
);
}
} else {
card = inputs.record;
}
if (!_.isUndefined(isSubscribed)) {
const cardSubscription = await CardSubscription.findOne({
cardId: card.id,
userId: inputs.user.id,
});
if (isSubscribed !== !!cardSubscription) {
if (isSubscribed) {
await CardSubscription.create({
cardId: card.id,
userId: inputs.user.id,
}).tolerate('E_UNIQUE');
} else {
await CardSubscription.destroyOne({
cardId: card.id,
userId: inputs.user.id,
});
}
sails.sockets.broadcast(
`user:${inputs.user.id}`,
'cardUpdate',
{
item: {
isSubscribed,
id: card.id,
},
},
inputs.request,
);
}
}
return card;
},
};