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:
parent
7956503a46
commit
fe91b5241e
478 changed files with 21226 additions and 19495 deletions
83
server/api/helpers/users/create-one.js
Normal file
83
server/api/helpers/users/create-one.js
Normal 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;
|
||||
},
|
||||
};
|
62
server/api/helpers/users/delete-one.js
Normal file
62
server/api/helpers/users/delete-one.js
Normal 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;
|
||||
},
|
||||
};
|
9
server/api/helpers/users/get-admin-ids.js
Normal file
9
server/api/helpers/users/get-admin-ids.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
module.exports = {
|
||||
async fn() {
|
||||
const users = await sails.helpers.users.getMany({
|
||||
isAdmin: true,
|
||||
});
|
||||
|
||||
return sails.helpers.utils.mapRecords(users);
|
||||
},
|
||||
};
|
15
server/api/helpers/users/get-board-memberships.js
Normal file
15
server/api/helpers/users/get-board-memberships.js
Normal 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,
|
||||
});
|
||||
},
|
||||
};
|
15
server/api/helpers/users/get-manager-project-ids.js
Normal file
15
server/api/helpers/users/get-manager-project-ids.js
Normal 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));
|
||||
},
|
||||
};
|
28
server/api/helpers/users/get-many.js
Normal file
28
server/api/helpers/users/get-many.js
Normal 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');
|
||||
},
|
||||
};
|
15
server/api/helpers/users/get-membership-board-ids.js
Normal file
15
server/api/helpers/users/get-membership-board-ids.js
Normal 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));
|
||||
},
|
||||
};
|
16
server/api/helpers/users/get-notifications.js
Normal file
16
server/api/helpers/users/get-notifications.js
Normal 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,
|
||||
});
|
||||
},
|
||||
};
|
16
server/api/helpers/users/get-one-by-email-or-username.js
Normal file
16
server/api/helpers/users/get-one-by-email-or-username.js
Normal 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(),
|
||||
});
|
||||
},
|
||||
};
|
29
server/api/helpers/users/get-one.js
Normal file
29
server/api/helpers/users/get-one.js
Normal 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);
|
||||
},
|
||||
};
|
15
server/api/helpers/users/get-project-managers.js
Normal file
15
server/api/helpers/users/get-project-managers.js
Normal 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,
|
||||
});
|
||||
},
|
||||
};
|
21
server/api/helpers/users/is-board-member.js
Normal file
21
server/api/helpers/users/is-board-member.js
Normal 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;
|
||||
},
|
||||
};
|
21
server/api/helpers/users/is-card-subscriber.js
Normal file
21
server/api/helpers/users/is-card-subscriber.js
Normal 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;
|
||||
},
|
||||
};
|
21
server/api/helpers/users/is-project-manager.js
Normal file
21
server/api/helpers/users/is-project-manager.js
Normal 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;
|
||||
},
|
||||
};
|
133
server/api/helpers/users/update-one.js
Normal file
133
server/api/helpers/users/update-one.js
Normal 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;
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue