Skip to content

Commit

Permalink
Merge pull request #108 from SaintAngeLs/external_api_integration
Browse files Browse the repository at this point in the history
Full integration with external API
  • Loading branch information
eggwhat authored Jan 12, 2024
2 parents df63045 + 7a57fec commit f580468
Show file tree
Hide file tree
Showing 115 changed files with 1,838 additions and 217 deletions.
18 changes: 16 additions & 2 deletions SwiftParcel.API.Gateway/src/SwiftParcel.API.Gateway/ntrada.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extensions:
- Total-Count

jwt:
issuerSigningKey: eiquief5phee9pazo0Faegaez9gohThailiur5woy2befiech1oarai4aiLi6ahVecah3ie9Aiz6Peij
issuerSigningKey: eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTY5ODYwMTg3NSwiaWF0IjoxNjk4NjAxODc1fQ.nxJlaEy9xNO4noVh84qD9BSoLvjq1xu4LGhwBaEF9Ec
validIssuer: swiftparcel
validateAudience: false
validateIssuer: true
Expand Down Expand Up @@ -122,7 +122,21 @@ modules:
downstream: orders-service/orders?customerId=@user_id
auth: true

- upstream: /orders/office-worker
- upstream: /requests
method: GET
use: downstream
downstream: orders-service/orders/requests?customerId=@user_id
auth: true

- upstream: /office-worker/pending
method: GET
use: downstream
downstream: orders-service/orders/office-worker/pending
auth: true
claims:
role: officeworker

