Skip to content

Commit

Permalink
Merge pull request #67 from 1223v/test
Browse files Browse the repository at this point in the history
Fix: 로그인성공 response 버그 수정
  • Loading branch information
1223v authored Mar 6, 2024
2 parents a9715a8 + 6342152 commit 5adc317
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,24 @@ List<SaleManagementDto> sumTotalAmountPerDayBetweenDates(@Param("storeId") Long
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate);

@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.store.id = :storeId AND o.createdAt BETWEEN :startOfMonth "
+ "AND :endOfMonth")
Optional<Long> sumTotalAmountByStoreIdForMonth(@Param("storeId") Long storeId,
@Param("startOfMonth") LocalDateTime startOfMonth, @Param("endOfMonth") LocalDateTime endOfMonth);

@Query("SELECT SUM(o.totalAmount) FROM Order o WHERE o.store.id = :storeId AND o.createdAt BETWEEN :startOfWeek "
+ "AND :endOfWeek")
Optional<Long> sumTotalAmountByStoreIdForWeek(@Param("storeId") Long storeId,
@Param("startOfWeek") LocalDateTime startOfWeek, @Param("endOfWeek") LocalDateTime endOfWeek);

@Query("SELECT COUNT(o) FROM Order o WHERE o.store.id = :storeId AND o.createdAt BETWEEN :startOfMonth "
+ "AND :endOfMonth")
Long countOrdersByStoreIdForMonth(@Param("storeId") Long storeId,
@Param("startOfMonth") LocalDateTime startOfMonth, @Param("endOfMonth") LocalDateTime endOfMonth);

