189 lines
4.1 KiB
JavaScript
189 lines
4.1 KiB
JavaScript
module.exports = (sequelize, DataTypes) => {
|
|
const Product = sequelize.define('Product', {
|
|
id: {
|
|
type: DataTypes.UUID,
|
|
defaultValue: DataTypes.UUIDV4,
|
|
primaryKey: true
|
|
},
|
|
name: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
validate: {
|
|
len: [2, 100]
|
|
}
|
|
},
|
|
description: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true
|
|
},
|
|
category: {
|
|
type: DataTypes.ENUM('compute', 'storage', 'networking', 'database', 'security', 'analytics', 'ai_ml', 'other'),
|
|
allowNull: false
|
|
},
|
|
subcategory: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true
|
|
},
|
|
sku: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
unique: true
|
|
},
|
|
basePrice: {
|
|
type: DataTypes.DECIMAL(10, 2),
|
|
allowNull: false,
|
|
validate: {
|
|
min: 0
|
|
}
|
|
},
|
|
currency: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
defaultValue: 'INR'
|
|
},
|
|
billingType: {
|
|
type: DataTypes.ENUM('one_time', 'recurring', 'usage_based', 'tiered'),
|
|
allowNull: false
|
|
},
|
|
billingCycle: {
|
|
type: DataTypes.ENUM('hourly', 'daily', 'weekly', 'monthly', 'quarterly', 'yearly'),
|
|
allowNull: true
|
|
},
|
|
unit: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true // e.g., 'GB', 'vCPU', 'instance', 'request'
|
|
},
|
|
specifications: {
|
|
type: DataTypes.JSON,
|
|
defaultValue: {}
|
|
},
|
|
features: {
|
|
type: DataTypes.JSON,
|
|
defaultValue: []
|
|
},
|
|
tierPricing: {
|
|
type: DataTypes.JSON,
|
|
defaultValue: {
|
|
bronze: { margin: 20 },
|
|
silver: { margin: 25 },
|
|
gold: { margin: 30 },
|
|
platinum: { margin: 35 },
|
|
diamond: { margin: 40 }
|
|
}
|
|
},
|
|
status: {
|
|
type: DataTypes.ENUM('active', 'inactive', 'deprecated', 'coming_soon'),
|
|
allowNull: false,
|
|
defaultValue: 'active'
|
|
},
|
|
availability: {
|
|
type: DataTypes.JSON,
|
|
defaultValue: {
|
|
regions: [],
|
|
zones: []
|
|
}
|
|
},
|
|
minimumCommitment: {
|
|
type: DataTypes.JSON,
|
|
allowNull: true
|
|
},
|
|
tags: {
|
|
type: DataTypes.JSON,
|
|
defaultValue: []
|
|
},
|
|
metadata: {
|
|
type: DataTypes.JSON,
|
|
defaultValue: {}
|
|
},
|
|
createdBy: {
|
|
type: DataTypes.UUID,
|
|
allowNull: true,
|
|
references: {
|
|
model: 'users',
|
|
key: 'id'
|
|
}
|
|
},
|
|
updatedBy: {
|
|
type: DataTypes.UUID,
|
|
allowNull: true,
|
|
references: {
|
|
model: 'users',
|
|
key: 'id'
|
|
}
|
|
}
|
|
}, {
|
|
tableName: 'products',
|
|
indexes: [
|
|
{
|
|
unique: true,
|
|
fields: ['sku']
|
|
},
|
|
{
|
|
fields: ['category']
|
|
},
|
|
{
|
|
fields: ['status']
|
|
},
|
|
{
|
|
fields: ['billing_type']
|
|
},
|
|
{
|
|
fields: ['name']
|
|
}
|
|
]
|
|
});
|
|
|
|
// Instance methods
|
|
Product.prototype.calculateResellerPrice = function(resellerTier, customMargin = null) {
|
|
let margin = customMargin;
|
|
|
|
if (!margin && this.tierPricing[resellerTier]) {
|
|
margin = this.tierPricing[resellerTier].margin;
|
|
}
|
|
|
|
if (!margin) {
|
|
margin = 20; // Default margin
|
|
}
|
|
|
|
const markupAmount = (this.basePrice * margin) / 100;
|
|
return parseFloat(this.basePrice) + markupAmount;
|
|
};
|
|
|
|
Product.prototype.getMarginForTier = function(tier) {
|
|
return this.tierPricing[tier]?.margin || 20;
|
|
};
|
|
|
|
Product.prototype.isAvailableInRegion = function(region) {
|
|
return this.availability.regions.length === 0 || this.availability.regions.includes(region);
|
|
};
|
|
|
|
Product.prototype.isActive = function() {
|
|
return this.status === 'active';
|
|
};
|
|
|
|
// Class methods
|
|
Product.associate = function(models) {
|
|
Product.belongsTo(models.User, {
|
|
foreignKey: 'createdBy',
|
|
as: 'creator'
|
|
});
|
|
|
|
Product.belongsTo(models.User, {
|
|
foreignKey: 'updatedBy',
|
|
as: 'updater'
|
|
});
|
|
|
|
Product.hasMany(models.ResellerPricing, {
|
|
foreignKey: 'productId',
|
|
as: 'resellerPricing'
|
|
});
|
|
|
|
Product.hasMany(models.OrderItem, {
|
|
foreignKey: 'productId',
|
|
as: 'orderItems'
|
|
});
|
|
};
|
|
|
|
return Product;
|
|
};
|