Skip to content

Commit

Permalink
Fix email body generation issue in booking confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
Leen-odeh3 committed Aug 24, 2024
1 parent 9d4545d commit 35d9d3d
Show file tree
Hide file tree
Showing 10 changed files with 3,442 additions and 40 deletions.
7 changes: 0 additions & 7 deletions HotelBookingPlatform.API/Controllers/AmenityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@ public AmenityController(IAmenityService amenityService, IResponseHandler respon
Tags = new[] { "Amenities" })]
public async Task<IActionResult> GetAllAmenities()
{
try
{
var amenities = await _amenityService.GetAllAmenity();
return _responseHandler.Success(amenities, "Amenities retrieved successfully.");
}
catch (KeyNotFoundException ex)
{
return _responseHandler.NotFound(ex.Message);
}
}
}

3,409 changes: 3,409 additions & 0 deletions HotelBookingPlatform.API/Production_Env_Log/logs202408.txt

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions HotelBookingPlatform.API/Profiles/BookingMappingProfile.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

namespace HotelBookingPlatform.API.Profiles;
namespace HotelBookingPlatform.API.Profiles;
public class BookingMappingProfile :Profile
{
public BookingMappingProfile()
{
CreateMap<Booking, BookingDto>()
.ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status.ToString()))
.ForMember(dest => dest.PaymentMethod, opt => opt.MapFrom(src => src.PaymentMethod.ToString()))
.ForMember(dest => dest.HotelName, opt => opt.MapFrom(src => src.Hotel.Name))
.ForMember(dest => dest.Numbers, opt => opt.MapFrom(src => src.Rooms.Select(r => r.Number).ToList()));
.ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Status.ToString()))
.ForMember(dest => dest.PaymentMethod, opt => opt.MapFrom(src => src.PaymentMethod.ToString()))
.ForMember(dest => dest.HotelName, opt => opt.MapFrom(src => src.Hotel.Name))
.ForMember(dest => dest.Numbers, opt => opt.MapFrom(src => src.Rooms.Select(r => r.Number).ToList()))
.ForMember(dest => dest.UserName, opt => opt.MapFrom(src => src.User.UserName));


