All files / app/models/car car-service.ts

97.52% Statements 118/121
96.43% Branches 27/28
100% Functions 7/7
97.48% Lines 116/119

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 4441x 1x                       1x                     1x 1x 1x         1x         1x         1x                                                   29x 29x   29x             29x   29x     3x 3x           26x 1x 1x         25x 25x         25x 2x 2x       60x 23x 2x 2x           58x 21x 2x 2x     19x             19x   19x   19x             19x   19x                                 6x 6x   6x     6x 6x 6x   3x 2x 2x   1x   1x     3x   3x                                                 8x 8x   8x   8x 8x 2x 2x       6x 6x 6x   6x         5x 2x 2x     3x 3x           3x 3x             3x   3x   3x 3x 2x 2x   1x             1x                                                     8x 8x   8x 8x           8x   8x 2x         2x       6x 6x 6x 6x     6x 2x         2x     4x   4x                   3x             3x         3x             3x   3x   3x 2x   1x 1x                                                                 9x 9x   9x     9x 9x           6x 2x 2x     4x   4x   2x     2x                                   1x  
import config from '@app/config';
import {
  CarColorAlreadyInUseError,
  CarInUseError,
  CarNameAlreadyInUseError,
  CarNotFoundError,
  InternalError,
  MaxCarAmountReachedError,
  MembershipNotFoundError,
  NotAdminOfGroupError,
  NotDriverOfCarError,
  NotMemberOfGroupError,
} from '@app/errors';
import {
  Car,
  CarColor,
  CarQueryOptions,
  CarRepository,
  GroupCarAction,
  GroupNotificationService,
  Membership,
  MembershipRepository,
  MembershipService,
} from '@models';
import debug from 'debug';
import sequelize from '@db';
import {bindUser} from '@util/user-bound-logging';
 
/**
 * Logger.
 */
const log = debug('group-car:car:service');
 
/**
 * Error logger.
 */
const error = debug('group-car:car:service:error');
 
/**
 * Service for all car operations.
 */
