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 targets = sequelize.define( 'targets', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, name: { type: DataTypes.TEXT, }, metric: { type: DataTypes.ENUM, values: [ "revenue", "deals_won", "calls", "meetings", "pipeline_amount" ], }, period: { type: DataTypes.ENUM, values: [ "weekly", "monthly", "quarterly", "yearly" ], }, target_value: { type: DataTypes.DECIMAL, }, 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, }, ); targets.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity //end loop db.targets.belongsTo(db.users, { as: 'owner_user', foreignKey: { name: 'owner_userId', }, constraints: false, }); db.targets.belongsTo(db.companies, { as: 'company', foreignKey: { name: 'companyId', }, constraints: false, }); db.targets.belongsTo(db.users, { as: 'createdBy', }); db.targets.belongsTo(db.users, { as: 'updatedBy', }); }; return targets; };