Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
feat: send mail notifications on discussion creation
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Sep 8, 2021
1 parent 2ae745a commit 71bd1a5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
46 changes: 46 additions & 0 deletions api/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,52 @@ module.exports = async function (editorConfig) {
}
})

router.post('/discussions/create', async (req, res, next) => {
try {
const userApolloClient = await getApolloClientForUser(req)

const result = await userApolloClient.mutate({
mutation: gql`
mutation ($headline: String!, $body: String!, $iri: String!) {
createThread (input: {
thread: {
headline: $headline,
body: $body,
iri: $iri,
threadType: DISCUSSION,
status: OPEN
}
}) {
thread {
id
}
}
}`,
variables: req.body
})

const url = `${editorConfig.editor.protocol}://${editorConfig.editor.host}/zom/discussion/${result.data.createThread.thread.id}`
sendMail({
recipients: await adminEmails(),
subject: 'New discussion',
text: dedent(`A discussion was created on "${editorConfig.editor.meta.title}".
Title: ${req.body.headline}
Discussion URL: ${url}
`)
})

res.json(result.data)
}
catch (err) {
if (_.get(err, 'request.headers.authorization')) {
err.request.headers.authorization = '[...]'
}
debug(err.message, err.request)
res.status(500).json({ message: err.message })
}
})

function getToken (req) {
if (!req.get('Authorization')) {
return
Expand Down
15 changes: 0 additions & 15 deletions apollo/mutations/createDiscussion.gql

This file was deleted.

7 changes: 5 additions & 2 deletions components/discussion/DiscussionCreate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

<script>
import _get from 'lodash/get'
import createDiscussion from '@/apollo/mutations/createDiscussion'
import axios from 'axios'
import Editor from '@/components/editor/Editor'
export default {
Expand Down Expand Up @@ -87,14 +87,17 @@ export default {
methods: {
_get,
create () {
const token = this.$apolloHelpers.getToken()
const variables = {
headline: this.headlineModel,
iri: this.iri,
body: this.body,
isDraft: false
}
this.$apollo.mutate({ mutation: createDiscussion, variables })
const headers = { headers: { authorization: `Bearer ${token}` } }
axios.post('/api/discussions/create', variables, headers)
.then((result) => {
const id = _get(result, 'data.createThread.thread.id')
if (id) {
Expand Down

0 comments on commit 71bd1a5

Please sign in to comment.