diff --git a/src/index.js b/src/index.js index 3e6eb48..c4915bf 100644 --- a/src/index.js +++ b/src/index.js @@ -27,11 +27,7 @@ logger.info('🤖 Initializing middleware') // you hit an endpoint in your terminal. It's here to help you debug. app.use(morgan('tiny', { stream: logger.stream })) -app.use( - cors({ - origin: `http://${process.env.PROJECT_NAME}-frontend.bridgeschoolapp.io` - }) -) +app.use(cors({ origin })) app.use(bodyParser.json()).use(bodyParser.urlencoded({ extended: true })) app.use(cors({ origin })) diff --git a/src/routes/channels/channels.controller.js b/src/routes/channels/channels.controller.js index e788d16..4b54a8b 100644 --- a/src/routes/channels/channels.controller.js +++ b/src/routes/channels/channels.controller.js @@ -1,5 +1,5 @@ const axios = require('axios') -const { parametize } = require('../../utils/parametize') +const parametize = require('../../utils/parametize') const channelsController = async (req, res, next) => { const params = parametize({ @@ -19,6 +19,4 @@ const channelsController = async (req, res, next) => { } } -module.exports = { - channelsController -} +module.exports = channelsController diff --git a/src/routes/channels/channels.router.js b/src/routes/channels/channels.router.js index ca74519..e155584 100644 --- a/src/routes/channels/channels.router.js +++ b/src/routes/channels/channels.router.js @@ -1,6 +1,6 @@ const express = require('express') -const { channelsController } = require('./channels.controller') +const channelsController = require('./channels.controller') const router = express.Router() diff --git a/src/routes/polls/polls.controller.js b/src/routes/polls/polls.controller.js index 368952d..54a9031 100644 --- a/src/routes/polls/polls.controller.js +++ b/src/routes/polls/polls.controller.js @@ -1,7 +1,7 @@ const axios = require('axios') -const { parametize } = require('../../utils/parametize') const db = require('../../db') +const parametize = require('../../utils/parametize') const createSlackBlock = require('../../utils/createSlackBlock') const sendPolltoSlack = async (interactionBlock, channel) => { @@ -18,11 +18,11 @@ const sendPolltoSlack = async (interactionBlock, channel) => { const res = await axios.post(url) return res.data } catch (err) { - next(err) + return err } } -const addPollsController = async (req, res) => { +const addPollsController = async (req, res, next) => { const { question, channel_name, channel_id } = req.body const newPoll = { question: question, @@ -42,10 +42,11 @@ const addPollsController = async (req, res) => { .then(async docRef => { const block = createSlackBlock(docRef.id, question) const sendPolltoSlackData = await sendPolltoSlack(block, channel_id) + if (sendPolltoSlackData.ok) { res.status(200).json({ id: docRef.id, - message: 'Poll successfully created in db and sent to slack' + message: 'Poll successfully created and sent to slack' }) } else { res.status(200).json({ @@ -56,12 +57,10 @@ const addPollsController = async (req, res) => { }) } }) - .catch(error => { - res.json({ error }) - }) + .catch(error => next(error)) } -const getPollsController = async (req, res) => { +const getPollsController = async (req, res, next) => { db.collection('/polls') .get() .then(snapshot => { @@ -74,17 +73,15 @@ const getPollsController = async (req, res) => { }) }) }) - .catch(error => { - res.json({ error }) - }) + .catch(error => next(error)) } -const getPollByIdController = async (req, res) => { +const getPollByIdController = async (req, res, next) => { db.collection('polls') .doc(req.params.id) .get() .then(snapshot => res.status(200).json(snapshot.data())) - .catch(error => res.json({ error })) + .catch(error => next(error)) } const getCurrentResponse = async block_id => { @@ -96,12 +93,12 @@ const getCurrentResponse = async block_id => { .then(snapshot => { currentResponse = snapshot.data().response }) - .catch(error => console.log(error)) + .catch(error => next(error)) return currentResponse } -const updatePollsController = async (req, res) => { +const updatePollsController = async (req, res, next) => { const action = JSON.parse(req.body.payload).actions[0] const { block_id, value } = action @@ -118,15 +115,15 @@ const updatePollsController = async (req, res) => { response: newResponse }) .then(res.status(200).send('Poll successfully updated')) - .catch(error => res.json({ error })) + .catch(error => next(error)) } -const deletePollsController = async (req, res) => { +const deletePollsController = async (req, res, next) => { db.collection('polls') .doc(req.params.id) .delete() .then(res.status(200).send('Poll successfully deleted')) - .catch(error => res.json({ error })) + .catch(error => next(error)) } module.exports = { diff --git a/src/utils/parametize.js b/src/utils/parametize.js index bda6b3a..53b48e1 100644 --- a/src/utils/parametize.js +++ b/src/utils/parametize.js @@ -9,4 +9,4 @@ const parametize = params => return (string += prefix + toInterpolate) }, '') -module.exports = { parametize } +module.exports = parametize