UNPKG

@wernerthiago/teo

Version:

Test Execution Optimizer

114 lines (90 loc) 2.74 kB
// Product management module export class ProductService { constructor() { this.products = new Map() this.categories = new Map() this.inventory = new Map() } async createProduct(name, description, price, categoryId) { const productId = this.generateProductId() const product = { id: productId, name, description, price, categoryId, createdAt: new Date(), updatedAt: new Date() } this.products.set(productId, product) this.inventory.set(productId, { quantity: 0, reserved: 0 }) return product } async updateProduct(productId, updates) { const product = this.products.get(productId) if (!product) { throw new Error('Product not found') } Object.assign(product, updates, { updatedAt: new Date() }) return product } async deleteProduct(productId) { const deleted = this.products.delete(productId) this.inventory.delete(productId) return deleted } async getProduct(productId) { return this.products.get(productId) } async searchProducts(query, categoryId = null, priceRange = null) { let results = Array.from(this.products.values()) if (query) { const searchTerm = query.toLowerCase() results = results.filter(product => product.name.toLowerCase().includes(searchTerm) || product.description.toLowerCase().includes(searchTerm) ) } if (categoryId) { results = results.filter(product => product.categoryId === categoryId) } if (priceRange) { results = results.filter(product => product.price >= priceRange.min && product.price <= priceRange.max ) } return results } async updateInventory(productId, quantity) { const inventory = this.inventory.get(productId) if (!inventory) { throw new Error('Product not found') } inventory.quantity = quantity inventory.updatedAt = new Date() return inventory } async reserveInventory(productId, quantity) { const inventory = this.inventory.get(productId) if (!inventory) { throw new Error('Product not found') } if (inventory.quantity - inventory.reserved < quantity) { throw new Error('Insufficient inventory') } inventory.reserved += quantity return inventory } async releaseInventory(productId, quantity) { const inventory = this.inventory.get(productId) if (!inventory) { throw new Error('Product not found') } inventory.reserved = Math.max(0, inventory.reserved - quantity) return inventory } generateProductId() { return 'prod_' + Math.random().toString(36).substring(2) + Date.now().toString(36) } } export default ProductService