1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 15:49:46 +02:00

Initial commit

This commit is contained in:
Maksim Eltyshev 2019-08-31 04:07:25 +05:00
commit 5ffef61fe7
613 changed files with 91659 additions and 0 deletions

View file

View file

@ -0,0 +1,52 @@
module.exports = {
inputs: {
card: {
type: 'ref',
required: true
},
user: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
}
},
fn: async function(inputs, exits) {
const action = await Action.create({
...inputs.values,
cardId: inputs.card.id,
userId: inputs.user.id
}).fetch();
sails.sockets.broadcast(`board:${inputs.card.boardId}`, 'actionCreate', {
item: action
});
const userIds = await sails.helpers.getSubscriptionUserIdsForCard(
action.cardId,
action.userId
);
userIds.forEach(async userId => {
const notification = await Notification.create({
userId,
actionId: action.id,
cardId: action.cardId
}).fetch();
sails.sockets.broadcast(`user:${userId}`, 'notificationCreate', {
item: notification,
included: {
users: [inputs.user],
cards: [inputs.card],
actions: [action]
}
});
});
return exits.success(action);
}
};

View file

@ -0,0 +1,70 @@
module.exports = {
inputs: {
project: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value => _.isPlainObject(value) && _.isFinite(value.position),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const boards = await sails.helpers.getBoardsForProject(inputs.project.id);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
boards
);
const userIds = await sails.helpers.getMembershipUserIdsForProject(
inputs.project.id
);
repositions.forEach(async ({ id, position }) => {
await Board.update({
id,
projectId: inputs.project.id
}).set({
position
});
userIds.forEach(userId => {
sails.sockets.broadcast(`user:${userId}`, 'boardUpdate', {
item: {
id,
position
}
});
});
});
const board = await Board.create({
...inputs.values,
position,
projectId: inputs.project.id
}).fetch();
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'boardCreate',
{
item: board,
included: {
lists: [],
labels: []
}
},
inputs.request
);
});
return exits.success(board);
}
};

View file

@ -0,0 +1,35 @@
module.exports = {
inputs: {
card: {
type: 'ref',
required: true
},
label: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const cardLabel = await CardLabel.create({
cardId: inputs.card.id,
labelId: inputs.label.id
})
.intercept('E_UNIQUE', 'conflict')
.fetch();
sails.sockets.broadcast(
`board:${inputs.card.boardId}`,
'cardLabelCreate',
{
item: cardLabel
},
inputs.request
);
return exits.success(cardLabel);
}
};

View file

@ -0,0 +1,60 @@
module.exports = {
inputs: {
card: {
type: 'ref',
required: true
},
userOrUserId: {
type: 'ref',
custom: value => _.isPlainObject(value) || _.isFinite(value),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const { userId = inputs.userOrUserId } = inputs.userOrUserId;
const cardMembership = await CardMembership.create({
userId,
cardId: inputs.card.id
})
.intercept('E_UNIQUE', 'conflict')
.fetch();
sails.sockets.broadcast(
`board:${inputs.card.boardId}`,
'cardMembershipCreate',
{
item: cardMembership
},
inputs.request
);
const cardSubscription = await CardSubscription.create({
cardId: cardMembership.cardId,
userId: cardMembership.userId,
isPermanent: false
})
.tolerate('E_UNIQUE')
.fetch();
if (cardSubscription) {
sails.sockets.broadcast(
`user:${cardMembership.userId}`,
'cardUpdate',
{
item: {
id: cardMembership.cardId,
isSubscribed: true
}
},
inputs.request
);
}
return exits.success(cardMembership);
}
};

View file

@ -0,0 +1,72 @@
module.exports = {
inputs: {
list: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value => _.isPlainObject(value) && _.isFinite(value.position),
required: true
},
user: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const cards = await sails.helpers.getCardsForList(inputs.list.id);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
cards
);
repositions.forEach(async ({ id, position }) => {
await Card.update({
id,
listId: inputs.list.id
}).set({
position
});
sails.sockets.broadcast(`board:${list.boardId}`, 'cardUpdate', {
item: {
id,
position
}
});
});
const card = await Card.create({
...inputs.values,
position,
listId: inputs.list.id,
boardId: inputs.list.boardId
}).fetch();
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardCreate',
{
item: card
},
inputs.request
);
const values = {
type: 'createCard',
data: {
list: _.pick(inputs.list, ['id', 'name'])
}
};
await sails.helpers.createAction(card, inputs.user, values);
return exits.success(card);
}
};

