fix: Cleanup and refactoring

pull/631/head
Maksim Eltyshev 2 years ago
parent 80499e68d5
commit 44eb6f9c1f

@ -31,6 +31,14 @@ services:
# - DEFAULT_ADMIN_NAME=Demo Demo # - DEFAULT_ADMIN_NAME=Demo Demo
# - DEFAULT_ADMIN_USERNAME=demo # - DEFAULT_ADMIN_USERNAME=demo
# Email Notifications (https://nodemailer.com/smtp/)
# - SMTP_HOST=
# - SMTP_PORT=587
# - SMTP_SECURE=true
# - SMTP_USER=
# - SMTP_PASSWORD=
# - SMTP_FROM="Demo Demo" <demo@demo.demo>
# - OIDC_ISSUER= # - OIDC_ISSUER=
# - OIDC_CLIENT_ID= # - OIDC_CLIENT_ID=
# - OIDC_CLIENT_SECRET= # - OIDC_CLIENT_SECRET=

@ -31,6 +31,14 @@ services:
# - DEFAULT_ADMIN_NAME=Demo Demo # - DEFAULT_ADMIN_NAME=Demo Demo
# - DEFAULT_ADMIN_USERNAME=demo # - DEFAULT_ADMIN_USERNAME=demo
# Email Notifications (https://nodemailer.com/smtp/)
# - SMTP_HOST=
# - SMTP_PORT=587
# - SMTP_SECURE=true
# - SMTP_USER=
# - SMTP_PASSWORD=
# - SMTP_FROM="Demo Demo" <demo@demo.demo>
# - OIDC_ISSUER= # - OIDC_ISSUER=
# - OIDC_CLIENT_ID= # - OIDC_CLIENT_ID=
# - OIDC_CLIENT_SECRET= # - OIDC_CLIENT_SECRET=

@ -22,6 +22,14 @@ SECRET_KEY=notsecretkey
# DEFAULT_ADMIN_NAME=Demo Demo # DEFAULT_ADMIN_NAME=Demo Demo
# DEFAULT_ADMIN_USERNAME=demo # DEFAULT_ADMIN_USERNAME=demo
# Email Notifications (https://nodemailer.com/smtp/)
# SMTP_HOST=
# SMTP_PORT=587
# SMTP_SECURE=true
# SMTP_USER=
# SMTP_PASSWORD=
# SMTP_FROM="Demo Demo" <demo@demo.demo>
# OIDC_ISSUER= # OIDC_ISSUER=
# OIDC_CLIENT_ID= # OIDC_CLIENT_ID=
# OIDC_CLIENT_SECRET= # OIDC_CLIENT_SECRET=
@ -35,14 +43,6 @@ SECRET_KEY=notsecretkey
# OIDC_IGNORE_ROLES=true # OIDC_IGNORE_ROLES=true
# OIDC_ENFORCED=true # OIDC_ENFORCED=true
# Email Notifications (https://nodemailer.com/smtp/)
SMTP_HOST=
SMTP_USER=
SMTP_PASSWORD=
SMTP_PORT=587
SMTP_SECURE=
SMTP_FROM="Demo Demo" <demo@demo.demo>
## Do not edit this ## Do not edit this
TZ=UTC TZ=UTC

