Skip to content

Commit a565b0a

Browse files
Merge pull request #87 from Donut-DONationUTile/feature/fcm
Feature/fcm
2 parents 9a7d6b5 + 32211a7 commit a565b0a

File tree

6 files changed

+114
-30
lines changed

6 files changed

+114
-30
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package zero.eight.donut.controller;
2+
3+
import com.google.firebase.messaging.FirebaseMessagingException;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.web.bind.annotation.*;
6+
import zero.eight.donut.common.response.ApiResponse;
7+
import zero.eight.donut.dto.fcm.FcmTokenRequestDto;
8+
import zero.eight.donut.exception.Success;
9+
import zero.eight.donut.service.DonationService;
10+
import zero.eight.donut.service.FcmService;
11+
12+
@RequiredArgsConstructor
13+
@RequestMapping("/api/fcm")
14+
@RestController
15+
public class FcmController {
16+
17+
private final FcmService fcmService;
18+
private final DonationService donationService;
19+
20+
@PostMapping("/token")
21+
public ApiResponse<?> createFcmToken(@RequestBody FcmTokenRequestDto requestDto) throws Exception {
22+
return fcmService.registerFcmToken(requestDto);
23+
}
24+
25+
@PostMapping("/test/37")
26+
public ApiResponse<?> test37() throws FirebaseMessagingException {
27+
return ApiResponse.success(Success.FCM_TEST_SUCCESS, fcmService.imminentWallet());
28+
}
29+
30+
@PostMapping("/test/7")
31+
public ApiResponse<?> test7() throws FirebaseMessagingException {
32+
return ApiResponse.success(Success.FCM_TEST_SUCCESS, fcmService.immminentGift());
33+
}
34+
35+
@PostMapping("/test/30")
36+
public ApiResponse<?> test30() throws FirebaseMessagingException {
37+
return ApiResponse.success(Success.FCM_TEST_SUCCESS, donationService.autoDonate());
38+
}
39+
40+
@GetMapping("/mock/37/{email}")
41+
public ApiResponse<?> mock37(@PathVariable("email") String email, @RequestParam("product") String product) throws FirebaseMessagingException {
42+
return ApiResponse.success(Success.FCM_TEST_SUCCESS, fcmService.mock37(email, product));
43+
}
44+
45+
@GetMapping("/mock/30/{email}")
46+
public ApiResponse<?> mock30(@PathVariable("email") String email, @RequestParam("product") String product) throws FirebaseMessagingException {
47+
return ApiResponse.success(Success.FCM_TEST_SUCCESS, fcmService.mock30(email, product));
48+
}
49+
50+
@GetMapping("/mock/7/{email}")
51+
public ApiResponse<?> mock7(@PathVariable("email") String email, @RequestParam("product") String product) throws FirebaseMessagingException {
52+
return ApiResponse.success(Success.FCM_TEST_SUCCESS, fcmService.mock30(email, product));
53+
}
54+
}