View file

@ -0,0 +1,33 @@
module.exports = {
inputs: {
board: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const label = await Label.create({
...inputs.values,
boardId: inputs.board.id
}).fetch();
sails.sockets.broadcast(
`board:${label.boardId}`,
'labelCreate',
{
item: label
},
inputs.request
);
return exits.success(label);
}
};

View file

@ -0,0 +1,58 @@
module.exports = {
inputs: {
board: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value => _.isPlainObject(value) && _.isFinite(value.position),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const lists = await sails.helpers.getListsForBoard(inputs.board.id);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
lists
);
repositions.forEach(async ({ id, position }) => {
await List.update({
id,
boardId: inputs.board.id
}).set({
position
});
sails.sockets.broadcast(`board:${inputs.board.id}`, 'listUpdate', {
item: {
id,
position
}
});
});
const list = await List.create({
...inputs.values,
position,
boardId: inputs.board.id
}).fetch();
sails.sockets.broadcast(
`board:${list.boardId}`,
'listCreate',
{
item: list
},
inputs.request
);
return exits.success(list);
}
};

View file

@ -0,0 +1,73 @@
module.exports = {
inputs: {
project: {
type: 'ref',
required: true
},
user: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
exits: {
conflict: {}
},
fn: async function(inputs, exits) {
const projectMembership = await ProjectMembership.create({
projectId: inputs.project.id,
userId: inputs.user.id
})
.intercept('E_UNIQUE', 'conflict')
.fetch();
const {
userIds,
projectMemberships
} = await sails.helpers.getMembershipUserIdsForProject(
projectMembership.projectId,
true
);
userIds.forEach(userId => {
if (userId !== projectMembership.userId) {
sails.sockets.broadcast(
`user:${userId}`,
'projectMembershipCreate',
{
item: projectMembership,
included: {
users: [inputs.user]
}
},
inputs.request
);
}
});
const users = await sails.helpers.getUsers(userIds);
const boards = await sails.helpers.getBoardsForProject(
projectMembership.projectId
);
sails.sockets.broadcast(
`user:${projectMembership.userId}`,
'projectCreate',
{
item: inputs.project,
included: {
users,
projectMemberships,
boards
}
}
);
return exits.success(projectMembership);
}
};

View file

@ -0,0 +1,51 @@
module.exports = {
inputs: {
values: {
type: 'json',
required: true
},
user: {
type: 'ref',
required: true
},
request: {
type: 'ref'
},
withProjectMembership: {
type: 'boolean',
defaultsTo: false
}
},
fn: async function(inputs, exits) {
const project = await Project.create(inputs.values).fetch();
const projectMembership = await ProjectMembership.create({
projectId: project.id,
userId: inputs.user.id
}).fetch();
sails.sockets.broadcast(
`user:${projectMembership.userId}`,
'projectCreate',
{
item: project,
included: {
users: [inputs.user],
projectMemberships: [projectMembership],
boards: []
}
},
inputs.request
);
return exits.success(
inputs.withProjectMembership
? {
project,
projectMembership
}
: project
);
}
};

View file

@ -0,0 +1,33 @@
module.exports = {
inputs: {
card: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const task = await Task.create({
...inputs.values,
cardId: inputs.card.id
}).fetch();
sails.sockets.broadcast(
`board:${inputs.card.boardId}`,
'taskCreate',
{
item: task
},
inputs.request
);
return exits.success(task);
}
};

View file

@ -0,0 +1,46 @@
const bcrypt = require('bcrypt');
module.exports = {
inputs: {
values: {
type: 'json',
custom: value =>
_.isPlainObject(value) &&
_.isString(value.email) &&
_.isString(value.password),
required: true
},
request: {
type: 'ref'
}
},
exits: {
conflict: {}
},
fn: async function(inputs, exits) {
const user = await User.create({
...inputs.values,
email: inputs.values.email.toLowerCase(),
password: bcrypt.hashSync(inputs.values.password, 10)
})
.intercept(undefined, 'conflict')
.fetch();
const userIds = await sails.helpers.getAdminUserIds();
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'userCreate',
{
item: user
},
inputs.request
);
});
return exits.success(user);
}
};

View file

@ -0,0 +1,32 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
board: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const action = await Action.archiveOne(inputs.record.id);
if (action) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'actionDelete',
{
item: action
},
inputs.request
);
}
return exits.success(action);
}
};

