Skip to content
Snippets Groups Projects
Commit ee7852c3 authored by Sebastian Mihalache's avatar Sebastian Mihalache
Browse files

feat(component-helper-service): check if user is active when sending emails

parent eb791fd2
No related branches found
No related tags found
1 merge request!14Sprint #15
......@@ -65,8 +65,8 @@ class Email {
? `${eic.firstName} ${eic.lastName}`
: collection.handlingEditor.name
let reviewers = await Promise.all(reviewerPromises)
reviewers = reviewers.filter(Boolean)
let reviewers = (await Promise.all(reviewerPromises)).filter(Boolean)
reviewers = reviewers.filter(rev => rev.isActive)
if (agree) {
subject = isSubmitted
......@@ -118,26 +118,25 @@ class Email {
parsedFragment: { title },
authors: { submittingAuthor: { firstName = '', lastName = '' } },
} = this
const reviewers = await Promise.all(
invitations.map(async inv => UserModel.find(inv.userId)),
)
reviewers.forEach(user =>
mailService.sendNotificationEmail({
emailType: 'submitting-reviewers-after-revision',
toEmail: user.email,
meta: {
baseUrl,
collection,
timestamp: Date.now(),
reviewerName: `${user.firstName} ${user.lastName}`,
fragment: {
title,
authorName: `${firstName} ${lastName}`,
id: newFragmentId,
;(await Promise.all(invitations.map(inv => UserModel.find(inv.userId))))
.filter(rev => rev.isActive)
.forEach(user =>
mailService.sendNotificationEmail({
emailType: 'submitting-reviewers-after-revision',
toEmail: user.email,
meta: {
baseUrl,
collection,
timestamp: Date.now(),
reviewerName: `${user.firstName} ${user.lastName}`,
fragment: {
title,
authorName: `${firstName} ${lastName}`,
id: newFragmentId,
},
},
},
}),
)
}),
)
}
async setupAuthorsEmail({
......@@ -147,6 +146,7 @@ class Email {
}) {
const {
baseUrl,
UserModel,
collection,
parsedFragment: { heRecommendation, id, title, newComments },
authors: { submittingAuthor: { email, firstName, lastName } },
......@@ -170,7 +170,10 @@ class Email {
]
} else {
const fragment = await FragmentModel.find(id)
toAuthors = fragment.authors.map(author => ({
const userHelper = new User({ UserModel })
const activeAuthors = userHelper.getActiveAuthors(fragment.authors)
toAuthors = activeAuthors.map(author => ({
email: author.email,
name: `${author.firstName} ${author.lastName}`,
}))
......
const get = require('lodash/get')
const User = require('./User')
class Fragment {
constructor({ fragment }) {
......@@ -69,11 +70,12 @@ class Fragment {
get(submittingAuthorData, 'id'),
)
const authorsPromises = authors.map(async author => {
const user = await UserModel.find(author.id)
return `${user.firstName} ${user.lastName}`
})
const authorsList = await Promise.all(authorsPromises)
const userHelper = new User({ UserModel })
const activeAuthors = userHelper.getActiveAuthors(authors)
const authorsList = activeAuthors.map(
author => `${author.firstName} ${author.lastName}`,
)
return {
authorsList,
......
......@@ -68,6 +68,16 @@ class User {
user.teams.push(teamId)
user.save()
}
async getActiveAuthors(authors) {
const activeUsers = (await Promise.all(
authors.map(author => this.UserModel.find(author.id)),
))
.filter(u => u.isActive)
.map(u => u.id)
return authors.filter(author => activeUsers.includes(author.id))
}
}
module.exports = User
const { services } = require('pubsweet-component-helper-service')
const { services, User } = require('pubsweet-component-helper-service')
module.exports = models => async (req, res) => {
// TO DO: add authsome
......@@ -11,15 +11,8 @@ module.exports = models => async (req, res) => {
})
const { authors = [] } = await models.Fragment.find(fragmentId)
const activeUsers = (await Promise.all(
authors.map(author => models.User.find(author.id)),
))
.filter(u => u.isActive)
.map(u => u.id)
const activeAuthors = authors.filter(author =>
activeUsers.includes(author.id),
)
const userHelper = new User({ UserModel: models.User })
const activeAuthors = userHelper.getActiveAuthors(authors)
return res.status(200).json(activeAuthors)
} catch (e) {
......
......@@ -296,8 +296,7 @@ const authsomeMode = async (userId, operation, object, context) => {
return applyEditorInChiefPolicy(user, operation, object, context)
}
if (user) {
if (!user.isActive) return false
if (user && user.isActive !== false) {
return applyAuthenticatedUserPolicy(user, operation, object, context)
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment