Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
server
Manage
Activity
Members
Labels
Plan
Issues
25
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cokoapps
server
Commits
f0e6fb94
Commit
f0e6fb94
authored
4 years ago
by
Yannis Barlas
Browse files
Options
Downloads
Patches
Plain Diff
feat(middleware): add email middleware
parent
d66f5147
No related branches found
No related tags found
1 merge request
!6
Email notifications
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/graphqlApi.js
+3
-2
3 additions, 2 deletions
src/graphqlApi.js
src/graphqlSchema.js
+39
-4
39 additions, 4 deletions
src/graphqlSchema.js
src/middleware/email.js
+40
-0
40 additions, 0 deletions
src/middleware/email.js
with
82 additions
and
6 deletions
src/graphqlApi.js
+
3
−
2
View file @
f0e6fb94
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
'
)
...
...
This diff is collapsed.
Click to expand it.
src/graphqlSchema.js
+
39
−
4
View file @
f0e6fb94
...
...
@@ -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
This diff is collapsed.
Click to expand it.
src/middleware/email.js
0 → 100644
+
40
−
0
View file @
f0e6fb94
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
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment