All files / app/models/group group.ts

100% Statements 18/18
100% Branches 0/0
100% Functions 3/3
100% Lines 18/18

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 1931x                     1x 1x 1x   1x   1x 1x           1x                                                                                   1x                                                                                                                               1x     86x         85x         85x   1x           1x         1x             1x                                                       1x  
import {
  Model,
  DataTypes,
  HasOneGetAssociationMixin,
  HasOneSetAssociationMixin,
  HasManySetAssociationsMixin,
  HasManyAddAssociationMixin,
  HasManyGetAssociationsMixin,
  HasManyCountAssociationsMixin,
  HasManyCreateAssociationMixin,
} from 'sequelize';
import {default as sequelize} from '@db';
import {InternalError} from '@errors';
import debug from 'debug';
import {ModelHooks} from 'sequelize/types/lib/hooks';
import {User, Membership, UserDto} from '@models';
 
const error = debug('group-car:group:error');
const log = debug('group-car:group');
 
 
/**
 * Model for groups.
 */
export class Group extends Model {
/**
   * Id of the group.
   *
   * Primary key.
   */
  public id!: number;
 
  /**
   * The name of the group.
   *
   * Multiple groups can have the same name.
   */
  public name!: string;
 
  /**
   * The description
   */
  public description!: string;
 
  /**
   * The userId of the owner.
   *
   * In the beginning this id will reference the user which created the group.
   * But the owner can transfer his/her ownership to another user of the group.
   */
  public ownerId!: number;
 
  /**
   * Date when the group was created.
   */
  public readonly createdAt!: Date;
 
  /**
   * Date when the group was last updated.
   */
  public readonly updatedAt!: Date;
 
  /**
   * List of attributes which should be used if group reference is eagerly
   * loaded.
   */
  public static simpleAttributes = [
    'id',
    'name',
    'description',
    'ownerId',
    'createdAt',
    'updatedAt',
  ];
 
  /**
   * Gets the owner.
   */
  public getOwner!: HasOneGetAssociationMixin<User>;
 
  /**
   * Sets the owner.
   */
  public setOwner!: HasOneSetAssociationMixin<User, number>;
 
  /**
   * Set users.
   */
  public setUsers!: HasManySetAssociationsMixin<User, number>;
 
  /**
   * Get users.
   */
  public getUsers!: HasManyGetAssociationsMixin<User>;
 
  /**
   * Add user to list.
   */
  public addUser!: HasManyAddAssociationMixin<User, number>;
 
  /**
   * Add users.
   */
  public addUsers!: HasManyAddAssociationMixin<User[], number>;
 
  /**
   * Get amount of users.
   */
  public countUsers!: HasManyCountAssociationsMixin;
 
  /**
   * Create new user for list.
   */
  public createUser!: HasManyCreateAssociationMixin<User>;
 
  /**
   * User data of the owner.
   *
   * Only exists if explicitly included in query.
   */
  public readonly Owner?: UserDto;
}
 
/**
 * Creates a new membership for the owner/creator of the group for that
 * group.
 *
 * Gives the owner/creator admin permissions.
 * @param group - The newly created group
 */
export const createMembershipForOwner = (
    group: Group,
): Promise<void> => {
  return Membership.create({
    groupId: group.id,
    userId: group.ownerId,
    isAdmin: true,
  }).then(() => {
    log(
        'Created admin membership for user %d and group %d',
        group.ownerId,
        group.id,
    );
    return;
  }).catch((err) => {
    error(
        'Couldn\'t create admin membership for user %d and group %d, error: %o',
        group.ownerId,
        group.id,
        err,
    );
    throw new InternalError();
  });
};
 
// Create the hooks object
const hooks: Partial<ModelHooks> = {
  afterCreate: createMembershipForOwner,
};
 
/**
 * Initialize the group model.
 */
Group.init(
    {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER,
      },
      name: {
        allowNull: false,
        type: DataTypes.STRING(30),
        validate: {
          notEmpty: true,
        },
      },
      description: {
        allowNull: true,
        type: DataTypes.STRING(200),
      },
    },
    {
      sequelize,
      modelName: 'group',
      hooks: hooks,
    },
);
 
 
export default Group;