| 1 | // A messy e-commerce product management system with tangled responsibilities |
| 2 | // Difficulty: 5/5 |
| 3 | |
| 4 | type ProductStatus = |
| 5 | | "active" |
| 6 | | "inactive" |
| 7 | | "discontinued" |
| 8 | | "out_of_stock" |
| 9 | | "backorder"; |
| 10 | type UserRole = "admin" | "manager" | "staff" | "customer"; |
| 11 | let DATABASE: any = { |
| 12 | products: new Map(), |
| 13 | users: new Map(), |
| 14 | cart: new Map(), |
| 15 | orders: [], |
| 16 | logs: [], |
| 17 | sessions: new Map(), |
| 18 | config: { taxRate: 0.08, discountThreshold: 1000 }, |
| 19 | }; |
| 20 | let CURRENT_USER: any = null; |
| 21 | let NOTIFICATIONS: any[] = []; |
| 22 | |
| 23 | // Global function to generate IDs (mixes concerns and not encapsulated) |
| 24 | function generateId(prefix: string) { |
| 25 | return `${prefix}_${Math.random().toString(36).substring(2, 9)}`; |
| 26 | } |
| 27 | |
| 28 | // Global function for logging (mixed with business logic) |
| 29 | function log(action: string, data: any) { |
| 30 | const timestamp = new Date().toISOString(); |
| 31 | DATABASE.logs.push({ |
| 32 | timestamp, |
| 33 | action, |
| 34 | data, |
| 35 | user: CURRENT_USER?.username || "system", |
| 36 | }); |
| 37 | console.log(`[${timestamp}] ${action}:`, data); |
| 38 | } |
| 39 | |
| 40 | class ProductManager { |
| 41 | // This class does way too much - violates SRP |
| 42 | static instance: ProductManager; |
| 43 | private constructor() {} |
| 44 | |
| 45 | // Singleton pattern used incorrectly |
| 46 | static getInstance() { |
| 47 | if (!ProductManager.instance) { |
| 48 | ProductManager.instance = new ProductManager(); |
| 49 | } |
| 50 | return ProductManager.instance; |
| 51 | } |
| 52 | |
| 53 | addProduct( |
| 54 | name: string, |
| 55 | price: number, |
| 56 | desc: string, |
| 57 | img: string, |
| 58 | category: string, |
| 59 | ) { |
| 60 | // Validation mixed with business logic |
| 61 | if (!name || price <= 0) { |
| 62 | throw new Error("Invalid product data"); |
| 63 | } |
| 64 | |
| 65 | // Authentication check shouldn't be here |
| 66 | if ( |
| 67 | !CURRENT_USER || |
| 68 | (CURRENT_USER.role !== "admin" && CURRENT_USER.role !== "manager") |
| 69 | ) { |
| 70 | throw new Error("Unauthorized access"); |
| 71 | } |
| 72 | |
| 73 | const id = generateId("prod"); |
| 74 | |
| 75 | // Direct database manipulation and notification logic mixed together |
| 76 | DATABASE.products.set(id, { |
| 77 | id, |
| 78 | name, |
| 79 | price, |
| 80 | description: desc, |
| 81 | image: img, |
| 82 | category, |
| 83 | status: "active", |
| 84 | created: new Date(), |
| 85 | createdBy: CURRENT_USER.id, |
| 86 | stock: 100, // Default hardcoded |
| 87 | ratings: [], |
| 88 | tags: category.split(",").map((t: string) => t.trim()), |
| 89 | lastModified: new Date(), |
| 90 | }); |
| 91 | |
| 92 | // Notification logic should be separated |
| 93 | NOTIFICATIONS.push({ |
| 94 | type: "product_added", |
| 95 | message: `New product added: ${name}`, |
| 96 | timestamp: new Date(), |
| 97 | }); |
| 98 | |
| 99 | // Logging should be a cross-cutting concern |
| 100 | log("product_added", { id, name, price }); |
| 101 | |
| 102 | // UI manipulation mixed with business logic |
| 103 | if (document) { |
| 104 | const productList = document.getElementById("product-list"); |
| 105 | if (productList) { |
| 106 | const productEl = document.createElement("div"); |
| 107 | productEl.innerHTML = `<div class="product">${name} - $${price}</div>`; |
| 108 | productList.appendChild(productEl); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Analytics tracking mixed in with product creation |
| 113 | if (window) { |
| 114 | try { |
| 115 | (window as any).ga("send", "event", "Products", "Create", name); |
| 116 | } catch (e) { |
| 117 | console.error("Analytics error", e); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return id; |
| 122 | } |
| 123 | |
| 124 | updateProduct(id: string, data: any) { |
| 125 | if (!DATABASE.products.has(id)) { |
| 126 | throw new Error("Product not found"); |
| 127 | } |
| 128 | |
| 129 | // Authentication check duplicated |
| 130 | if ( |
| 131 | !CURRENT_USER || |
| 132 | (CURRENT_USER.role !== "admin" && CURRENT_USER.role !== "manager") |
| 133 | ) { |
| 134 | throw new Error("Unauthorized access"); |
| 135 | } |
| 136 | |
| 137 | const product = DATABASE.products.get(id); |
| 138 | |
| 139 | // Updating with direct object manipulation |
| 140 | Object.keys(data).forEach((key) => { |
| 141 | if (key !== "id" && key !== "created" && key !== "createdBy") { |
| 142 | product[key] = data[key]; |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | product.lastModified = new Date(); |
| 147 | product.lastModifiedBy = CURRENT_USER.id; |
| 148 | |
| 149 | DATABASE.products.set(id, product); |
| 150 | |
| 151 | // More mixed responsibilities |
| 152 | log("product_updated", { id, changes: data }); |
| 153 | |
| 154 | // Duplicated notification logic |
| 155 | NOTIFICATIONS.push({ |
| 156 | type: "product_updated", |
| 157 | message: `Product updated: ${product.name}`, |
| 158 | timestamp: new Date(), |
| 159 | }); |
| 160 | |
| 161 | // Email notification logic mixed in |
| 162 | if (product.status === "discontinued") { |
| 163 | this.sendEmailToSubscribers( |
| 164 | product.id, |
| 165 | "Product Discontinued", |
| 166 | `The product ${product.name} has been discontinued.`, |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | return product; |
| 171 | } |
| 172 | |
| 173 | deleteProduct(id: string) { |
| 174 | // More authentication mixed in business logic |
| 175 | if (!CURRENT_USER || CURRENT_USER.role !== "admin") { |
| 176 | throw new Error("Only admins can delete products"); |
| 177 | } |
| 178 | |
| 179 | if (!DATABASE.products.has(id)) { |
| 180 | throw new Error("Product not found"); |
| 181 | } |
| 182 | |
| 183 | const product = DATABASE.products.get(id); |
| 184 | |
| 185 | // Business rules mixed with deletion logic |
| 186 | if (product.status === "active" && product.stock > 0) { |
| 187 | throw new Error("Cannot delete active product with stock"); |
| 188 | } |
| 189 | |
| 190 | // More mixed concerns |
| 191 | log("product_deleted", { id, name: product.name }); |
| 192 | NOTIFICATIONS.push({ |
| 193 | type: "product_deleted", |
| 194 | message: `Product deleted: ${product.name}`, |
| 195 | timestamp: new Date(), |
| 196 | }); |
| 197 | |
| 198 | DATABASE.products.delete(id); |
| 199 | |
| 200 | // Cache clearing mixed with business logic |
| 201 | if (window && (window as any).cache) { |
| 202 | (window as any).cache.invalidate("products"); |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | // Email functionality mixed in the product manager |
| 209 | sendEmailToSubscribers(productId: string, subject: string, body: string) { |
| 210 | const product = DATABASE.products.get(productId); |
| 211 | if (!product) return; |
| 212 | |
| 213 | // Email sending logic mixed with product management |
| 214 | console.log(`Sending email about ${product.name} to subscribers`); |
| 215 | // Imagine API call to email service here |
| 216 | } |
| 217 | |
| 218 | // Search functionality mixed in the same class |
| 219 | searchProducts(query: string, filters: any = {}) { |
| 220 | let results: any[] = []; |
| 221 | |
| 222 | DATABASE.products.forEach((product: any) => { |
| 223 | let matches = |
| 224 | product.name.toLowerCase().includes(query.toLowerCase()) || |
| 225 | product.description.toLowerCase().includes(query.toLowerCase()); |
| 226 | |
| 227 | if (!matches) return; |
| 228 | |
| 229 | // Filter logic mixed in search |
| 230 | if (filters.category && product.category !== filters.category) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (filters.minPrice && product.price < filters.minPrice) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | if (filters.maxPrice && product.price > filters.maxPrice) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (filters.status && product.status !== filters.status) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | results.push({ ...product }); |
| 247 | }); |
| 248 | |
| 249 | // Logging mixed with search functionality |
| 250 | log("product_search", { query, filters, resultCount: results.length }); |
| 251 | |
| 252 | return results; |
| 253 | } |
| 254 | |
| 255 | // Cart functionality shouldn't be in product manager |
| 256 | addToCart(productId: string, quantity: number = 1) { |
| 257 | if (!CURRENT_USER) { |
| 258 | throw new Error("Must be logged in to add to cart"); |
| 259 | } |
| 260 | |
| 261 | const product = DATABASE.products.get(productId); |
| 262 | if (!product) { |
| 263 | throw new Error("Product not found"); |
| 264 | } |
| 265 | |
| 266 | if (product.status !== "active") { |
| 267 | throw new Error("Product is not available"); |
| 268 | } |
| 269 | |
| 270 | if (product.stock < quantity) { |
| 271 | throw new Error("Not enough stock"); |
| 272 | } |
| 273 | |
| 274 | let cart = DATABASE.cart.get(CURRENT_USER.id) || []; |
| 275 | const existingItem = cart.find((item: any) => item.productId === productId); |
| 276 | |
| 277 | if (existingItem) { |
| 278 | existingItem.quantity += quantity; |
| 279 | } else { |
| 280 | cart.push({ |
| 281 | productId, |
| 282 | quantity, |
| 283 | price: product.price, |
| 284 | name: product.name, |
| 285 | addedAt: new Date(), |
| 286 | }); |
| 287 | } |
| 288 | |
| 289 | DATABASE.cart.set(CURRENT_USER.id, cart); |
| 290 | product.stock -= quantity; |
| 291 | DATABASE.products.set(productId, product); |
| 292 | |
| 293 | log("cart_add", { productId, quantity, userId: CURRENT_USER.id }); |
| 294 | |
| 295 | return cart; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Usage would be something like: |
| 300 | // const pm = ProductManager.getInstance(); |
| 301 | // pm.addProduct('Gaming Laptop', 1299.99, 'High performance gaming laptop', 'laptop.jpg', 'Electronics,Computers'); |
| 302 | // pm.addToCart('prod_1abc123', 2); |