Skip to content

Commit e74d596

Browse files
authored
Merge pull request #161 from kt-cloud-basic-project/SHOP-124
feat: DiscountEventListener ๊ฐœ๋ฐœ
2 parents eaaad36 + 1554a40 commit e74d596

4 files changed

Lines changed: 97 additions & 5 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.kt.integration.eventlistener;
2+
3+
import com.kt.event.ProductDiscountEvent;
4+
import com.kt.notification.MailSendRequest;
5+
import com.kt.notification.MailSendService;
6+
import com.kt.repository.cart.CartRepository;
7+
import com.kt.repository.wishlist.WishlistRepository;
8+
import com.kt.service.discount.DiscountService;
9+
import jakarta.validation.Valid;
10+
import lombok.RequiredArgsConstructor;
11+
import org.springframework.context.event.EventListener;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.util.HashSet;
15+
import java.util.List;
16+
import java.util.Set;
17+
18+
@Component
19+
@RequiredArgsConstructor
20+
public class DiscountEventListener {
21+
22+
private final DiscountService discountService;
23+
private final MailSendService mailSendService;
24+
private final WishlistRepository wishlistRepository;
25+
private final CartRepository cartRepository;
26+
27+
@EventListener
28+
public void sendMail(ProductDiscountEvent event) {
29+
30+
List<String> wishlistEmails =
31+
wishlistRepository.findDistinctEmailsByProductId(event.productId());
32+
List<String> cartEmails =
33+
cartRepository.findDistinctEmailsByProductId(event.productId());
34+
35+
Set<String> distinct_Emails = new HashSet<>(wishlistEmails);
36+
distinct_Emails.addAll(cartEmails);
37+
38+
if (distinct_Emails.isEmpty()) return;
39+
40+
for (String email : distinct_Emails) {
41+
MailSendRequest request = new MailSendRequest(
42+
email,
43+
"ํ˜„์žฌ ์ƒํ’ˆ " + event.productName() + " ์ด(๊ฐ€) ํ• ์ธ์ค‘์ž…๋‹ˆ๋‹ค.",
44+
"์ƒํ’ˆ๋ช…: " + event.productName() + "\nํ• ์ธ๊ธˆ์•ก: " + event.discountValue() + "\nํ• ์ธ๊ฐ€: 289,000์›\n๋งํฌ: https://your-shop.com/products/" + event.productId() + "\n\n์ง€๊ธˆ ๊ตฌ๋งคํ•˜๋ฉด ํ˜œํƒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค."
45+
);
46+
mailSendService.sendEmail(request);
47+
}
48+
}
49+
}
50+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.kt.notification;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.context.annotation.Profile;
5+
import org.springframework.stereotype.Component;
6+
7+
@Slf4j
8+
@Component
9+
@Profile("test")
10+
public class FakeMailSendService implements MailSendService {
11+
12+
@Override
13+
public void sendEmail(MailSendRequest mailSendRequest) {
14+
log.info("[TEST] skip email send: to={}, subject={}",
15+
mailSendRequest.email(), mailSendRequest.title());
16+
}
17+
}
18+

โ€Žsrc/main/java/com/kt/repository/cart/CartRepository.javaโ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.data.domain.Pageable;
1111
import org.springframework.data.jpa.repository.EntityGraph;
1212
import org.springframework.data.jpa.repository.JpaRepository;
13+
import org.springframework.data.jpa.repository.Query;
1314

1415
public interface CartRepository extends JpaRepository<Cart, Long> {
1516
default Cart findByIdOrThrow(Long id, ErrorCode errorCode) {
@@ -20,4 +21,15 @@ default Cart findByIdOrThrow(Long id, ErrorCode errorCode) {
2021

2122
@EntityGraph(attributePaths = {"user", "product", "user.membership"})
2223
Page<Cart> findByUserId(Long userId, Pageable pageable);
24+
25+
//์ƒํ’ˆ์„ ์ฐœํ•œ ์œ ์ € ์ด๋ฉ”์ผ๋งŒ ๋ฝ‘๋Š” ์ฟผ๋ฆฌ, jpa๋Š” ๋น„ํšจ์œจ์ ์ด๋ผ ์‚ฌ์šฉx
26+
@Query("""
27+
select distinct u.email
28+
from Wishlist w
29+
join w.user u
30+
where w.product.id = :productId
31+
and u.isDeleted = false
32+
and u.email is not null
33+
""")
34+
List<String> findDistinctEmailsByProductId(Long productId);
2335
}

โ€Žsrc/main/java/com/kt/repository/wishlist/WishlistRepository.javaโ€Ž

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@
33
import java.util.List;
44

55
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
67
import org.springframework.stereotype.Repository;
78

89
import com.kt.domain.wishlist.Wishlist;
910

1011
@Repository
1112
public interface WishlistRepository extends JpaRepository<Wishlist,Long> {
12-
boolean existsByUserIdAndProductId(Long userId, Long productId);
13-
List<Wishlist> findByUserId(Long userId);
14-
List<Wishlist> findByProductId(Long productId);
13+
boolean existsByUserIdAndProductId(Long userId, Long productId);
14+
List<Wishlist> findByUserId(Long userId);
15+
List<Wishlist> findByProductId(Long productId);
1516

16-
void deleteByUserIdAndProductId(Long userId, Long productId);
17+
void deleteByUserIdAndProductId(Long userId, Long productId);
1718

18-
void deleteByUserId(Long userId);
19+
void deleteByUserId(Long userId);
20+
21+
//์ƒํ’ˆ์„ ์ฐœํ•œ ์œ ์ € ์ด๋ฉ”์ผ๋งŒ ๋ฝ‘๋Š” ์ฟผ๋ฆฌ, jpa๋Š” ๋น„ํšจ์œจ์ ์ด๋ผ ์‚ฌ์šฉx
22+
@Query("""
23+
select distinct u.email
24+
from Wishlist w
25+
join w.user u
26+
where w.product.id = :productId
27+
and u.isDeleted = false
28+
and u.email is not null
29+
""")
30+
List<String> findDistinctEmailsByProductId(Long productId);
1931
}

0 commit comments

Comments
ย (0)