All files / app/routes/api/group/create create-group-controller.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 1/1
100% Lines 11/11

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 361x   1x                           1x 16x 16x 16x   16x         3x     13x   13x     1x  
import {GroupService} from '@app/models';
import {RequestHandler} from 'express';
import {BadRequestError} from '@errors';
 
/**
 * Controller for handling create group request.
 *
 * Creates a new group with the name and description
 * in the body of the request.
 *
 * Sets the user which sent the request as the owner
 * of that group.
 * @param req  - Express request
 * @param res  - Express response
 * @param next - Next function
 */
const createGroupController: RequestHandler = async (req, res, next) => {
  const name = req.body.name;
  const description = req.body.description;
  const user = req.user;
 
  if (
    typeof name !== 'string' ||
    (typeof description !== 'string' && description) ||
     typeof user !== 'object'
  ) {
    throw new BadRequestError('Incorrect arguments');
  }
 
  const group = await GroupService.create(user, {name, description});
 
  res.status(201).send(group);
};
 
export default createGroupController;