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,83 @@
const bcrypt = require('bcrypt');
module.exports = {
inputs: {
values: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isString(value.email)) {
return false;
}
if (!_.isString(value.password)) {
return false;
}
if (value.username && !_.isString(value.username)) {
return false;
}
return true;
},
required: true,
},
request: {
type: 'ref',
},
},
exits: {
emailAlreadyInUse: {},
usernameAlreadyInUse: {},
},
async fn(inputs) {
if (inputs.values.username) {
// eslint-disable-next-line no-param-reassign
inputs.values.username = inputs.values.username.toLowerCase();
}
const user = await User.create({
...inputs.values,
email: inputs.values.email.toLowerCase(),
password: bcrypt.hashSync(inputs.values.password, 10),
})
.intercept(
{
message:
'Unexpected error from database adapter: conflicting key value violates exclusion constraint "user_email_unique"',
},
'emailAlreadyInUse',
)
.intercept(
{
message:
'Unexpected error from database adapter: conflicting key value violates exclusion constraint "user_username_unique"',
},
'usernameAlreadyInUse',
)
.fetch();
// const userIds = await sails.helpers.users.getAdminIds();
const users = await sails.helpers.users.getMany();
const userIds = sails.helpers.utils.mapRecords(users);
userIds.forEach((userId) => {
sails.sockets.broadcast(
`user:${userId}`,
'userCreate',
{
item: user,
},
inputs.request,
);
});
return user;
},
};

View file

@ -0,0 +1,62 @@
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
await ProjectManager.destroy({
userId: inputs.record.id,
});
await BoardMembership.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 projectIds = await sails.helpers.users.getManagerProjectIds(user.id);
const userIds = _.union(
[user.id],
await sails.helpers.users.getAdminIds(),
await sails.helpers.projects.getManagerAndBoardMemberUserIds(projectIds),
); */
const users = await sails.helpers.users.getMany();
const userIds = [inputs.record.id, ...sails.helpers.utils.mapRecords(users)];
userIds.forEach((userId) => {
sails.sockets.broadcast(
`user:${userId}`,
'userDelete',
{
item: user,
},
inputs.request,
);
});
}
return user;
},
};

View file

@ -0,0 +1,9 @@
module.exports = {
async fn() {
const users = await sails.helpers.users.getMany({
isAdmin: true,
});
return sails.helpers.utils.mapRecords(users);
},
};

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.boardMemberships.getMany({
userId: 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 projectManagers = await sails.helpers.users.getProjectManagers(inputs.idOrIds);
return sails.helpers.utils.mapRecords(projectManagers, 'projectId', _.isArray(inputs.idOrIds));
},
};

View file

@ -0,0 +1,28 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: (value) => _.isArray(value) || _.isPlainObject(value),
},
withDeleted: {
type: 'boolean',
defaultsTo: false,
},
},
async fn(inputs) {
const criteria = {};
if (_.isArray(inputs.criteria)) {
criteria.id = inputs.criteria;
} else if (_.isPlainObject(inputs.criteria)) {
Object.assign(criteria, inputs.criteria);
}
if (!inputs.withDeleted) {
criteria.deletedAt = null;
}
return User.find(criteria).sort('id');
},
};

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 boardMemberships = await sails.helpers.users.getBoardMemberships(inputs.idOrIds);
return sails.helpers.utils.mapRecords(boardMemberships, 'boardId', _.isArray(inputs.idOrIds));
},
};

View file

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

View file

@ -0,0 +1,16 @@
module.exports = {
inputs: {
emailOrUsername: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const fieldName = inputs.emailOrUsername.includes('@') ? 'email' : 'username';
return sails.helpers.users.getOne({
[fieldName]: inputs.emailOrUsername.toLowerCase(),
});
},
};

View file

