39454-vm/backend/src/db/models/customer_profiles.js
2026-04-03 13:51:50 +00:00

168 lines
2.2 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 customer_profiles = sequelize.define(
'customer_profiles',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
company_name: {
type: DataTypes.TEXT,
},
vat_number: {
type: DataTypes.TEXT,
},
billing_address: {
type: DataTypes.TEXT,
},
shipping_address: {
type: DataTypes.TEXT,
},
account_status: {
type: DataTypes.ENUM,
values: [
"prospect",
"active",
"on_hold",
"blacklisted"
],
},
credit_limit: {
type: DataTypes.DECIMAL,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
customer_profiles.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.customer_profiles.hasMany(db.shopping_carts, {
as: 'shopping_carts_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.customer_profiles.hasMany(db.orders, {
as: 'orders_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
db.customer_profiles.hasMany(db.quotes, {
as: 'quotes_customer',
foreignKey: {
name: 'customerId',
},
constraints: false,
});
//end loop
db.customer_profiles.belongsTo(db.users, {
as: 'user',
foreignKey: {
name: 'userId',
},
constraints: false,
});
db.customer_profiles.belongsTo(db.users, {
as: 'createdBy',
});
db.customer_profiles.belongsTo(db.users, {
as: 'updatedBy',
});
};
return customer_profiles;
};