View file

@ -0,0 +1,36 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const board = await Board.archiveOne(inputs.record.id);
if (board) {
sails.sockets.leaveAll(`board:${board.id}`);
const userIds = await sails.helpers.getMembershipUserIdsForProject(
board.projectId
);
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'boardDelete',
{
item: board
},
inputs.request
);
});
}
return exits.success(board);
}
};

View file

@ -0,0 +1,32 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
board: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const cardLabel = await CardLabel.destroyOne(inputs.record.id);
if (cardLabel) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'cardLabelDelete',
{
item: cardLabel
},
inputs.request
);
}
return exits.success(cardLabel);
}
};

View file

@ -0,0 +1,47 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
board: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const cardMembership = await CardMembership.destroyOne(inputs.record.id);
if (cardMembership) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'cardMembershipDelete',
{
item: cardMembership
},
inputs.request
);
const cardSubscription = await CardSubscription.destroyOne({
cardId: cardMembership.cardId,
userId: cardMembership.userId,
isPermanent: false
});
if (cardSubscription) {
sails.sockets.broadcast(`user:${cardMembership.userId}`, 'cardUpdate', {
item: {
id: cardMembership.cardId,
isSubscribed: false
}
});
}
}
return exits.success(cardMembership);
}
};

View file

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

View file

@ -0,0 +1,32 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
await CardLabel.destroy({
labelId: inputs.record.id
});
const label = await Label.archiveOne(inputs.record.id);
if (label) {
sails.sockets.broadcast(
`board:${label.boardId}`,
'labelDelete',
{
item: label
},
inputs.request
);
}
return exits.success(label);
}
};

View file

@ -0,0 +1,26 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const list = await List.archiveOne(inputs.record.id);
sails.sockets.broadcast(
`board:${list.boardId}`,
'listDelete',
{
item: list
},
inputs.request
);
return exits.success(list);
}
};

View file

@ -0,0 +1,70 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const boards = await sails.helpers.getBoardsForProject(
inputs.record.projectId
);
const boardIds = sails.helpers.mapRecords(boards);
const cards = await sails.helpers.getCardsForBoard(boardIds);
const cardIds = sails.helpers.mapRecords(cards);
await CardSubscription.destroy({
cardId: cardIds,
userId: inputs.record.userId
});
await CardMembership.destroy({
cardId: cardIds,
userId: inputs.record.userId
});
const projectMembership = await ProjectMembership.destroyOne(
inputs.record.id
);
if (projectMembership) {
const userIds = await sails.helpers.getMembershipUserIdsForProject(
projectMembership.projectId
);
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'projectMembershipDelete',
{
item: projectMembership
},
inputs.request
);
});
sails.sockets.removeRoomMembersFromRooms(
`user:${projectMembership.userId}`,
boardIds.map(boardId => `board:${boardId}`)
);
const project = await Project.findOne(projectMembership.projectId);
sails.sockets.broadcast(
`user:${projectMembership.userId}`,
'projectDelete',
{
item: project
}
);
}
return exits.success(projectMembership);
}
};

View file

@ -0,0 +1,41 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const projectMemberships = await ProjectMembership.destroy({
projectId: inputs.record.id
}).fetch();
const project = await Project.archiveOne(inputs.record.id);
if (project) {
const userIds = sails.helpers.mapRecords(projectMemberships, 'userId');
const boards = await sails.helpers.getBoardsForProject(project.id);
const boardRooms = boards.map(board => `board:${board.id}`);
userIds.forEach(userId => {
sails.sockets.removeRoomMembersFromRooms(`user:${userId}`, boardRooms);
sails.sockets.broadcast(
`user:${userId}`,
'projectDelete',
{
item: project
},
inputs.request
);
});
}
return exits.success(project);
}
};

View file

@ -0,0 +1,32 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
board: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const task = await Task.archiveOne(inputs.record.id);
if (task) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'taskDelete',
{
item: task
},
inputs.request
);
}
return exits.success(task);
}
};

View file

