huridocs/uwazi

View on GitHub
app/api/settings/routes.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import settings from 'api/settings/settings';
import { Application } from 'express';
import needsAuthorization from '../auth/authMiddleware';

export default (app: Application) => {
  app.get('/api/settings', (req, res, next) => {
    const select = req.user && req.user.role === 'admin' ? '+publicFormDestination' : {};
    settings
      .get({}, select)
      .then(response => {
        const { features, ...partialSettings } = response;
        if (req.user?.role === 'admin' || req.user?.role === 'editor') {
          res.json(response);
        } else {
          res.json(partialSettings);
        }
      })
      .catch(next);
  });

  app.post('/api/settings', needsAuthorization(), (req, res, next) => {
    settings
      .save(req.body)
      .then(response => res.json(response))
      .catch(next);
  });

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  app.get('/api/settings/links', (req, res, next) => {
    settings
      .get()
      .then(response => res.json(response.links))
      .catch(next);
  });

  app.post('/api/settings/links', needsAuthorization(), (req, res, next) => {
    settings
      .save({ links: req.body })
      .then(response => res.json(response))
      .catch(next);
  });
};