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 harvests = sequelize.define( 'harvests', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, harvested_at: { type: DataTypes.DATE, }, total_weight_kg: { type: DataTypes.DECIMAL, }, total_count: { type: DataTypes.INTEGER, }, avg_weight_g: { type: DataTypes.DECIMAL, }, grade: { type: DataTypes.ENUM, values: [ "small", "medium", "large", "mixed" ], }, notes: { type: DataTypes.TEXT, }, importHash: { type: DataTypes.STRING(255), allowNull: true, unique: true, }, }, { timestamps: true, paranoid: true, freezeTableName: true, }, ); harvests.associate = (db) => { /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity db.harvests.hasMany(db.marketplace_listings, { as: 'marketplace_listings_harvest', foreignKey: { name: 'harvestId', }, constraints: false, }); //end loop db.harvests.belongsTo(db.tenants, { as: 'tenant', foreignKey: { name: 'tenantId', }, constraints: false, }); db.harvests.belongsTo(db.batches, { as: 'batch', foreignKey: { name: 'batchId', }, constraints: false, }); db.harvests.belongsTo(db.users, { as: 'recorded_by', foreignKey: { name: 'recorded_byId', }, constraints: false, }); db.harvests.belongsTo(db.organizations, { as: 'organizations', foreignKey: { name: 'organizationsId', }, constraints: false, }); db.harvests.belongsTo(db.users, { as: 'createdBy', }); db.harvests.belongsTo(db.users, { as: 'updatedBy', }); }; return harvests; };