@ -78,12 +78,12 @@ module.exports = {
async fn(inputs) { async fn(inputs) {
const { currentUser } = this.req; const { currentUser } = this.req;
const { list } = await sails.helpers.lists const { board, list } = await sails.helpers.lists
.getProjectPath(inputs.listId) .getProjectPath(inputs.listId)
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND); .intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({ const boardMembership = await BoardMembership.findOne({
boardId: list.boardId, boardId: board.id,
userId: currentUser.id, userId: currentUser.id,
}); });
@ -99,6 +99,7 @@ module.exports = {
const card = await sails.helpers.cards.createOne const card = await sails.helpers.cards.createOne
.with({ .with({
board,
values: { values: {
...values, ...values,
list, list,

@ -32,12 +32,12 @@ module.exports = {
async fn(inputs) { async fn(inputs) {
const { currentUser } = this.req; const { currentUser } = this.req;
const { card } = await sails.helpers.cards const { board, card } = await sails.helpers.cards
.getProjectPath(inputs.cardId) .getProjectPath(inputs.cardId)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND); .intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({ const boardMembership = await BoardMembership.findOne({
boardId: card.boardId, boardId: board.id,
userId: currentUser.id, userId: currentUser.id,
}); });
@ -55,6 +55,7 @@ module.exports = {
}; };
const action = await sails.helpers.actions.createOne.with({ const action = await sails.helpers.actions.createOne.with({
board,
values: { values: {
...values, ...values,
card, card,

@ -21,6 +21,10 @@ module.exports = {
custom: valuesValidator, custom: valuesValidator,
required: true, required: true,
}, },
board: {
type: 'ref',
required: true,
},
request: { request: {
type: 'ref', type: 'ref',
}, },
@ -56,6 +60,9 @@ module.exports = {
userId, userId,
action, action,
}, },
user: values.user,
board: inputs.board,
card: values.card,
}), }),
), ),
); );

@ -25,6 +25,10 @@ module.exports = {
custom: valuesValidator, custom: valuesValidator,
required: true, required: true,
}, },
board: {
type: 'ref',
required: true,
},
request: { request: {
type: 'ref', type: 'ref',
}, },
@ -104,6 +108,7 @@ module.exports = {
}, },
user: values.creatorUser, user: values.creatorUser,
}, },
board: inputs.board,
}); });
return card; return card;

@ -232,6 +232,7 @@ module.exports = {
toList: _.pick(values.list, ['id', 'name']), toList: _.pick(values.list, ['id', 'name']),
}, },
}, },
board: inputs.board,
}); });
} }

@ -1,18 +1,3 @@
const nodemailer = require('nodemailer');
const emailTransporter =
process.env.SMTP_HOST &&
nodemailer.createTransport({
pool: true,
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: process.env.SMTP_SECURE === 'true',
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASSWORD,
},
});
const valuesValidator = (value) => { const valuesValidator = (value) => {
if (!_.isPlainObject(value)) { if (!_.isPlainObject(value)) {
return false; return false;
@ -29,51 +14,39 @@ const valuesValidator = (value) => {
return true; return true;
}; };
async function sendEmailNotification({ notification, action }) { // TODO: use templates (views) to build html
const actionUser = await sails.helpers.users.getOne(action.userId); const buildAndSendEmail = async (user, board, card, action, notifiableUser) => {
const actionCard = await Card.findOne(action.cardId); let emailData;
const notificationUser = await sails.helpers.users.getOne(notification.userId);
const actionBoard = await Board.findOne(actionCard.boardId);
let email;
switch (action.type) { switch (action.type) {
case Action.Types.COMMENT_CARD: case Action.Types.MOVE_CARD:
email = { emailData = {
subject: `${actionUser.name} commented the card ${actionCard.name} on ${actionBoard.name}`, subject: `${user.name} moved ${card.name} from ${action.data.fromList.name} to ${action.data.toList.name} on ${board.name}`,
html: html:
`<p>${actionUser.name} commented the card ` + `<p>${user.name} moved ` +
`<a href="${process.env.BASE_URL}/cards/${actionCard.id}">${actionCard.name}</a> ` + `<a href="${process.env.BASE_URL}/cards/${card.id}">${card.name}</a> ` +
`on <a href="${process.env.BASE_URL}/boards/${actionBoard.id}">${actionBoard.name}</a></p>` + `from ${action.data.fromList.name} to ${action.data.toList.name} ` +
`<p>${action.data.text}</p>`, `on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>`,
}; };
break; break;
case Action.Types.MOVE_CARD: case Action.Types.COMMENT_CARD:
email = { emailData = {
subject: `${actionUser.name} moved the card ${actionCard.name} from ${action.data.fromList.name} to ${action.data.toList.name} on ${actionBoard.name}`, subject: `${user.name} left a new comment to ${card.name} on ${board.name}`,
html: html:
`<p>${actionUser.name} moved the card ` + `<p>${user.name} left a new comment to ` +
`<a href="${process.env.BASE_URL}/cards/${actionCard.id}">${actionCard.name}</a> ` + `<a href="${process.env.BASE_URL}/cards/${card.id}">${card.name}</a> ` +
`from ${action.data.fromList.name} to ${action.data.toList.name} ` + `on <a href="${process.env.BASE_URL}/boards/${board.id}">${board.name}</a></p>` +
`on <a href="${process.env.BASE_URL}/boards/${actionBoard.id}">${actionBoard.name}</a></p>`, `<p>${action.data.text}</p>`,
}; };
break; break;
default: default:
break;
}
if (!email) {
return; return;
} }
emailTransporter.sendMail(
{ from: process.env.SMTP_FROM, to: notificationUser.email, ...email }, await sails.helpers.utils.sendEmail.with({
(error, info) => { ...emailData,
if (error) { to: notifiableUser.email,
sails.log.error(error); });
} else { };
sails.log.info('Email sent: %s', info.messageId);
}
},
);
}
module.exports = { module.exports = {
inputs: { inputs: {
@ -82,6 +55,18 @@ module.exports = {
custom: valuesValidator, custom: valuesValidator,
required: true, required: true,
}, },
user: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
card: {
type: 'ref',
required: true,
},
}, },
async fn(inputs) { async fn(inputs) {
@ -97,14 +82,21 @@ module.exports = {
cardId: values.action.cardId, cardId: values.action.cardId,
}).fetch(); }).fetch();
if (emailTransporter) {
sendEmailNotification({ notification, action: values.action });
}
sails.sockets.broadcast(`user:${notification.userId}`, 'notificationCreate', { sails.sockets.broadcast(`user:${notification.userId}`, 'notificationCreate', {
item: notification, item: notification,
}); });
if (sails.hooks.smtp.isActive()) {
let notifiableUser;
if (values.user) {
notifiableUser = values.user;
} else {
notifiableUser = await sails.helpers.users.getOne(notification.userId);
}
buildAndSendEmail(inputs.user, inputs.board, inputs.card, values.action, notifiableUser);
}
return notification; return notification;
}, },
}; };