@ -0,0 +1,59 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
await ProjectMembership.destroy({
userId: inputs.record.id
});
await CardSubscription.destroy({
userId: inputs.record.id
});
await CardMembership.destroy({
userId: inputs.record.id
});
const user = await User.updateOne({
id: inputs.record.id,
deletedAt: null
}).set({
deletedAt: new Date().toUTCString()
});
if (user) {
const adminUserIds = await sails.helpers.getAdminUserIds();
const projectIds = await sails.helpers.getMembershipProjectIdsForUser(
user.id
);
const userIdsForProject = await sails.helpers.getMembershipUserIdsForProject(
projectIds
);
const userIds = _.union([user.id], adminUserIds, userIdsForProject);
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'userDelete',
{
item: user
},
inputs.request
);
});
}
return exits.success(user);
}
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true
}
},
exits: {
notFound: {}
},
fn: async function(inputs, exits) {
const action = await Action.findOne(inputs.criteria);
if (!action) {
throw 'notFound';
}
const path = await sails.helpers
.getCardToProjectPath(action.cardId)
.intercept('notFound', path => ({
notFound: {
action,
...path
}
}));
return exits.success({
action,
...path
});
}
};

View file

@ -0,0 +1,30 @@
const LIMIT = 10;
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
beforeId: {
type: 'number'
}
},
fn: async function(inputs, exits) {
const criteria = {
cardId: inputs.id
};
if (!_.isUndefined(inputs.beforeId)) {
criteria.id = {
'<': inputs.beforeId
};
}
const actions = await sails.helpers.getActions(criteria, LIMIT);
return exits.success(actions);
}
};

View file

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

View file

@ -0,0 +1,11 @@
module.exports = {
fn: async function(inputs, exits) {
const users = await sails.helpers.getUsers({
isAdmin: true
});
const userIds = sails.helpers.mapRecords(users);
return exits.success(userIds);
}
};

View file

@ -0,0 +1,35 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true
}
},
exits: {
notFound: {}
},
fn: async function(inputs, exits) {
const board = await Board.findOne(inputs.criteria);
if (!board) {
throw 'notFound';
}
const project = await Project.findOne(board.projectId);
if (!project) {
throw {
notFound: {
board
}
};
}
return exits.success({
board,
project
});
}
};

View file

@ -0,0 +1,29 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
exceptBoardId: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value)
}
},
fn: async function(inputs, exits) {
const criteria = {
projectId: inputs.id
};
if (!_.isUndefined(inputs.exceptBoardId)) {
criteria.id = {
'!=': inputs.exceptBoardId
};
}
const boards = await sails.helpers.getBoards(criteria);
return exits.success(boards);
}
};

View file

@ -0,0 +1,14 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const boards = await Board.find(inputs.criteria).sort('position');
return exits.success(boards);
}
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const cardLabels = await sails.helpers.getCardLabels({
cardId: inputs.id
});
return exits.success(cardLabels);
}
};

View file

@ -0,0 +1,14 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const cardLabels = await CardLabel.find(inputs.criteria).sort('id');
return exits.success(cardLabels);
}
};

View file

@ -0,0 +1,16 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const cardMemberships = await CardMembership.find(inputs.criteria).sort(
'id'
);
return exits.success(cardMemberships);
}
};

View file

@ -0,0 +1,16 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const cardSubscriptions = await CardSubscription.find(inputs.criteria).sort(
'id'
);
return exits.success(cardSubscriptions);
}
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true
}
},
exits: {
notFound: {}
},
fn: async function(inputs, exits) {
const card = await Card.findOne(inputs.criteria);
if (!card) {
throw 'notFound';
}
const path = await sails.helpers
.getListToProjectPath(card.listId)
.intercept('notFound', path => ({
notFound: {
card,
...path
}
}));
return exits.success({
card,
...path
});
}
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const cards = await sails.helpers.getCards({
boardId: inputs.id
});
return exits.success(cards);
}
};

View file

@ -0,0 +1,29 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
exceptCardId: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value)
}
},
fn: async function(inputs, exits) {
const criteria = {
listId: inputs.id
};
if (!_.isUndefined(inputs.exceptCardId)) {
criteria.id = {
'!=': inputs.exceptCardId
};
}
const cards = await sails.helpers.getCards(criteria);
return exits.success(cards);
}
};

View file

