using CMSMicroservice.Application.Common.Interfaces; using CMSMicroservice.Domain.Enums; namespace CMSMicroservice.Application.UserOrderCQ.Commands.UpdateOrderStatus; public class UpdateOrderStatusCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; private readonly ILogger _logger; public UpdateOrderStatusCommandHandler( IApplicationDbContext context, ILogger logger) { _context = context; _logger = logger; } public async Task Handle(UpdateOrderStatusCommand request, CancellationToken cancellationToken) { // TODO: پیاده‌سازی تغییر وضعیت سفارش // 1. پیدا کردن سفارش: // - await _context.UserOrders.FirstOrDefaultAsync(o => o.Id == request.OrderId) // - بررسی null و پرتاب NotFoundException // // 2. بررسی‌های انتقال وضعیت (State Transition Validation): // - نمی‌توان از Delivered به Cancelled رفت // - نمی‌توان از Cancelled به سایر وضعیت‌ها رفت // - الگوی معمول: Pending → Processing → Shipped → Delivered // - Cancelled می‌تواند از Pending, Processing, Shipped باشد // // 3. تغییر وضعیت: // - order.DeliveryStatus = request.NewStatus // - اگر Description داریم: order.DeliveryDescription = request.Description // - تنظیم تاریخ‌های مربوطه: // * اگر NewStatus == Delivered → order.DeliveredAt = DateTime.UtcNow // * اگر NewStatus == Shipped → order.ShippedAt = DateTime.UtcNow // * اگر NewStatus == Processing → order.ProcessedAt = DateTime.UtcNow // // 4. ذخیره و Log: // - await _context.SaveChangesAsync(cancellationToken) // - _logger.LogInformation("Order {OrderId} status changed to {NewStatus}", request.OrderId, request.NewStatus) // // 5. برگشت Response: // - Success = true // - Message = "وضعیت سفارش با موفقیت تغییر کرد" // - CurrentStatus = order.DeliveryStatus // // نکته: برای validation دقیق‌تر، می‌توان یک State Machine برای انتقال‌های مجاز تعریف کرد throw new NotImplementedException("UpdateOrderStatus needs implementation"); } }