@ -0,0 +1,31 @@
module.exports = {
inputs: {
to: {
type: 'string',
required: true,
},
subject: {
type: 'string',
required: true,
},
html: {
type: 'string',
required: true,
},
},
async fn(inputs) {
const transporter = sails.hooks.smtp.getTransporter(); // TODO: check if active?
try {
const info = await transporter.sendMail({
...inputs,
from: sails.config.custom.smtpFrom,
});
sails.log.info('Email sent: %s', info.messageId);
} catch (error) {
sails.log.error(error);
}
},
};

@ -0,0 +1,35 @@
const nodemailer = require('nodemailer');
module.exports = function smtpServiceHook(sails) {
let transporter = null;
return {
/**
* Runs when this Sails app loads/lifts.
*/
async initialize() {
if (sails.config.custom.smtpHost) {
transporter = nodemailer.createTransport({
pool: true,
host: sails.config.custom.smtpHost,
port: sails.config.custom.smtpPort,
secure: sails.config.custom.smtpSecure,
auth: sails.config.custom.smtpUser && {
user: sails.config.custom.smtpUser,
pass: sails.config.custom.smtpPassword,
},
});
sails.log.info('SMTP hook has been loaded successfully');
}
},
getTransporter() {
return transporter;
},
isActive() {
return transporter !== null;
},
};
};

@ -34,6 +34,13 @@ module.exports.custom = {
defaultAdminEmail: defaultAdminEmail:
process.env.DEFAULT_ADMIN_EMAIL && process.env.DEFAULT_ADMIN_EMAIL.toLowerCase(), process.env.DEFAULT_ADMIN_EMAIL && process.env.DEFAULT_ADMIN_EMAIL.toLowerCase(),
smtpHost: process.env.SMTP_HOST,
smtpPort: process.env.SMTP_PORT || 587,
smtpSecure: process.env.SMTP_SECURE === 'true',
smtpUser: process.env.SMTP_USER,
smtpPassword: process.env.SMTP_PASSWORD,
smtpFrom: process.env.SMTP_FROM,
oidcIssuer: process.env.OIDC_ISSUER, oidcIssuer: process.env.OIDC_ISSUER,
oidcClientId: process.env.OIDC_CLIENT_ID, oidcClientId: process.env.OIDC_CLIENT_ID,
oidcClientSecret: process.env.OIDC_CLIENT_SECRET, oidcClientSecret: process.env.OIDC_CLIENT_SECRET,

Loading…
Cancel
Save