database_DatabaseDefinitions.js

const Sequelize = require('sequelize');
const options = {
    paranoid: true,
    timestamps: true,
};

/**
 * @namespace Database
 * @memberOf Myintranet
 */

/**
 * creation od all database models and joins them with eachothers
 * @memberof Myintranet.Database
 */
class DatabaseDefinitions {


    constructor(connexion) {
        this.conn = connexion
        //tables column definition
        this.userCols = require('./models/User');
        this.consultantCols = require('./models/Consultant');
        this.employeurCols = require('./models/Employeur');
        this.employeur2ConsultantCols = require('./models/Employeur2Consultant');
        this.clientCols = require('./models/Client');
        this.crahistoCols = require('./models/CraHistory');
        this.prevhistoCols = require('./models/PrevHistory');
        this.ActionCols = require('./models/Action');
        this.MissionfilesCols=require('./models/Missionfiles');

        this.securedLinkCols = require('./models/SecuredLink');

        this.employeur2ClientCols = require('./models/Employeur2Client');
        this.consultant2ClientCols = require('./models/Consultant2Client');
        this.consultantFilesCols = require('./models/ConsultantFiles');
        this.SMSHistoCols = require('./models/SMSHisto');
        this.BugsCols = require('./models/Bug');
        this.BugsFilesCols = require('./models/BugFiles');
        this.CoptationsCols = require("./models/Coptations");
        this.GarantieCols = require("./models/Garantie")

        //this.contract2CraCols = require('./models/contract2Cra');
        this.define();
    }

    /**
     *
     * @returns {Object} The sequelize connection object
     */
    getConnexion() {
        return this.conn;
    }

    define() {
        //tables definitions
        this.Coptations = this.conn.define('Coptations', this.CoptationsCols, options);
        this.User = this.conn.define('Utilisateur', this.userCols, options);
        this.Consultant = this.conn.define('Consultant', this.consultantCols, options);
        this.Employeur = this.conn.define('Employeur', this.employeurCols, options);
        this.Action = this.conn.define('Action', this.ActionCols, options);
        this.Employeur2Consultant = this.conn.define('Employeur2Consultant', this.employeur2ConsultantCols, options);
        this.Client = this.conn.define('Client', this.clientCols, options);
        this.Bugs = this.conn.define('Bug', this.BugsCols, options);
        this.BugsFiles = this.conn.define('BugFile', this.BugsFilesCols, options);
        this.Missionfiles=this.conn.define('Missionfile',this.MissionfilesCols,options)
        this.Garantie=this.conn.define('Garantie',this.GarantieCols,options)

        this.User.hasMany(this.Bugs);
        this.Bugs.belongsTo(this.User);

        this.Bugs.hasMany(this.BugsFiles);
        this.BugsFiles.belongsTo(this.Bugs);

        this.Consultant.hasOne(this.User);
        this.User.belongsTo(this.Consultant);



        this.Employeur.hasMany(this.Consultant);
        this.Consultant.belongsToMany(this.Employeur, { through: this.Employeur2Consultant })


        this.Employeur.belongsToMany(this.Consultant, { through: this.Employeur2Consultant })

        this.Employeur2Client = this.conn.define('ContratPrestationClient', this.employeur2ClientCols, options);

        this.Consultant2Client = this.conn.define('Mission', this.consultant2ClientCols, options);
        this.SecuredLink = this.conn.define('securedLink', this.securedLinkCols, options);

        this.ConsultantFiles = this.conn.define('consultantFile', this.consultantFilesCols, options);
        this.SMSHisto =  this.conn.define('SMSHisto', this.SMSHistoCols, options);
        this.Consultant.hasMany(this.ConsultantFiles)
        this.ConsultantFiles.belongsTo(this.Consultant)

        this.User.hasMany(this.SMSHisto);
        this.SMSHisto.belongsTo(this.User);

        // this.Consultant2Client.hasMany(SecuredLink)
        // this.SecuredLink.belongsTo(this.Consultant2Client)
        this.User.hasMany(this.Action);
        this.Action.belongsTo(this.User)

        this.SecuredLink.belongsTo(this.Consultant2Client);
        this.Consultant2Client.hasMany(this.SecuredLink);

        this.User.hasMany(this.SecuredLink )
        this.SecuredLink.belongsTo(this.User)

        this.Client.belongsToMany(this.Employeur, { through: this.Employeur2Client })
        this.Employeur.belongsToMany(this.Client, { through: this.Employeur2Client })




        // this.Client.belongsToMany(this.Consultant, { through: this.Consultant2Client })
        // this.Consultant.belongsToMany(this.Client, { through: this.Consultant2Client })

        this.Consultant.hasMany(this.Consultant2Client)
        this.Consultant2Client.belongsTo(this.Consultant)
        this.Consultant2Client.belongsTo(this.Client)
        this.Consultant2Client.belongsTo(this.Employeur)
        this.CraHistory = this.conn.define('CraHistory', this.crahistoCols, options);


       // this.CraHistory.belongsTo(this.Consultant);
        this.CraHistory.belongsTo(this.Consultant2Client);
        this.Consultant2Client.hasMany(this.CraHistory);

        this.PrevHistory = this.conn.define('PrevHistory', this.prevhistoCols, options);
      //  this.PrevHistory.belongsTo(this.Consultant);
        this.PrevHistory.belongsTo(this.Consultant2Client);
        this.Consultant2Client.hasMany(this.PrevHistory)


        this.Employeur2Admin= this.conn.define('Employeur2Admin', {});


        //this.Employeur.hasMany(this.User);

        this.User.belongsToMany(this.Employeur,{ through: this.Employeur2Admin });
        this.Employeur.belongsToMany(this.User,{ through: this.Employeur2Admin });
        this.Mission =  this.Consultant2Client;

        this.Mission.hasMany(this.Missionfiles )
        this.Missionfiles.belongsTo(this.Mission)
        this.Employeur.hasMany(this.Coptations)
        this.Coptations.belongsTo(this.Employeur)
       

    }


    /**
     * @returns {object} this object contains a refenence to db models defined by sequelize
     */
    getDb() {
        return {
            Sequelize,
            sequelize: this.conn,
            Action:this.Action,
            Employeur2Admin:this.Employeur2Admin,
            Mission:this.Mission,
            User: this.User,
            Utilisateur: this.User,
            Employeur: this.Employeur,
            Consultant: this.Consultant,
            
            Consultant2Client:this.Consultant2Client,
            Client:this.Client,
            Employeur2Client:this.Employeur2Client,
            PrevHistory:this.PrevHistory,
            CraHistory:this.CraHistory,
            Coptations:this.Coptations,
            SecuredLink:this.SecuredLink,
            ConsultantFiles:this.ConsultantFiles,
            SMSHisto:this.SMSHisto,
            Bugs:this.Bugs,
            BugsFiles:this.BugsFiles,
            Missionfiles:this.Missionfiles,
            Garantie:this.Garantie
        }
    }
}


module.exports = DatabaseDefinitions;