diff --git a/package.json b/package.json index d0389697275f14ef15d4bb998473eab6574d332a..810f2e9754fd9551778fe76b7f7e4bd27858f339 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "moment": "^2.24.0", "moment-range": "^4.0.2", "multer": "^1.4.2", + "node-fetch": "^2.6.0", "oauth2-server": "^3.0.1", "pg": "^7.12.1", "scrypt-kdf": "^2.0.1", diff --git a/src/http/controllers/api/feedback.js b/src/http/controllers/api/feedback.js new file mode 100644 index 0000000000000000000000000000000000000000..dcf1ac32cd283f17fd633794f10c6a31a9733e8a --- /dev/null +++ b/src/http/controllers/api/feedback.js @@ -0,0 +1,78 @@ +const fetch = require('node-fetch') + +const SLACK_WEBHOOK = process.env.SLACK_WEBHOOK + +function createSlackPayload({ name, email, message = '' }) { + return { + "blocks": [ + { + "type": "divider" + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "Incoming message from an app user!" + } + }, + { + "type": "section", + "text": { + "type": "plain_text", + text: message, + emoji: true, + } + }, + { + "type": "divider" + }, + { + "type": "context", + "elements": [ + { + "type": "mrkdwn", + "text": (new Date()).toDateString(), + }, + name && { + "type": "plain_text", + "text": `From: ${ name }` + }, + email && { + "type": "plain_text", + "text": `Email: ${ email }` + } + ].filter(Boolean) + } + ] + } +} + +exports.send = async ctx => { + const { message } = ctx.request.body + const payload = { message } + const user = await ctx.services.authService.getUser() + if (user) { + payload.name = user.name + payload.email = user.email + } + const slackObject = createSlackPayload(payload) + + const response = await fetch(SLACK_WEBHOOK, { + method: 'POST', + body: JSON.stringify(slackObject), + headers: { + 'Content-Type': 'application/json', + }, + }).catch(e => e) + + if (response instanceof Error) { + console.error(response) + ctx.status = 500 + ctx.body = { + message: 'Something went wrong' + } + } else { + ctx.status = response.status + ctx.body = { message: 'Something Happened'} + } +} diff --git a/src/http/routes.js b/src/http/routes.js index 472ea2629d4d0e2194f4b8908f57c52fa395bd36..035f1a238357d343680642e166b8ea3748329656 100644 --- a/src/http/routes.js +++ b/src/http/routes.js @@ -22,36 +22,41 @@ web.post('/auth/authorize', AuthServer.authorize) web.post('/auth/token', AuthServer.token) // --- API ROUTES -const api = new Router({ prefix: '/api' }) -api.use(require('./middleware/api/errors')) -api.use(includes) -api.post('/metrics', controller('api/content', 'postMetric')) -api.get('/metrics', controller('api/content', 'getWithin')) +function mount(api) { + api.use(require('./middleware/api/errors')) + api.use(includes) + api.post('/metrics', controller('api/content', 'postMetric')) + api.get('/metrics', controller('api/content', 'getWithin')) -api.get('/images', controller('api/storage', 'getFiles')) -api.post('/images', upload.single('featured_image'), controller('api/storage', 'saveFile')) -api.post('/images/:imageId/feature', controller('api/storage', 'featureImage')) + api.get('/images', controller('api/storage', 'getFiles')) + api.post('/images', upload.single('featured_image'), controller('api/storage', 'saveFile')) + api.post('/images/:imageId/feature', controller('api/storage', 'featureImage')) -/** @deprecated */ -api.post('/feature', upload.single('featured_image'), controller('api/storage', 'saveFile')) + /** @deprecated */ + api.post('/feature', upload.single('featured_image'), controller('api/storage', 'saveFile')) -api.get('/feed', controller('api/storage', 'feed')) -api.post('/feed/:fileId/like',controller('api/storage', 'like')) -api.post('/feed/:fileId/unlike', controller('api/storage', 'unlike')) + api.get('/feed', controller('api/storage', 'feed')) + api.post('/feed/:fileId/like',controller('api/storage', 'like')) + api.post('/feed/:fileId/unlike', controller('api/storage', 'unlike')) -api.post('/register', controller('api/auth', 'register')) -api.post('/login', controller('api/auth', 'login')) + api.post('/register', controller('api/auth', 'register')) + api.post('/login', controller('api/auth', 'login')) -api.get('/self', controller('api/user', 'self')) -api.put('/self/:property', controller('api/user', 'updateOne')) + api.get('/self', controller('api/user', 'self')) + api.put('/self/:property', controller('api/user', 'updateOne')) -const apiApi = new Router({ prefix: '/api' }) -apiApi.use(api.routes()) -apiApi.use(api.allowedMethods()) + api.post('/feedback', controller('api/feedback', 'send')) +} + +const apiRouter = new Router({ prefix: '/api' }) +const apiLegacy = new Router({ prefix: '/api/api' }) + +mount(apiRouter) +mount(apiLegacy) module.exports = { - api, web, - apiApi, + apiRouter, + apiLegacy, }