CreateMap<BookingCreateRequest, Booking>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public async Task<BookingDto> CreateBookingAsync(BookingCreateRequest request, s
if (request.CheckInDateUtc >= request.CheckOutDateUtc)
throw new BadRequestException("Check-out date must be greater than check-in date.");

// حساب السعر الإجمالي
var totalPrice = await CalculateTotalPriceAsync(request.RoomIds.ToList(), request.CheckInDateUtc, request.CheckOutDateUtc);

// حساب السعر بعد تطبيق الخصم
var discountedTotalPrice = await CalculateDiscountedPriceAsync(request.RoomIds.ToList(), request.CheckInDateUtc, request.CheckOutDateUtc);

var booking = new Booking
Expand Down Expand Up @@ -92,35 +95,30 @@ private async Task<decimal> CalculateTotalPriceAsync(List<int> roomIds, DateTime

private async Task<decimal> CalculateDiscountedPriceAsync(List<int> roomIds, DateTime checkInDate, DateTime checkOutDate)
{
decimal totalPrice = 0m;
int numberOfNights = (checkOutDate - checkInDate).Days;
decimal discountedTotalPrice = 0;
var numberOfNights = (checkOutDate - checkInDate).Days;

foreach (var roomId in roomIds)
{
var room = await _unitOfWork.RoomRepository.GetByIdAsync(roomId);
var discount = await _unitOfWork.DiscountRepository.GetActiveDiscountForRoomAsync(roomId, checkInDate, checkOutDate);

if (room is not null)
if (room != null)
{
decimal roomPrice = room.PricePerNight * numberOfNights;
Console.WriteLine($"Room ID: {roomId}, Original Price: {roomPrice}");

if (discount is not null)
var activeDiscount = await _unitOfWork.DiscountRepository.GetActiveDiscountForRoomAsync(roomId, checkInDate, checkOutDate);
if (activeDiscount != null && activeDiscount.IsActive)
{
if (checkInDate >= discount.StartDateUtc && checkOutDate <= discount.EndDateUtc)
{
decimal discountAmount = (discount.Percentage / 100) * roomPrice;
roomPrice -= discountAmount;
}
var discountPrice = room.PricePerNight * (1 - (activeDiscount.Percentage / 100.0m));
discountedTotalPrice += discountPrice * numberOfNights;
}
else
{
discountedTotalPrice += room.PricePerNight * numberOfNights;
}

totalPrice += roomPrice;
}
}

Console.WriteLine($"Total Discounted Price: {totalPrice}");
return totalPrice;
return discountedTotalPrice;
}

public static string GenerateConfirmationNumber()
{
return Guid.NewGuid().ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

namespace HotelBookingPlatform.Application.Core.Implementations;
namespace HotelBookingPlatform.Application.Core.Implementations;
public class DiscountService : BaseService<Discount>, IDiscountService
{
public DiscountService(IUnitOfWork<Discount> unitOfWork, IMapper mapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task CreateInvoiceAsync(InvoiceCreateRequest request)
public async Task<InvoiceResponseDto> GetInvoiceAsync(int id)
{
var invoiceRecord = await _unitOfWork.InvoiceRecordRepository.GetByIdAsync(id);
if (invoiceRecord == null)
if (invoiceRecord is null)
throw new NotFoundException("Invoice not found");

return _mapper.Map<InvoiceResponseDto>(invoiceRecord);
Expand All @@ -31,7 +31,7 @@ public async Task<IEnumerable<InvoiceResponseDto>> GetInvoicesByBookingAsync(int
public async Task UpdateInvoiceAsync(int id, InvoiceCreateRequest request)
{
var invoiceRecord = await _unitOfWork.InvoiceRecordRepository.GetByIdAsync(id);
if (invoiceRecord == null)
if (invoiceRecord is null)
throw new NotFoundException("Invoice not found");

_mapper.Map(request, invoiceRecord);
Expand All @@ -42,7 +42,7 @@ public async Task UpdateInvoiceAsync(int id, InvoiceCreateRequest request)
public async Task DeleteInvoiceAsync(int id)
{
var invoiceRecord = await _unitOfWork.InvoiceRecordRepository.GetByIdAsync(id);
if (invoiceRecord == null)
if (invoiceRecord is null)
throw new NotFoundException("Invoice not found");

await _unitOfWork.InvoiceRecordRepository.DeleteAsync(id);
Expand Down
4 changes: 3 additions & 1 deletion HotelBookingPlatform.Application/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ private string GenerateEmailBody(BookingConfirmation confirmation)
<html>
<body>
<h2>Booking Confirmation</h2>
<p><strong>Confirmation Number:</strong> {confirmation.ConfirmationNumber}</p>
<p><strong>Hotel Name:</strong> {confirmation.HotelName}</p>
<p><strong>Hotel Address:</strong> {confirmation.HotelAddress}</p>
<p><strong>Room Type:</strong> {confirmation.RoomType}</p>
<p><strong>Check-In Date:</strong> {confirmation.CheckInDate:yyyy-MM-dd HH:mm:ss}</p>
<p><strong>Check-Out Date:</strong> {confirmation.CheckOutDate:yyyy-MM-dd HH:mm:ss}</p>
<p><strong>Total Price:</strong> {confirmation.TotalPrice:C}</p>
<p><strong>User Email:</strong> {confirmation.AfterDiscountedPrice:C}</p>
<p><strong>Discount Percentage:</strong> {confirmation.Percentage}%</p>
<p><strong>Price After Discount:</strong> {confirmation.AfterDiscountedPrice:C}</p>
<p><strong>User Email:</strong> {confirmation.UserEmail}</p>
<p>Thank you for booking with us!</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public class BookingConfirmation
public DateTime CheckOutDate { get; set; }
public decimal TotalPrice { get; set; }
public string UserEmail { get; set; }
public decimal Percentage { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public async Task<Discount> GetActiveDiscountForRoomAsync(int roomId, DateTime c
{
return await _appDbContext.Discounts
.Where(d => d.RoomID == roomId &&
d.StartDateUtc <= checkInDate &&
d.EndDateUtc >= checkOutDate &&
d.StartDateUtc <= checkOutDate &&
d.EndDateUtc >= checkInDate &&
DateTime.UtcNow >= d.StartDateUtc &&
DateTime.UtcNow <= d.EndDateUtc)
.FirstOrDefaultAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Should_Not_Have_Error_When_All_Fields_Are_Valid()
// Arrange
var model = new HotelCreateRequest
{
Name = "Valid Hotel",
Name = "The Ritz-Carlton",
StarRating = 3,
PhoneNumber = "123456789",
OwnerID = 1
Expand Down

0 comments on commit 35d9d3d

Please sign in to comment.