Skip to content
Snippets Groups Projects
Commit f0e6fb94 authored by Yannis Barlas's avatar Yannis Barlas
Browse files

feat(middleware): add email middleware

parent d66f5147
No related branches found
No related tags found
1 merge request!6Email notifications
const { ApolloServer } = require('apollo-server-express')
const isEmpty = require('lodash/isEmpty')
const config = require('config')
const logger = require('@pubsweet/logger')
const errors = require('@pubsweet/errors')
const config = require('config')
const connectors = require('pubsweet-server/src/connectors')
const loaders = require('pubsweet-server/src/graphql/loaders')
const helpers = require('pubsweet-server/src/helpers/authorization')
const schema = require('./graphqlSchema')
const hostname = config.has('pubsweet-server.hostname')
......
......@@ -2,12 +2,47 @@ const config = require('config')
const isEmpty = require('lodash/isEmpty')
const { applyMiddleware } = require('graphql-middleware')
const { shield } = require('graphql-shield')
let schema = require('pubsweet-server/src/graphql/schema')
const logger = require('@pubsweet/logger')
const schema = require('pubsweet-server/src/graphql/schema')
const emailMiddleware = require('./middleware/email')
const baseMessage = 'Coko server =>'
const logRegistration = name =>
logger.info(`${baseMessage} Middleware: Registered ${name} middleware`)
const middleware = []
console.log('') // eslint-disable-line no-console
logger.info(`${baseMessage} Registering graphql middleware...`)
/**
* Authorization middleware
*/
const permissions = config.has('permissions') && config.get('permissions')
if (permissions && !isEmpty(permissions)) {
schema = applyMiddleware(schema, shield(permissions))
if (!isEmpty(permissions)) {
const authorizationMiddleware = shield(permissions)
middleware.push(authorizationMiddleware)
logRegistration('authorization')
}
module.exports = schema
/**
* Email middleware
*/
const emailConfig =
config.has('emailMiddleware') && config.get('emailMiddleware')
if (!isEmpty(emailConfig)) {
middleware.push(emailMiddleware)
logRegistration('email')
}
console.log('') // eslint-disable-line no-console
const schemaWithMiddleWare = applyMiddleware(schema, ...middleware)
module.exports = schemaWithMiddleWare
const config = require('config')
const { isEmpty } = require('lodash')
const { middleware } = require('graphql-middleware')
// const logger = require('@pubsweet/logger')
const generator = middleware(schema => {
const emailService =
config.has('emailMiddleware.service') &&
config.get('emailMiddleware.service')
if (isEmpty(emailService)) {
throw new Error('Email middleware: No email service provided!')
}
/**
* Email notifications are only applicable on mutations
*/
const typeMap = schema.getTypeMap()
const { Mutation } = typeMap
const mutationFields = Mutation.getFields()
const mutationEntries = Object.keys(mutationFields).reduce(
(middlewareSchema, field) => {
const serviceFieldEntry = emailService[field]
if (!serviceFieldEntry) return middlewareSchema
/* eslint-disable-next-line no-param-reassign */
middlewareSchema[field] = serviceFieldEntry
return middlewareSchema
},
{},
)
return {
Mutation: mutationEntries,
}
})
module.exports = generator
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