Skip to content

Commit

Permalink
Merge pull request #212 from PLADI-ALM/feat/PDS-189-asserts-notification
Browse files Browse the repository at this point in the history
[PDS-189/feat] 자산 정보 확인 요청 알림 구현
  • Loading branch information
leeseunghakhello authored Nov 22, 2023
2 parents eaaf68d + 4bd52df commit 6691ef8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public static class NotificationCategory{
public static final String CAR = "차량";
public static final String EQUIPMENT = "장비";
public static final String OFFICE = "회의실";
public static final String ASSETS = "자산 정보 확인 요청";
public static final String ASSETS_TITLE = "자산 정보 확인 요청";
public static final String ASSETS_CHECK_MESSAGE = " 정보 확인과 수정 부탁드립니다.";
public static final String ASSETS_INDIVIDUAL = "개인 자산 ";
}

public static class Notification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum BaseResponseCode {
EXPIRED_TOKEN("T0005", HttpStatus.FORBIDDEN, "만료된 토큰 값입니다."),
NOT_ACCESS_HEADER("T0006", HttpStatus.INTERNAL_SERVER_ERROR, "헤더에 접근할 수 없습니다."),
BLACKLIST_TOKEN("T0007", HttpStatus.FORBIDDEN, "로그아웃 혹은 회원 탈퇴된 토큰입니다."),
NOTIFICATION_TOKEN_NOT_FOUND("T0008", HttpStatus.NOT_FOUND, "유효하지 않은 알림 토큰입니다."),


// User
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package com.example.pladialmserver.notification.service;

import com.example.pladialmserver.global.Constants;
import com.example.pladialmserver.global.exception.BaseException;
import com.example.pladialmserver.global.exception.BaseResponseCode;
import com.example.pladialmserver.global.utils.EmailUtil;
import com.example.pladialmserver.notification.dto.FcmMessage;
import com.example.pladialmserver.notification.entity.PushNotification;
import com.example.pladialmserver.notification.repository.PushNotificationRepository;
import com.example.pladialmserver.user.dto.request.SendAssetsEmailReq;
import com.example.pladialmserver.user.entity.User;
import com.example.pladialmserver.user.repository.user.UserRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auth.oauth2.GoogleCredentials;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.tomcat.util.bcel.Const;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

import static com.example.pladialmserver.global.Constants.EmailNotification.*;


@Slf4j
@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -28,6 +37,7 @@ public class PushNotificationService {
private final ObjectMapper objectMapper;
private final PushNotificationRepository notificationRepository;
private final UserRepository userRepository;
private final EmailUtil emailUtil;

@Transactional
public void sendNotification(String category, String type, User user) throws IOException {
Expand All @@ -40,10 +50,39 @@ public void sendNotification(String category, String type, User user) throws IOE
}
}

public void sendAssetsNotification() {
List<User> all = userRepository.findAll();
String title = Constants.NotificationCategory.ASSETS;
@Transactional
public void sendNotification(User user, String title, String messageBody) throws IOException {
if (user.getFcmToken() != null) {
FcmMessage fcmMessage = FcmMessage.makeMessage(user.getFcmToken(), title, messageBody);
Response response = sendMessage(objectMapper.writeValueAsString(fcmMessage));
notificationRepository.save(PushNotification.toEntity(title, messageBody, user));
}
}

// 자산 정보 확인 알림 / 알림 전송
@Scheduled(cron = "0 0 9 1 */3 ?", zone = "GMT+9:00")
public void sendAssetsNotification(){
String title = Constants.NotificationCategory.ASSETS_TITLE;
userRepository.findByAssetsIsNotNull().forEach(user -> {
try {
sendNotification(user, title, extractAssetsMessageBody(user.getAssets()));
} catch (IOException e) {
throw new BaseException(BaseResponseCode.NOTIFICATION_TOKEN_NOT_FOUND);
}
});
}

// 자산 정보 확인 알림 / 메일 전송
@Scheduled(cron = "0 0 9 1 */3 ?", zone = "GMT+9:00")
public void sendAssetsEmail(){
userRepository.findByAssetsIsNotNull().forEach(user -> {
emailUtil.sendEmail(user.getEmail(), COMPANY_NAME + ASSETS_TITLE
, emailUtil.createAssetsData(SendAssetsEmailReq.toDto(user)), ASSETS_TEMPLATE);
});
}

private String extractAssetsMessageBody(String assets) {
return Constants.NotificationCategory.ASSETS_INDIVIDUAL + assets + Constants.NotificationCategory.ASSETS_CHECK_MESSAGE;
}

private String getTitle(String category, String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,6 @@ public UserRes getUserInfo(User user) {
return UserRes.toDto(user);
}

// 자산 정보 확인 알림 / 메일 전송
@Scheduled(cron = "0 0 9 1 */3 ?", zone = "GMT+9:00")
public void sendAssetsEmail(){
userRepository.findByAssetsIsNotNull().forEach(user -> {
emailUtil.sendEmail(user.getEmail(), COMPANY_NAME + ASSETS_TITLE
, emailUtil.createAssetsData(SendAssetsEmailReq.toDto(user)), ASSETS_TEMPLATE);
});
}

// ===================================================================================================================
// [관리자-사용자]
// ===================================================================================================================
Expand Down

0 comments on commit 6691ef8

Please sign in to comment.