using Application.Abstractions.Crypto; using Application.Abstractions.Data; using Application.Abstractions.Messaging; using Microsoft.EntityFrameworkCore; using SharedKernel.Results; namespace Application.Features.Admin.Store.Shipment.Get; public sealed class Handler(IAppDbContext db, IFieldEncryptor encryptor) : IQueryHandler> { public async Task> Handle(Query request, CancellationToken ct) { var shipment = await db.Shipment .AsNoTracking() .Include(s => s.Order).ThenInclude(o => o!.Member) .Where(s => s.ID == request.ID) .FirstOrDefaultAsync(ct); if (shipment is null) { return Result.Failure(Error.NotFound("Shipment.NotFound", "배송 정보를 찾을 수 없습니다.")); } string? recipientName = null; string? phone = null; string? zipCode = null; string? address1 = null; string? address2 = null; if (request.WithAddress) { recipientName = encryptor.Decrypt(shipment.RecipientName_Encrypted, shipment.KeyVersion); phone = encryptor.Decrypt(shipment.Phone_Encrypted, shipment.KeyVersion); zipCode = shipment.ZipCode; address1 = encryptor.Decrypt(shipment.Address1_Encrypted, shipment.KeyVersion); address2 = encryptor.Decrypt(shipment.Address2_Encrypted, shipment.KeyVersion); } return Result.Success(new Response( ID: shipment.ID, OrderID: shipment.OrderID, OrderNumber: shipment.Order?.OrderNumber ?? "-", BuyerMemberID: shipment.Order?.MemberID ?? 0, BuyerEmail: shipment.Order?.Member?.Email ?? "-", Status: shipment.Status, Carrier: shipment.Carrier, TrackingNumber: shipment.TrackingNumber, ShippingFee: shipment.ShippingFee, AdminMemo: shipment.AdminMemo, CreatedAt: shipment.CreatedAt, ShippedAt: shipment.ShippedAt, DeliveredAt: shipment.DeliveredAt, RecipientName: recipientName, Phone: phone, ZipCode: zipCode, Address1: address1, Address2: address2 )); } }