All files / app/routes/api/group/group-id/invite invite-user-to-group-controller.ts

100% Statements 15/15
100% Branches 8/8
100% Functions 1/1
100% Lines 15/15

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 45 46 47 48 49 501x     1x                 1x           18x 18x 18x 18x   18x               5x           13x 2x   11x     13x   6x     1x  
import {
  BadRequestError,
} from '@errors';
import {GroupService} from '@models';
import {RequestHandler} from 'express';
 
/**
 * Invites the user to the group by creating a database entry.
 * @param req   - Request
 * @param res   - Response
 * @param _next  - Next
 */
export const inviteUserController: RequestHandler = async (
  req,
  res,
  _next,
) => {
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  const user = req.user;
  const userId = parseInt(req.body.userId, 10);
  const username = req.body.username;
  const groupId = parseInt(req.params.groupId, 10);
 
  if (
    typeof user !== 'object' ||
    isNaN(groupId) ||
    (
      isNaN(userId) &&
      typeof username !== 'string'
    )
  ) {
    throw new BadRequestError('Incorrect Parameters');
  }
 
  // UserId will take precedence
  let idOrUsername: number | string;
 
  if (isNaN(userId)) {
    idOrUsername = username;
  } else {
    idOrUsername = userId;
  }
 
  const invite = await GroupService.inviteUser(user, groupId, idOrUsername);
 
  res.status(201).send(invite);
};
 
export default inviteUserController;