@Query("SELECT COUNT(o) FROM Order o WHERE o.store.id = :storeId AND o.createdAt BETWEEN :startOfWeek "
+ "AND :endOfWeek")
Long countOrdersByStoreIdForWeek(@Param("storeId") Long storeId, @Param("startOfWeek") LocalDateTime startOfWeek,
@Param("endOfWeek") LocalDateTime endOfWeek);

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public String createRefreshToken() {
public void sendAccessAndRefreshToken(HttpServletResponse response, String accessToken, String refreshToken,
Role role) {

jwtTokenizer.addAccessRefreshTokenResponseBody(accessToken, refreshToken, role);
jwtTokenizer.addAccessRefreshTokenResponseBody(response, accessToken, refreshToken, role);
jwtTokenizer.addAccessTokenCookie(response, accessToken);
jwtTokenizer.addRefreshTokenCookie(response, refreshToken);
log.info("Access Token, Refresh Token 헤더 설정 완료");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void addRefreshTokenCookie(HttpServletResponse response, String refreshTo
jwtConfig.getRefreshTokenExpirationPeriod().intValue(), true);
}

public void addAccessRefreshTokenResponseBody(String accessToken,
public void addAccessRefreshTokenResponseBody(HttpServletResponse response, String accessToken,
String refreshToken, Role role) {
tokenSendManager.addTokenResponseBody(accessToken, refreshToken, role);
tokenSendManager.addTokenResponseBody(response, accessToken, refreshToken, role);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.readyvery.readyverydemo.security.jwt.service.sendmanger;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.IOException;

import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -32,10 +32,12 @@ public void addTokenCookie(HttpServletResponse response, String name, String val

}

public ResponseEntity<CeoLoginSuccessRes> addTokenResponseBody(String accessToken, String refreshToken, Role role) {
public void addTokenResponseBody(HttpServletResponse response, String accessToken,
String refreshToken, Role role) {

// JSON 응답 생성 및 전송

response.setContentType("application/json;charset=UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
CeoLoginSuccessRes ceoLoginSuccessRes = CeoLoginSuccessRes.builder()
.success(true)
.message("로그인 성공")
Expand All @@ -44,9 +46,12 @@ public ResponseEntity<CeoLoginSuccessRes> addTokenResponseBody(String accessToke
.role(role)
.build();

return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(ceoLoginSuccessRes);
try {
String jsonResponse = objectMapper.writeValueAsString(ceoLoginSuccessRes);
response.getWriter().write(jsonResponse);
} catch (IOException e) {
log.error("응답 작성 중 에러 발생", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.readyvery.readyverydemo.domain.repository.OrderRepository;
import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.global.exception.ExceptionCode;
import com.readyvery.readyverydemo.src.ceo.CeoService;
import com.readyvery.readyverydemo.src.ceo.CeoServiceFacade;
import com.readyvery.readyverydemo.src.order.dto.OrderMapper;
import com.readyvery.readyverydemo.src.order.dto.OrderRegisterRes;
Expand All @@ -50,15 +49,11 @@
public class OrderServiceImpl implements OrderService {
private final OrderRepository orderRepository;
private final OrderMapper orderMapper;
private final CeoService ceoServiceImpl;
private final TossPaymentConfig tosspaymentConfig;
private final SolApiConfig solApiConfig;

private final CeoServiceFacade ceoServiceFacade;

private final PointService pointService;


@Override
public OrderRegisterRes getOrders(Long id, Progress progress) {
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ public SaleManagementRes getSaleManagement(@AuthenticationPrincipal CustomUserDe
return saleServiceImpl.getSaleManagementMoney(userDetails.getId(), request);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

import com.readyvery.readyverydemo.src.sale.dto.SaleManagementReq;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementRes;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalMoneyReq;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalMoneyRes;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalOrderReq;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalOrderRes;
import com.readyvery.readyverydemo.src.sale.dto.TotalSaleRes;

public interface SaleService {
TotalSaleRes getTotalSaleMoney(Long id);

SaleManagementRes getSaleManagementMoney(Long id, SaleManagementReq request);
SaleManagementRes getSaleManagementMoney(Long id, SaleManagementReq saleManagementReq);

SaleManagementTotalMoneyRes getWeekSaleManagementMoney(Long id,
SaleManagementTotalMoneyReq saleManagementTotalMoneyReq);

SaleManagementTotalMoneyRes getMonthlySalesAmount(Long id, SaleManagementTotalMoneyReq saleManagementTotalMoneyReq);

SaleManagementTotalOrderRes getWeekSaleManagementOrder(Long id,
SaleManagementTotalOrderReq saleManagementTotalOrderReq);

SaleManagementTotalOrderRes getMonthlySalesOrder(Long id, SaleManagementTotalOrderReq saleManagementTotalOrderReq);
}
150 changes: 135 additions & 15 deletions src/main/java/com/readyvery/readyverydemo/src/sale/SaleServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package com.readyvery.readyverydemo.src.sale;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.readyvery.readyverydemo.domain.CeoInfo;
import com.readyvery.readyverydemo.domain.repository.CeoRepository;
import com.readyvery.readyverydemo.domain.repository.OrderRepository;
import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.global.exception.ExceptionCode;
import com.readyvery.readyverydemo.src.ceo.CeoServiceFacade;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementDto;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementReq;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementRes;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalMoneyReq;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalMoneyRes;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalOrderReq;
import com.readyvery.readyverydemo.src.sale.dto.SaleManagementTotalOrderRes;
import com.readyvery.readyverydemo.src.sale.dto.SaleMapper;
import com.readyvery.readyverydemo.src.sale.dto.TotalSaleRes;

Expand All @@ -29,21 +36,22 @@
@RequiredArgsConstructor
public class SaleServiceImpl implements SaleService {

private final CeoRepository ceoRepository;
private final CeoServiceFacade ceoServiceFacade;
private final SaleMapper saleMapper;
private final OrderRepository orderRepository;

@Override
public TotalSaleRes getTotalSaleMoney(Long id) {
CeoInfo ceoInfo = getCeoInfo(id);
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
return saleMapper.totalSaleToTotalSaleRes(getTotalSale(ceoInfo.getStore().getId()));
}

@Override
public SaleManagementRes getSaleManagementMoney(Long id, SaleManagementReq request) {
public SaleManagementRes getSaleManagementMoney(Long id, SaleManagementReq saleManagementReq) {

CeoInfo ceoInfo = getCeoInfo(id);
List<SaleManagementDto> saleManagementList = getSaleManagement(ceoInfo.getStore().getId(), request);
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
List<SaleManagementDto> saleManagementList = getSaleMoneyManagement(ceoInfo.getStore().getId(),
convertToDateTime(saleManagementReq.getMonday()));

return SaleManagementRes.builder()
.message("매출관리 조회 성공")
Expand All @@ -52,10 +60,64 @@ public SaleManagementRes getSaleManagementMoney(Long id, SaleManagementReq reque
.build();
}

private CeoInfo getCeoInfo(Long id) {
return ceoRepository.findById(id).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.USER_NOT_FOUND)
);
@Override
public SaleManagementTotalMoneyRes getWeekSaleManagementMoney(Long id,
SaleManagementTotalMoneyReq saleManagementTotalMoneyReq) {
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
Optional<Long> saleManagementTotal = sumTotalAmountByStoreIdForWeek(ceoInfo.getStore().getId(),
getStartOfMonth(convertToDateTime(saleManagementTotalMoneyReq.getMonday())),
getEndOfMonth(convertToDateTime(saleManagementTotalMoneyReq.getMonday())));

return SaleManagementTotalMoneyRes.builder()
.message("해당 주차 매출관리 조회 성공")
.success(true)
.totalMoney(saleManagementTotal)
.build();
}

@Override
public SaleManagementTotalMoneyRes getMonthlySalesAmount(Long id,
SaleManagementTotalMoneyReq saleManagementTotalMoneyReq) {
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
Optional<Long> saleManagementTotal = sumTotalAmountByStoreIdForMonth(ceoInfo.getStore().getId(),
getStartOfMonth(convertToDateTime(saleManagementTotalMoneyReq.getMonday())),
getEndOfMonth(convertToDateTime(saleManagementTotalMoneyReq.getMonday())));

return SaleManagementTotalMoneyRes.builder()
.message("해당 월 매출관리 조회 성공")
.success(true)
.totalMoney(saleManagementTotal)
.build();
}

@Override
public SaleManagementTotalOrderRes getWeekSaleManagementOrder(Long id,
SaleManagementTotalOrderReq saleManagementTotalOrderReq) {
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
Long saleManagementTotal = countOrdersByStoreIdForWeek(ceoInfo.getStore().getId(),
getStartOfMonth(convertToDateTime(saleManagementTotalOrderReq.getMonday())),
getEndOfMonth(convertToDateTime(saleManagementTotalOrderReq.getMonday())));

return SaleManagementTotalOrderRes.builder()
.message("해당 주차 매출관리 조회 성공")
.success(true)
.totalOrder(saleManagementTotal)
.build();
}

@Override
public SaleManagementTotalOrderRes getMonthlySalesOrder(Long id,
SaleManagementTotalOrderReq saleManagementTotalOrderReq) {
CeoInfo ceoInfo = ceoServiceFacade.getCeoInfo(id);
Long saleManagementTotal = countOrdersByStoreIdForMonth(ceoInfo.getStore().getId(),
getStartOfMonth(convertToDateTime(saleManagementTotalOrderReq.getMonday())),
getEndOfMonth(convertToDateTime(saleManagementTotalOrderReq.getMonday())));

return SaleManagementTotalOrderRes.builder()
.message("해당 월 매출관리 조회 성공")
.success(true)
.totalOrder(saleManagementTotal)
.build();
}

private Long getTotalSale(Long id) {
Expand All @@ -64,13 +126,58 @@ private Long getTotalSale(Long id) {
);
}

private List<SaleManagementDto> getSaleManagement(Long id, SaleManagementReq request) {
LocalDateTime startDateTime = convertToDateTime(request.getMonday());
LocalDateTime endDateTime = convertToDateTime(request.getMonday()).plusDays(7);
return getSaleManagementData(id, startDateTime, endDateTime);
public List<SaleManagementDto> getSaleMoneyManagement(Long id, LocalDateTime day) {
LocalDateTime startDateTime = day;
LocalDateTime endDateTime = day.plusDays(7);
return getSaleManagementMoneyData(id, startDateTime, endDateTime);
}

public List<SaleManagementDto> getSaleManagementData(Long storeId, LocalDateTime startDate, LocalDateTime endDate) {
public List<SaleManagementDto> getSaleOrderManagement(Long id, LocalDateTime day) {
LocalDateTime startDateTime = day;
LocalDateTime endDateTime = day.plusDays(7);
return getSaleManagementOrderData(id, startDateTime, endDateTime);
}

public Optional<Long> sumTotalAmountByStoreIdForMonth(Long storeId, LocalDateTime startOfMonth,
LocalDateTime endOfMonth) {
return orderRepository.sumTotalAmountByStoreIdForMonth(storeId, startOfMonth, endOfMonth);
}

public Optional<Long> sumTotalAmountByStoreIdForWeek(Long storeId, LocalDateTime startOfWeek,
LocalDateTime endOfWeek) {
return orderRepository.sumTotalAmountByStoreIdForWeek(storeId, startOfWeek, endOfWeek);
}

public Long countOrdersByStoreIdForMonth(Long storeId, LocalDateTime startOfMonth,
LocalDateTime endOfMonth) {
return orderRepository.countOrdersByStoreIdForMonth(storeId, startOfMonth, endOfMonth);
}

public Long countOrdersByStoreIdForWeek(Long storeId, LocalDateTime startOfWeek,
LocalDateTime endOfWeek) {
return orderRepository.countOrdersByStoreIdForWeek(storeId, startOfWeek, endOfWeek);
}

public List<SaleManagementDto> getSaleManagementMoneyData(Long storeId, LocalDateTime startDate,
LocalDateTime endDate) {
List<SaleManagementDto> queryResult = orderRepository.sumTotalAmountPerDayBetweenDates(storeId, startDate,
endDate);
List<SaleManagementDto> resultWithAllDays = initializeWeekData();

Map<String, SaleManagementDto> resultMap = queryResult.stream()
.collect(Collectors.toMap(SaleManagementDto::getDay, Function.identity()));

resultWithAllDays.forEach(dayDto -> {
if (resultMap.containsKey(dayDto.getDay())) {
dayDto.setSale(resultMap.get(dayDto.getDay()).getSale());
}
});

return resultWithAllDays;
}

public List<SaleManagementDto> getSaleManagementOrderData(Long storeId, LocalDateTime startDate,
LocalDateTime endDate) {
List<SaleManagementDto> queryResult = orderRepository.sumTotalAmountPerDayBetweenDates(storeId, startDate,
endDate);
List<SaleManagementDto> resultWithAllDays = initializeWeekData();
Expand Down Expand Up @@ -103,4 +210,17 @@ public LocalDateTime convertToDateTime(String dateStr) {
LocalDate date = LocalDate.parse(dateStr);
return date.atStartOfDay(); // 자정 시간으로 설정
}

// 주어진 날짜의 주 시작을 LocalDateTime으로 찾음
public LocalDateTime getStartOfWeek(LocalDateTime dateTime) {
return dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
}

public LocalDateTime getStartOfMonth(LocalDateTime dateTime) {
return dateTime.with(TemporalAdjusters.firstDayOfMonth()).toLocalDate().atStartOfDay();
}

public LocalDateTime getEndOfMonth(LocalDateTime dateTime) {
return dateTime.with(TemporalAdjusters.lastDayOfMonth()).toLocalDate().atTime(23, 59, 59);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.readyvery.readyverydemo.src.sale.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SaleManagementTotalMoneyReq {
private String monday;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.readyvery.readyverydemo.src.sale.dto;

import java.util.Optional;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class SaleManagementTotalMoneyRes {

private String message;
private boolean success;
private Optional<Long> totalMoney;
}
Loading

0 comments on commit 5adc317

Please sign in to comment.