@ -0,0 +1,14 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const cards = await Card.find(inputs.criteria).sort('position');
return exits.success(cards);
}
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true
}
},
exits: {
notFound: {}
},
fn: async function(inputs, exits) {
const label = await Label.findOne(inputs.criteria);
if (!label) {
throw 'notFound';
}
const path = await sails.helpers
.getBoardToProjectPath(label.boardId)
.intercept('notFound', path => ({
notFound: {
label,
...path
}
}));
return exits.success({
label,
...path
});
}
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const labels = await Label.find({
boardId: inputs.id
}).sort('id');
return exits.success(labels);
}
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true
}
},
exits: {
notFound: {}
},
fn: async function(inputs, exits) {
const list = await List.findOne(inputs.criteria);
if (!list) {
throw 'notFound';
}
const path = await sails.helpers
.getBoardToProjectPath(list.boardId)
.intercept('notFound', path => ({
notFound: {
list,
...path
}
}));
return exits.success({
list,
...path
});
}
};

View file

@ -0,0 +1,29 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
exceptListId: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value)
}
},
fn: async function(inputs, exits) {
const criteria = {
boardId: inputs.id
};
if (!_.isUndefined(inputs.exceptListId)) {
criteria.id = {
'!=': inputs.exceptListId
};
}
const lists = await List.find(criteria).sort('position');
return exits.success(lists);
}
};

View file

@ -0,0 +1,23 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const projectMemberships = await sails.helpers.getProjectMembershipsForUser(
inputs.id
);
const projectIds = sails.helpers.mapRecords(
projectMemberships,
'projectId',
_.isArray(inputs.id)
);
return exits.success(projectIds);
}
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
withProjectMemberships: {
type: 'boolean',
defaultsTo: false
}
},
fn: async function(inputs, exits) {
const projectMemberships = await sails.helpers.getMembershipsForProject(
inputs.id
);
const userIds = sails.helpers.mapRecords(
projectMemberships,
'userId',
_.isArray(inputs.id)
);
return exits.success(
inputs.withProjectMemberships
? {
userIds,
projectMemberships
}
: userIds
);
}
};

View file

@ -0,0 +1,29 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
exceptUserId: {
type: 'number',
custom: value => _.isInteger(value) || _.isArray(value)
}
},
fn: async function(inputs, exits) {
const criteria = {
cardId: inputs.id
};
if (!_.isUndefined(inputs.exceptUserId)) {
criteria.userId = {
'!=': inputs.exceptUserId
};
}
const cardMemberships = await sails.helpers.getCardMemberships(criteria);
return exits.success(cardMemberships);
}
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const projectMemberships = await sails.helpers.getProjectMemberships({
projectId: inputs.id
});
return exits.success(projectMemberships);
}
};

View file

@ -0,0 +1,18 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const notifications = await sails.helpers.getNotifications({
isRead: false,
userId: inputs.id
});
return exits.success(notifications);
}
};

View file

@ -0,0 +1,16 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const notifications = await Notification.find(inputs.criteria).sort(
'id DESC'
);
return exits.success(notifications);
}
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const projectMemberships = await sails.helpers.getProjectMemberships({
userId: inputs.id
});
return exits.success(projectMemberships);
}
};

View file

@ -0,0 +1,16 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const projectMemberships = await ProjectMembership.find(
inputs.criteria
).sort('id');
return exits.success(projectMemberships);
}
};

View file

@ -0,0 +1,14 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const projects = await Project.find(inputs.criteria).sort('id');
return exits.success(projects);
}
};

View file

@ -0,0 +1,39 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
exceptUserId: {
type: 'number',
custom: value => _.isInteger(value) || _.isArray(value)
},
withCardSubscriptions: {
type: 'boolean',
defaultsTo: false
}
},
fn: async function(inputs, exits) {
const cardSubscriptions = await sails.helpers.getSubscriptionsForCard(
inputs.id,
inputs.exceptUserId
);
const userIds = sails.helpers.mapRecords(
cardSubscriptions,
'userId',
_.isArray(inputs.id)
);
return exits.success(
inputs.withCardSubscriptions
? {
userIds,
cardSubscriptions
}
: userIds
);
}
};

View file

@ -0,0 +1,22 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
userId: {
type: 'json',
required: true
}
},
fn: async function(inputs, exits) {
const cardSubscriptions = await sails.helpers.getCardSubscriptions({
cardId: inputs.id,
userId: inputs.userId
});
return exits.success(cardSubscriptions);
}
};

View file

@ -0,0 +1,31 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
},
exceptUserId: {
type: 'number',
custom: value => _.isInteger(value) || _.isArray(value)
}
},
fn: async function(inputs, exits) {
const criteria = {
cardId: inputs.id
};
if (!_.isUndefined(inputs.exceptUserId)) {
criteria.userId = {
'!=': inputs.exceptUserId
};
}
const cardSubscriptions = await sails.helpers.getCardSubscriptions(
criteria
);
return exits.success(cardSubscriptions);
}
};

