/**
* Represents a product variation,such as size or flavor options.
*/
class Variation {
/**
* Creates a new Variation instance.
* @param {string} variationId - Unique identifier for the variation.
* @param {string} productId - Associated product ID.
* @param {string} name - Name of the variation (e.g., Large, Extra Spicy).
* @param {number} [priceAdjustment=0] - Additional price (+/-) for this variation.
* @param {boolean} [isAvailable=true] -Availability status of the variation.
* @param {object} [attributes={}] - Custom attributes for the variation (e.g., size, spice level).
* @param {number} [stock=0] - Stock quantity for the variation.
* @param {number} [preparationTime=0] - Additional preparation time in minutes.
* @param {boolean} [isDefault=false] - Marks if this variation is the default option.
*/
constructor(variationId, productId, name, priceAdjustment = 0, isAvailable = true, attributes = {}, stock = 0, preparationTime = 0, isDefault = false) {
this.variationId = variationId; // Unique identifier for the variation
this.productId = productId; // Associated product ID
this.name = name; // Name of the variation (e.g., Large, Extra Spicy)
this.priceAdjustment = priceAdjustment; // Additional price (+/-) for this variation
this.isAvailable = isAvailable; // Availability status
this.attributes = attributes; // Custom attributes (e.g., size, spice level)
this.stock = stock >= 0 ? stock : 0; // Ensure stock is zero if negative
this.preparationTime = preparationTime >=0 ? preparationTime: 0; //Ensure preparation time is zero if negative
this.isDefault = isDefault; // Marks the default variation
this.createdAt = new Date();
this.updatedAt = new Date();
}
/**
* Updates the details of the variation.
* @param {string} name - New name for the variation.
* @param {number} priceAdjustment - New price adjustment value.
* @param {boolean} isAvailable - New availability status.
* @param {object} attributes - Updated custom attributes.
* @param {number} stock - Updated stock quantity.
* @param {number} preparationTime - Updated preparation time in minutes.
* @param {boolean} isDefault - Updated default status.
*/
updateDetails(name, priceAdjustment, isAvailable, attributes, stock, preparationTime, isDefault) {
this.name = name;
this.priceAdjustment = priceAdjustment;
this.isAvailable = isAvailable;
this.attributes = attributes;
this.stock = stock >= 0 ? stock : 0; //prevent negative stock
this.preparationTime = preparationTime >=0 ? preparationTime: 0; //prevent negative preparation time
this.isDefault = isDefault;
this.updatedAt = new Date();
}
/**
* Toggles the availability status of the variation.
*/
toggleAvailability() {
this.isAvailable = !this.isAvailable;
this.updatedAt = new Date();
}
/**
* Adjusts the stock quantity by a specified amount.
* @param {number} quantity - The amount to adjust stock by (positive or negative).
*/
adjustStock(quantity) {
this.stock = Math.max(0, this.stock + quantity); //prevent negative stock
this.updatedAt = new Date();
}
/**
* Calculates the final price based on a base price and the variation's price adjustment.
* @param {number} basePrice - The base price of the product.
* @returns {number} The final price after applying the variation's price adjustment.
*/
getFinalPrice(basePrice) {
return Math.max(0,basePrice + this.priceAdjustment);
}
}
module.exports = Variation;