@ -0,0 +1,29 @@
module.exports = {
inputs: {
criteria: {
type: 'json',
custom: (value) => _.isString(value) || _.isPlainObject(value),
required: true,
},
withDeleted: {
type: 'boolean',
defaultsTo: false,
},
},
async fn(inputs) {
const criteria = {};
if (_.isString(inputs.criteria)) {
criteria.id = inputs.criteria;
} else if (_.isPlainObject(inputs.criteria)) {
Object.assign(criteria, inputs.criteria);
}
if (!inputs.withDeleted) {
criteria.deletedAt = null;
}
return User.findOne(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) {
return sails.helpers.projectManagers.getMany({
userId: inputs.idOrIds,
});
},
};

View file

@ -0,0 +1,21 @@
module.exports = {
inputs: {
id: {
type: 'string',
required: true,
},
boardId: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const boardMembership = await BoardMembership.findOne({
boardId: inputs.boardId,
userId: inputs.id,
});
return !!boardMembership;
},
};

View file

@ -0,0 +1,21 @@
module.exports = {
inputs: {
id: {
type: 'string',
required: true,
},
cardId: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const cardSubscription = await CardSubscription.findOne({
cardId: inputs.cardId,
userId: inputs.id,
});
return !!cardSubscription;
},
};

View file

@ -0,0 +1,21 @@
module.exports = {
inputs: {
id: {
type: 'string',
required: true,
},
projectId: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const projectManager = await ProjectManager.findOne({
projectId: inputs.projectId,
userId: inputs.id,
});
return !!projectManager;
},
};

View file

@ -0,0 +1,133 @@
const path = require('path');
const bcrypt = require('bcrypt');
const rimraf = require('rimraf');
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
values: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isUndefined(value.email) && !_.isString(value.email)) {
return false;
}
if (!_.isUndefined(value.password) && !_.isString(value.password)) {
return false;
}
if (value.username && !_.isString(value.username)) {
return false;
}
if (!_.isUndefined(value.avatarUrl) && !_.isNull(value.avatarUrl)) {
return false;
}
return true;
},
required: true,
},
request: {
type: 'ref',
},
},
exits: {
emailAlreadyInUse: {},
usernameAlreadyInUse: {},
},
async fn(inputs) {
if (!_.isUndefined(inputs.values.email)) {
// eslint-disable-next-line no-param-reassign
inputs.values.email = inputs.values.email.toLowerCase();
}
let isOnlyPasswordChange = false;
if (!_.isUndefined(inputs.values.password)) {
// eslint-disable-next-line no-param-reassign
inputs.values.password = bcrypt.hashSync(inputs.values.password, 10);
if (Object.keys(inputs.values).length === 1) {
isOnlyPasswordChange = true;
}
}
if (inputs.values.username) {
// eslint-disable-next-line no-param-reassign
inputs.values.username = inputs.values.username.toLowerCase();
}
if (!_.isUndefined(inputs.values.avatarUrl)) {
/* eslint-disable no-param-reassign */
inputs.values.avatarDirname = null;
delete inputs.values.avatarUrl;
/* eslint-enable no-param-reassign */
}
const user = await User.updateOne({
id: inputs.record.id,
deletedAt: null,
})
.set(inputs.values)
.intercept(
{
message:
'Unexpected error from database adapter: conflicting key value violates exclusion constraint "user_email_unique"',
},
'emailAlreadyInUse',
)
.intercept(
{
message:
'Unexpected error from database adapter: conflicting key value violates exclusion constraint "user_username_unique"',
},
'usernameAlreadyInUse',
);
if (user) {
if (inputs.record.avatarDirname && user.avatarDirname !== inputs.record.avatarDirname) {
try {
rimraf.sync(path.join(sails.config.custom.userAvatarsPath, inputs.record.avatarDirname));
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
}
if (!isOnlyPasswordChange) {
/* const projectIds = await sails.helpers.users.getManagerProjectIds(user.id);
const userIds = _.union(
[user.id],
await sails.helpers.users.getAdminIds(),
await sails.helpers.projects.getManagerAndBoardMemberUserIds(projectIds),
); */
const users = await sails.helpers.users.getMany();
const userIds = sails.helpers.utils.mapRecords(users);
userIds.forEach((userId) => {
sails.sockets.broadcast(
`user:${userId}`,
'userUpdate',
{
item: user,
},
inputs.request,
);
});
}
}
return user;
},
};