View file

@ -0,0 +1,34 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
required: true
}
},
exits: {
notFound: {}
},
fn: async function(inputs, exits) {
const task = await Task.findOne(inputs.criteria);
if (!task) {
throw 'notFound';
}
const path = await sails.helpers
.getCardToProjectPath(task.cardId)
.intercept('notFound', path => ({
notFound: {
task,
...path
}
}));
return exits.success({
task,
...path
});
}
};

View file

@ -0,0 +1,17 @@
module.exports = {
inputs: {
id: {
type: 'json',
custom: value => _.isInteger(value) || _.isArray(value),
required: true
}
},
fn: async function(inputs, exits) {
const tasks = await sails.helpers.getTasks({
cardId: inputs.id
});
return exits.success(tasks);
}
};

View file

@ -0,0 +1,14 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const tasks = await Task.find(inputs.criteria).sort('id');
return exits.success(tasks);
}
};

View file

@ -0,0 +1,25 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isInteger(value) || _.isPlainObject(value),
required: true
}
},
fn: async function(inputs, exits) {
const criteria = {
deletedAt: null
};
if (_.isInteger(inputs.criteria)) {
criteria.id = inputs.criteria;
} else if (_.isPlainObject(inputs.criteria)) {
Object.assign(criteria, inputs.criteria);
}
const user = await User.findOne(criteria);
return exits.success(user);
}
};

View file

@ -0,0 +1,24 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: value => _.isArray(value) || _.isPlainObject(value)
}
},
fn: async function(inputs, exits) {
const criteria = {
deletedAt: null
};
if (_.isArray(inputs.criteria)) {
criteria.id = inputs.criteria;
} else if (_.isPlainObject(inputs.criteria)) {
Object.assign(criteria, inputs.criteria);
}
const users = await User.find(criteria).sort('id');
return exits.success(users);
}
};

View file

@ -0,0 +1,131 @@
const GAP = 2 ** 14;
const MIN_GAP = 0.125;
const MAX_POSITION = 2 ** 40; // 2 ** 50
const findBeginnings = positions => {
positions.unshift(0);
let prevPosition = positions.pop();
const beginnings = [prevPosition];
_.forEachRight(positions, position => {
if (prevPosition - MIN_GAP >= position) {
return false;
}
prevPosition = position;
beginnings.unshift(prevPosition);
});
return beginnings;
};
const getRepositionsMap = positions => {
const repositionsMap = {};
if (positions.length <= 1) {
if (!_.isUndefined(positions[0]) && positions[0] > MAX_POSITION) {
return null;
}
return repositionsMap;
}
let prevPosition = positions.shift();
for (let i = 0; i < positions.length; i++) {
const position = positions[i];
const nextPosition = positions[i + 1];
if (prevPosition + MIN_GAP <= position) {
break;
}
if (
!_.isUndefined(nextPosition) &&
prevPosition + MIN_GAP * 2 <= nextPosition
) {
(repositionsMap[position] || (repositionsMap[position] = [])).push(
prevPosition + (nextPosition - prevPosition) / 2
);
break;
}
prevPosition += GAP;
if (prevPosition > MAX_POSITION) {
return null;
}
(repositionsMap[position] || (repositionsMap[position] = [])).push(
prevPosition
);
}
return repositionsMap;
};
const getFullRepositionsMap = positions => {
const repositionsMap = {};
_.forEach(positions, (position, index) => {
(repositionsMap[position] || (repositionsMap[position] = [])).push(
GAP * (index + 1)
);
});
return repositionsMap;
};
module.exports = {
sync: true,
inputs: {
position: {
type: 'number',
required: true
},
records: {
type: 'ref',
required: true
}
},
fn: function(inputs, exits) {
const lowers = [];
const uppers = [];
inputs.records.forEach(({ position }) => {
(position <= inputs.position ? lowers : uppers).push(position);
});
const beginnings = findBeginnings([...lowers, inputs.position]);
const repositionsMap =
getRepositionsMap([...beginnings, ...uppers]) ||
getFullRepositionsMap([...lowers, inputs.position, ...uppers]);
let position = repositionsMap[inputs.position]
? repositionsMap[inputs.position].pop()
: inputs.position;
const repositions = [];
_.forEachRight(inputs.records, ({ id, position }) => {
if (_.isEmpty(repositionsMap[position])) {
return;
}
repositions.unshift({
id,
position: repositionsMap[position].pop()
});
});
return exits.success({
position,
repositions
});
}
};