export const CarService = {
  /**
   * Create a new car.
   *
   * @throws {@link NotAdminOfGroupError}
   * if the current user is not an
   * admin of the specified group.
   * @throws {@link CarNameAlreadyInUseError}
   * if a car with the specified
   * name already exists for the specified group.
   * @throws {@link CarColorAlreadyInUseError}
   * if the color is already used
   * for a car in the group.
   * @param currentUser - Currently logged-in user
   * @param groupId - ID of the group this car belongs to
   * @param name    - Name of the car
   * @param color   - Color of the car
   * @param options - Options for the return value
   */
  async create(
      currentUser: Express.User,
      groupId: number,
      name: string,
      color: CarColor,
      options?: Partial<CarQueryOptions>,
  ): Promise<Car> {
    const userLog = bindUser(log, currentUser.id);
    const userError = bindUser(error, currentUser.id);
 
    userError(
        'create car for group %d with name $s',
        groupId,
        name,
    );
 
    let membership: Membership;
    try {
      // Check if user is admin of the group
      membership = await MembershipRepository
          .findById({groupId, userId: currentUser.id});
    } catch (e) {
      Eif (e instanceof MembershipNotFoundError) {
        throw new NotAdminOfGroupError();
      } else {
        throw e;
      }
    }
 
    if (!membership.isAdmin) {
      userError('Not admin of group %d', currentUser.id, groupId);
      throw new NotAdminOfGroupError();
    }
 
    // Check if group has less than max-amount of cars
    let cars;
    try {
      cars = await CarRepository.findByGroup(groupId);
    } catch (e) {
      userLog('Error while getting cars of group %d: ', groupId, e);
      throw new InternalError('Couldn\'t create car');
    }
    if (cars.length >= config.group.maxCars) {
      userError('Group %d has max amount of cars', groupId);
      throw new MaxCarAmountReachedError(cars.length);
    }
 
    // Check if name already used.
    const nameUsed = cars.some((car) => car.name === name);
    if (nameUsed) {
      userError('Car with name %s already exists in group %d', name, groupId);
      throw new CarNameAlreadyInUseError(name);
    }
 
    /**
     * Check if color already used.
     */
    const colorUsed = cars.some((car) => car.color === color);
    if (colorUsed) {
      userError('Car with color %s already exists in group %d', color, groupId);
      throw new CarColorAlreadyInUseError(color);
    }
 
    userLog(
        'create car for group %d with name %s',
        groupId,
        name,
    );
 
    // Create car
    const car = await CarRepository.create(groupId, name, color, options);
 
    userLog('Successfully created car %d in group %d', car.carId, groupId);
 
    GroupNotificationService.notifyCarUpdate(
        groupId,
        car.carId,
        GroupCarAction.Add,
        car,
    );
 
    userLog('Send notification that new car was created');
 
    return car.get({plain: true}) as Car;
  },
 
  /**
   * Find all cars of the specified group.
   *
   * Throws {@link NotMemberOfGroupError} if the current user is not
   * a member of the specified group.
   * @param currentUser   - The currently logged-in user
   * @param groupId       - The id of the group
   * @param options       - Query options
   */
  async findByGroup(
      currentUser: Express.User,
      groupId: number,
      options?: Partial<CarQueryOptions>,
  ): Promise<Car[]> {
    const userLog = bindUser(log, currentUser.id);
    const userError = bindUser(error, currentUser.id);
 
    userLog('Find group %d', groupId);
 
    // Check if current user is a member of the group
    userLog('Check if user member of group %d', groupId);
    try {
      await MembershipRepository.findById({groupId, userId: currentUser.id});
    } catch (e) {
      if (e instanceof MembershipNotFoundError) {
        userError('User is not a member of group %d', groupId);
        throw new NotMemberOfGroupError();
      } else {
        userError('Unexpected error while checking membership of ' +
          'user to group %d: %o', groupId, e);
        throw e;
      }
    }
    userLog('User is member of group, find group %d', groupId);
 
    return CarRepository.findByGroup(groupId, options);
  },
 
  /**
   * Try to register the current user
   * as driver of the specified car.
   *
   * A user can only be registered if
   * the user is not the driver of any
   * other car and if the car doesn't currently
   * have another driver.
   *
   * Throws {@link NotMemberOfGroupError} if the current user
   * is not a member of the group with the specified `groupId`.
   * Throws {@link CarInUseError} if the specified car is
   * used by a user. (`driverId` is not `null`)
   * @param currentUser - The currently logged-in user
   * @param groupId     - The id of the group
   * @param carId       - The id of the car
   */
  async driveCar(
      currentUser: Express.User,
      groupId: number,
      carId: number,
  ): Promise<void> {
    const userLog = bindUser(log, currentUser.id);
    const userError = bindUser(log, currentUser.id);
 
    userLog('Drive car %d of group %d', carId, groupId);
 
    userLog('Check if user is member of the group', groupId);
    if (!await MembershipService.isMember(currentUser, groupId)) {
      userError('User is not a member of the group', groupId);
      throw new NotMemberOfGroupError();
    }
 
    // Start transaction
    const t = await sequelize.transaction();
    try {
      userLog('Get car %d of group %d', carId, groupId);
      // Get car and check if it is already used
      const car = await CarRepository.findById(
          {groupId, carId},
          {transaction: t},
      );
 
      if (car.driverId !== null) {
        userError('Car is already in use');
        throw new CarInUseError();
      }
 
      userLog('Set user as driver of car %d of group %d', carId, groupId);
      const updatedCar = await car.update({
        driverId: currentUser.id,
        latitude: null,
        longitude: null,
      }, {transaction: t});
 
      userLog('Send notification that car is being driven');
      GroupNotificationService.notifyCarUpdate(
          groupId,
          carId,
          GroupCarAction.Drive,
          updatedCar.get({plain: true}) as Car,
      );
 
      await t.commit();
    } catch (e) {
      userError('Some error occurred while trying to set' +
        ' driver, rollback all changes');
      await t.rollback();
      if (e instanceof CarNotFoundError || e instanceof CarInUseError) {
        userError('Expected error occurred');
        throw e;
      } else {
        error(
            'Error while setting user as driver for car %d of group %d: %o',
            currentUser.id,
            carId,
            groupId,
            e,
        );
        throw new InternalError('Error while registering a driver');
      }
    }
  },
 
  /**
   * Parks the car at the specified location if the
   * user is the current driver.
   *
   * Throws {@link NotMemberOfGroupError} if the current
   * user is not a member of the group with the
   * specified `groupId`.
   * Throws {@link NotDriverOfCarError} if the current
   * user is not the driver of the car.
   * @param currentUser - The logged-in user
   * @param groupId     - ID of the group
   * @param carId       - ID of the car
   * @param latitude    - Latitude of the location
   * @param longitude   - Longitude of the location
   */
  async parkCar(
      currentUser: Express.User,
      groupId: number,
      carId: number,
      latitude: number,
      longitude: number,
  ): Promise<void> {
    const userLog = bindUser(log, currentUser.id);
    const userError = bindUser(error, currentUser.id);
 
    const carPk = {carId, groupId};
    userLog(
        'request to park car %o to location %o',
        carPk,
        {latitude, longitude},
    );
 
    userLog('Check if user is a member of group %d', groupId);
    // Check if user is member of group
    if (!(await MembershipService.isMember(currentUser, groupId))) {
      userError(
          'can\'t park car %o, user not member of group %d',
          carPk,
          groupId,
      );
      throw new NotMemberOfGroupError();
    }
 
    // Get car and check driver
    const t = await sequelize.transaction();
    try {
      userLog('Check if user is driver of car %d of group %d', carId, groupId);
      const car = await CarRepository
          .findById(carPk, {transaction: t});
 
      if (car.driverId !== currentUser.id) {
        userError(
            'can\'t park car %o, user is not driver of car',
            currentUser.id,
            carPk,
        );
        throw new NotDriverOfCarError();
      }
 
      userLog('Set new location of car %d of group %d', carId, groupId);
 
      const updatedCar = await car.update(
          {
            driverId: null,
            latitude,
            longitude,
          },
          {
            transaction: t,
          },
      );
      userLog(
          'User %d: Successfully parked car %o at location %o',
          currentUser.id,
          carPk,
          {latitude, longitude},
      );
 
      userLog(
          'Send notification that location changed of car %d of group %d',
          carId,
          groupId,
      );
      GroupNotificationService.notifyCarUpdate(
          groupId,
          carId,
          GroupCarAction.Park,
          updatedCar.get({plain: true}) as Car,
      );
 
      t.commit();
    } catch (e) {
      t.rollback();
 
      if (e instanceof NotDriverOfCarError) {
        throw e;
      } else {
        userError('Error while parking car %o: %s', carPk, e);
        throw new InternalError('Couldn\'t park car');
      }
    }
  },
 
  /**
   * Delete a car of a group if user has permission.
   *
   * Before deleting a car this method checks if
   * the `currentUser` is a member and an admin
   * of the specified group. If so, the car
   * is deleted. After the successful deletion, a delete
   * event is emitted with {@link GroupNotificationService}.
   *
   * ***Note:*** *The deletion event only includes `groupId`
   * and `carId` and no other field.*
   *
   * @param currentUser - Currently logged-in user
   * @param groupId - ID of the group
   * @param carId - ID of the car
   *
   * @throws {@link NotMemberOfGroupError}
   * If `currentUser` is not a member of the group
   * @throws {@link NotAdminOfGroupError}
   * If `currentUser` is not an admin of the group
   * @throws {@link CarNotFoundError}
   * if there is no car with the specified id
   */
  async delete(
      currentUser: Express.User,
      groupId: number,
      carId: number,
  ): Promise<void> {
    const userLog = bindUser(log, currentUser.id);
    const userError = bindUser(error, currentUser.id);
 
    userLog('Requesting to delete car %d of group %d', carId, groupId);
 
    // Get membership of user in group
    userLog('Check if user is an admin of the group %d', groupId);
    const membership = await MembershipService.findById(
        currentUser,
        {groupId, userId: currentUser.id},
    );
 
    // Check if user is an admin
    if (!membership.isAdmin) {
      userError('User is not an admin of group %d', groupId);
      throw new NotAdminOfGroupError();
    }
 
    userLog('Delete car %d of group %d', carId, groupId);
    // User is allowed to delete the car
    await CarRepository.delete({groupId, carId});
 
    userLog('Send notification that car %d ' +
      'of group % was deleted', carId, groupId);
    // Notify of the deletion
    GroupNotificationService.notifyCarUpdate(
        groupId,
        carId,
        GroupCarAction.Delete,
        /*
         * This should be enough data for a client to know which car is deleted.
         * But other fields of the car are missing, which has to be taken into
         * account on the client. Therefore, it should be noted in the
         * documentation.
         */
        {
          carId,
          groupId,
        } as Car,
    );
  },
};
 
export default CarService;