Skip to content

Commit

Permalink
Merge pull request #137 from 9uttery/feat/notification-#132
Browse files Browse the repository at this point in the history
[Feature] 토큰 등록, 알림 조회 API 구현
  • Loading branch information
mingeun0507 authored Feb 28, 2024
2 parents 8cbf851 + ff8d14a commit 5f26c0e
Show file tree
Hide file tree
Showing 17 changed files with 313 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.guttery.madii.common.converter.StringToYearMonthConverter;
import com.guttery.madii.common.converter.YearMonthToStringConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
Expand All @@ -11,11 +10,6 @@

@Configuration
public class MongoConfiguration {
private static final String MONGO_DB_NAME = "madii";

@Value("${spring.data.mongodb.uri}")
private String mongoDbUrl;

@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public enum ErrorDetails {
FILE_UPLOAD_FAILED("F001", HttpStatus.INTERNAL_SERVER_ERROR.value(), "파일 업로드에 실패했습니다."),

FIREBASE_INTEGRATION_FAILED("FI001", HttpStatus.INTERNAL_SERVER_ERROR.value(), "Firebase 연동 중 오류가 발생했습니다. 다시 시도해 주세요."),
FIREBASE_NOTIFICATION_SEND_ERROR("FI002", HttpStatus.INTERNAL_SERVER_ERROR.value(), "Firebase 알림 전송 중 오류가 발생했습니다."),
USER_TOKEN_INFORMATION_NOT_EXISTS("FI003", HttpStatus.NOT_FOUND.value(), "사용자의 토큰 정보가 존재하지 않습니다."),

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public CalenderDailyJoyAchievementResponse getDailyJoyAchievementInfos(final Lon
.from(achievement)
.join(achievement.joy, joy)
.join(achievement.achiever, user)
.where(achievement.achiever.userId.eq(userId), achievement.createdAt.between(startOfDay, endOfDay), achievement.finishInfo.isFinished.isTrue())
.where(achievement.achiever.userId.eq(userId), achievement.finishInfo.finishedAt.between(startOfDay, endOfDay), achievement.finishInfo.isFinished.isTrue())
.fetch();

return new CalenderDailyJoyAchievementResponse(dailyJoyAchievementInfos);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.guttery.madii.domain.notification.application.dto;

public record NotificationData(
String title,
String contents
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.guttery.madii.domain.notification.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "알림 정보")
public record NotificationInfo(
@Schema(description = "알림 이름")
String title,
@Schema(description = "알림 내용")
String contents,
@Schema(description = "알림 생성 날짜")
String createdAt
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.guttery.madii.domain.notification.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

@Schema(description = "알림 리스트 응답")
public record NotificationListResponse(
@Schema(description = "알림 리스트")
List<NotificationInfo> notificationInfos
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.guttery.madii.domain.notification.application.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;

@Schema(description = "토큰 저장 요청")
public record SaveTokenRequest(
@NotBlank
@Schema(description = "토큰", example = "fcm_token_value")
String token
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.guttery.madii.domain.notification.application.mapper;

import com.guttery.madii.domain.notification.application.dto.NotificationInfo;
import com.guttery.madii.domain.notification.domain.model.Notification;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.format.DateTimeFormatter;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class NotificationMapper {
public static NotificationInfo toNotificationInfo(Notification notification) {
return new NotificationInfo(
notification.getTitle(),
notification.getContents(),
notification.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.guttery.madii.domain.notification.application.service;

import com.guttery.madii.domain.notification.application.dto.NotificationInfo;
import com.guttery.madii.domain.notification.application.dto.NotificationListResponse;
import com.guttery.madii.domain.notification.application.mapper.NotificationMapper;
import com.guttery.madii.domain.notification.domain.repository.NotificationRepository;
import com.guttery.madii.domain.user.application.service.UserServiceHelper;
import com.guttery.madii.domain.user.domain.model.User;
import com.guttery.madii.domain.user.domain.model.UserPrincipal;
import com.guttery.madii.domain.user.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
@Service
public class NotificationService {
private static final int DATE_CRITERIA_OFFSET = 30;

private final NotificationRepository notificationRepository;
private final UserRepository userRepository;

public NotificationListResponse getNotificationList(final UserPrincipal userPrincipal) {
final User foundUser = UserServiceHelper.findExistingUser(userRepository, userPrincipal);
final LocalDateTime dateCriteria = LocalDateTime.now().toLocalDate().atStartOfDay().minusDays(DATE_CRITERIA_OFFSET);
final List<NotificationInfo> notificationInfos = notificationRepository.findAllByUserAndCreatedAtIsAfter(foundUser, dateCriteria)
.stream()
.map(NotificationMapper::toNotificationInfo)
.toList();

return new NotificationListResponse(notificationInfos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.guttery.madii.domain.notification.application.service;

import com.guttery.madii.common.exception.CustomException;
import com.guttery.madii.common.exception.ErrorDetails;
import com.guttery.madii.domain.notification.application.dto.SaveTokenRequest;
import com.guttery.madii.domain.notification.domain.model.UserTokens;
import com.guttery.madii.domain.notification.domain.repository.UserTokensRepository;
import com.guttery.madii.domain.user.application.service.UserServiceHelper;
import com.guttery.madii.domain.user.domain.model.UserPrincipal;
import com.guttery.madii.domain.user.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Slf4j
@Service
public class UserTokenService {
private final UserTokensRepository userTokensRepository;
private final UserRepository userRepository;

@Transactional
public void saveUserToken(final SaveTokenRequest saveTokenRequest, final UserPrincipal userPrincipal) {
UserServiceHelper.findExistingUser(userRepository, userPrincipal);

final UserTokens foundUserTokens = userTokensRepository.findById(userPrincipal.id().toString())
.orElseGet(() -> UserTokens.createEmpty(userPrincipal.id().toString()));

foundUserTokens.addToken(saveTokenRequest.token());
userTokensRepository.save(foundUserTokens);
}

@Transactional
public void deleteUserToken(final String token, final UserPrincipal userPrincipal) {
UserServiceHelper.findExistingUser(userRepository, userPrincipal);
final UserTokens foundUserTokens = userTokensRepository.findById(userPrincipal.id().toString())
.orElseThrow(() -> CustomException.of(ErrorDetails.USER_TOKEN_INFORMATION_NOT_EXISTS));

foundUserTokens.removeToken(token);
userTokensRepository.save(foundUserTokens);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.guttery.madii.domain.notification.domain.model;

import com.guttery.madii.common.domain.model.BaseTimeEntity;
import com.guttery.madii.domain.user.domain.model.User;
import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "t_notification")
@Access(AccessType.FIELD)
public class Notification extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long notificationId;
@ManyToOne(fetch = FetchType.LAZY)
private User user;
private String title;
private String contents;
private Boolean isChecked;

private Notification(User user, String title, String contents, Boolean isChecked) {
this.user = user;
this.title = title;
this.contents = contents;
this.isChecked = isChecked;
}

public static Notification create(User user, String title, String contents) {
return new Notification(user, title, contents, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.guttery.madii.domain.notification.domain.model;

import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bson.codecs.pojo.annotations.BsonProperty;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.List;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Document(collection = "usertokens")
public class UserTokens {
@Id
@BsonProperty("_id")
private String _id;
private List<String> tokens;

public static UserTokens createEmpty(String userId) {
return new UserTokens(userId, new ArrayList<>());
}

public void addToken(String token) {
tokens.add(token);
}

public void removeToken(String token) {
tokens.remove(token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.guttery.madii.domain.notification.domain.repository;

import com.guttery.madii.domain.notification.domain.model.Notification;
import com.guttery.madii.domain.user.domain.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;
import java.util.List;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findAllByUserAndCreatedAtIsAfter(User user, LocalDateTime createdAt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.guttery.madii.domain.notification.domain.repository;

import com.guttery.madii.domain.notification.domain.model.UserTokens;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserTokensRepository extends MongoRepository<UserTokens, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.guttery.madii.common.exception.CustomException;
import com.guttery.madii.common.exception.ErrorDetails;
import com.guttery.madii.domain.notification.application.dto.NotificationData;
import com.guttery.madii.domain.notification.domain.service.NotificationClient;
import org.springframework.stereotype.Component;

@Component
public class FirebaseNotificationClient implements NotificationClient {
public String sendNotification(String content, String token) {
private static final String NOTIFICATION_TITLE_KEY = "title";
private static final String NOTIFICATION_MESSAGE_KEY = "message";

public String sendNotification(final NotificationData notificationData, final String token) {
try {
final Message message = Message.builder()
.setToken(token)
.putData("title", content)
.putData(NOTIFICATION_TITLE_KEY, notificationData.title())
.putData(NOTIFICATION_MESSAGE_KEY, notificationData.contents())
.build();

return FirebaseMessaging.getInstance().send(message);

} catch (FirebaseMessagingException e) {
return "Failed";
} catch (final FirebaseMessagingException e) {
throw CustomException.of(ErrorDetails.FIREBASE_NOTIFICATION_SEND_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.guttery.madii.domain.notification.presentation;

import com.guttery.madii.domain.notification.application.dto.NotificationListResponse;
import com.guttery.madii.domain.notification.application.dto.SaveTokenRequest;
import com.guttery.madii.domain.notification.application.service.NotificationService;
import com.guttery.madii.domain.notification.application.service.UserTokenService;
import com.guttery.madii.domain.user.domain.model.UserPrincipal;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/notification")
@Validated
@Tag(name = "Notification", description = "Notification 관련 API입니다.")
public class NotificationController {
private final UserTokenService userTokenService;
private final NotificationService notificationService;

@PostMapping("/token")
public void saveUserToken(
@RequestBody @Valid SaveTokenRequest request,
@NotNull @AuthenticationPrincipal UserPrincipal userPrincipal
) {
userTokenService.saveUserToken(request, userPrincipal);
}

@GetMapping
public NotificationListResponse getNotificationList(
@NotNull @AuthenticationPrincipal UserPrincipal userPrincipal
) {
return notificationService.getNotificationList(userPrincipal);
}
}
Loading

0 comments on commit 5f26c0e

Please sign in to comment.