Handler.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Application.Abstractions.Crypto;
  2. using Application.Abstractions.Data;
  3. using Application.Abstractions.Messaging;
  4. using Microsoft.EntityFrameworkCore;
  5. using SharedKernel.Results;
  6. namespace Application.Features.Admin.Store.Shipment.Get;
  7. public sealed class Handler(IAppDbContext db, IFieldEncryptor encryptor) : IQueryHandler<Query, Result<Response>>
  8. {
  9. public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
  10. {
  11. var shipment = await db.Shipment
  12. .AsNoTracking()
  13. .Include(s => s.Order).ThenInclude(o => o!.Member)
  14. .Where(s => s.ID == request.ID)
  15. .FirstOrDefaultAsync(ct);
  16. if (shipment is null)
  17. {
  18. return Result.Failure<Response>(Error.NotFound("Shipment.NotFound", "배송 정보를 찾을 수 없습니다."));
  19. }
  20. string? recipientName = null;
  21. string? phone = null;
  22. string? zipCode = null;
  23. string? address1 = null;
  24. string? address2 = null;
  25. if (request.WithAddress)
  26. {
  27. recipientName = encryptor.Decrypt(shipment.RecipientName_Encrypted, shipment.KeyVersion);
  28. phone = encryptor.Decrypt(shipment.Phone_Encrypted, shipment.KeyVersion);
  29. zipCode = shipment.ZipCode;
  30. address1 = encryptor.Decrypt(shipment.Address1_Encrypted, shipment.KeyVersion);
  31. address2 = encryptor.Decrypt(shipment.Address2_Encrypted, shipment.KeyVersion);
  32. }
  33. return Result.Success(new Response(
  34. ID: shipment.ID,
  35. OrderID: shipment.OrderID,
  36. OrderNumber: shipment.Order?.OrderNumber ?? "-",
  37. BuyerMemberID: shipment.Order?.MemberID ?? 0,
  38. BuyerEmail: shipment.Order?.Member?.Email ?? "-",
  39. Status: shipment.Status,
  40. Carrier: shipment.Carrier,
  41. TrackingNumber: shipment.TrackingNumber,
  42. ShippingFee: shipment.ShippingFee,
  43. AdminMemo: shipment.AdminMemo,
  44. CreatedAt: shipment.CreatedAt,
  45. ShippedAt: shipment.ShippedAt,
  46. DeliveredAt: shipment.DeliveredAt,
  47. RecipientName: recipientName,
  48. Phone: phone,
  49. ZipCode: zipCode,
  50. Address1: address1,
  51. Address2: address2
  52. ));
  53. }
  54. }