Skip to content

Commit 8890aea

Browse files
author
Lee Euije
authored
YEL-162 [deploy] v1.196
YEL-162 [deploy] v1.196
2 parents a1f8a51 + fddc35d commit 8890aea

File tree

16 files changed

+193
-82
lines changed

16 files changed

+193
-82
lines changed

src/main/java/com/yello/server/domain/friend/service/FriendService.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.yello.server.domain.friend.service;
22

33
import static com.yello.server.global.common.ErrorCode.EXIST_FRIEND_EXCEPTION;
4-
import static com.yello.server.global.common.ErrorCode.LACK_USER_EXCEPTION;
5-
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
64
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
75
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;
86

@@ -20,6 +18,7 @@
2018
import com.yello.server.domain.user.entity.User;
2119
import com.yello.server.domain.user.repository.UserRepository;
2220
import com.yello.server.domain.vote.repository.VoteRepository;
21+
import com.yello.server.domain.vote.service.VoteManager;
2322
import com.yello.server.global.common.factory.PaginationFactory;
2423
import java.lang.Character.UnicodeBlock;
2524
import java.util.ArrayList;
@@ -43,6 +42,7 @@ public class FriendService {
4342
private final FriendRepository friendRepository;
4443
private final UserRepository userRepository;
4544
private final VoteRepository voteRepository;
45+
private final VoteManager voteManager;
4646

4747
public FriendsResponse findAllFriends(Pageable pageable, Long userId) {
4848
final Page<Friend> friendsData = friendRepository.findAllFriendsByUserId(pageable, userId);
@@ -77,20 +77,7 @@ public Friend addFriend(Long userId, Long targetId) {
7777
public List<FriendShuffleResponse> findShuffledFriend(Long userId) {
7878
final User user = userRepository.getById(userId);
7979

80-
final List<Friend> friends =
81-
new ArrayList<>(friendRepository.findAllByUserId(user.getId()));
82-
List<Friend> friendList = new ArrayList<>(friends);
83-
84-
if (friendList.size() < RANDOM_COUNT) {
85-
throw new FriendException(LACK_USER_EXCEPTION);
86-
}
87-
88-
Collections.shuffle(friendList);
89-
90-
return friendList.stream()
91-
.map(FriendShuffleResponse::of)
92-
.limit(RANDOM_COUNT)
93-
.toList();
80+
return voteManager.getShuffledFriends(user);
9481
}
9582

9683
public RecommendFriendResponse findAllRecommendSchoolFriends(Pageable pageable, Long userId) {

src/main/java/com/yello/server/domain/purchase/service/PurchaseManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ void handleAppleTransactionError(ResponseEntity<TransactionInfoResponse> respons
2424

2525
void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO);
2626

27+
void refundAppleInApp(AppleNotificationPayloadVO payloadVO);
2728
}

src/main/java/com/yello/server/domain/purchase/service/PurchaseManagerImpl.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import static com.yello.server.global.common.ErrorCode.APPLE_TOKEN_SERVER_EXCEPTION;
44
import static com.yello.server.global.common.ErrorCode.GOOGLE_SUBSCRIPTIONS_SUBSCRIPTION_EXCEPTION;
55
import static com.yello.server.global.common.ErrorCode.NOT_FOUND_TRANSACTION_EXCEPTION;
6+
import static com.yello.server.global.common.util.ConstantUtil.REFUND_FIVE_TICKET;
7+
import static com.yello.server.global.common.util.ConstantUtil.REFUND_ONE_TICKET;
8+
import static com.yello.server.global.common.util.ConstantUtil.REFUND_TWO_TICKET;
69

710
import com.fasterxml.jackson.databind.ObjectMapper;
811
import com.yello.server.domain.purchase.dto.apple.AppleNotificationPayloadVO;
@@ -68,7 +71,7 @@ public void handleAppleTransactionError(ResponseEntity<TransactionInfoResponse>
6871

6972
@Override
7073
public AppleNotificationPayloadVO decodeApplePayload(String signedPayload) {
71-
74+
7275
Map<String, Object> jsonPayload = DecodeTokenFactory.decodeToken(signedPayload);
7376
ObjectMapper objectMapper = new ObjectMapper();
7477

@@ -117,5 +120,35 @@ public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) {
117120
}
118121
}
119122

123+
@Override
124+
public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) {
125+
String transactionId =
126+
decodeAppleNotificationData(payloadVO.data().signedTransactionInfo());
127+
Purchase purchase = purchaseRepository.findByTransactionId(transactionId)
128+
.orElseThrow(() -> new PurchaseNotFoundException(NOT_FOUND_TRANSACTION_EXCEPTION));
129+
User user = purchase.getUser();
130+
131+
switch (purchase.getProductType()) {
132+
case YELLO_PLUS -> {
133+
user.setSubscribe(Subscribe.NORMAL);
134+
}
135+
case ONE_TICKET -> {
136+
validateTicketCount(REFUND_ONE_TICKET, user);
137+
}
138+
case TWO_TICKET -> {
139+
validateTicketCount(REFUND_TWO_TICKET, user);
140+
}
141+
case FIVE_TICKET -> {
142+
validateTicketCount(REFUND_FIVE_TICKET, user);
143+
}
144+
}
145+
}
146+
147+
public void validateTicketCount(int ticketCount, User user) {
148+
if (user.getTicketCount() >= ticketCount) {
149+
user.setTicketCount(-Math.abs(ticketCount));
150+
}
151+
}
152+
120153

121154
}

src/main/java/com/yello/server/domain/purchase/service/PurchaseService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void verifyAppleSubscriptionTransaction(Long userId,
110110
}
111111

112112
purchaseManager.createSubscribe(user, Gateway.APPLE, request.transactionId());
113-
user.addTicketCount(3);
113+
user.setTicketCount(3);
114114
}
115115

