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

ref: Remove board types, refactoring

This commit is contained in:
Maksim Eltyshev 2022-12-26 21:10:50 +01:00
parent 2b131f76c1
commit 6ffa817b53
182 changed files with 1573 additions and 1239 deletions

View file

@ -12,16 +12,16 @@ const Errors = {
},
};
const emailOrUsernameValidator = (value) =>
value.includes('@')
? validator.isEmail(value)
: value.length >= 3 && value.length <= 16 && /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/.test(value);
module.exports = {
inputs: {
emailOrUsername: {
type: 'string',
custom: (value) =>
value.includes('@')
? validator.isEmail(value)
: value.length >= 3 &&
value.length <= 16 &&
/^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/.test(value),
custom: emailOrUsernameValidator,
required: true,
},
password: {

View file

@ -82,13 +82,15 @@ module.exports = {
const file = _.last(files);
const fileData = await sails.helpers.attachments.processUploadedFile(file);
const attachment = await sails.helpers.attachments.createOne(
fileData,
currentUser,
card,
inputs.requestId,
this.req,
);
const attachment = await sails.helpers.attachments.createOne.with({
values: {
...fileData,
card,
creatorUser: currentUser,
},
requestId: inputs.requestId,
request: this.req,
});
return exits.success({
item: attachment,

View file

@ -48,7 +48,12 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
attachment = await sails.helpers.attachments.deleteOne(attachment, board, card, this.req);
attachment = await sails.helpers.attachments.deleteOne.with({
board,
card,
record: attachment,
request: this.req,
});
if (!attachment) {
throw Errors.ATTACHMENT_NOT_FOUND;

View file

@ -53,7 +53,13 @@ module.exports = {
}
const values = _.pick(inputs, ['name']);
attachment = await sails.helpers.attachments.updateOne(attachment, values, board, this.req);
attachment = await sails.helpers.attachments.updateOne.with({
values,
board,
record: attachment,
request: this.req,
});
if (!attachment) {
throw Errors.ATTACHMENT_NOT_FOUND;

View file

@ -68,8 +68,15 @@ module.exports = {
const values = _.pick(inputs, ['role', 'canComment']);
const boardMembership = await sails.helpers.boardMemberships
.createOne(values, user, board, this.req)
const boardMembership = await sails.helpers.boardMemberships.createOne
.with({
values: {
...values,
board,
user,
},
request: this.req,
})
.intercept('userAlreadyBoardMember', () => Errors.USER_ALREADY_BOARD_MEMBER);
return {

View file

@ -36,15 +36,15 @@ module.exports = {
);
if (!isProjectManager) {
throw Errors.BOARD_MEMBERSHIP_NOT_FOUND;
throw Errors.BOARD_MEMBERSHIP_NOT_FOUND; // Forbidden
}
}
boardMembership = await sails.helpers.boardMemberships.deleteOne(
boardMembership,
boardMembership = await sails.helpers.boardMemberships.deleteOne.with({
project,
this.req,
);
record: boardMembership,
request: this.req,
});
if (!boardMembership) {
throw Errors.BOARD_MEMBERSHIP_NOT_FOUND;

View file

@ -44,11 +44,11 @@ module.exports = {
const values = _.pick(inputs, ['role', 'canComment']);
boardMembership = await sails.helpers.boardMemberships.updateOne(
boardMembership,
boardMembership = await sails.helpers.boardMemberships.updateOne.with({
values,
this.req,
);
record: boardMembership,
request: this.req,
});
return {
item: boardMembership,

View file

@ -20,11 +20,6 @@ module.exports = {
regex: /^[0-9]+$/,
required: true,
},
type: {
type: 'string',
isIn: Object.values(Board.Types),
required: true,
},
position: {
type: 'number',
required: true,
@ -70,7 +65,7 @@ module.exports = {
throw Errors.PROJECT_NOT_FOUND; // Forbidden
}
const values = _.pick(inputs, ['type', 'position', 'name']);
const values = _.pick(inputs, ['position', 'name']);
let boardImport;
if (inputs.importType && Object.values(Board.ImportTypes).includes(inputs.importType)) {
@ -102,14 +97,16 @@ module.exports = {
}
}
const { board, boardMembership } = await sails.helpers.boards.createOne(
values,
boardImport,
currentUser,
project,
inputs.requestId,
this.req,
);
const { board, boardMembership } = await sails.helpers.boards.createOne.with({
values: {
...values,
project,
},
import: boardImport,
user: currentUser,
requestId: inputs.requestId,
request: this.req,
});
if (this.req.isSocket) {
sails.sockets.join(this.req, `board:${board.id}`); // TODO: only when subscription needed

View file

@ -39,7 +39,10 @@ module.exports = {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
board = await sails.helpers.boards.deleteOne(board, this.req);
board = await sails.helpers.boards.deleteOne.with({
record: board,
request: this.req,
});
if (!board) {
throw Errors.BOARD_NOT_FOUND;

View file

@ -47,7 +47,7 @@ module.exports = {
const labels = await sails.helpers.boards.getLabels(board.id);
const lists = await sails.helpers.boards.getLists(board.id);
const cards = await sails.helpers.boards.getCards(board);
const cards = await sails.helpers.boards.getCards(board.id);
const cardIds = sails.helpers.utils.mapRecords(cards);
const cardSubscriptions = await sails.helpers.cardSubscriptions.getMany({
@ -69,7 +69,8 @@ module.exports = {
);
cards.forEach((card) => {
card.isSubscribed = isSubscribedByCardId[card.id] || false; // eslint-disable-line no-param-reassign
// eslint-disable-next-line no-param-reassign
card.isSubscribed = isSubscribedByCardId[card.id] || false;
});
if (this.req.isSocket) {

View file

@ -47,7 +47,12 @@ module.exports = {
}
const values = _.pick(inputs, ['position', 'name']);
board = await sails.helpers.boards.updateOne(board, values, this.req);
board = await sails.helpers.boards.updateOne.with({
values,
record: board,
request: this.req,
});
if (!board) {
throw Errors.BOARD_NOT_FOUND;

View file

@ -71,8 +71,14 @@ module.exports = {
throw Errors.LABEL_NOT_FOUND;
}
const cardLabel = await sails.helpers.cardLabels
.createOne(label, card, this.req)
const cardLabel = await sails.helpers.cardLabels.createOne
.with({
values: {
card,
label,
},
request: this.req,
})
.intercept('labelAlreadyInCard', () => Errors.LABEL_ALREADY_IN_CARD);
return {

View file

@ -65,7 +65,11 @@ module.exports = {
throw Errors.LABEL_NOT_IN_CARD;
}
cardLabel = await sails.helpers.cardLabels.deleteOne(cardLabel, board, this.req);
cardLabel = await sails.helpers.cardLabels.deleteOne.with({
board,
record: cardLabel,
request: this.req,
});
if (!cardLabel) {
throw Errors.LABEL_NOT_IN_CARD;

View file

@ -68,8 +68,14 @@ module.exports = {
throw Errors.USER_NOT_FOUND;
}
const cardMembership = await sails.helpers.cardMemberships
.createOne(inputs.userId, card, this.req)
const cardMembership = await sails.helpers.cardMemberships.createOne
.with({
values: {
card,
userId: inputs.userId,
},
request: this.req,
})
.intercept('userAlreadyCardMember', () => Errors.USER_ALREADY_CARD_MEMBER);
return {

View file

@ -65,7 +65,11 @@ module.exports = {
throw Errors.USER_NOT_CARD_MEMBER;
}
cardMembership = await sails.helpers.cardMemberships.deleteOne(cardMembership, board, this.req);
cardMembership = await sails.helpers.cardMemberships.deleteOne.with({
board,
record: cardMembership,
request: this.req,
});
if (!cardMembership) {
throw Errors.USER_NOT_CARD_MEMBER;

View file

@ -4,30 +4,42 @@ const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
BOARD_NOT_FOUND: {
boardNotFound: 'Board not found',
},
LIST_NOT_FOUND: {
listNotFound: 'List not found',
},
LIST_MUST_BE_PRESENT: {
listMustBePresent: 'List must be present',
},
POSITION_MUST_BE_PRESENT: {
positionMustBePresent: 'Position must be present',
},
};
const dueDateValidator = (value) => moment(value, moment.ISO_8601, true).isValid();
const timerValidator = (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (
!_.isNull(value.startedAt) &&
_.isString(value.startedAt) &&
!moment(value.startedAt, moment.ISO_8601, true).isValid()
) {
return false;
}
if (!_.isFinite(value.total)) {
return false;
}
return true;
};
module.exports = {
inputs: {
boardId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
listId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
position: {
type: 'number',
@ -43,29 +55,11 @@ module.exports = {
},
dueDate: {
type: 'string',
custom: (value) => moment(value, moment.ISO_8601, true).isValid(),
custom: dueDateValidator,
},
timer: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (
!_.isNull(value.startedAt) &&
_.isString(value.startedAt) &&
!moment(value.startedAt, moment.ISO_8601, true).isValid()
) {
return false;
}
if (!_.isFinite(value.total)) {
return false;
}
return true;
},
custom: timerValidator,
},
},
@ -73,15 +67,9 @@ module.exports = {
notEnoughRights: {
responseType: 'forbidden',
},
boardNotFound: {
responseType: 'notFound',
},
listNotFound: {
responseType: 'notFound',
},
listMustBePresent: {
responseType: 'unprocessableEntity',
},
positionMustBePresent: {
responseType: 'unprocessableEntity',
},
@ -90,40 +78,34 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;
const { board } = await sails.helpers.boards
.getProjectPath(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND);
const { list } = await sails.helpers.lists
.getProjectPath(inputs.listId)
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
boardId: list.boardId,
userId: currentUser.id,
});
if (!boardMembership) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
throw Errors.LIST_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
let list;
if (!_.isUndefined(inputs.listId)) {
list = await List.findOne({
id: inputs.listId,
boardId: board.id,
});
if (!list) {
throw Errors.LIST_NOT_FOUND;
}
}
const values = _.pick(inputs, ['position', 'name', 'description', 'dueDate', 'timer']);
const card = await sails.helpers.cards
.createOne(values, currentUser, board, list, this.req)
.intercept('listMustBePresent', () => Errors.LIST_MUST_BE_PRESENT)
const card = await sails.helpers.cards.createOne
.with({
values: {
...values,
list,
creatorUser: currentUser,
},
request: this.req,
})
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT);
return {

View file

@ -45,7 +45,10 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
card = await sails.helpers.cards.deleteOne(card, this.req);
card = await sails.helpers.cards.deleteOne.with({
record: card,
request: this.req,
});
if (!card) {
throw Errors.CARD_NOT_FOUND;

View file

@ -1,74 +0,0 @@
const Errors = {
BOARD_NOT_FOUND: {
boardNotFound: 'Board not found',
},
};
module.exports = {
inputs: {
boardId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
beforeId: {
type: 'string',
regex: /^[0-9]+$/,
},
},
exits: {
boardNotFound: {
responseType: 'notFound',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const { board } = await sails.helpers.boards
.getProjectPath(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND);
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, board.id);
if (!isBoardMember) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
const cards = await sails.helpers.boards.getCards(board, inputs.beforeId);
const cardIds = sails.helpers.utils.mapRecords(cards);
const cardSubscriptions = await sails.helpers.cardSubscriptions.getMany({
cardId: cardIds,
userId: currentUser.id,
});
const isSubscribedByCardId = cardSubscriptions.reduce(
(result, cardSubscription) => ({
...result,
[cardSubscription.cardId]: true,
}),
{},
);
cards.forEach((card) => {
card.isSubscribed = isSubscribedByCardId[card.id] || false; // eslint-disable-line no-param-reassign
});
const cardMemberships = await sails.helpers.cards.getCardMemberships(cardIds);
const cardLabels = await sails.helpers.cards.getCardLabels(cardIds);
const tasks = await sails.helpers.cards.getTasks(cardIds);
const attachments = await sails.helpers.cards.getAttachments(cardIds);
return {
items: cards,
included: {
cardMemberships,
cardLabels,
tasks,
attachments,
},
};
},
};

View file

@ -21,6 +21,28 @@ const Errors = {
},
};
const dueDateValidator = (value) => moment(value, moment.ISO_8601, true).isValid();
const timerValidator = (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (
!_.isNull(value.startedAt) &&
_.isString(value.startedAt) &&
!moment(value.startedAt, moment.ISO_8601, true).isValid()
) {
return false;
}
if (!_.isFinite(value.total)) {
return false;
}
return true;
};
module.exports = {
inputs: {
id: {
@ -55,30 +77,12 @@ module.exports = {
},
dueDate: {
type: 'string',
custom: (value) => moment(value, moment.ISO_8601, true).isValid(),
custom: dueDateValidator,
allowNull: true,
},
timer: {
type: 'json',
custom: (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (
!_.isNull(value.startedAt) &&
_.isString(value.startedAt) &&
!moment(value.startedAt, moment.ISO_8601, true).isValid()
) {
return false;
}
if (!_.isFinite(value.total)) {
return false;
}
return true;
},
custom: timerValidator,
},
isSubscribed: {
type: 'boolean',
@ -171,10 +175,21 @@ module.exports = {
'isSubscribed',
]);
card = await sails.helpers.cards
.updateOne(card, values, nextBoard, nextList, currentUser, board, list, this.req)
.intercept('nextListMustBePresent', () => Errors.LIST_MUST_BE_PRESENT)
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT);
card = await sails.helpers.cards.updateOne
.with({
board,
list,
record: card,
values: {
...values,
board: nextBoard,
list: nextList,
},
user: currentUser,
request: this.req,
})
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT)
.intercept('listMustBeInValues', () => Errors.LIST_MUST_BE_PRESENT);
if (!card) {
throw Errors.CARD_NOT_FOUND;

View file

@ -54,7 +54,14 @@ module.exports = {
data: _.pick(inputs, ['text']),
};
const action = await sails.helpers.actions.createOne(values, currentUser, card, this.req);
const action = await sails.helpers.actions.createOne.with({
values: {
...values,
card,
user: currentUser,
},
request: this.req,
});
return {
item: action,

View file

@ -59,7 +59,11 @@ module.exports = {
}
}
action = await sails.helpers.actions.deleteOne(action, board, this.req);
action = await sails.helpers.actions.deleteOne.with({
board,
record: action,
request: this.req,
});
if (!action) {
throw Errors.COMMENT_ACTION_NOT_FOUND;

View file

@ -67,7 +67,12 @@ module.exports = {
data: _.pick(inputs, ['text']),
};
action = await sails.helpers.actions.updateOne(action, values, board, this.req);
action = await sails.helpers.actions.updateOne.with({
values,
board,
record: action,
request: this.req,
});
if (!action) {
throw Errors.COMMENT_ACTION_NOT_FOUND;

View file

@ -56,7 +56,14 @@ module.exports = {
}
const values = _.pick(inputs, ['name', 'color']);
const label = await sails.helpers.labels.createOne(values, board, this.req);
const label = await sails.helpers.labels.createOne.with({
values: {
...values,
board,
},
request: this.req,
});
return {
item: label,

View file

@ -45,7 +45,10 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
label = await sails.helpers.labels.deleteOne(label, this.req);
label = await sails.helpers.labels.deleteOne.with({
record: label,
request: this.req,
});
if (!label) {
throw Errors.LABEL_NOT_FOUND;

View file

@ -56,7 +56,12 @@ module.exports = {
}
const values = _.pick(inputs, ['name', 'color']);
label = await sails.helpers.labels.updateOne(label, values, this.req);
label = await sails.helpers.labels.updateOne.with({
values,
record: label,
request: this.req,
});
return {
item: label,

View file

@ -54,7 +54,14 @@ module.exports = {
}
const values = _.pick(inputs, ['position', 'name']);
const list = await sails.helpers.lists.createOne(values, board, this.req);
const list = await sails.helpers.lists.createOne.with({
values: {
...values,
board,
},
request: this.req,
});
return {
item: list,

View file

@ -45,7 +45,10 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
list = await sails.helpers.lists.deleteOne(list, this.req);
list = await sails.helpers.lists.deleteOne.with({
record: list,
request: this.req,
});
if (!list) {
throw Errors.LIST_NOT_FOUND;

View file

@ -53,7 +53,12 @@ module.exports = {
}
const values = _.pick(inputs, ['position', 'name']);
list = await sails.helpers.lists.updateOne(list, values, this.req);
list = await sails.helpers.lists.updateOne.with({
values,
record: list,
request: this.req,
});
if (!list) {
throw Errors.LIST_NOT_FOUND;

View file

@ -15,12 +15,12 @@ module.exports = {
const values = _.pick(inputs, ['isRead']);
const notifications = await sails.helpers.notifications.updateMany(
inputs.ids.split(','),
const notifications = await sails.helpers.notifications.updateMany.with({
values,
currentUser,
this.req,
);
recordsOrIds: inputs.ids.split(','),
user: currentUser,
request: this.req,
});
return {
items: notifications,

View file

@ -57,8 +57,14 @@ module.exports = {
throw Error.USER_NOT_FOUND;
}
const projectManager = await sails.helpers.projectManagers
.createOne(user, project, this.req)
const projectManager = await sails.helpers.projectManagers.createOne
.with({
values: {
project,
user,
},
request: this.req,
})
.intercept('userAlreadyProjectManager', () => Errors.USER_ALREADY_PROJECT_MANAGER);
return {

View file

@ -38,7 +38,10 @@ module.exports = {
}
// TODO: check if the last one
projectManager = await sails.helpers.projectManagers.deleteOne(projectManager, this.req);
projectManager = await sails.helpers.projectManagers.deleteOne.with({
record: projectManager,
request: this.req,
});
if (!projectManager) {
throw Errors.PROJECT_MANAGER_NOT_FOUND;

View file

@ -11,11 +11,11 @@ module.exports = {
const values = _.pick(inputs, ['name']);
const { project, projectManager } = await sails.helpers.projects.createOne(
const { project, projectManager } = await sails.helpers.projects.createOne.with({
values,
currentUser,
this.req,
);
user: currentUser,
request: this.req,
});
return {
item: project,

View file

@ -34,7 +34,10 @@ module.exports = {
throw Errors.PROJECT_NOT_FOUND; // Forbidden
}
project = await sails.helpers.projects.deleteOne(project, this.req);
project = await sails.helpers.projects.deleteOne.with({
record: project,
request: this.req,
});
if (!project) {
throw Errors.PROJECT_NOT_FOUND;

View file

@ -6,7 +6,6 @@ module.exports = {
const managerProjects = await sails.helpers.projects.getMany(managerProjectIds);
let boardMemberships = await sails.helpers.users.getBoardMemberships(currentUser.id);
const membershipBoardIds = sails.helpers.utils.mapRecords(boardMemberships, 'boardId');
let membershipBoards = await sails.helpers.boards.getMany({

View file

@ -85,13 +85,13 @@ module.exports = {
return Errors.FILE_IS_NOT_IMAGE;
});
project = await sails.helpers.projects.updateOne(
project,
{
project = await sails.helpers.projects.updateOne.with({
record: project,
values: {
backgroundImage: fileData,
},
this.req,
);
request: this.req,
});
if (!project) {
throw Errors.PROJECT_NOT_FOUND;

View file

@ -4,6 +4,36 @@ const Errors = {
},
};
const backgroundValidator = (value) => {
if (_.isNull(value)) {
return true;
}
if (!_.isPlainObject(value)) {
return false;
}
if (!Object.values(Project.BackgroundTypes).includes(value.type)) {
return false;
}
if (
value.type === Project.BackgroundTypes.GRADIENT &&
_.size(value) === 2 &&
Project.BACKGROUND_GRADIENTS.includes(value.name)
) {
return true;
}
if (value.type === Project.BackgroundTypes.IMAGE && _.size(value) === 1) {
return true;
}
return false;
};
const backgroundImageValidator = (value) => _.isNull(value);
module.exports = {
inputs: {
id: {
@ -17,37 +47,11 @@ module.exports = {
},
background: {
type: 'json',
custom: (value) => {
if (_.isNull(value)) {
return true;
}
if (!_.isPlainObject(value)) {
return false;
}
if (!Object.values(Project.BackgroundTypes).includes(value.type)) {
return false;
}
if (
value.type === Project.BackgroundTypes.GRADIENT &&
_.size(value) === 2 &&
Project.BACKGROUND_GRADIENTS.includes(value.name)
) {
return true;
}
if (value.type === Project.BackgroundTypes.IMAGE && _.size(value) === 1) {
return true;
}
return false;
},
custom: backgroundValidator,
},
backgroundImage: {
type: 'json',
custom: (value) => _.isNull(value),
custom: backgroundImageValidator,
},
},
@ -73,7 +77,12 @@ module.exports = {
}
const values = _.pick(inputs, ['name', 'background', 'backgroundImage']);
project = await sails.helpers.projects.updateOne(project, values, this.req);
project = await sails.helpers.projects.updateOne.with({
values,
record: project,
request: this.req,
});
if (!project) {
throw Errors.PROJECT_NOT_FOUND;

View file

@ -57,7 +57,14 @@ module.exports = {
}
const values = _.pick(inputs, ['position', 'name', 'isCompleted']);
const task = await sails.helpers.tasks.createOne(values, card, this.req);
const task = await sails.helpers.tasks.createOne.with({
values: {
...values,
card,
},
request: this.req,
});
return {
item: task,

View file

@ -48,7 +48,11 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
task = await sails.helpers.tasks.deleteOne(task, board, this.req);
task = await sails.helpers.tasks.deleteOne.with({
board,
record: task,
request: this.req,
});
if (!task) {
throw Errors.TASK_NOT_FOUND;

View file

@ -59,7 +59,13 @@ module.exports = {
}
const values = _.pick(inputs, ['position', 'name', 'isCompleted']);
task = await sails.helpers.tasks.updateOne(task, values, board, this.req);
task = await sails.helpers.tasks.updateOne.with({
values,
board,
record: task,
request: this.req,
});
if (!task) {
throw Errors.TASK_NOT_FOUND;

View file

@ -9,6 +9,8 @@ const Errors = {
},
};
const passwordValidator = (value) => zxcvbn(value).score >= 2; // TODO: move to config
module.exports = {
inputs: {
email: {
@ -18,7 +20,7 @@ module.exports = {
},
password: {
type: 'string',
custom: (value) => zxcvbn(value).score >= 2, // TODO: move to config
custom: passwordValidator,
required: true,
},
name: {
@ -74,8 +76,11 @@ module.exports = {
'subscribeToOwnCards',
]);
const user = await sails.helpers.users
.createOne(values, this.req)
const user = await sails.helpers.users.createOne
.with({
values,
request: this.req,
})
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE)
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE);

View file

@ -26,7 +26,10 @@ module.exports = {
throw Errors.USER_NOT_FOUND;
}
user = await sails.helpers.users.deleteOne(user, this.req);
user = await sails.helpers.users.deleteOne.with({
record: user,
request: this.req,
});
if (!user) {
throw Errors.USER_NOT_FOUND;

View file

@ -86,14 +86,14 @@ module.exports = {
return Errors.FILE_IS_NOT_IMAGE;
});
user = await sails.helpers.users.updateOne(
user,
{
user = await sails.helpers.users.updateOne.with({
record: user,
values: {
avatar: fileData,
},
currentUser,
this.req,
);
user: currentUser,
request: this.req,
});
if (!user) {
throw Errors.USER_NOT_FOUND;

View file

@ -68,8 +68,13 @@ module.exports = {
const values = _.pick(inputs, ['email']);
user = await sails.helpers.users
.updateOne(user, values, currentUser, this.req)
user = await sails.helpers.users.updateOne
.with({
values,
record: user,
user: currentUser,
request: this.req,
})
.intercept('emailAlreadyInUse', () => Errors.EMAIL_ALREADY_IN_USE);
if (!user) {

View file

@ -12,6 +12,8 @@ const Errors = {
},
};
const passwordValidator = (value) => zxcvbn(value).score >= 2; // TODO: move to config
module.exports = {
inputs: {
id: {
@ -21,7 +23,7 @@ module.exports = {
},
password: {
type: 'string',
custom: (value) => zxcvbn(value).score >= 2, // TODO: move to config
custom: passwordValidator,
required: true,
},
currentPassword: {
@ -64,7 +66,13 @@ module.exports = {
}
const values = _.pick(inputs, ['password']);
user = await sails.helpers.users.updateOne(user, values, currentUser, this.req);
user = await sails.helpers.users.updateOne.with({
values,
record: user,
user: currentUser,
request: this.req,
});
if (!user) {
throw Errors.USER_NOT_FOUND;

View file

@ -70,8 +70,13 @@ module.exports = {
const values = _.pick(inputs, ['username']);
user = await sails.helpers.users
.updateOne(user, values, currentUser, this.req)
user = await sails.helpers.users.updateOne
.with({
values,
record: user,
user: currentUser,
request: this.req,
})
.intercept('usernameAlreadyInUse', () => Errors.USERNAME_ALREADY_IN_USE);
if (!user) {

View file

@ -4,6 +4,8 @@ const Errors = {
},
};
const avatarUrlValidator = (value) => _.isNull(value);
module.exports = {
inputs: {
id: {
@ -20,7 +22,7 @@ module.exports = {
},
avatarUrl: {
type: 'json',
custom: (value) => _.isNull(value),
custom: avatarUrlValidator,
},
phone: {
type: 'string',
@ -77,7 +79,12 @@ module.exports = {
avatar: inputs.avatarUrl,
};
user = await sails.helpers.users.updateOne(user, values, currentUser, this.req);
user = await sails.helpers.users.updateOne.with({
values,
record: user,
user: currentUser,
request: this.req,
});
if (!user) {
throw Errors.USER_NOT_FOUND;