236 lines
3.0 KiB
JavaScript
236 lines
3.0 KiB
JavaScript
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 leads = sequelize.define(
|
|
'leads',
|
|
{
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true,
|
|
},
|
|
|
|
title: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
description: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
status: {
|
|
type: DataTypes.ENUM,
|
|
|
|
|
|
|
|
values: [
|
|
|
|
"new",
|
|
|
|
|
|
"contacted",
|
|
|
|
|
|
"qualified",
|
|
|
|
|
|
"unqualified",
|
|
|
|
|
|
"converted"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
priority: {
|
|
type: DataTypes.ENUM,
|
|
|
|
|
|
|
|
values: [
|
|
|
|
"low",
|
|
|
|
|
|
"medium",
|
|
|
|
|
|
"high"
|
|
|
|
],
|
|
|
|
},
|
|
|
|
estimated_value: {
|
|
type: DataTypes.DECIMAL,
|
|
|
|
|
|
|
|
},
|
|
|
|
last_activity_at: {
|
|
type: DataTypes.DATE,
|
|
|
|
|
|
|
|
},
|
|
|
|
next_follow_up_at: {
|
|
type: DataTypes.DATE,
|
|
|
|
|
|
|
|
},
|
|
|
|
do_not_contact: {
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
defaultValue: false,
|
|
|
|
|
|
|
|
},
|
|
|
|
unqualified_reason: {
|
|
type: DataTypes.TEXT,
|
|
|
|
|
|
|
|
},
|
|
|
|
importHash: {
|
|
type: DataTypes.STRING(255),
|
|
allowNull: true,
|
|
unique: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: true,
|
|
paranoid: true,
|
|
freezeTableName: true,
|
|
},
|
|
);
|
|
|
|
leads.associate = (db) => {
|
|
|
|
|
|
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.leads.hasMany(db.deals, {
|
|
as: 'deals_lead',
|
|
foreignKey: {
|
|
name: 'leadId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
db.leads.hasMany(db.activities, {
|
|
as: 'activities_lead',
|
|
foreignKey: {
|
|
name: 'leadId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
db.leads.hasMany(db.notes, {
|
|
as: 'notes_lead',
|
|
foreignKey: {
|
|
name: 'leadId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//end loop
|
|
|
|
|
|
|
|
db.leads.belongsTo(db.organizations, {
|
|
as: 'organization',
|
|
foreignKey: {
|
|
name: 'organizationId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.leads.belongsTo(db.users, {
|
|
as: 'owner',
|
|
foreignKey: {
|
|
name: 'ownerId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.leads.belongsTo(db.companies, {
|
|
as: 'company',
|
|
foreignKey: {
|
|
name: 'companyId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.leads.belongsTo(db.contacts, {
|
|
as: 'primary_contact',
|
|
foreignKey: {
|
|
name: 'primary_contactId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
db.leads.belongsTo(db.lead_sources, {
|
|
as: 'lead_source',
|
|
foreignKey: {
|
|
name: 'lead_sourceId',
|
|
},
|
|
constraints: false,
|
|
});
|
|
|
|
|
|
|
|
|
|
db.leads.belongsTo(db.users, {
|
|
as: 'createdBy',
|
|
});
|
|
|
|
db.leads.belongsTo(db.users, {
|
|
as: 'updatedBy',
|
|
});
|
|
};
|
|
|
|
|
|
|
|
return leads;
|
|
};
|
|
|
|
|