All files / app/routes/api/user/search search-user-controller.ts

91.67% Statements 11/12
88.89% Branches 8/9
100% Functions 1/1
91.67% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  1x 1x               1x   9x     3x     6x 6x 6x   6x 6x           6x          
import {RequestHandler} from 'express';
import {BadRequestError} from '@app/errors';
import {UserService} from '@app/models';
 
/**
 * Controller for handling search for users.
 * @param req   - Request
 * @param res   - Response
 * @param next  - Next
 */
export const searchUserController: RequestHandler = async (req, res, next) => {
  // Check query parameters
  if (typeof req.query.filter !== 'string' ||
      (typeof req.query.limit !== 'string' &&
      typeof req.query.limit !== 'undefined')) {
    throw new BadRequestError();
  }
 
  const currentUser = req.user;
  const startsWith = req.query.filter;
  const limit = req.query.limit ? parseInt(req.query.limit, 10) : undefined;
 
  Eif (typeof currentUser === 'object') {
    const users = await UserService.findLimitedWithFilter(
        currentUser,
        startsWith,
        limit,
    );
 
    res.send({users});
  } else {
    throw new BadRequestError();
  }
};