Skip to content
Snippets Groups Projects
Commit 458b3871 authored by Sebastian's avatar Sebastian
Browse files

feat(component-email): add new email endpoint

parent 0af770fe
No related branches found
No related tags found
1 merge request!5Refactor component invite
_build/
api/
logs/
node_modules/
uploads/
.env.*
.env
config/local*.*
\ No newline at end of file
module.exports = {
backend: () => app => {
require('./src/Emails')(app)
},
}
{
"name": "pubsweet-component-email",
"version": "0.0.1",
"description": "email component for faraday",
"license": "MIT",
"author": "Collaborative Knowledge Foundation",
"files": ["src"],
"main": "index.js",
"scripts": {
"test": "jest"
},
"repository": {
"type": "git",
"url": "https://gitlab.coko.foundation/xpub/xpub-faraday",
"path": "component-user-manager"
},
"dependencies": {
"body-parser": "^1.17.2",
"chance": "^1.0.13"
},
"peerDependencies": {
"@pubsweet/logger": "^0.0.1",
"pubsweet-server": "^1.0.1",
"pubsweet-component-mail-service": "0.0.1"
},
"devDependencies": {
"jest": "^22.1.1",
"supertest": "^3.0.0"
},
"jest": {
"verbose": true,
"testRegex": "/src/.*.test.js$"
},
"publishConfig": {
"access": "public"
}
}
const bodyParser = require('body-parser')
const CollectionsInvitations = app => {
app.use(bodyParser.json())
const basePath = '/api/emails'
const routePath = './routes/emails'
const authBearer = app.locals.passport.authenticate('bearer', {
session: false,
})
app.post(
basePath,
authBearer,
require(`${routePath}/post`)(app.locals.models),
)
}
module.exports = CollectionsInvitations
const mailService = require('pubsweet-component-mail-service')
const logger = require('@pubsweet/logger')
const helpers = require('./helpers')
module.exports = {
setupAssignEmail: async (req, email, res, role) => {
const url = `${req.protocol}://${req.get('host')}`
let emailType
switch (role) {
case 'handlingEditor':
emailType = 'assign-handling-editor'
break
case 'author':
emailType = 'add-author'
break
default:
return res
.status(400)
.json({ error: `Role ${role} cannot be used with an assigned email` })
}
try {
await mailService.setupAssignEmail(email, emailType, url)
return res.status(200).json({})
} catch (e) {
logger.error(e)
return res.status(500).json({ error: 'Email could not be sent.' })
}
},
setupNewUserEmail: async (req, res, email, role, UserModel) => {
let user
try {
user = UserModel.findByEmail(email)
} catch (e) {
const notFoundError = await helpers.handleNotFoundError(e, 'user')
return res.status(notFoundError.status).json({
error: notFoundError.message,
})
}
if (user.passwordResetToken === undefined) {
return res
.status(400)
.json({ error: 'User does not have a password reset token' })
}
const url = `${req.protocol}://${req.get('host')}`
let emailType
switch (role) {
case 'handlingEditor':
case 'editorInChief':
case 'admin':
emailType = 'invite'
break
case 'author':
emailType = 'invite-author'
break
default:
return res.status(400).json({
error: `Role ${role} cannot be used with a password reset email`,
})
}
try {
await mailService.setupInviteEmail(
email,
emailType,
user.passwordResetToken,
url,
)
return res.status(200).json({})
} catch (e) {
logger.error(e)
return res.status(500).json({ error: 'Email could not be sent.' })
}
},
}
const logger = require('@pubsweet/logger')
const checkForUndefinedParams = (...params) => {
if (params.includes(undefined)) {
return false
}
return true
}
const handleNotFoundError = async (error, item) => {
const response = {
success: false,
status: 500,
message: 'Something went wrong',
}
if (error.name === 'NotFoundError') {
logger.error(`invalid ${item} id`)
response.status = 404
response.message = `${item} not found`
return response
}
logger.error(error)
return response
}
module.exports = {
checkForUndefinedParams,
handleNotFoundError,
}
const logger = require('@pubsweet/logger')
const helpers = require('../../helpers/helpers')
const emailHelper = require('../../helpers/Email')
module.exports = models => async (req, res) => {
const { email, type, role } = req.body
if (!helpers.checkForUndefinedParams(email, type, role)) {
res.status(400).json({ error: 'Email and type are required' })
logger.error('User ID and role are missing')
return
}
switch (type) {
case 'invite':
return emailHelper.setupNewUserEmail(req, res, email, role, models.User)
case 'assign':
return emailHelper.setupAssignEmail(req, email, res, role)
default:
return res
.status(400)
.json({ error: `Email type ${type} is not defined` })
}
}
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