All files / app/models/car car.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7

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 1161x   1x 1x               1x                                                                                                       1x                                           9x                                                         1x  
import {DataTypes, Model} from 'sequelize';
import {User} from '@models';
import {default as sequelize} from '@db';
import {CarColor} from './car-color';
 
/**
 * Model class for cars.
 *
 * A car can only exist in the context of a
 * group.
 */
export class Car extends Model {
  /**
   * Id of the group.
   */
  public readonly groupId!: number;
 
  /**
   * Id of the car.
   */
  public readonly carId!: number;
 
  /**
   * Name of the car.
   */
  public name!: string;
 
  /**
   * Date when the car was created.
   */
  public readonly createdAt!: Date;
 
  /**
   * Date when the car was last updated.
   */
  public readonly updatedAt!: Date;
 
  /**
   * The color of the car.
   */
  public color!: CarColor;
 
  /**
   * The id of the driver
   */
  public driverId!: number | null;
 
  /**
   * The driver.
   */
  public Driver?: User;
 
  /**
   * Latitude of the location of the car.
   */
  public latitude?: number;
 
  /**
   * Longitude of the location of the car.
   */
  public longitude?: number;
}
 
Car.init(
    {
      carId: {
        allowNull: false,
        type: DataTypes.INTEGER,
        primaryKey: true,
      },
      groupId: {
        allowNull: false,
        type: DataTypes.INTEGER,
        primaryKey: true,
      },
      name: {
        type: DataTypes.STRING(30),
        allowNull: false,
        validate: {
          notEmpty: true,
        },
      },
      color: {
        type: DataTypes.ENUM,
        values: Object.values(CarColor)
            .filter((value) => isNaN(Number(value))) as string[],
        allowNull: false,
      },
      latitude: DataTypes.DOUBLE,
      longitude: DataTypes.DOUBLE,
    },
    {
      sequelize,
      modelName: 'car',
      indexes: [
        /* Index which enforces that each car in a group has a unique name */
        {
          name: 'unique_name_per_group',
          unique: true,
          fields: ['groupId', 'name'],
        },
        /*
         * Index which enforces that a color
         * is only used one time within a group
         */
        {
          name: 'unique_color_per_group',
          unique: true,
          fields: ['groupId', 'color'],
        },
      ],
    },
);
 
export default Car;