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

100% Statements 14/14
100% Branches 4/4
100% Functions 1/1
100% Lines 14/14

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 36 37 38 39 40 41 42 43 44 451x 1x 1x           1x         1x               1x 26x 26x   26x 24x   24x                   19x   2x 2x      
import {BadRequestError} from '@errors';
import {CarService} from '@models';
import debug from 'debug';
import {RequestHandler} from 'express';
 
/**
 * Logger.
 */
const log = debug('group-car:group:car:create');
 
/**
 * Error logger.
 */
const error = debug('group-car:group:car:create:error');
 
/**
 * Controller for handling creating a new car for the specified group.
 * @param req   - Request
 * @param res   - Response
 * @param next  - Next
 */
export const createCarController: RequestHandler = async (req, res, next) => {
  const currentUser = req.user;
  const groupId = parseInt(req.params.groupId, 10);
 
  if (typeof currentUser === 'object' && !isNaN(groupId)) {
    log('User %d: create car for group %d', currentUser.id, groupId);
 
    const group = await CarService.create(
        currentUser,
        groupId,
        req.body.name,
        req.body.color,
        {
          withDriverData: false,
        },
    );
 
    res.status(201).send(group);
  } else {
    error('At least one parameter is missing');
    throw new BadRequestError();
  }
};