- upstream: /office-worker
method: GET
use: downstream
downstream: orders-service/orders/office-worker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public static async Task Main(string[] args)
.UseDispatcherEndpoints(endpoints => endpoints
.Get("", ctx => ctx.Response.WriteAsync(ctx.RequestServices.GetService<AppOptions>().Name))
.Get<GetParcelExpirationStatus, ExpirationStatusDto>("parcels/{parcelId}/offer")
.Get<GetOrderRequests, IEnumerable<OrderDto>>("orders/requests")
.Get<GetOrders, IEnumerable<OrderDto>>("orders")
.Post<AddParcel>("parcels")
.Post<CreateOrder>("orders")
.Post<ConfirmOrder>("orders/{orderId}/confirm")
.Post<CancelOrder>("orders/{orderId}/cancel")
))
.UseLogging()
.UseVault()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Convey.CQRS.Commands;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.Commands
{
public class CancelOrder: ICommand
{
public Guid OrderId { get; set; }
public CancelOrder(Guid orderId)
{
OrderId = orderId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Convey.CQRS.Commands;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.Commands
{
public class ConfirmOrder: ICommand
{
public Guid OrderId { get; set; }
public ConfirmOrder(Guid orderId)
{
OrderId = orderId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Convey.CQRS.Commands;
using SwiftParcel.ExternalAPI.Lecturer.Core.Entities;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.Commands
{
public class CreateOrder: ICommand
{
public Guid OrderId { get; }
public Guid CustomerId { get; }
public Guid ParcelId { get; }
public string Name { get; }
public string Email { get; }
public Address Address { get; }

public CreateOrder(Guid orderId, Guid customerId, Guid parcelId, string name, string email, Address address)
{
OrderId = orderId;
CustomerId = customerId;
ParcelId = parcelId;
Name = name;
Email = email;
Address = address;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public async Task HandleAsync(AddParcel command, CancellationToken cancellationT
var pickupDate = DateTime.Parse(command.PickupDate);
var delivaryDate = DateTime.Parse(command.DeliveryDate);
var inquiry = new InquiryDto(command.Weight, command.Height, command.Depth, _dimensionUnit, _currency, command.Weight,
_weightUnit, command.SourceStreet, command.SourceBuildingNumber, command.SourceApartmentNumber, command.SourceCity,
command.SourceZipCode, command.SourceCountry, command.DestinationStreet, command.DestinationBuildingNumber,
command.DestinationApartmentNumber, command.DestinationCity, command.DestinationZipCode, command.DestinationCountry,
_weightUnit, command.SourceBuildingNumber, command.SourceApartmentNumber, command.SourceStreet, command.SourceCity,
command.SourceZipCode, command.SourceCountry, command.DestinationBuildingNumber, command.DestinationApartmentNumber,
command.DestinationStreet, command.DestinationCity, command.DestinationZipCode, command.DestinationCountry,
pickupDate, delivaryDate, command.AtWeekend, command.Priority, command.IsCompany, command.VipPackage);
var response = await _inquiresServiceClient.PostAsync(token, inquiry);
if(response == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Convey.CQRS.Commands;
using SwiftParcel.ExternalAPI.Lecturer.Application.Services.Clients;
using SwiftParcel.ExternalAPI.Lecturer.Core.Repositories;
using SwiftParcel.ExternalAPI.Lecturer.Application.Exceptions;
using SwiftParcel.ExternalAPI.Lecturer.Core.Entities;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.Commands.Handlers
{
public class CancelOrderHandler : ICommandHandler<CancelOrder>
{
private readonly IIdentityManagerServiceClient _identityManagerServiceClient;
private readonly IOfferSnippetRepository _offerSnippetRepository;
private readonly IOffersServiceClient _officeServiceClient;

public CancelOrderHandler(IIdentityManagerServiceClient identityManagerServiceClient,
IOfferSnippetRepository offerSnippetRepository, IOffersServiceClient officeServiceClient)
{
_identityManagerServiceClient = identityManagerServiceClient;
_offerSnippetRepository = offerSnippetRepository;
_officeServiceClient = officeServiceClient;
}

public async Task HandleAsync(CancelOrder command, CancellationToken cancellationToken)
{
var offer = await _offerSnippetRepository.GetAsync(command.OrderId);
if (offer is null)
{
throw new OfferNotFoundException(command.OrderId);
}
if(offer.Status != OfferSnippetStatus.Approved)
{
throw new OfferNotApprovedException(command.OrderId, offer.Status);
}


var token = await _identityManagerServiceClient.GetToken();
var response = await _officeServiceClient.DeleteCancelOffer(token, offer.OfferId.ToString());
if(response is null)
{
throw new OffersServiceConnectionException();
}
if(!response.IsSuccessStatusCode)
{
throw new OffersServiceException(response.ReasonPhrase);
}

offer.Cancel();
await _offerSnippetRepository.UpdateAsync(offer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Convey.CQRS.Commands;
using SwiftParcel.ExternalAPI.Lecturer.Application.Services.Clients;
using SwiftParcel.ExternalAPI.Lecturer.Core.Repositories;
using SwiftParcel.ExternalAPI.Lecturer.Application.Exceptions;
using SwiftParcel.ExternalAPI.Lecturer.Core.Entities;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.Commands.Handlers
{
public class ConfirmOrderHandler : ICommandHandler<ConfirmOrder>
{
private readonly IIdentityManagerServiceClient _identityManagerServiceClient;
private readonly IOfferSnippetRepository _offerSnippetRepository;
private readonly IOffersServiceClient _officeServiceClient;

public ConfirmOrderHandler(IIdentityManagerServiceClient identityManagerServiceClient,
IOfferSnippetRepository offerSnippetRepository, IOffersServiceClient officeServiceClient)
{
_identityManagerServiceClient = identityManagerServiceClient;
_offerSnippetRepository = offerSnippetRepository;
_officeServiceClient = officeServiceClient;
}

public async Task HandleAsync(ConfirmOrder command, CancellationToken cancellationToken)
{
var offer = await _offerSnippetRepository.GetAsync(command.OrderId);
if (offer is null)
{
throw new OfferNotFoundException(command.OrderId);
}
if(offer.Status != OfferSnippetStatus.Approved)
{
throw new OfferNotApprovedException(command.OrderId, offer.Status);
}

var token = await _identityManagerServiceClient.GetToken();
var response = await _officeServiceClient.PostConfirmOffer(token, offer.OfferId.ToString());
if(response is null)
{
throw new OffersServiceConnectionException();
}
if(!response.IsSuccessStatusCode)
{
throw new OffersServiceException(response.ReasonPhrase);
}

offer.Confirm();
await _offerSnippetRepository.UpdateAsync(offer);
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Convey.CQRS.Commands;
using SwiftParcel.ExternalAPI.Lecturer.Application.Services.Clients;
using SwiftParcel.ExternalAPI.Lecturer.Application.DTO;
using SwiftParcel.ExternalAPI.Lecturer.Application.Exceptions;
using SwiftParcel.ExternalAPI.Lecturer.Core.Repositories;
using SwiftParcel.ExternalAPI.Lecturer.Core.Entities;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.Commands.Handlers
{
public class CreateOrderHandler : ICommandHandler<CreateOrder>
{
private readonly IIdentityManagerServiceClient _identityManagerServiceClient;
private readonly IOffersServiceClient _offersServiceClient;
private readonly IOfferSnippetRepository _offerSnippetRepository;

public CreateOrderHandler(IIdentityManagerServiceClient identityManagerServiceClient,
IOffersServiceClient offersServiceClient, IOfferSnippetRepository offerSnippetRepository)
{
_identityManagerServiceClient = identityManagerServiceClient;
_offersServiceClient = offersServiceClient;
_offerSnippetRepository = offerSnippetRepository;
}
public async Task HandleAsync(CreateOrder command, CancellationToken cancellationToken)
{
var token = await _identityManagerServiceClient.GetToken();
var response = await _offersServiceClient.PostAsync(token, new OfferRequestDto
{
InquiryId = command.ParcelId,
Name = command.Name,
Email = command.Email,
Address = new InquiryAddressDto
{
HouseNumber = command.Address.BuildingNumber,
ApartmentNumber = command.Address.ApartmentNumber,
Street = command.Address.Street,
City = command.Address.City,
Country = command.Address.Country,
ZipCode = command.Address.ZipCode
}
});
if(response == null)
{
throw new OffersServiceConnectionException();
}
if (!response.Response.IsSuccessStatusCode)
{
throw new OffersServiceException(response.Response.ReasonPhrase);
}

var offerSnippet = new OfferSnippet(response.Result.OfferRequestId, null, command.CustomerId,
response.Result.ValidTo, OfferSnippetStatus.WaitingForDecision);

await _offerSnippetRepository.AddAsync(offerSnippet);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using SwiftParcel.ExternalAPI.Lecturer.Core.Entities;

namespace SwiftParcel.ExternalAPI.Lecturer.Application.DTO
{
public class AddressDto
{
public string Street { get; set; }
public string BuildingNumber { get; set; }
public string ApartmentNumber { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }

public AddressDto(string street, string buildingNumber, string apartmentNumber, string city, string zipCode, string country)
{
Street = street;
BuildingNumber = buildingNumber;
ApartmentNumber = apartmentNumber;
City = city;
ZipCode = zipCode;
Country = country;
}

public AddressDto(InquiryAddressDto address)
{
Street = address.Street;
BuildingNumber = address.HouseNumber;
ApartmentNumber = address.ApartmentNumber;
City = address.City;
ZipCode = address.ZipCode;
Country = address.Country;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class InquiryDto
public string Currency { get; set; }
public double Weight { get; set; }
public string WeightUnit { get; set; }
public AddressDto Source { get; set; }
public AddressDto Destination { get; set; }
public InquiryAddressDto Source { get; set; }
public InquiryAddressDto Destination { get; set; }
public DateTime PickupDate { get; set; }
public DateTime DeliveryDay { get; set; }
public bool DeliveryInWeekend { get; set; }
Expand All @@ -32,7 +32,7 @@ public InquiryDto(double width, double height, double length, string dimensionUn
Currency = currency;
Weight = weight;
WeightUnit = weightUnit;
Source = new AddressDto
Source = new InquiryAddressDto
{
HouseNumber = sourceHouseNumber,
ApartmentNumber = sourceApartmentNumber,
Expand All @@ -41,7 +41,7 @@ public InquiryDto(double width, double height, double length, string dimensionUn
ZipCode = sourceZipCode,
Country = sourceCountry
};
Destination = new AddressDto
Destination = new InquiryAddressDto
{
HouseNumber = destinationHouseNumber,
ApartmentNumber = destinationApartmentNumber,
Expand All @@ -68,7 +68,7 @@ public class DimensionDto
public string DimensionUnit { get; set; }
}

public class AddressDto
public class InquiryAddressDto
{
public string HouseNumber { get; set; }
public string ApartmentNumber { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace SwiftParcel.ExternalAPI.Lecturer.Application.DTO
{
public class OfferDto
{
public Guid OfferId { get; set; }
public DimensionDto Dimensions { get; set; }
public InquiryAddressDto Source { get; set; }
public InquiryAddressDto Destination { get; set; }
public double Weight { get; set; }
public string WeightUnit { get; set; }
public DateTime PickupDate { get; set; }
public DateTime DeliveryDate { get; set; }
public DateTime ValidTo { get; set; }
public bool DeliveryInWeekend { get; set; }
public string Priority { get; set; }
public bool VipPackage { get; set; }
public List<PriceBreakDownItemDto> PriceBreakDown { get; set; }
public double TotalPrice { get; set; }
public string Currency { get; set; }
public DateTime InquireDate { get; set; }
public DateTime OfferRequestDate { get; set; }
public DateTime DecisionDate { get; set; }
public string OfferStatus { get; set; }
public string BuyerName { get; set; }
public InquiryAddressDto BuyerAddress { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SwiftParcel.ExternalAPI.Lecturer.Application.DTO
{
public class OfferRequestDto
{
public Guid InquiryId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public InquiryAddressDto Address { get; set; }
}
}
Loading

0 comments on commit f580468

Please sign in to comment.