diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 1573033..a16cb12 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -41,7 +41,8 @@ "Bash(/tmp/fix-celery-beat.sh:*)", "Bash(/tmp/bypass-hdwallet-unlock.sh:*)", "Bash(/tmp/fix-db-initialization.sh:*)", - "SlashCommand(/code-review)" + "SlashCommand(/code-review)", + "Read(//mnt/c/Production/Source/SilverLABS/SilverPAY.NET/**)" ], "deny": [], "ask": [] diff --git a/LittleShop/Program.cs b/LittleShop/Program.cs index a98af9e..fb78a9e 100644 --- a/LittleShop/Program.cs +++ b/LittleShop/Program.cs @@ -223,7 +223,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddHttpClient(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddScoped(); diff --git a/LittleShop/Services/TeleBotMessagingService.cs b/LittleShop/Services/TeleBotMessagingService.cs index 7086d57..49088bd 100644 --- a/LittleShop/Services/TeleBotMessagingService.cs +++ b/LittleShop/Services/TeleBotMessagingService.cs @@ -1,9 +1,7 @@ -using System.Text; -using System.Text.Json; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using LittleShop.Data; +using LittleShop.DTOs; using LittleShop.Enums; using LittleShop.Models; @@ -12,25 +10,17 @@ namespace LittleShop.Services; public class TeleBotMessagingService : ITeleBotMessagingService { private readonly LittleShopContext _context; - private readonly IConfiguration _configuration; + private readonly ICustomerMessageService _customerMessageService; private readonly ILogger _logger; - private readonly HttpClient _httpClient; - private readonly string? _teleBotApiUrl; - private readonly string? _teleBotApiKey; public TeleBotMessagingService( LittleShopContext context, - IConfiguration configuration, - ILogger logger, - HttpClient httpClient) + ICustomerMessageService customerMessageService, + ILogger logger) { _context = context; - _configuration = configuration; + _customerMessageService = customerMessageService; _logger = logger; - _httpClient = httpClient; - - _teleBotApiUrl = _configuration["TeleBot:ApiUrl"]; - _teleBotApiKey = _configuration["TeleBot:ApiKey"]; } public async Task SendOrderStatusUpdateAsync(Guid orderId, OrderStatus newStatus) @@ -58,7 +48,7 @@ public class TeleBotMessagingService : ITeleBotMessagingService $"📦 Total: £{order.TotalAmount:F2}\n" + $"⏱️ Expected processing: Within 24 hours"; - return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message); + return await QueueMessageAsync(order.Customer.Id, orderId, MessageType.OrderUpdate, "Payment Confirmed", message); } public async Task SendOrderAcceptedAsync(Guid orderId) @@ -72,7 +62,7 @@ public class TeleBotMessagingService : ITeleBotMessagingService $"⏱️ Expected packing: Within 24 hours\n" + $"🚚 We'll notify you when it's dispatched"; - return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message); + return await QueueMessageAsync(order.Customer.Id, orderId, MessageType.OrderUpdate, "Order Accepted", message); } public async Task SendOrderPackingAsync(Guid orderId) @@ -85,7 +75,7 @@ public class TeleBotMessagingService : ITeleBotMessagingService $"🚚 We'll send tracking details once dispatched.\n" + $"⏱️ Expected dispatch: Later today"; - return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message); + return await QueueMessageAsync(order.Customer.Id, orderId, MessageType.OrderUpdate, "Order Packing", message); } public async Task SendOrderDispatchedAsync(Guid orderId, string? trackingNumber = null) @@ -103,7 +93,7 @@ public class TeleBotMessagingService : ITeleBotMessagingService $"⏱️ Estimated delivery: 1-3 working days\n" + $"📍 Track your package for real-time updates"; - return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message); + return await QueueMessageAsync(order.Customer.Id, orderId, MessageType.ShippingInfo, "Order Dispatched", message); } public async Task SendOrderDeliveredAsync(Guid orderId) @@ -116,7 +106,7 @@ public class TeleBotMessagingService : ITeleBotMessagingService $"⭐ Please consider leaving a review using the /review command.\n" + $"🛒 Thank you for choosing us for your order!"; - return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message); + return await QueueMessageAsync(order.Customer.Id, orderId, MessageType.OrderUpdate, "Order Delivered", message); } public async Task SendOrderOnHoldAsync(Guid orderId, string? reason = null) @@ -133,7 +123,7 @@ public class TeleBotMessagingService : ITeleBotMessagingService $"💬 Please contact support if you have any questions.\n" + $"⏱️ We'll resolve this as quickly as possible"; - return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message); + return await QueueMessageAsync(order.Customer.Id, orderId, MessageType.OrderUpdate, "Order On Hold", message); } public async Task SendTestMessageAsync(Guid customerId, string message) @@ -142,25 +132,14 @@ public class TeleBotMessagingService : ITeleBotMessagingService if (customer == null) return false; var testMessage = $"🧪 *Test Message*\n\n{message}"; - return await SendTeleBotMessageAsync(customer.TelegramUserId, testMessage); + return await QueueMessageAsync(customerId, null, MessageType.CustomerService, "Test Message", testMessage); } public async Task IsAvailableAsync() { - if (string.IsNullOrEmpty(_teleBotApiUrl) || string.IsNullOrEmpty(_teleBotApiKey)) - { - return false; - } - - try - { - var response = await _httpClient.GetAsync($"{_teleBotApiUrl}/health"); - return response.IsSuccessStatusCode; - } - catch - { - return false; - } + // TeleBot is always "available" since it polls for messages from the database queue + // No need to check HTTP endpoint availability + return true; } private async Task GetOrderWithCustomerAsync(Guid orderId) @@ -170,48 +149,40 @@ public class TeleBotMessagingService : ITeleBotMessagingService .FirstOrDefaultAsync(o => o.Id == orderId); } - private async Task SendTeleBotMessageAsync(long telegramUserId, string message) + private async Task QueueMessageAsync(Guid customerId, Guid? orderId, MessageType type, string subject, string content) { - if (!await IsAvailableAsync()) - { - _logger.LogWarning("TeleBot API not available, skipping message to user {UserId}", telegramUserId); - return false; - } - try { - var requestData = new + var createMessageDto = new CreateCustomerMessageDto { - userId = telegramUserId, - message = message, - parseMode = "Markdown" + CustomerId = customerId, + OrderId = orderId, + Type = type, + Subject = subject, + Content = content, + Priority = 5, // Normal priority + IsUrgent = false }; - var json = JsonSerializer.Serialize(requestData); - var content = new StringContent(json, Encoding.UTF8, "application/json"); + var result = await _customerMessageService.CreateMessageAsync(createMessageDto); - // Add API key header - _httpClient.DefaultRequestHeaders.Clear(); - _httpClient.DefaultRequestHeaders.Add("X-API-Key", _teleBotApiKey); - - var response = await _httpClient.PostAsync($"{_teleBotApiUrl}/api/messages/send", content); - - if (response.IsSuccessStatusCode) + if (result != null) { - _logger.LogInformation("Successfully sent TeleBot message to user {UserId}", telegramUserId); + _logger.LogInformation("Queued message {MessageId} for customer {CustomerId} - {Subject}", + result.Id, customerId, subject); return true; } else { - var responseContent = await response.Content.ReadAsStringAsync(); - _logger.LogWarning("Failed to send TeleBot message to user {UserId}: {StatusCode} - {Response}", - telegramUserId, response.StatusCode, responseContent); + _logger.LogWarning("Failed to queue message for customer {CustomerId} - {Subject}", + customerId, subject); return false; } } catch (Exception ex) { - _logger.LogError(ex, "Error sending TeleBot message to user {UserId}", telegramUserId); + _logger.LogError(ex, "Error queuing message for customer {CustomerId} - {Subject}", + customerId, subject); return false; } }