const config = require('../../config'); const providers = config.providers; const crypto = require('crypto'); const bcrypt = require('bcrypt'); const moment = require('moment'); module.exports = function(sequelize, DataTypes) { const projects = sequelize.define( 'projects', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, description: { type: DataTypes.TEXT, }, status: { type: DataTypes.ENUM, values: [ "planned", "active", "on_hold", "completed", "archived" ], }, start_at: { type: DataTypes.DATE, }, end_at: { type: DataTypes.DATE, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); projects.associate = (db) => { db.projects.belongsToMany(db.users, { as: 'members', foreignKey: { name: 'projects_membersId', }, constraints: false, through: 'projectsMembersUsers', }); db.projects.belongsToMany(db.users, { as: 'members_filter', foreignKey: { name: 'projects_membersId', }, constraints: false, through: 'projectsMembersUsers', }); /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.projects.belongsTo(db.users, { as: 'owner', foreignKey: { name: 'ownerId', }, constraints: false, }); db.projects.hasMany(db.file, { as: 'attachments', foreignKey: 'belongsToId', constraints: false, scope: { belongsTo: db.projects.getTableName(), belongsToColumn: 'attachments', }, }); db.projects.belongsTo(db.users, { as: 'createdBy', }); db.projects.belongsTo(db.users, { as: 'updatedBy', }); }; return projects; };