diff --git a/src/main/java/vn/webapp/backend/auction/config/security/SecurityConfiguration.java b/src/main/java/vn/webapp/backend/auction/config/security/SecurityConfiguration.java index 462a6eb..d62f1fe 100644 --- a/src/main/java/vn/webapp/backend/auction/config/security/SecurityConfiguration.java +++ b/src/main/java/vn/webapp/backend/auction/config/security/SecurityConfiguration.java @@ -53,6 +53,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .requestMatchers(HttpMethod.POST, Endpoints.PUBLIC_POST_ENDPOINTS).permitAll() .requestMatchers(HttpMethod.PUT, Endpoints.PUBLIC_PUT_ENDPOINTS).permitAll() + .requestMatchers(HttpMethod.GET, Endpoints.STAFF_GET_ENDPOINTS).hasAuthority(Role.STAFF.name()) + .requestMatchers(HttpMethod.GET, Endpoints.MANAGER_GET_ENDPOINTS).hasAuthority(Role.MANAGER.name()) .requestMatchers(HttpMethod.POST, Endpoints.MANAGER_POST_ENDPOINTS).hasAuthority(Role.MANAGER.name()) .requestMatchers(HttpMethod.PUT, Endpoints.MANAGER_PUT_ENDPOINTS).hasAuthority(Role.MANAGER.name()) diff --git a/src/main/java/vn/webapp/backend/auction/controller/AuctionController.java b/src/main/java/vn/webapp/backend/auction/controller/AuctionController.java index 373061c..0d06198 100644 --- a/src/main/java/vn/webapp/backend/auction/controller/AuctionController.java +++ b/src/main/java/vn/webapp/backend/auction/controller/AuctionController.java @@ -1,6 +1,8 @@ package vn.webapp.backend.auction.controller; +import jakarta.mail.MessagingException; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -12,6 +14,7 @@ import vn.webapp.backend.auction.enums.AuctionState; import vn.webapp.backend.auction.model.Auction; import vn.webapp.backend.auction.service.auction.AuctionService; +import vn.webapp.backend.auction.service.email.EmailService; import java.util.List; @@ -21,7 +24,8 @@ @RequestMapping("/api/v1/auction") public class AuctionController { private final AuctionService auctionService; - + @Autowired + private EmailService emailService; @GetMapping("/sorted-and-paged") public ResponseEntity> getAllAuctionsSortedAndPaged( @RequestParam(defaultValue = "startDate") String sortBy, @@ -139,4 +143,10 @@ public ResponseEntity> getAuctionRegistrations( Pageable pageable = PageRequest.of(page, size, direction, sortBy); return ResponseEntity.ok(auctionService.getAuctionRegistrations(auctionState, auctionName, pageable)); } + + @GetMapping("/delete-result/{id}") + public ResponseEntity deleteResult(@PathVariable Integer id) throws MessagingException { + auctionService.deleteAuctionResult(id); + return ResponseEntity.ok().build(); + } } diff --git a/src/main/java/vn/webapp/backend/auction/controller/AuctionRegistrationController.java b/src/main/java/vn/webapp/backend/auction/controller/AuctionRegistrationController.java index c328428..37982da 100644 --- a/src/main/java/vn/webapp/backend/auction/controller/AuctionRegistrationController.java +++ b/src/main/java/vn/webapp/backend/auction/controller/AuctionRegistrationController.java @@ -20,7 +20,6 @@ public class AuctionRegistrationController { private final AuctionRegistrationService auctionRegistrationService; - // API to retrieve all registrations for a specific auction @GetMapping("/auction/{auctionId}") public ResponseEntity> getRegistrationsForAuction(@PathVariable Integer auctionId) { List registrations = auctionRegistrationService.findByAuctionIdAndValid(auctionId); diff --git a/src/main/java/vn/webapp/backend/auction/controller/VNPAYController.java b/src/main/java/vn/webapp/backend/auction/controller/VNPAYController.java index 38b9218..01b6ff9 100644 --- a/src/main/java/vn/webapp/backend/auction/controller/VNPAYController.java +++ b/src/main/java/vn/webapp/backend/auction/controller/VNPAYController.java @@ -69,8 +69,7 @@ private String handleTransactionPaymentCallback(Integer transactionId, String st if (!status.equals("00")) { redirectUrl += "?paymentStatus=failed"; } else { - transactionService.setTransactionState(transactionId, "SUCCEED"); - transactionService.setTransactionMethod(transactionId, "BANKING"); + transactionService.setTransactionAfterPaySuccess(transactionId); redirectUrl += "?paymentStatus=success"; } diff --git a/src/main/java/vn/webapp/backend/auction/exception/GlobalExceptionHandler.java b/src/main/java/vn/webapp/backend/auction/exception/GlobalExceptionHandler.java index e291af3..575fc8e 100644 --- a/src/main/java/vn/webapp/backend/auction/exception/GlobalExceptionHandler.java +++ b/src/main/java/vn/webapp/backend/auction/exception/GlobalExceptionHandler.java @@ -48,7 +48,7 @@ public ResponseEntity handleUserNotFoundException(UserNotFoundExc return ResponseEntity.status(HttpStatus.NOT_FOUND).body(err); } - @ExceptionHandler(UnauthorizedException.class) + @ExceptionHandler({UnauthorizedException.class, UserNotAllowedAccess.class}) public ResponseEntity handleUnauthorizedException(UnauthorizedException ex) { ErrorResponse err = new ErrorResponse(HttpStatus.FORBIDDEN.value(), ex.getMessage()); return ResponseEntity.status(HttpStatus.FORBIDDEN).body(err); diff --git a/src/main/java/vn/webapp/backend/auction/exception/UserNotAllowedAccess.java b/src/main/java/vn/webapp/backend/auction/exception/UserNotAllowedAccess.java new file mode 100644 index 0000000..bc9242b --- /dev/null +++ b/src/main/java/vn/webapp/backend/auction/exception/UserNotAllowedAccess.java @@ -0,0 +1,7 @@ +package vn.webapp.backend.auction.exception; + +public class UserNotAllowedAccess extends RuntimeException { + public UserNotAllowedAccess(String message) { + super(message); + } +} diff --git a/src/main/java/vn/webapp/backend/auction/model/ErrorMessages.java b/src/main/java/vn/webapp/backend/auction/model/ErrorMessages.java index 8f7483e..e5edec8 100644 --- a/src/main/java/vn/webapp/backend/auction/model/ErrorMessages.java +++ b/src/main/java/vn/webapp/backend/auction/model/ErrorMessages.java @@ -3,6 +3,7 @@ public class ErrorMessages { public static final String USER_NOT_FOUND = "Không tìm thấy người dùng."; + public static final String USER_NOT_VERIFIED = "Người dùng chưa xác thực."; public static final String USER_ALREADY_EXIST = "Người dùng đã tồn tại."; public static final String REQUEST_APPROVAL_NOT_FOUND = "Không tìm thấy yêu cầu."; public static final String JEWELRY_NOT_FOUND = "Không tìm thấy trang sức."; diff --git a/src/main/java/vn/webapp/backend/auction/model/ReasonMessages.java b/src/main/java/vn/webapp/backend/auction/model/ReasonMessages.java new file mode 100644 index 0000000..c0c550b --- /dev/null +++ b/src/main/java/vn/webapp/backend/auction/model/ReasonMessages.java @@ -0,0 +1,5 @@ +package vn.webapp.backend.auction.model; + +public class ReasonMessages { + public static final String DO_NOT_PAY_ON_TIME = "Không thanh toán giao dịch đúng thời hạn quy định."; +} diff --git a/src/main/java/vn/webapp/backend/auction/repository/AuctionRepository.java b/src/main/java/vn/webapp/backend/auction/repository/AuctionRepository.java index c8b93ef..12db52d 100644 --- a/src/main/java/vn/webapp/backend/auction/repository/AuctionRepository.java +++ b/src/main/java/vn/webapp/backend/auction/repository/AuctionRepository.java @@ -25,8 +25,8 @@ public interface AuctionRepository extends JpaRepository { List findByState(@Param("auctionState") AuctionState auctionState); // @Query("SELECT a FROM Auction a WHERE a.state = :auctionState AND (:auctionName IS NULL OR a.name LIKE %:auctionName%)") - @Query("SELECT a FROM Auction a WHERE (:auctionState IS NULL AND a.state <> 'DELETED') OR (a.state = :auctionState) AND (:auctionName IS NULL OR a.name LIKE %:auctionName%)") - List findByState(@Param("auctionState") AuctionState auctionState, @Param("auctionName") String auctionName, Pageable pageable); + @Query("SELECT a FROM Auction a WHERE ((:auctionState IS NULL AND a.state <> 'DELETED') OR (a.state = :auctionState)) AND (:auctionName IS NULL OR a.name LIKE %:auctionName%)") + List findByState(@Param("auctionState") AuctionState auctionState, @Param("auctionName") String auctionName); @Query("SELECT a FROM Auction a WHERE " + "((:auctionState = 'DELETED' AND a.state != 'DELETED') " + diff --git a/src/main/java/vn/webapp/backend/auction/repository/TransactionRepository.java b/src/main/java/vn/webapp/backend/auction/repository/TransactionRepository.java index f0ef19f..448ff2e 100644 --- a/src/main/java/vn/webapp/backend/auction/repository/TransactionRepository.java +++ b/src/main/java/vn/webapp/backend/auction/repository/TransactionRepository.java @@ -61,10 +61,11 @@ public interface TransactionRepository extends JpaRepository findOverdueTransactions(@Param("userName") String userName, - @Param("threeDaysAgo") LocalDateTime threeDaysAgo, + @Param("sevenDaysAgo") LocalDateTime sevenDaysAgo, Pageable pageable); @Query("SELECT SUM(t.totalPrice) FROM Transaction t WHERE t.type = 'PAYMENT_TO_WINNER' AND t.user.username = :username") diff --git a/src/main/java/vn/webapp/backend/auction/security/Endpoints.java b/src/main/java/vn/webapp/backend/auction/security/Endpoints.java index 0aec530..f6906b1 100644 --- a/src/main/java/vn/webapp/backend/auction/security/Endpoints.java +++ b/src/main/java/vn/webapp/backend/auction/security/Endpoints.java @@ -30,6 +30,10 @@ private Endpoints() { "/api/v1/transaction/set-method/**", "/api/v1/jewelry/set-holding/**" }; + public static final String[] STAFF_GET_ENDPOINTS = { + "/api/v1/user/get-user-registration/**", + }; + public static final String[] MANAGER_GET_ENDPOINTS = { "/api/v1/transaction/get-handover" }; diff --git a/src/main/java/vn/webapp/backend/auction/service/auction/AuctionService.java b/src/main/java/vn/webapp/backend/auction/service/auction/AuctionService.java index 0cb101d..dc97ba0 100644 --- a/src/main/java/vn/webapp/backend/auction/service/auction/AuctionService.java +++ b/src/main/java/vn/webapp/backend/auction/service/auction/AuctionService.java @@ -1,5 +1,6 @@ package vn.webapp.backend.auction.service.auction; +import jakarta.mail.MessagingException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import vn.webapp.backend.auction.dto.AuctionRegistrationDTO; @@ -23,5 +24,6 @@ public interface AuctionService { Page getByStaffID(Integer id, String auctionName, Pageable pageable); Auction getCurrentAuctionByJewelryId(Integer id); Auction createNewAuction(AuctionRequest request); + void deleteAuctionResult(Integer transactionId) throws MessagingException; Page getAuctionRegistrations(AuctionState state, String auctionName, Pageable pageable); } diff --git a/src/main/java/vn/webapp/backend/auction/service/auction/AuctionServiceImpl.java b/src/main/java/vn/webapp/backend/auction/service/auction/AuctionServiceImpl.java index 647ff60..3ebed03 100644 --- a/src/main/java/vn/webapp/backend/auction/service/auction/AuctionServiceImpl.java +++ b/src/main/java/vn/webapp/backend/auction/service/auction/AuctionServiceImpl.java @@ -1,5 +1,6 @@ package vn.webapp.backend.auction.service.auction; +import jakarta.mail.MessagingException; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -8,17 +9,14 @@ import org.springframework.stereotype.Service; import vn.webapp.backend.auction.dto.AuctionRegistrationDTO; import vn.webapp.backend.auction.dto.AuctionRequest; +import vn.webapp.backend.auction.enums.AccountState; import vn.webapp.backend.auction.enums.AuctionState; import vn.webapp.backend.auction.enums.JewelryState; +import vn.webapp.backend.auction.enums.TransactionState; import vn.webapp.backend.auction.exception.ResourceNotFoundException; -import vn.webapp.backend.auction.model.Auction; -import vn.webapp.backend.auction.model.ErrorMessages; -import vn.webapp.backend.auction.model.Jewelry; -import vn.webapp.backend.auction.model.User; -import vn.webapp.backend.auction.repository.AuctionRegistrationRepository; -import vn.webapp.backend.auction.repository.AuctionRepository; -import vn.webapp.backend.auction.repository.JewelryRepository; -import vn.webapp.backend.auction.repository.UserRepository; +import vn.webapp.backend.auction.model.*; +import vn.webapp.backend.auction.repository.*; +import vn.webapp.backend.auction.service.email.EmailService; import java.sql.Timestamp; import java.time.LocalDate; @@ -38,7 +36,9 @@ public class AuctionServiceImpl implements AuctionService{ private final AuctionRepository auctionRepository; private final UserRepository userRepository; private final JewelryRepository jewelryRepository; + private final TransactionRepository transactionRepository; private final AuctionRegistrationRepository auctionRegistrationRepository; + private final EmailService emailService; @Override public List getAll() { @@ -136,6 +136,34 @@ public void setAuctionState(Integer id, String state) { existingAuction.setState(AuctionState.valueOf(state)); } + @Override + public void deleteAuctionResult(Integer transactionId) throws MessagingException { + var existingTransaction = transactionRepository.findById(transactionId) + .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.TRANSACTION_NOT_FOUND)); + Integer auctionId = existingTransaction.getAuction().getId(); + Integer jewelryId = existingTransaction.getAuction().getJewelry().getId(); + Integer userId = existingTransaction.getUser().getId(); + var existingAuction = auctionRepository.findById(auctionId) + .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.AUCTION_NOT_FOUND)); + var existingJewelry = jewelryRepository.findById(jewelryId) + .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.JEWELRY_NOT_FOUND)); + var existingUser = userRepository.findById(userId) + .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.USER_NOT_FOUND)); + + existingTransaction.setState(TransactionState.HIDDEN); + existingAuction.setState(AuctionState.FINISHED); + existingJewelry.setState(JewelryState.ACTIVE); + existingUser.setState(AccountState.DISABLE); + + String reason = ReasonMessages.DO_NOT_PAY_ON_TIME; + emailService.sendBlockAccountEmail( + existingUser.getEmail(), + existingUser.getFullName(), + existingUser.getUsername(), + reason + ); + } + @Override public List getAuctionByState(AuctionState state) { return auctionRepository.findByState(state); @@ -148,7 +176,7 @@ public Page getAuctionsByStates(List states, Pageable pag @Override public Page getAuctionRegistrations(AuctionState state, String auctionName, Pageable pageable) { - List auctions = auctionRepository.findByState(state, auctionName, pageable); + List auctions = auctionRepository.findByState(state, auctionName); List list = auctions.stream() .map(auction -> { Integer numberOfParticipants = auctionRegistrationRepository.countValidParticipantsByAuctionId(auction.getId()); diff --git a/src/main/java/vn/webapp/backend/auction/service/auction_registration/AuctionRegistrationServiceImpl.java b/src/main/java/vn/webapp/backend/auction/service/auction_registration/AuctionRegistrationServiceImpl.java index 0bf4568..fe5925c 100644 --- a/src/main/java/vn/webapp/backend/auction/service/auction_registration/AuctionRegistrationServiceImpl.java +++ b/src/main/java/vn/webapp/backend/auction/service/auction_registration/AuctionRegistrationServiceImpl.java @@ -5,11 +5,9 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; -import vn.webapp.backend.auction.enums.AuctionRegistrationState; -import vn.webapp.backend.auction.enums.PaymentMethod; -import vn.webapp.backend.auction.enums.TransactionState; -import vn.webapp.backend.auction.enums.TransactionType; +import vn.webapp.backend.auction.enums.*; import vn.webapp.backend.auction.exception.ResourceNotFoundException; +import vn.webapp.backend.auction.exception.UserNotAllowedAccess; import vn.webapp.backend.auction.model.*; import vn.webapp.backend.auction.repository.AuctionRegistrationRepository; import vn.webapp.backend.auction.repository.AuctionRepository; @@ -36,6 +34,10 @@ public void registerUserForAuction(String username, Integer auctionId) { User user = userRepository.findByUsername(username).orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.USER_NOT_FOUND)); Auction auction = auctionRepository.findById(auctionId).orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.AUCTION_NOT_FOUND)); + if (user.getState() != AccountState.VERIFIED) { + throw new UserNotAllowedAccess(ErrorMessages.USER_NOT_VERIFIED); + } + double registrationFee = auction.getParticipationFee() + auction.getDeposit(); Transaction transaction = Transaction.builder() diff --git a/src/main/java/vn/webapp/backend/auction/service/email/EmailContent.java b/src/main/java/vn/webapp/backend/auction/service/email/EmailContent.java index 64b4cb5..ed6614d 100644 --- a/src/main/java/vn/webapp/backend/auction/service/email/EmailContent.java +++ b/src/main/java/vn/webapp/backend/auction/service/email/EmailContent.java @@ -159,4 +159,80 @@ public String setHtmlConfirmHoldingContent(String fullName, String assetName "\n"; return content; } + + public String setHtmlBlockAccountContent(String fullName, String userName, String reason + ) { + String imageUrl = "https://raw.githubusercontent.com/phuuthanh2003/AuctionWebApp_BE/main/logo.png"; + String content = "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " Activation Code\n" + + " \n" + + "\n" + + "\n" + + "
\n" + + "
\n" + + " \"Company\n" + + "
\n" + + "

Xin chào, " + fullName + "

\n" + + "

Rất tiếc phải thông báo Tài khoản " + userName + " đã bị khóa.

\n" + + "

Lý do: "+ reason +"

\n" + + "
Cảm ơn bạn đã tin tưởng và đồng hành cùng DGS. Mọi thắc mắc xin liên hệ (+84) 0123456789 để được hỗ trợ và tư vấn.
\n" + + "
\n" + + "\n" + + "\n"; + return content; + } } diff --git a/src/main/java/vn/webapp/backend/auction/service/email/EmailService.java b/src/main/java/vn/webapp/backend/auction/service/email/EmailService.java index a887153..bf7b3c4 100644 --- a/src/main/java/vn/webapp/backend/auction/service/email/EmailService.java +++ b/src/main/java/vn/webapp/backend/auction/service/email/EmailService.java @@ -67,13 +67,26 @@ public void sendConfirmHoldingEmail(String to, String fullName, String assetName MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); - String html = emailContent.setHtmlConfirmHoldingContent(fullName,assetName); helper.setFrom(emailUsername); helper.setTo(to); helper.setSubject("Xác nhận tài sản được gửi tới DGS thành công ."); helper.setText(html, true); + javaMailSender.send(message); + } + + @Async + public void sendBlockAccountEmail(String to, String fullName, String userName, String reason) throws MessagingException { + MimeMessage message = javaMailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true); + + String html = emailContent.setHtmlBlockAccountContent(fullName,userName,reason); + + helper.setFrom(emailUsername); + helper.setTo(to); + helper.setSubject("Tài khoản DGS của bạn sẽ bị khóa!."); + helper.setText(html, true); javaMailSender.send(message); } diff --git a/src/main/java/vn/webapp/backend/auction/service/request_approval/RequestApprovalServiceImpl.java b/src/main/java/vn/webapp/backend/auction/service/request_approval/RequestApprovalServiceImpl.java index 24d33e0..21d5294 100644 --- a/src/main/java/vn/webapp/backend/auction/service/request_approval/RequestApprovalServiceImpl.java +++ b/src/main/java/vn/webapp/backend/auction/service/request_approval/RequestApprovalServiceImpl.java @@ -13,6 +13,7 @@ import vn.webapp.backend.auction.enums.RequestApprovalState; import vn.webapp.backend.auction.enums.Role; import vn.webapp.backend.auction.exception.ResourceNotFoundException; +import vn.webapp.backend.auction.exception.UserNotFoundException; import vn.webapp.backend.auction.model.ErrorMessages; import vn.webapp.backend.auction.model.Jewelry; import vn.webapp.backend.auction.model.RequestApproval; @@ -23,7 +24,6 @@ import java.sql.Timestamp; import java.time.Instant; -import java.util.Optional; @Transactional @Service @@ -45,10 +45,10 @@ public void setRequestState(Integer id, Integer responderId, String state) { .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.REQUEST_APPROVAL_NOT_FOUND)); var existUser = userRepository.findById(responderId) .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.USER_NOT_FOUND)); - if(existUser.getRole().equals(Role.STAFF)) { + if (existUser.getRole().equals(Role.STAFF)) { existingRequest.setStaff(existUser); } - if(existUser.getRole().equals(Role.MEMBER)) { + if (existUser.getRole().equals(Role.MEMBER)) { existingRequest.getJewelry().setState(JewelryState.HIDDEN); } existingRequest.setResponder(existUser); @@ -87,23 +87,18 @@ public void cancelRequest(CancelRequestApproval request) { } @Override - public Page getRequestBySenderRole(Role role, String jewelryName,String category, Pageable pageable) { - return requestApprovalRepository.findRequestApprovalBySenderRole(role,jewelryName,category, pageable); + public Page getRequestBySenderRole(Role role, String jewelryName, String category, Pageable pageable) { + return requestApprovalRepository.findRequestApprovalBySenderRole(role, jewelryName, category, pageable); } @Override public RequestApproval requestFromUser(UserRequestApproval request) { - Optional existSender = userRepository.findById(request.senderId()); - if (existSender.isEmpty()) { - throw new IllegalArgumentException("User with ID " + request.senderId() + " not found"); - } + User sender = userRepository.findById(request.senderId()) + .orElseThrow(() -> new UserNotFoundException(ErrorMessages.USER_NOT_FOUND)); + + Jewelry jewelry = jewelryRepository.findById(request.jewelryId()) + .orElseThrow(() -> new IllegalArgumentException(ErrorMessages.JEWELRY_NOT_FOUND)); - Optional existJewelry = jewelryRepository.findById(request.jewelryId()); - if (existJewelry.isEmpty()) { - throw new IllegalArgumentException("Jewelry with ID " + request.jewelryId() + " not found"); - } - User sender = existSender.get(); - Jewelry jewelry = existJewelry.get(); RequestApproval newRequest = new RequestApproval(); newRequest.setRequestTime(request.requestTime()); newRequest.setJewelry(jewelry); @@ -117,17 +112,12 @@ public RequestApproval requestFromUser(UserRequestApproval request) { @Override public RequestApproval requestFromStaff(StaffRequestApproval request) { - Optional existSender = userRepository.findById(request.senderId()); - if (existSender.isEmpty()) { - throw new IllegalArgumentException("User with ID " + request.senderId() + " not found"); - } + User sender = userRepository.findById(request.senderId()) + .orElseThrow(() -> new UserNotFoundException(ErrorMessages.USER_NOT_FOUND)); + + RequestApproval oldRequest = requestApprovalRepository.findById(request.requestApprovalId()) + .orElseThrow(() -> new IllegalArgumentException(ErrorMessages.REQUEST_APPROVAL_NOT_FOUND)); - Optional existRequestApproval = requestApprovalRepository.findById(request.requestApprovalId()); - if (existRequestApproval.isEmpty()) { - throw new IllegalArgumentException("Request with ID " + request.requestApprovalId() + " not found"); - } - User sender = existSender.get(); - RequestApproval oldRequest = existRequestApproval.get(); RequestApproval newRequest = new RequestApproval(); newRequest.setRequestTime(request.requestTime()); newRequest.setJewelry(oldRequest.getJewelry()); @@ -143,19 +133,13 @@ public RequestApproval requestFromStaff(StaffRequestApproval request) { @Override public RequestApproval requestFromManager(ManagerRequestApproval request) { - Optional existSender = userRepository.findById(request.senderId()); - if (existSender.isEmpty()) { - throw new IllegalArgumentException("User with ID " + request.senderId() + " not found"); - } + User sender = userRepository.findById(request.senderId()) + .orElseThrow(() -> new UserNotFoundException(ErrorMessages.USER_NOT_FOUND)); - Optional existRequestApproval = requestApprovalRepository.findById(request.requestApprovalId()); - if (existRequestApproval.isEmpty()) { - throw new IllegalArgumentException("Request with ID " + request.requestApprovalId() + " not found"); - } - User sender = existSender.get(); - RequestApproval oldRequest = existRequestApproval.get(); - RequestApproval newRequest = new RequestApproval(); + RequestApproval oldRequest = requestApprovalRepository.findById(request.requestApprovalId()) + .orElseThrow(() -> new IllegalArgumentException(ErrorMessages.REQUEST_APPROVAL_NOT_FOUND)); + RequestApproval newRequest = new RequestApproval(); newRequest.setRequestTime(request.requestTime()); newRequest.setJewelry(oldRequest.getJewelry()); newRequest.setConfirm(false); @@ -180,6 +164,6 @@ public Page getRequestApprovalPassed(String jewelryName, String @Override public Page getRequestNeedConfirmByMember(Integer memberId, Pageable pageable) { - return requestApprovalRepository.findRequestNeedConfirmByMember(memberId,pageable); + return requestApprovalRepository.findRequestNeedConfirmByMember(memberId, pageable); } } diff --git a/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionService.java b/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionService.java index cabde17..7005256 100644 --- a/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionService.java +++ b/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionService.java @@ -32,4 +32,6 @@ public interface TransactionService { List createTransactionForWinnerIfNotExists(Integer userId); Page getOverdueTransactions(String userName,Pageable pageable); + + void setTransactionAfterPaySuccess(Integer transactionId); } diff --git a/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionServiceImpl.java b/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionServiceImpl.java index 8978cfc..3d56f48 100644 --- a/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionServiceImpl.java +++ b/src/main/java/vn/webapp/backend/auction/service/transaction/TransactionServiceImpl.java @@ -14,7 +14,9 @@ import vn.webapp.backend.auction.model.*; import vn.webapp.backend.auction.repository.*; +import java.sql.Timestamp; import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -76,16 +78,25 @@ public Page getTransactionHandover(TransactionType typename, String @Override public void setTransactionState(Integer id, String state) { - var existingAuction = transactionRepository.findById(id) + var existingTransaction = transactionRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.AUCTION_NOT_FOUND)); - existingAuction.setState(TransactionState.valueOf(state)); + existingTransaction.setState(TransactionState.valueOf(state)); } @Override public void setTransactionMethod(Integer id, String method) { - var existingAuction = transactionRepository.findById(id) + var existingTransaction = transactionRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.AUCTION_NOT_FOUND)); - existingAuction.setPaymentMethod(PaymentMethod.valueOf(method)); + existingTransaction.setPaymentMethod(PaymentMethod.valueOf(method)); + } + + @Override + public void setTransactionAfterPaySuccess(Integer transactionId) { + var existingAuction = transactionRepository.findById(transactionId) + .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.AUCTION_NOT_FOUND)); + existingAuction.setState(TransactionState.SUCCEED); + existingAuction.setPaymentMethod(PaymentMethod.BANKING); + existingAuction.setPaymentTime(Timestamp.valueOf(LocalDateTime.now(ZoneId.of("Asia/Ho_Chi_Minh")))); } @Override @@ -97,7 +108,7 @@ public User createTransactionForWinner(Integer auctionId) { .orElseThrow(() -> new ResourceNotFoundException(ErrorMessages.USER_WINNER_NOT_FOUND)); if (hasTransactionForAuctionAndUser(auctionId, userWin.getId())) { - throw new ResourceNotFoundException(ErrorMessages.TRANSACTION_ALREADY_EXISTS); + return userWin; } Transaction transaction = Transaction.builder() @@ -161,7 +172,7 @@ public List createTransactionForWinnerIfNotExists(Integer userId) { @Override public Page getOverdueTransactions(String userName,Pageable pageable) { - LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(3); - return transactionRepository.findOverdueTransactions(userName,threeDaysAgo, pageable); + LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7); + return transactionRepository.findOverdueTransactions(userName,sevenDaysAgo, pageable); } }