src/main/java/zero/eight/donut/exception/Success.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum Success {
1313
SUCCESS(HttpStatus.OK, "Request successfully processed"),
1414

1515
// 200 OK SUCCESS
16+
FCM_TEST_SUCCESS(HttpStatus.OK, "Successfully completed test case"),
1617
PATCH_STATUS_SUCCESS(HttpStatus.OK, "Successfully patch status"),
1718
MYPAGE_RECEIVER_SUCCESS(HttpStatus.OK, "Get request for receiver's info completed successfully"),
1819
MYPAGE_GIVER_SUCCESS(HttpStatus.OK, "Get request for giver's info completed successfully"),

src/main/java/zero/eight/donut/repository/GiftRepository.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public interface GiftRepository extends JpaRepository<Gift, Long> {
2727
@Query(value = "SELECT * FROM gift g WHERE g.status = 'STORED' AND g.is_assigned = false AND g.auto_donation = true AND g.due_date = :endDate", nativeQuery = true)
2828
List<Gift> findAllByNotAssignedAndStoredAndAutoDonation(@Param("endDate") LocalDateTime endDate);
2929

30-
@Query(value = "SELECT * FROM gift g WHERE g.status = 'UNUSED' AND g.is_assigned = true AND g.due_date = :endDate", nativeQuery = true)
31-
List<Gift> findAllByAssignedAndUnused(@Param("endDate") LocalDateTime endDate);
32-
33-
@Query(value = "SELECT * FROM gift g WHERE g.auto_donation = true AND g.status = 'STORED' AND g.giver_id = :giverId AND g.due_date >= :today", nativeQuery = true)
30+
@Query(value = "SELECT * FROM gift g WHERE g.status = 'STORED' AND g.giver_id = :giverId AND g.due_date >= :today", nativeQuery = true)
3431
List<Gift> findAllByGiverAndStatusAndDueDateAfterOrToday(@Param("giverId") Long giverId, @Param("today") LocalDateTime today);
3532

3633
@Query(value = "SELECT COUNT(*) FROM gift g WHERE g.giver_id = :giverId AND g.is_assigned = false", nativeQuery = true)

src/main/java/zero/eight/donut/service/DonationService.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.time.LocalDateTime;
19+
import java.util.ArrayList;
1920
import java.util.List;
2021

2122
@Slf4j
@@ -29,13 +30,18 @@ public class DonationService {
2930
@Async
3031
@Transactional
3132
@Scheduled(cron = "0 0 0 * * *")
32-
public void autoDonate() throws FirebaseMessagingException {
33+
public List<String> autoDonate() throws FirebaseMessagingException {
3334
List<Gift> giftList = giftRepository.findAllByNotAssignedAndStatusAndDueDateBetween( "STORED", LocalDateTime.now(), LocalDateTime.now().minusDays(30));
35+
List<String> fcmList = new ArrayList<>();
36+
3437
for (Gift gift : giftList) {
3538
gift.updateStatus("UNUSED");
3639
giftRepository.save(gift);
3740
fcmUtils.sendMessage(gift.getGiver().getId(), "wallet: D-30", "Your item" + gift.getProduct() + "is donated now!");
41+
fcmList.add("fcmReceiver: " + gift.getGiver().getName() + "(ROLE_GIVER), fcm title: wallet: D-30, fcm body: Your item" + gift.getProduct() + "is donated now!");
3842
}
43+
44+
return fcmList;
3945
}
4046

4147
public synchronized ApiResponse<?> assignGiftbox(GiftboxRequestDto giftboxRequestDto) {

src/main/java/zero/eight/donut/service/FcmService.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@
88
import org.springframework.transaction.annotation.Transactional;
99
import zero.eight.donut.common.response.ApiResponse;
1010
import zero.eight.donut.config.firebase.FcmUtils;
11-
import zero.eight.donut.domain.FcmToken;
12-
import zero.eight.donut.domain.Gift;
13-
import zero.eight.donut.domain.Giftbox;
14-
import zero.eight.donut.domain.Receiver;
11+
import zero.eight.donut.domain.*;
1512
import zero.eight.donut.dto.fcm.FcmMemberDto;
1613
import zero.eight.donut.dto.fcm.FcmTokenRequestDto;
1714
import zero.eight.donut.exception.Success;
18-
import zero.eight.donut.repository.FcmTokenRepository;
19-
import zero.eight.donut.repository.GiftRepository;
20-
import zero.eight.donut.repository.GiftboxRepository;
15+
import zero.eight.donut.repository.*;
2116

2217
import java.time.LocalDateTime;
18+
import java.util.ArrayList;
2319
import java.util.List;
2420

2521
@RequiredArgsConstructor
@@ -29,6 +25,8 @@ public class FcmService {
2925
private final FcmTokenRepository fcmTokenRepository;
3026
private final GiftRepository giftRepository;
3127
private final GiftboxRepository giftboxRepository;
28+
private final GiverRepository giverRepository;
29+
private final ReceiverRepository receiverRepository;
3230

3331
public ApiResponse<?> registerFcmToken(FcmTokenRequestDto requestDto) throws Exception {
3432
final String token = requestDto.getToken();
@@ -50,27 +48,55 @@ public ApiResponse<?> registerFcmToken(FcmTokenRequestDto requestDto) throws Exc
5048
@Async
5149
@Transactional
5250
@Scheduled(cron = "0 0 0 * * *")
53-
public void imminentWallet() throws FirebaseMessagingException {
51+
public List<String> imminentWallet() throws FirebaseMessagingException {
5452
List<Gift> giftList = giftRepository.findAllByNotAssignedAndStoredAndAutoDonation(LocalDateTime.now().plusDays(37));
53+
List<String> fcmList = new ArrayList<>();
54+
5555
for (Gift gift : giftList) {
5656
// 기프티콘의 giverId로 FCM 전송
5757
fcmUtils.sendMessage(gift.getGiver().getId(), "wallet: D-37", "Your item" + gift.getProduct() + "is expiring soon! It will be automatically donated.");
58+
fcmList.add("fcmReceiver: " + gift.getGiver().getName() + "(ROLE_GIVER), fcm title: wallet: D-37, fcm body: Your item" + gift.getProduct() + "is expiring soon! It will be automatically donated.");
5859
}
60+
61+
return fcmList;
5962
}
6063

6164
// 사용 기한 D-7 push 알림 (수혜자)
6265
@Async
6366
@Transactional
6467
@Scheduled(cron = "0 0 0 * * *")
65-
public void immminentGift() throws FirebaseMessagingException {
68+
public List<String> immminentGift() throws FirebaseMessagingException {
6669
// 7일 뒤 만료되는 꾸러미 찾기
6770
List<Giftbox> giftboxList = giftboxRepository.findAllByIsAvailableAndDueDate(LocalDateTime.now().plusDays(7));
71+
List<String> fcmList = new ArrayList<>();
72+
6873
for (Giftbox giftbox : giftboxList) {
6974
// 수혜자 찾기
7075
Receiver receiver = giftbox.getReceiver();
7176

7277
// 조회된 수혜자에게 FCM 전송
7378
fcmUtils.sendMessage(receiver.getId(), "giftbox: D-7", "Your gift box is expiring soon! You can use it at" + giftbox.getStore() + ".");
79+
fcmList.add("fcmReceiver: " + giftbox.getReceiver().getName() + "(ROLE_RECEIVER), fcm title: giftbox: D-7, fcm body: Your gift box is expiring soon! You can use it at" + giftbox.getStore() + ".");
7480
}
81+
82+
return fcmList;
83+
}
84+
85+
public String mock37(String email, String product) throws FirebaseMessagingException {
86+
Giver giver = giverRepository.findByEmail(email).orElseThrow();
87+
fcmUtils.sendMessage(giver.getId(), "wallet: D-37", "Your item" + product + "is expiring soon! It will be automatically donated.");
88+
return "fcmReceiver: " + email + "(ROLE_GIVER), fcm title: wallet: D-37, fcm body: Your item" + product + "is expiring soon! It will be automatically donated.";
89+
}
90+
91+
public String mock30(String email, String product) throws FirebaseMessagingException {
92+
Giver giver = giverRepository.findByEmail(email).orElseThrow();
93+
fcmUtils.sendMessage(giver.getId(), "wallet: D-30", "Your item" + product + "is donated now!");
94+
return "fcmReceiver: " + giver.getName() + "(ROLE_GIVER), fcm title: wallet: D-30, fcm body: Your item" + product + "is donated now!";
95+
}
96+
97+
public String mock7(String name, String store) throws FirebaseMessagingException {
98+
Receiver receiver = receiverRepository.findByName(name).orElseThrow();
99+
fcmUtils.sendMessage(receiver.getId(), "giftbox: D-7", "Your gift box is expiring soon! You can use it at" + store + ".");
100+
return "fcmReceiver: " + receiver.getName() + "(ROLE_RECEIVER), fcm title: giftbox: D-7, fcm body: Your gift box is expiring soon! You can use it at" + store + ".";
75101
}
76102
}

src/main/java/zero/eight/donut/service/MessageService.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package zero.eight.donut.service;
22

3+
import com.google.firebase.messaging.FirebaseMessagingException;
4+
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
36
import org.springframework.stereotype.Service;
47
import org.springframework.transaction.annotation.Transactional;
58
import zero.eight.donut.common.response.ApiResponse;
9+
import zero.eight.donut.config.firebase.FcmUtils;
610
import zero.eight.donut.config.jwt.AuthUtils;
711
import zero.eight.donut.domain.Gift;
812
import zero.eight.donut.domain.Giftbox;
@@ -14,26 +18,27 @@
1418
import zero.eight.donut.repository.GiftboxRepository;
1519
import zero.eight.donut.repository.MessageRepository;
1620

17-
import java.util.List;
18-
21+
@Getter
22+
@RequiredArgsConstructor
1923
@Service
2024
public class MessageService {
2125

2226
private final GiftboxRepository giftboxRepository;
2327

2428
private final AuthUtils authUtils;
29+
private final FcmUtils fcmUtils;
2530
private final MessageRepository messageRepository;
2631
private final GiftRepository giftRepository;
2732

28-
public MessageService(GiftboxRepository giftboxRepository, AuthUtils authUtils, MessageRepository messageRepository, GiftRepository giftRepository) {
29-
this.giftboxRepository = giftboxRepository;
30-
this.authUtils = authUtils;
31-
this.messageRepository = messageRepository;
32-
this.giftRepository= giftRepository;
33-
}
33+
// public MessageService(GiftboxRepository giftboxRepository, AuthUtils authUtils, MessageRepository messageRepository, GiftRepository giftRepository) {
34+
// this.giftboxRepository = giftboxRepository;
35+
// this.authUtils = authUtils;
36+
// this.messageRepository = messageRepository;
37+
// this.giftRepository= giftRepository;
38+
// }
3439

3540
@Transactional
36-
public ApiResponse<?> sendMessage(SendMessageRequestDto requestDto) {
41+
public ApiResponse<?> sendMessage(SendMessageRequestDto requestDto) throws FirebaseMessagingException {
3742
// 수혜자
3843
Receiver receiver = authUtils.getReceiver();
3944

@@ -63,14 +68,9 @@ public ApiResponse<?> sendMessage(SendMessageRequestDto requestDto) {
6368
.giver(gift.getGiver())
6469
.build();
6570
messageRepository.save(message);
66-
/////////////////////////////////////
67-
/////////////////////////////////////
68-
/////////////////////////////////////
69-
// 기부자에게 알림 보내기
70-
/////////////////////////////////////
71-
/////////////////////////////////////
72-
/////////////////////////////////////
7371

72+
// 기부자에게 알림 보내기
73+
fcmUtils.sendMessage(gift.getGiver().getId(), "message", "You've got a message!");
7474

7575
// 반환하기
7676
return ApiResponse.success(Success.SEND_MESSAGE_SUCCESS);

0 commit comments

Comments
 (0)