Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6aa50c7
feat: Coupon 엔티티 추가 (#355)
sangu1026 May 16, 2025
83968a6
feat: Event관련 Exception과 ExceptionType 정의 (#355)
sangu1026 May 16, 2025
23bf110
feat: Coupon 이벤트 관련 요청 및 응답 DTO 정의 (#355)
sangu1026 May 16, 2025
3827b70
feat: CouponRepository에 쿠폰 코드로 쿠폰 엔티티 찾는 메서드 구현 (#355)
sangu1026 May 16, 2025
b87e71a
feat: CouponService에서 쿠폰 사용 메서드 구현 (#355)
sangu1026 May 16, 2025
366f08e
feat: 쿠폰 등록 API 구현 (#355)
sangu1026 May 16, 2025
d5c1b24
chore: coupon 테이블 생성 마이그레이션 스크립트 작성 (#355)
sangu1026 May 16, 2025
f817190
refactor: 개행 컨벤션에 따라 마지막 줄 개행 제거 (#355)
sangu1026 May 16, 2025
cb456dd
feat: 전체 유저에게 알림을 보낼 관련 피드 아이디를 담은 Request Dto 작성 (#358)
sangu1026 May 17, 2025
be1be0b
feat: 전체 유저에게 알림을 보내는 sendNotificationToAllUser 메서드 구현 (#358)
sangu1026 May 17, 2025
34499eb
feat: 전체 유저에게 알림을 보내는 sendNotificationToAllUser API 구현 (#358)
sangu1026 May 17, 2025
d2dfc7e
refactor: ENUM 명에 맞게 COUPON_EVENT_REWARD의 displayName 수정 (#355)
sangu1026 May 17, 2025
8c7c426
refactor: 개행 컨벤션에 따라 클래스내 첫번째 줄은 개행 추가 (#355)
sangu1026 May 17, 2025
67e4993
refactor: 알림 title과 body는 request DTO로 받은대로 설정 (#358)
sangu1026 May 17, 2025
b2567cb
Merge pull request #356 from buddy-ya/feature/buddyya-coupon-point-ev…
sangu1026 May 17, 2025
8eeeee8
Merge pull request #361 from buddy-ya/feature/send-alarm-to-all-user#358
sangu1026 May 17, 2025
5ac6e3b
feat: 로그인시 기존 유저일 경우 매칭 프로필 전달 (#362)
mangsuyo May 18, 2025
42f7dac
Merge pull request #363 from buddy-ya/fix/match-profile#362
mangsuyo May 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@
import com.team.buddyya.certification.dto.response.TestAccountResponse;
import com.team.buddyya.certification.exception.PhoneAuthenticationException;
import com.team.buddyya.certification.exception.PhoneAuthenticationExceptionType;
import com.team.buddyya.certification.repository.*;
import com.team.buddyya.certification.repository.AdminAccountRepository;
import com.team.buddyya.certification.repository.PhoneInfoRepository;
import com.team.buddyya.certification.repository.RegisteredPhoneRepository;
import com.team.buddyya.certification.repository.StudentIdCardRepository;
import com.team.buddyya.certification.repository.TestAccountRepository;
import com.team.buddyya.point.domain.Point;
import com.team.buddyya.point.service.FindPointService;
import com.team.buddyya.student.domain.MatchingProfile;
import com.team.buddyya.student.domain.Student;
import com.team.buddyya.student.dto.response.UserResponse;
import com.team.buddyya.student.exception.StudentException;
import com.team.buddyya.student.exception.StudentExceptionType;
import com.team.buddyya.student.repository.MatchingProfileRepository;
import com.team.buddyya.student.repository.StudentRepository;
import com.team.buddyya.student.service.InvitationService;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -37,6 +44,7 @@ public class PhoneAuthenticationService {
private final TestAccountRepository testAccountRepository;
private final FindPointService findPointService;
private final AdminAccountRepository adminAccountRepository;
private final MatchingProfileRepository matchingProfileRepository;
private final InvitationService invitationService;
private final JwtUtils jwtUtils;

Expand Down Expand Up @@ -68,7 +76,9 @@ public UserResponse checkMembership(String phoneNumber) {
student.getAvatar().setLoggedOut(false);
boolean isStudentIdCardRequested = studentIdCardRepository.findByStudent(student).isPresent();
Point point = findPointService.findByStudent(student);
return UserResponse.fromCheckMembership(student, isStudentIdCardRequested, EXISTING_MEMBER, accessToken, refreshToken, point);
MatchingProfile matchingProfile = getMatchingProfile(student);
return UserResponse.fromCheckMembership(student, isStudentIdCardRequested, EXISTING_MEMBER, accessToken,
refreshToken, point, matchingProfile);
}
return UserResponse.fromCheckMembership(NEW_MEMBER);
}
Expand Down Expand Up @@ -106,7 +116,13 @@ private void resetMessageCountIfExists(String udId) {
return;
}
PhoneInfo phoneInfo = phoneInfoRepository.findPhoneInfoByUdId(udId)
.orElseThrow(() -> new PhoneAuthenticationException(PhoneAuthenticationExceptionType.PHONE_INFO_NOT_FOUND));
.orElseThrow(
() -> new PhoneAuthenticationException(PhoneAuthenticationExceptionType.PHONE_INFO_NOT_FOUND));
phoneInfo.resetMessageSendCount();
}

private MatchingProfile getMatchingProfile(Student student) {
return matchingProfileRepository.findByStudent(student)
.orElseThrow(() -> new StudentException(StudentExceptionType.MATCHING_PROFILE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.team.buddyya.event.controller;

import com.team.buddyya.auth.domain.CustomUserDetails;
import com.team.buddyya.event.dto.CouponRequest;
import com.team.buddyya.event.dto.CouponResponse;
import com.team.buddyya.event.service.EventService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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;

@RestController
@RequestMapping("/events")
@RequiredArgsConstructor
public class EventController {

private final EventService eventService;

@PostMapping("/coupon")
public ResponseEntity<CouponResponse> registerCoupon(@AuthenticationPrincipal CustomUserDetails userDetails,
@RequestBody CouponRequest request) {
CouponResponse response = eventService.useCoupon(userDetails.getStudentInfo(), request);
return ResponseEntity.ok(response);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/team/buddyya/event/domain/Coupon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.team.buddyya.event.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.GenerationType.IDENTITY;

@Entity
@Table(name = "coupon")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Coupon {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(name = "code", length = 10, nullable = false, unique = true)
private String code;

@Column(name = "used", nullable = false)
private Boolean isUsed;

@Builder
public Coupon(String code) {
this.code = code;
this.isUsed = false;
}

public void markAsUsed() {
this.isUsed = true;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/team/buddyya/event/dto/CouponRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.team.buddyya.event.dto;

public record CouponRequest(String code) {
}
12 changes: 12 additions & 0 deletions src/main/java/com/team/buddyya/event/dto/CouponResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.team.buddyya.event.dto;

import com.team.buddyya.point.domain.Point;
import com.team.buddyya.point.domain.PointType;

public record CouponResponse(Integer point,
int pointChange) {

public static CouponResponse from(Point point, PointType pointType) {
return new CouponResponse(point.getCurrentPoint(), pointType.getPointChange());
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/team/buddyya/event/exception/EventException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.team.buddyya.event.exception;

import com.team.buddyya.common.exception.BaseException;
import com.team.buddyya.common.exception.BaseExceptionType;

public class EventException extends BaseException {

private final EventExceptionType exceptionType;

public EventException(EventExceptionType exceptionType) {
this.exceptionType = exceptionType;
}

@Override
public BaseExceptionType exceptionType() {
return exceptionType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.team.buddyya.event.exception;

import com.team.buddyya.common.exception.BaseExceptionType;
import org.springframework.http.HttpStatus;

public enum EventExceptionType implements BaseExceptionType {

COUPON_NOT_FOUND(12000, HttpStatus.NOT_FOUND, "The coupon does not exist."),
COUPON_ALREADY_USED(12001, HttpStatus.BAD_REQUEST, "The coupon has already been used.");

private final int errorCode;
private final HttpStatus httpStatus;
private final String errorMessage;

EventExceptionType(int errorCode, HttpStatus httpStatus, String errorMessage) {
this.errorCode = errorCode;
this.httpStatus = httpStatus;
this.errorMessage = errorMessage;
}

@Override
public int errorCode() {
return errorCode;
}

@Override
public HttpStatus httpStatus() {
return httpStatus;
}

@Override
public String errorMessage() {
return errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.team.buddyya.event.repository;

import com.team.buddyya.event.domain.Coupon;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CouponRepository extends JpaRepository<Coupon, Long> {

Optional<Coupon> findByCode(String code);
}
39 changes: 39 additions & 0 deletions src/main/java/com/team/buddyya/event/service/EventService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.team.buddyya.event.service;

import com.team.buddyya.auth.domain.StudentInfo;
import com.team.buddyya.event.domain.Coupon;
import com.team.buddyya.event.dto.CouponRequest;
import com.team.buddyya.event.dto.CouponResponse;
import com.team.buddyya.event.exception.EventException;
import com.team.buddyya.event.exception.EventExceptionType;
import com.team.buddyya.event.repository.CouponRepository;
import com.team.buddyya.point.domain.Point;
import com.team.buddyya.point.domain.PointType;
import com.team.buddyya.point.service.UpdatePointService;
import com.team.buddyya.student.domain.Student;
import com.team.buddyya.student.service.FindStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class EventService {

private final FindStudentService findStudentService;
private final UpdatePointService updatePointService;
private final CouponRepository couponRepository;

public CouponResponse useCoupon(StudentInfo studentInfo, CouponRequest request) {
Coupon coupon = couponRepository.findByCode(request.code())
.orElseThrow(() -> new EventException(EventExceptionType.COUPON_NOT_FOUND));
if (coupon.getIsUsed()) {
throw new EventException(EventExceptionType.COUPON_ALREADY_USED);
}
Student student = findStudentService.findByStudentId(studentInfo.id());
Point updatedPoint = updatePointService.updatePoint(student, PointType.COUPON_EVENT_REWARD);
coupon.markAsUsed();
return CouponResponse.from(updatedPoint, PointType.COUPON_EVENT_REWARD);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.team.buddyya.notification.controller;

import com.team.buddyya.auth.domain.CustomUserDetails;
import com.team.buddyya.notification.dto.request.PushToAllUsersRequest;
import com.team.buddyya.notification.dto.request.SaveTokenRequest;
import com.team.buddyya.notification.dto.response.SaveTokenResponse;
import com.team.buddyya.notification.service.NotificationService;
Expand All @@ -21,4 +22,10 @@ public ResponseEntity<SaveTokenResponse> registerPushToken(@AuthenticationPrinci
@RequestBody SaveTokenRequest request) {
return ResponseEntity.ok(notificationService.savePushToken(userDetails.getStudentInfo().id(), request.token()));
}

@PostMapping("/send-to-all")
public ResponseEntity<Void> sendNotificationToAllUser(@RequestBody PushToAllUsersRequest request) {
notificationService.sendNotificationToAllUser(request);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.team.buddyya.notification.dto.request;

public record PushToAllUsersRequest(long feedId, String title, String body) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import com.team.buddyya.chatting.domain.Chatroom;
import com.team.buddyya.feed.domain.Comment;
import com.team.buddyya.feed.domain.Feed;
import com.team.buddyya.feed.exception.FeedException;
import com.team.buddyya.feed.exception.FeedExceptionType;
import com.team.buddyya.feed.repository.FeedRepository;
import com.team.buddyya.match.domain.MatchRequest;
import com.team.buddyya.notification.domain.ExpoToken;
import com.team.buddyya.notification.domain.RequestNotification;
import com.team.buddyya.notification.dto.request.PushToAllUsersRequest;
import com.team.buddyya.notification.dto.response.SaveTokenResponse;
import com.team.buddyya.notification.exception.NotificationException;
import com.team.buddyya.notification.exception.NotificationExceptionType;
Expand Down Expand Up @@ -42,6 +46,7 @@ public class NotificationService {

private final ExpoTokenRepository expoTokenRepository;
private final FindStudentService findStudentService;
private final FeedRepository feedRepository;
private final RestTemplate restTemplate;
private final ObjectMapper objectMapper;

Expand Down Expand Up @@ -217,6 +222,35 @@ private String getCommentReplyNotificationTitle(boolean isKorean) {
return isKorean ? FEED_REPLY_TITLE_KR : FEED_REPLY_TITLE_EN;
}

public void sendNotificationToAllUser(PushToAllUsersRequest request) {
Feed feed = feedRepository.findById(request.feedId())
.orElseThrow(() -> new FeedException(FeedExceptionType.FEED_NOT_FOUND));
String title = request.title();
String body = request.body();
Map<String, Object> data = Map.of(
"feedId", feed.getId(),
"type", "FEED"
);
List<ExpoToken> tokens = expoTokenRepository.findAll();
for (ExpoToken expoToken : tokens) {
try {
String token = expoToken.getToken();
if (token == null || token.isBlank()) continue;
RequestNotification notification = RequestNotification.builder()
.to(token)
.title(title)
.body(body)
.data(data)
.build();
sendToExpo(notification);
} catch (NotificationException e) {
log.warn("전체 유저 알림 전송 실패 - studentId: {}, error: {}",
expoToken.getStudent().getId(),
e.exceptionType().errorMessage());
}
}
}

public void sendCommentNotification(Long writerId, Feed feed, String commentContent) {
boolean isFeedOwner = feed.isFeedOwner(writerId);
if (!isFeedOwner) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/team/buddyya/point/domain/PointType.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum PointType {
MISSION_CERTIFICATION_REWARD("mission_certification_reward", 100, PointChangeType.MISSION),
MISSION_VISIT_REWARD("mission_visit_reward", 10, PointChangeType.MISSION),
FESTIVAL_REWARD("festival_reward", 50, PointChangeType.EARN),
COUPON_EVENT_REWARD("coupon_event_reward", 100, PointChangeType.EARN),
EVENT_REWARD("event_reward", 10, PointChangeType.EARN);

private final String displayName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public static UserResponse fromOnboard(Student student, Boolean isStudentIdCardR
}

public static UserResponse fromCheckMembership(Student student, Boolean isStudentIdCardRequested, String status,
String accessToken, String refreshToken, Point point) {
String accessToken, String refreshToken, Point point,
MatchingProfile matchingProfile) {
return new UserResponse(
student.getId(),
student.getRole().name(),
Expand All @@ -163,9 +164,9 @@ public static UserResponse fromCheckMembership(Student student, Boolean isStuden
null,
accessToken,
refreshToken,
null,
null,
null,
matchingProfile.getIntroduction(),
matchingProfile.getBuddyActivity(),
matchingProfile.isCompleted(),
student.getUniversity().getIsMatchingActive(),
student.getUniversity().getIsFeedActive()
);
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/db/migration/V31__Create_coupon_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE coupon
(
id BIGINT AUTO_INCREMENT NOT NULL,
code VARCHAR(10) NOT NULL,
used BIT(1) NOT NULL,
CONSTRAINT pk_coupon PRIMARY KEY (id)
);

ALTER TABLE coupon
ADD CONSTRAINT uc_coupon_code UNIQUE (code);