waengine 1.7.3 → 1.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,362 @@
1
+ import { getStorage } from "./storage.js";
2
+
3
+ export class BusinessManager {
4
+ constructor(client) {
5
+ this.client = client;
6
+ this.storage = getStorage();
7
+ this.profile = null;
8
+ this.products = new Map();
9
+ this.orders = new Map();
10
+
11
+ // Load existing data
12
+ this.loadBusinessData();
13
+ }
14
+
15
+ // ===== BUSINESS PROFILE MANAGEMENT =====
16
+
17
+ /**
18
+ * Set business profile information
19
+ */
20
+ async setProfile(profileData) {
21
+ // Parameter-Validierung
22
+ if (!profileData || typeof profileData !== 'object') {
23
+ throw new Error('profileData muss ein Objekt sein');
24
+ }
25
+
26
+ const profile = {
27
+ name: profileData.name || "My Business",
28
+ category: profileData.category || "Technology",
29
+ description: profileData.description || "WhatsApp Bot Service",
30
+ website: profileData.website || "",
31
+ email: profileData.email || "",
32
+ phone: profileData.phone || "",
33
+ address: profileData.address || "",
34
+ hours: profileData.hours || "24/7",
35
+ logo: profileData.logo || "",
36
+ verified: profileData.verified || false,
37
+ created: new Date(),
38
+ updated: new Date()
39
+ };
40
+
41
+ this.profile = profile;
42
+ this.storage.write.in("business").set("profile", profile);
43
+
44
+ return profile;
45
+ }
46
+
47
+ /**
48
+ * Get business profile
49
+ */
50
+ getProfile() {
51
+ return this.profile || this.storage.read.from("business").get("profile");
52
+ }
53
+
54
+ /**
55
+ * Update business profile
56
+ */
57
+ async updateProfile(updates) {
58
+ const currentProfile = this.getProfile() || {};
59
+ const updatedProfile = {
60
+ ...currentProfile,
61
+ ...updates,
62
+ updated: new Date()
63
+ };
64
+
65
+ return await this.setProfile(updatedProfile);
66
+ }
67
+
68
+ // ===== PRODUCT CATALOG MANAGEMENT =====
69
+
70
+ /**
71
+ * Create a new product
72
+ */
73
+ async createProduct(productData) {
74
+ const product = {
75
+ id: productData.id || `prod_${Date.now()}`,
76
+ name: productData.name,
77
+ description: productData.description || "",
78
+ price: productData.price || 0,
79
+ currency: productData.currency || "EUR",
80
+ category: productData.category || "General",
81
+ images: productData.images || [],
82
+ inStock: productData.inStock !== false,
83
+ stockCount: productData.stockCount || 0,
84
+ sku: productData.sku || "",
85
+ tags: productData.tags || [],
86
+ created: new Date(),
87
+ updated: new Date()
88
+ };
89
+
90
+ this.products.set(product.id, product);
91
+ this.storage.write.in("business").set(`products.${product.id}`, product);
92
+
93
+ return product;
94
+ }
95
+
96
+ /**
97
+ * Get product by ID
98
+ */
99
+ getProduct(productId) {
100
+ return this.products.get(productId) ||
101
+ this.storage.read.from("business").get(`products.${productId}`);
102
+ }
103
+
104
+ /**
105
+ * Get all products
106
+ */
107
+ getAllProducts() {
108
+ const storedProducts = this.storage.read.from("business").get("products") || {};
109
+ return Object.values(storedProducts);
110
+ }
111
+
112
+ /**
113
+ * Update product
114
+ */
115
+ async updateProduct(productId, updates) {
116
+ const product = this.getProduct(productId);
117
+ if (!product) throw new Error(`Product ${productId} not found`);
118
+
119
+ const updatedProduct = {
120
+ ...product,
121
+ ...updates,
122
+ updated: new Date()
123
+ };
124
+
125
+ this.products.set(productId, updatedProduct);
126
+ this.storage.write.in("business").set(`products.${productId}`, updatedProduct);
127
+
128
+ return updatedProduct;
129
+ }
130
+
131
+ /**
132
+ * Delete product
133
+ */
134
+ async deleteProduct(productId) {
135
+ this.products.delete(productId);
136
+ this.storage.delete.from("business").key(`products.${productId}`);
137
+ return true;
138
+ }
139
+
140
+ /**
141
+ * Search products
142
+ */
143
+ searchProducts(query, filters = {}) {
144
+ const products = this.getAllProducts();
145
+
146
+ return products.filter(product => {
147
+ // Text search
148
+ if (query) {
149
+ const searchText = query.toLowerCase();
150
+ const matchesName = product.name.toLowerCase().includes(searchText);
151
+ const matchesDescription = product.description.toLowerCase().includes(searchText);
152
+ const matchesTags = product.tags.some(tag => tag.toLowerCase().includes(searchText));
153
+
154
+ if (!matchesName && !matchesDescription && !matchesTags) {
155
+ return false;
156
+ }
157
+ }
158
+
159
+ // Category filter
160
+ if (filters.category && product.category !== filters.category) {
161
+ return false;
162
+ }
163
+
164
+ // Price range filter
165
+ if (filters.minPrice && product.price < filters.minPrice) {
166
+ return false;
167
+ }
168
+ if (filters.maxPrice && product.price > filters.maxPrice) {
169
+ return false;
170
+ }
171
+
172
+ // In stock filter
173
+ if (filters.inStock !== undefined && product.inStock !== filters.inStock) {
174
+ return false;
175
+ }
176
+
177
+ return true;
178
+ });
179
+ }
180
+
181
+ // ===== PAYMENT INTEGRATION =====
182
+
183
+ /**
184
+ * Send payment request
185
+ */
186
+ async sendPaymentRequest(chatId, amount, currency = "EUR", description = "", productId = null) {
187
+ const paymentRequest = {
188
+ id: `pay_${Date.now()}`,
189
+ chatId: chatId,
190
+ amount: amount,
191
+ currency: currency,
192
+ description: description,
193
+ productId: productId,
194
+ status: "pending",
195
+ created: new Date()
196
+ };
197
+
198
+ // Store payment request
199
+ this.storage.write.in("business").set(`payments.${paymentRequest.id}`, paymentRequest);
200
+
201
+ // Send payment message (simplified - in real implementation would use WhatsApp Pay)
202
+ const message = `💳 **Zahlungsanfrage**\n\n` +
203
+ `💰 Betrag: ${amount} ${currency}\n` +
204
+ `📝 Beschreibung: ${description}\n` +
205
+ `🆔 Payment ID: ${paymentRequest.id}\n\n` +
206
+ `Bitte kontaktieren Sie uns für die Zahlung.`;
207
+
208
+ await this.client.socket.sendMessage(chatId, { text: message });
209
+
210
+ return paymentRequest;
211
+ }
212
+
213
+ /**
214
+ * Process payment (webhook simulation)
215
+ */
216
+ async processPayment(paymentId, status = "completed") {
217
+ const payment = this.storage.read.from("business").get(`payments.${paymentId}`);
218
+ if (!payment) throw new Error(`Payment ${paymentId} not found`);
219
+
220
+ payment.status = status;
221
+ payment.processed = new Date();
222
+
223
+ this.storage.write.in("business").set(`payments.${paymentId}`, payment);
224
+
225
+ // Notify customer
226
+ const message = status === "completed"
227
+ ? `✅ Zahlung erfolgreich! Payment ID: ${paymentId}`
228
+ : `❌ Zahlung fehlgeschlagen. Payment ID: ${paymentId}`;
229
+
230
+ await this.client.socket.sendMessage(payment.chatId, { text: message });
231
+
232
+ return payment;
233
+ }
234
+
235
+ // ===== ORDER MANAGEMENT =====
236
+
237
+ /**
238
+ * Create order
239
+ */
240
+ async createOrder(chatId, items, customerInfo = {}) {
241
+ const order = {
242
+ id: `order_${Date.now()}`,
243
+ chatId: chatId,
244
+ items: items, // [{ productId, quantity, price }]
245
+ customerInfo: customerInfo,
246
+ total: items.reduce((sum, item) => sum + (item.price * item.quantity), 0),
247
+ status: "pending",
248
+ created: new Date(),
249
+ updated: new Date()
250
+ };
251
+
252
+ this.orders.set(order.id, order);
253
+ this.storage.write.in("business").set(`orders.${order.id}`, order);
254
+
255
+ return order;
256
+ }
257
+
258
+ /**
259
+ * Get order
260
+ */
261
+ getOrder(orderId) {
262
+ return this.orders.get(orderId) ||
263
+ this.storage.read.from("business").get(`orders.${orderId}`);
264
+ }
265
+
266
+ /**
267
+ * Update order status
268
+ */
269
+ async updateOrderStatus(orderId, status) {
270
+ const order = this.getOrder(orderId);
271
+ if (!order) throw new Error(`Order ${orderId} not found`);
272
+
273
+ order.status = status;
274
+ order.updated = new Date();
275
+
276
+ this.orders.set(orderId, order);
277
+ this.storage.write.in("business").set(`orders.${orderId}`, order);
278
+
279
+ // Notify customer
280
+ const statusMessages = {
281
+ confirmed: "✅ Bestellung bestätigt",
282
+ processing: "⚙️ Bestellung wird bearbeitet",
283
+ shipped: "🚚 Bestellung versendet",
284
+ delivered: "📦 Bestellung zugestellt",
285
+ cancelled: "❌ Bestellung storniert"
286
+ };
287
+
288
+ const message = `📋 **Bestellstatus Update**\n\n` +
289
+ `🆔 Bestellung: ${orderId}\n` +
290
+ `📊 Status: ${statusMessages[status] || status}`;
291
+
292
+ await this.client.socket.sendMessage(order.chatId, { text: message });
293
+
294
+ return order;
295
+ }
296
+
297
+ // ===== BUSINESS ANALYTICS =====
298
+
299
+ /**
300
+ * Get business statistics
301
+ */
302
+ getBusinessStats() {
303
+ const products = this.getAllProducts();
304
+ const orders = Object.values(this.storage.read.from("business").get("orders") || {});
305
+ const payments = Object.values(this.storage.read.from("business").get("payments") || {});
306
+
307
+ return {
308
+ products: {
309
+ total: products.length,
310
+ inStock: products.filter(p => p.inStock).length,
311
+ categories: [...new Set(products.map(p => p.category))].length
312
+ },
313
+ orders: {
314
+ total: orders.length,
315
+ pending: orders.filter(o => o.status === "pending").length,
316
+ completed: orders.filter(o => o.status === "delivered").length,
317
+ totalRevenue: orders
318
+ .filter(o => o.status === "delivered")
319
+ .reduce((sum, o) => sum + o.total, 0)
320
+ },
321
+ payments: {
322
+ total: payments.length,
323
+ completed: payments.filter(p => p.status === "completed").length,
324
+ pending: payments.filter(p => p.status === "pending").length,
325
+ totalAmount: payments
326
+ .filter(p => p.status === "completed")
327
+ .reduce((sum, p) => sum + p.amount, 0)
328
+ }
329
+ };
330
+ }
331
+
332
+ // ===== HELPER METHODS =====
333
+
334
+ /**
335
+ * Load business data from storage
336
+ */
337
+ loadBusinessData() {
338
+ this.profile = this.storage.read.from("business").get("profile");
339
+
340
+ const products = this.storage.read.from("business").get("products") || {};
341
+ Object.entries(products).forEach(([id, product]) => {
342
+ this.products.set(id, product);
343
+ });
344
+
345
+ const orders = this.storage.read.from("business").get("orders") || {};
346
+ Object.entries(orders).forEach(([id, order]) => {
347
+ this.orders.set(id, order);
348
+ });
349
+ }
350
+
351
+ /**
352
+ * Export business data
353
+ */
354
+ exportData() {
355
+ return {
356
+ profile: this.profile,
357
+ products: Object.fromEntries(this.products),
358
+ orders: Object.fromEntries(this.orders),
359
+ stats: this.getBusinessStats()
360
+ };
361
+ }
362
+ }