View file

@ -0,0 +1,21 @@
module.exports = {
inputs: {
id: {
type: 'number',
required: true
},
userId: {
type: 'number',
required: true
}
},
fn: async function(inputs, exits) {
const projectMembership = await ProjectMembership.findOne({
projectId: inputs.id,
userId: inputs.userId
});
return exits.success(!!projectMembership);
}
};

View file

@ -0,0 +1,28 @@
module.exports = {
sync: true,
inputs: {
records: {
type: 'ref',
custom: value => _.isArray(value),
required: true
},
attribute: {
type: 'string',
defaultsTo: 'id'
},
unique: {
type: 'boolean',
defaultsTo: false
}
},
fn: function(inputs, exits) {
let result = _.map(inputs.records, inputs.attribute);
if (inputs.unique) {
result = _.uniq(result);
}
return exits.success(result);
}
};

View file

@ -0,0 +1,18 @@
var jwt = require('jsonwebtoken');
module.exports = {
sync: true,
inputs: {
payload: {
type: 'json',
required: true
}
},
fn: function(inputs, exits) {
const token = jwt.sign(inputs.payload, sails.config.session.secret);
return exits.success(token);
}
};

View file

@ -0,0 +1,36 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
board: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const action = await Action.updateOne(inputs.record.id).set(inputs.values);
if (action) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'actionUpdate',
{
item: action
},
inputs.request
);
}
return exits.success(action);
}
};

View file

@ -0,0 +1,73 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value =>
_.isPlainObject(value) &&
(_.isUndefined(value.position) || _.isFinite(value.position)),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const userIds = await sails.helpers.getMembershipUserIdsForProject(
inputs.record.projectId
);
if (!_.isUndefined(inputs.values.position)) {
const boards = await sails.helpers.getBoardsForProject(
inputs.record.projectId,
inputs.record.id
);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
boards
);
inputs.values.position = position;
repositions.forEach(async ({ id, position }) => {
await Board.update({
id,
projectId: inputs.record.projectId
}).set({
position
});
userIds.forEach(userId => {
sails.sockets.broadcast(`user:${userId}`, 'boardUpdate', {
item: {
id,
position
}
});
});
});
}
const board = await Board.updateOne(inputs.record.id).set(inputs.values);
if (board) {
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'boardUpdate',
{
item: board
},
inputs.request
);
});
}
return exits.success(board);
}
};

View file

@ -0,0 +1,147 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value =>
_.isPlainObject(value) &&
(_.isUndefined(value.position) || _.isFinite(value.position)),
required: true
},
toList: {
type: 'ref'
},
list: {
type: 'ref',
required: true
},
user: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const { isSubscribed, ...values } = inputs.values;
let listId;
if (inputs.toList) {
listId = inputs.toList.id;
if (listId !== inputs.list.id) {
values.listId = listId;
} else {
delete inputs.toList;
}
} else {
listId = inputs.list.id;
}
if (!_.isUndefined(values.position)) {
const cards = await sails.helpers.getCardsForList(
listId,
inputs.record.id
);
const { position, repositions } = sails.helpers.insertToPositionables(
values.position,
cards
);
values.position = position;
repositions.forEach(async ({ id, position }) => {
await Card.update({
id,
listId
}).set({
position
});
sails.sockets.broadcast(
`board:${inputs.record.boardId}`,
'cardUpdate',
{
item: {
id,
position
}
}
);
});
}
let card;
if (!_.isEmpty(values)) {
card = await Card.updateOne(inputs.record.id).set(values);
if (!card) {
return exits.success(card);
}
sails.sockets.broadcast(
`board:${card.boardId}`,
'cardUpdate',
{
item: card
},
inputs.request
);
if (inputs.toList) {
const values = {
type: 'moveCard',
data: {
fromList: _.pick(inputs.list, ['id', 'name']),
toList: _.pick(inputs.toList, ['id', 'name'])
}
};
await sails.helpers.createAction(card, inputs.user, values);
}
} 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 exits.success(card);
}
};

View file

