bcgov/nr-get-token

View on GitHub
app/src/routes/v1/users.js

Summary

Maintainability
D
1 day
Test Coverage
A
100%
const usersRouter = require('express').Router();
const { param, validationResult } = require('express-validator');
const Problem = require('api-problem');
const validator = require('validator');

const users = require('../../components/users');

/** Returns acronyms associated with a user */
usersRouter.get('/:keycloakId/acronyms', [
  param('keycloakId', 'Must be a valid UUID')
    .custom(value => validator.isUUID(value))
], async (req, res) => {
  // TODO: Move this into middleware or equivalent
  // Validate for Bad Requests
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return new Problem(422, {
      detail: 'Validation failed',
      errors: errors.array()
    }).send(res);
  }

  const result = await users.getUserAcronyms(req.params.keycloakId);
  if (result === null) {
    return new Problem(404).send(res);
  } else {
    res.status(200).json(result);
  }
});

/** Returns acronym service clients associated with a user */
usersRouter.get('/:keycloakId/acronyms/clients', [
  param('keycloakId', 'Must be a valid UUID')
    .custom(value => validator.isUUID(value))
], async (req, res) => {
  // TODO: Move this into middleware or equivalent
  // Validate for Bad Requests
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return new Problem(422, {
      detail: 'Validation failed',
      errors: errors.array()
    }).send(res);
  }

  const result = await users.getUserAcronymClients(req.params.keycloakId);
  if (result === null) {
    return new Problem(404).send(res);
  } else {
    res.status(200).json(result);
  }
});


module.exports = usersRouter;