116116
@Transactional
@@ -126,17 +126,17 @@ public void verifyAppleTicketTransaction(Long userId, AppleTransaction request)
126126
case ONE_TICKET_ID:
127127
purchaseManager.createTicket(user, ProductType.ONE_TICKET, Gateway.APPLE,
128128
request.transactionId());
129-
user.addTicketCount(1);
129+
user.setTicketCount(1);
130130
break;
131131
case TWO_TICKET_ID:
132132
purchaseManager.createTicket(user, ProductType.TWO_TICKET, Gateway.APPLE,
133133
request.transactionId());
134-
user.addTicketCount(2);
134+
user.setTicketCount(2);
135135
break;
136136
case FIVE_TICKET_ID:
137137
purchaseManager.createTicket(user, ProductType.FIVE_TICKET, Gateway.APPLE,
138138
request.transactionId());
139-
user.addTicketCount(5);
139+
user.setTicketCount(5);
140140
break;
141141
default:
142142
throw new PurchaseException(NOT_FOUND_TRANSACTION_EXCEPTION);
@@ -206,7 +206,7 @@ public GoogleSubscriptionGetResponse verifyGoogleSubscriptionTransaction(Long us
206206
case ConstantUtil.GOOGLE_PURCHASE_SUBSCRIPTION_ACTIVE -> {
207207
final Purchase subscribe =
208208
purchaseManager.createSubscribe(user, Gateway.GOOGLE, request.orderId());
209-
user.addTicketCount(3);
209+
user.setTicketCount(3);
210210
subscribe.setTransactionId(request.orderId());
211211
}
212212
}
@@ -259,7 +259,7 @@ public GoogleTicketGetResponse verifyGoogleTicketTransaction(Long userId,
259259
Purchase ticket =
260260
purchaseManager.createTicket(user, getProductType(request.productId()),
261261
Gateway.GOOGLE, request.orderId());
262-
user.addTicketCount(getTicketAmount(request.productId()) * request.quantity());
262+
user.setTicketCount(getTicketAmount(request.productId()) * request.quantity());
263263
ticket.setTransactionId(inAppResponse.getBody().orderId());
264264
} else {
265265
throw new GoogleBadRequestException(GOOGLE_INAPP_BAD_REQUEST_EXCEPTION);
@@ -304,7 +304,7 @@ public void appleNotification(AppleNotificationRequest request) {
304304
purchaseManager.changeSubscriptionStatus(payloadVO);
305305
break;
306306
case APPLE_NOTIFICATION_REFUND:
307-
System.out.println("dd");
307+
purchaseManager.refundAppleInApp(payloadVO);
308308
break;
309309
case APPLE_NOTIFICATION_TEST:
310310
return;

src/main/java/com/yello/server/domain/user/entity/User.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public static User of(SignUpRequest signUpRequest, School group) {
122122
.group(group)
123123
.groupAdmissionYear(signUpRequest.groupAdmissionYear())
124124
.email(signUpRequest.email())
125-
.deviceToken(Objects.equals(signUpRequest.deviceToken(), "") ? null : signUpRequest.deviceToken())
125+
.deviceToken(Objects.equals(signUpRequest.deviceToken(), "") ? null
126+
: signUpRequest.deviceToken())
126127
.subscribe(Subscribe.NORMAL)
127128
.ticketCount(0)
128129
.build();
@@ -167,7 +168,7 @@ public void increaseRecommendPoint() {
167168
}
168169

169170
public void plusPoint(Integer point) {
170-
if (this.getSubscribe() == Subscribe.NORMAL) {
171+
if (this.getSubscribe()==Subscribe.NORMAL) {
171172
this.point += point;
172173
return;
173174
}
@@ -194,7 +195,7 @@ public void setSubscribe(Subscribe subscribe) {
194195
this.subscribe = subscribe;
195196
}
196197

197-
public void addTicketCount(int ticketCount) {
198+
public void setTicketCount(int ticketCount) {
198199
this.ticketCount += ticketCount;
199200
}
200201

src/main/java/com/yello/server/domain/vote/dto/response/VoteAvailableResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
public record VoteAvailableResponse(
1414
Boolean isPossible,
1515
Integer point,
16-
String createdAt
16+
String createdAt,
17+
Integer friendStatus
1718
) {
1819

19-
public static VoteAvailableResponse of(User user, Cooldown cooldown) {
20+
public static VoteAvailableResponse of(User user, Cooldown cooldown, Integer friendStatus) {
2021
return VoteAvailableResponse.builder()
2122
.isPossible(cooldown.isPossible())
2223
.point(user.getPoint())
2324
.createdAt(toDateFormattedString(cooldown.getCreatedAt()))
25+
.friendStatus(friendStatus)
2426
.build();
2527
}
2628

src/main/java/com/yello/server/domain/vote/service/VoteManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.yello.server.domain.vote.service;
22

3+
import com.yello.server.domain.friend.dto.response.FriendShuffleResponse;
34
import com.yello.server.domain.keyword.dto.response.KeywordCheckResponse;
45
import com.yello.server.domain.question.dto.response.QuestionForVoteResponse;
56
import com.yello.server.domain.question.entity.Question;
@@ -19,4 +20,7 @@ public interface VoteManager {
1920
KeywordCheckResponse useKeywordHint(User user, Vote vote);
2021

2122
void makeGreetingVote(User user);
23+
24+
List<FriendShuffleResponse> getShuffledFriends(User user);
25+
2226
}

src/main/java/com/yello/server/domain/vote/service/VoteManagerImpl.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import static com.yello.server.global.common.ErrorCode.DUPLICATE_VOTE_EXCEPTION;
44
import static com.yello.server.global.common.ErrorCode.INVALID_VOTE_EXCEPTION;
55
import static com.yello.server.global.common.ErrorCode.LACK_POINT_EXCEPTION;
6+
import static com.yello.server.global.common.ErrorCode.LACK_USER_EXCEPTION;
67
import static com.yello.server.global.common.factory.WeightedRandomFactory.randomPoint;
78
import static com.yello.server.global.common.util.ConstantUtil.KEYWORD_HINT_POINT;
89
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_DEFAULT;
910
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_POINT;
11+
import static com.yello.server.global.common.util.ConstantUtil.NO_FRIEND_COUNT;
1012
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
1113
import static com.yello.server.global.common.util.ConstantUtil.VOTE_COUNT;
1214
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
1315
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;
1416

1517
import com.yello.server.domain.friend.dto.response.FriendShuffleResponse;
1618
import com.yello.server.domain.friend.entity.Friend;
19+
import com.yello.server.domain.friend.exception.FriendException;
1720
import com.yello.server.domain.friend.repository.FriendRepository;
1821
import com.yello.server.domain.keyword.dto.response.KeywordCheckResponse;
1922
import com.yello.server.domain.keyword.entity.Keyword;
@@ -107,19 +110,19 @@ public List<QuestionForVoteResponse> generateVoteQuestion(User user, List<Questi
107110

108111
@Override
109112
public int useNameHint(User sender, Vote vote) {
110-
if (sender.getPoint() < NAME_HINT_POINT && sender.getSubscribe() == Subscribe.NORMAL) {
113+
if (sender.getPoint() < NAME_HINT_POINT && sender.getSubscribe()==Subscribe.NORMAL) {
111114
throw new VoteForbiddenException(LACK_POINT_EXCEPTION);
112115
}
113116

114-
if (vote.getNameHint() != NAME_HINT_DEFAULT) {
117+
if (vote.getNameHint()!=NAME_HINT_DEFAULT) {
115118
throw new VoteNotFoundException(INVALID_VOTE_EXCEPTION);
116119
}
117120

118121
final ThreadLocalRandom random = ThreadLocalRandom.current();
119122
int randomIndex = random.nextInt(2);
120123
vote.checkNameIndexOf(randomIndex);
121124

122-
if (sender.getSubscribe() == Subscribe.NORMAL) {
125+
if (sender.getSubscribe()==Subscribe.NORMAL) {
123126
sender.minusPoint(NAME_HINT_POINT);
124127
return randomIndex;
125128
}
@@ -160,24 +163,35 @@ public void makeGreetingVote(User user) {
160163
voteRepository.save(createFirstVote(sender, user, greetingQuestion));
161164
}
162165

163-
private boolean isDuplicatedVote(int index, List<VoteAnswer> voteAnswers) {
164-
return index > 0 && voteAnswers.get(index - 1).questionId()
165-
.equals(voteAnswers.get(index).questionId());
166-
}
167-
168-
private List<FriendShuffleResponse> getShuffledFriends(User user) {
166+
@Override
167+
public List<FriendShuffleResponse> getShuffledFriends(User user) {
169168
List<String> uuidList = Arrays.asList(YELLO_FEMALE, YELLO_MALE);
170169
final List<Friend> friends = friendRepository.findAllByUserIdNotIn(user.getId(), uuidList);
171170

172171
List<Friend> friendList = new ArrayList<>(friends);
173172
Collections.shuffle(friendList);
174173

174+
if (friends.size()==NO_FRIEND_COUNT) {
175+
throw new FriendException(LACK_USER_EXCEPTION);
176+
}
177+
178+
if (friends.size() > NO_FRIEND_COUNT && friends.size() < RANDOM_COUNT) {
179+
return friendList.stream()
180+
.map(FriendShuffleResponse::of)
181+
.toList();
182+
}
183+
175184
return friendList.stream()
176185
.map(FriendShuffleResponse::of)
177186
.limit(RANDOM_COUNT)
178187
.toList();
179188
}
180189

190+
private boolean isDuplicatedVote(int index, List<VoteAnswer> voteAnswers) {
191+
return index > 0 && voteAnswers.get(index - 1).questionId()
192+
.equals(voteAnswers.get(index).questionId());
193+
}
194+
181195
private List<String> getShuffledKeywords(Question question) {
182196
final List<Keyword> keywords = question.getKeywordList();
183197
List<Keyword> keywordList = new ArrayList<>(keywords);

src/main/java/com/yello/server/domain/vote/service/VoteService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static com.yello.server.global.common.util.ConstantUtil.CHECK_FULL_NAME;
1010
import static com.yello.server.global.common.util.ConstantUtil.COOL_DOWN_TIME;
1111
import static com.yello.server.global.common.util.ConstantUtil.MINUS_TICKET_COUNT;
12+
import static com.yello.server.global.common.util.ConstantUtil.NO_FRIEND_COUNT;
1213
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
1314

1415
import com.yello.server.domain.cooldown.entity.Cooldown;
@@ -122,7 +123,7 @@ public List<QuestionForVoteResponse> findVoteQuestionList(Long userId) {
122123
final User user = userRepository.getById(userId);
123124

124125
final List<Friend> friends = friendRepository.findAllByUserId(user.getId());
125-
if (friends.size() < RANDOM_COUNT) {
126+
if (friends.size()==NO_FRIEND_COUNT) {
126127
throw new FriendException(LACK_USER_EXCEPTION);
127128
}
128129

@@ -144,14 +145,18 @@ public VoteAvailableResponse checkVoteAvailable(Long userId) {
144145
final String messageId = UUID.randomUUID().toString();
145146
final List<Friend> friends = friendRepository.findAllByUserId(user.getId());
146147

147-
if (friends.size() < RANDOM_COUNT) {
148+
if (friends.size()==NO_FRIEND_COUNT) {
148149
throw new FriendException(LACK_USER_EXCEPTION);
149150
}
150151

151152
final Cooldown cooldown = cooldownRepository.findByUserId(user.getId())
152153
.orElse(Cooldown.of(user, messageId, minusTime(LocalDateTime.now(), COOL_DOWN_TIME)));
153154

154-
return VoteAvailableResponse.of(user, cooldown);
155+
if (friends.size() > NO_FRIEND_COUNT && friends.size() < RANDOM_COUNT) {
156+
return VoteAvailableResponse.of(user, cooldown, 0);
157+
}
158+
159+
return VoteAvailableResponse.of(user, cooldown, 1);
155160
}
156161

157162
@Transactional
@@ -195,7 +200,7 @@ public RevealFullNameResponse revealFullName(Long userId, Long voteId) {
195200
}
196201

197202
vote.checkNameIndexOf(CHECK_FULL_NAME);
198-
sender.addTicketCount(MINUS_TICKET_COUNT);
203+
sender.setTicketCount(MINUS_TICKET_COUNT);
199204

200205
return RevealFullNameResponse.of(vote.getSender());
201206
}

src/main/java/com/yello/server/global/common/ErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public enum ErrorCode {
2222
REQUEST_VALIDATION_EXCEPTION(BAD_REQUEST, "잘못된 요청입니다."),
2323
YELLOID_REQUIRED_EXCEPTION(BAD_REQUEST, "쿼리 스트링에 yelloId를 포함해야 합니다."),
2424
OAUTH_ACCESS_TOKEN_REQUIRED_EXCEPTION(BAD_REQUEST, "소셜 액세스 토큰이 없습니다."),
25-
LACK_USER_EXCEPTION(BAD_REQUEST, "친구가 4명 이하입니다."),
25+
LACK_USER_EXCEPTION(BAD_REQUEST, "친구가 부족합니다."),
2626
SIGNIN_FIELD_REQUIRED_EXCEPTION(BAD_REQUEST, "회원가입에 필요한 값이 없습니다."),
2727
FIELD_REQUIRED_EXCEPTION(BAD_REQUEST, "필요한 값이 없습니다."),
2828
INVALID_VOTE_EXCEPTION(BAD_REQUEST, "이미 공개한 투표입니다"),

0 commit comments

Comments
 (0)