const express = require('express');
const { requireCapability } = require('../middleware/auth');
const { publicConfig, saveConfig, sendTest } = require('../services/teams');

const router = express.Router();

router.get('/teams/settings', requireCapability('admin.integrations'), (req, res) => {
  res.json({ settings: publicConfig() });
});

router.put('/teams/settings', requireCapability('admin.integrations'), (req, res) => {
  res.json({ settings: saveConfig(req.body, req.user.id) });
});

router.post('/teams/test', requireCapability('admin.integrations'), async (req, res, next) => {
  try {
    res.json(await sendTest(req.user.id));
  } catch (error) {
    next(error);
  }
});

module.exports = router;