@ -0,0 +1,32 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const label = await Label.updateOne(inputs.record.id).set(inputs.values);
if (label) {
sails.sockets.broadcast(
`board:${label.boardId}`,
'labelUpdate',
{
item: label
},
inputs.request
);
}
return exits.success(label);
}
};

View file

@ -0,0 +1,69 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value =>
_.isPlainObject(value) &&
(_.isUndefined(value.position) || _.isFinite(value.position)),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
if (!_.isUndefined(inputs.values.position)) {
const lists = await sails.helpers.getListsForBoard(
inputs.record.boardId,
inputs.record.id
);
const { position, repositions } = sails.helpers.insertToPositionables(
inputs.values.position,
lists
);
inputs.values.position = position;
repositions.forEach(async ({ id, position }) => {
await List.update({
id,
boardId: inputs.record.boardId
}).set({
position
});
sails.sockets.broadcast(
`board:${inputs.record.boardId}`,
'listUpdate',
{
item: {
id,
position
}
}
);
});
}
const list = await List.updateOne(inputs.record.id).set(inputs.values);
if (list) {
sails.sockets.broadcast(
`board:${list.boardId}`,
'listUpdate',
{
item: list
},
inputs.request
);
}
return exits.success(list);
}
};

View file

@ -0,0 +1,42 @@
module.exports = {
inputs: {
ids: {
type: 'json',
custom: value => _.isArray(value),
required: true
},
user: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const notifications = await Notification.update({
id: inputs.ids,
userId: inputs.user.id
})
.set(inputs.values)
.fetch();
notifications.forEach(notification => {
sails.sockets.broadcast(
`user:${notification.userId}`,
'notificationUpdate',
{
item: notification
},
inputs.request
);
});
return exits.success(notifications);
}
};

View file

@ -0,0 +1,40 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const project = await Project.updateOne(inputs.record.id).set(
inputs.values
);
if (project) {
const userIds = await sails.helpers.getMembershipUserIdsForProject(
project.id
);
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'projectUpdate',
{
item: project
},
inputs.request
);
});
}
return exits.success(project);
}
};

View file

@ -0,0 +1,36 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
required: true
},
board: {
type: 'ref',
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
const task = await Task.updateOne(inputs.record.id).set(inputs.values);
if (task) {
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'taskUpdate',
{
item: task
},
inputs.request
);
}
return exits.success(task);
}
};

View file

@ -0,0 +1,73 @@
const fs = require('fs');
const path = require('path');
const bcrypt = require('bcrypt');
module.exports = {
inputs: {
record: {
type: 'ref',
required: true
},
values: {
type: 'json',
custom: value =>
_.isPlainObject(value) &&
(_.isUndefined(value.email) || _.isString(value.email)) &&
(_.isUndefined(value.password) || _.isString(value.password)),
required: true
},
request: {
type: 'ref'
}
},
fn: async function(inputs, exits) {
if (!_.isUndefined(inputs.values.email)) {
inputs.values.email = inputs.values.email.toLowerCase();
}
if (!_.isUndefined(inputs.values.password)) {
inputs.values.password = bcrypt.hashSync(inputs.values.password, 10);
}
const user = await User.updateOne({
id: inputs.record.id,
deletedAt: null
}).set(inputs.values);
if (user) {
if (inputs.record.avatar && user.avatar !== inputs.record.avatar) {
try {
fs.unlinkSync(
path.join(sails.config.custom.uploadsPath, inputs.record.avatar)
);
} catch (unusedError) {}
}
const adminUserIds = await sails.helpers.getAdminUserIds();
const projectIds = await sails.helpers.getMembershipProjectIdsForUser(
user.id
);
const userIdsForProject = await sails.helpers.getMembershipUserIdsForProject(
projectIds
);
const userIds = _.union([user.id], adminUserIds, userIdsForProject);
userIds.forEach(userId => {
sails.sockets.broadcast(
`user:${userId}`,
'userUpdate',
{
item: user
},
inputs.request
);
});
}
return exits.success(user);
}
};

View file

@ -0,0 +1,28 @@
const jwt = require('jsonwebtoken');
module.exports = {
sync: true,
inputs: {
token: {
type: 'string',
required: true
}
},
exits: {
notValid: {}
},
fn: function(inputs, exits) {
let payload;
try {
payload = jwt.verify(inputs.token, sails.config.session.secret);
} catch (unusedError) {
throw 'notValid';
}
return exits.success(payload);
}
};