<commit_before>"use strict";

const
  moment  = require('moment'),
  _       = require('underscore'),
  Promise = require('bluebird'),
  Exception = require('../../error'),
  CalendarMonth = require('../calendar_month');

module.exports = function(sequelize, DataTypes) {
  let Department = sequelize.define("Department", {
    // TODO add validators!
    name : {
      type      : DataTypes.STRING,
      allowNull : false
    },
    allowance : {
      type         : DataTypes.INTEGER,
      allowNull    : false,
      defaultValue : 20,
    },
    include_public_holidays : {
      type         : DataTypes.BOOLEAN,
      allowNull    : false,
      defaultValue : true,
    },
    is_accrued_allowance : {
      type         : DataTypes.BOOLEAN,
      allowNull    : false,
      defaultValue : false,
    },
  }, {
      indexes : [
        {
          fields : ['companyId'],
        },
        {
          fields : ['id'],
        }
      ],
      classMethods: {
          loadScope : function( models ) {

            Department.addScope(
              'with_simple_users',
              {
                include : [
                  { model : models.User, as : 'users' },
                ],
              }
            );

            Department.addScope(
              'with_boss',
              {
                include : [
                  { model : models.User, as : 'boss' },
                ]
              }
            );

            Department.addScope(
              'with_supervisors',
              {
                include : [
                  { model : models.User, as : 'supervisors' },
                ]
              }
            );
          },
          associate : function( models ) {
            // We have constrains OFF as to prevent ORM complaining about
            // cycle reference
            Department.belongsTo ( models.User,    { as : 'boss', constraints: false});
            Department.belongsTo ( models.Company, { as : 'company'});
            Department.hasMany   ( models.User,    { as : 'users'});

            Department.hasMany( models.DepartmentSupervisor, {
              as         : 'supervisors_link',
              foreignKey : 'department_id',
            });

            Department.belongsToMany(models.User, {
              as         : 'supervisors',
              foreignKey : 'department_id',
              otherKey   : 'user_id',
              through    : models.DepartmentSupervisor,
            });<commit_msg>add constraint do department_id foreign key on department.js<commit_after>"use strict";

const
  moment  = require('moment'),
  _       = require('underscore'),
  Promise = require('bluebird'),
  Exception = require('../../error'),
  CalendarMonth = require('../calendar_month');

module.exports = function(sequelize, DataTypes) {
  let Department = sequelize.define("Department", {
    // TODO add validators!
    name : {
      type      : DataTypes.STRING,
      allowNull : false
    },
    allowance : {
      type         : DataTypes.INTEGER,
      allowNull    : false,
      defaultValue : 20,
    },
    include_public_holidays : {
      type         : DataTypes.BOOLEAN,
      allowNull    : false,
      defaultValue : true,
    },
    is_accrued_allowance : {
      type         : DataTypes.BOOLEAN,
      allowNull    : false,
      defaultValue : false,
    },
  }, {
      indexes : [
        {
          fields : ['companyId'],
        },
        {
          fields : ['id'],
        }
      ],
      classMethods: {
          loadScope : function( models ) {

            Department.addScope(
              'with_simple_users',
              {
                include : [
                  { model : models.User, as : 'users' },
                ],
              }
            );

            Department.addScope(
              'with_boss',
              {
                include : [
                  { model : models.User, as : 'boss' },
                ]
              }
            );

            Department.addScope(
              'with_supervisors',
              {
                include : [
                  { model : models.User, as : 'supervisors' },
                ]
              }
            );
          },
          associate : function( models ) {
            // We have constrains OFF as to prevent ORM complaining about
            // cycle reference
            Department.belongsTo ( models.User,    { as : 'boss', constraints: false});
            Department.belongsTo ( models.Company, { as : 'company'});
            Department.hasMany   ( models.User,    { as : 'users'});

            Department.hasMany( models.DepartmentSupervisor, {
              as         : 'supervisors_link',
              foreignKey : {name: 'department_id', allowNull: false},
            });

            Department.belongsToMany(models.User, {
              as         : 'supervisors',
              foreignKey : 'department_id',
              otherKey   : 'user_id',
              through    : models.DepartmentSupervisor,
            });
{'fraction_non_alphanumeric': 0.11196834817012859, 'fraction_numerical': 0.000791295746785361, 'mean_word_length': 1.2877828054298643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3189752', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1295, 'n_words': 252}