Skip to content

Commit

Permalink
Merge pull request #160 from Hoang-Nguyen-Huy/PBS-53-Display-order
Browse files Browse the repository at this point in the history
[TriNT][PBS-53] feat: create auto get orderHandlerId for orderDetail …
  • Loading branch information
nguyenhcp2004 authored Nov 9, 2024
2 parents 931aafa + cafd78d commit bfa3be2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public interface AssignmentRepository extends JpaRepository<Assignment, String>
@Query("SELECT a.staffId FROM Assignment a WHERE a.weekDate = :weekDate AND a.slot = :slot")
List<String> findStaffIdsByWeekDateAndSlot(@Param("weekDate") String weekDate, @Param("slot") String slot);

@Query("SELECT a.staffId " +
"FROM Assignment a " +
"JOIN Account ac ON ac.id = a.staffId " +
"WHERE a.slot = :slot " +
"AND a.weekDate = :weekDate " +
"AND ac.buildingNumber = :buildingId")
String findStaffForMatchingOrder(@Param("slot") String slot,
@Param("weekDate") String weekDate,
@Param("buildingId") Integer buildingId);



}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public AssignmentResponse createAssignment(AssignmentCreationRequest request) {
String slotStartTime = slotTimes[0].toString();
String slotEndTime = slotTimes[1].toString();

logger.info("Creating assignment for staffId: {} (Staff building: {}), weekDate: {}, dayOfWeek: {}, weekDay: {}, " +
"slot: {}, slotStartTime: {}, slotEndTime: {}, assigning orders where startTime > CURRENT_TIMESTAMP",
newAssignment.getStaffId(), staff.getBuildingNumber(), weekDate, dayOfWeek, weekDay, slot, slotStartTime, slotEndTime);
orderDetailRepository.assignOrdersToStaff(newAssignment.getStaffId(), weekDay, slotStartTime, slotEndTime, staff.getBuildingNumber());

return assignmentMapper.toAssignmentResponse(assignmentRepository.save(newAssignment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -53,6 +50,7 @@ public class OrderDetailService {
private final RoomService roomService;
private final RoomRepository roomRepository;
private final OrderDetailAmenityRepository orderDetailAmenityRepository;
private final AssignmentRepository assignmentRepository;

//GET:
public List<OrderDetailResponse> getAllOrders() {
Expand Down Expand Up @@ -351,13 +349,28 @@ private void createOrderDetailAmenities(OrderDetail orderDetail, List<Amenity> a
}
}

public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order order, Room room, OrderStatus status, Account account, LocalDateTime startTime, LocalDateTime endTime) {
public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order order, Room room, OrderStatus status, Account account, LocalDateTime startTime, LocalDateTime endTime) {
try {
ServicePackage servicePackage = servicePackageRepository.findById(request.getServicePackage().getId())
.orElseThrow(() -> new IllegalArgumentException("Service package not found"));
Building building = buildingRepository.findById(request.getBuilding().getId())
.orElseThrow(() -> new IllegalArgumentException("Building not found"));


DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
String formattedStartTime = startTime.format(timeFormatter);
String formattedEndTime = endTime.format(timeFormatter);
String slot = formattedStartTime + " - " + formattedEndTime;

DayOfWeek dayOfWeek = startTime.getDayOfWeek();
String weekDate = getDayLabel(dayOfWeek);
Account orderHandler;
String orderHandlerId = assignmentRepository.findStaffForMatchingOrder(slot, weekDate, building.getId());
if (orderHandlerId == null){
orderHandler = null;
} else {
orderHandler = accountRepository.getById(orderHandlerId);
}
OrderDetail response = new OrderDetail();

response.setCustomer(account);
Expand All @@ -366,7 +379,7 @@ public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order o
response.setBuilding(building);
response.setRoom(room);
response.setServicePackage(servicePackage);
response.setOrderHandler(null);
response.setOrderHandler(orderHandler);
response.setPriceRoom(request.getPriceRoom());
response.setDiscountPercentage(servicePackage.getDiscountPercentage());
response.setStartTime(startTime);
Expand All @@ -385,6 +398,27 @@ public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order o
}
}

private String getDayLabel(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case MONDAY:
return "T2";
case TUESDAY:
return "T3";
case WEDNESDAY:
return "T4";
case THURSDAY:
return "T5";
case FRIDAY:
return "T6";
case SATURDAY:
return "T7";
case SUNDAY:
return "CN";
default:
return "";
}
}

public List<OrderDetail> getNextDayBookings(LocalDate dayNow) {
LocalDateTime startOfDay = dayNow.plusDays(1).atStartOfDay();
LocalDateTime endOfDay = dayNow.plusDays(1).atTime(LocalTime.MAX);
Expand Down

0 comments on commit bfa3be2

Please sign in to comment.