Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YEL-162 [deploy] v1.196 #337

Merged
merged 10 commits into from
Sep 15, 2023
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.yello.server.domain.friend.service;

import static com.yello.server.global.common.ErrorCode.EXIST_FRIEND_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_USER_EXCEPTION;
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;

Expand All @@ -20,6 +18,7 @@
import com.yello.server.domain.user.entity.User;
import com.yello.server.domain.user.repository.UserRepository;
import com.yello.server.domain.vote.repository.VoteRepository;
import com.yello.server.domain.vote.service.VoteManager;
import com.yello.server.global.common.factory.PaginationFactory;
import java.lang.Character.UnicodeBlock;
import java.util.ArrayList;
Expand All @@ -43,6 +42,7 @@ public class FriendService {
private final FriendRepository friendRepository;
private final UserRepository userRepository;
private final VoteRepository voteRepository;
private final VoteManager voteManager;

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

final List<Friend> friends =
new ArrayList<>(friendRepository.findAllByUserId(user.getId()));
List<Friend> friendList = new ArrayList<>(friends);

if (friendList.size() < RANDOM_COUNT) {
throw new FriendException(LACK_USER_EXCEPTION);
}

Collections.shuffle(friendList);

return friendList.stream()
.map(FriendShuffleResponse::of)
.limit(RANDOM_COUNT)
.toList();
return voteManager.getShuffledFriends(user);
}

public RecommendFriendResponse findAllRecommendSchoolFriends(Pageable pageable, Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ void handleAppleTransactionError(ResponseEntity<TransactionInfoResponse> respons

void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO);

void refundAppleInApp(AppleNotificationPayloadVO payloadVO);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static com.yello.server.global.common.ErrorCode.APPLE_TOKEN_SERVER_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.GOOGLE_SUBSCRIPTIONS_SUBSCRIPTION_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.NOT_FOUND_TRANSACTION_EXCEPTION;
import static com.yello.server.global.common.util.ConstantUtil.REFUND_FIVE_TICKET;
import static com.yello.server.global.common.util.ConstantUtil.REFUND_ONE_TICKET;
import static com.yello.server.global.common.util.ConstantUtil.REFUND_TWO_TICKET;

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

@Override
public AppleNotificationPayloadVO decodeApplePayload(String signedPayload) {

Map<String, Object> jsonPayload = DecodeTokenFactory.decodeToken(signedPayload);
ObjectMapper objectMapper = new ObjectMapper();

Expand Down Expand Up @@ -117,5 +120,35 @@ public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) {
}
}

@Override
public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) {
String transactionId =
decodeAppleNotificationData(payloadVO.data().signedTransactionInfo());
Purchase purchase = purchaseRepository.findByTransactionId(transactionId)
.orElseThrow(() -> new PurchaseNotFoundException(NOT_FOUND_TRANSACTION_EXCEPTION));
User user = purchase.getUser();

switch (purchase.getProductType()) {
case YELLO_PLUS -> {
user.setSubscribe(Subscribe.NORMAL);
}
case ONE_TICKET -> {
validateTicketCount(REFUND_ONE_TICKET, user);
}
case TWO_TICKET -> {
validateTicketCount(REFUND_TWO_TICKET, user);
}
case FIVE_TICKET -> {
validateTicketCount(REFUND_FIVE_TICKET, user);
}
}
}

public void validateTicketCount(int ticketCount, User user) {
if (user.getTicketCount() >= ticketCount) {
user.setTicketCount(-Math.abs(ticketCount));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void verifyAppleSubscriptionTransaction(Long userId,
}

purchaseManager.createSubscribe(user, Gateway.APPLE, request.transactionId());
user.addTicketCount(3);
user.setTicketCount(3);
}

@Transactional
Expand All @@ -126,17 +126,17 @@ public void verifyAppleTicketTransaction(Long userId, AppleTransaction request)
case ONE_TICKET_ID:
purchaseManager.createTicket(user, ProductType.ONE_TICKET, Gateway.APPLE,
request.transactionId());
user.addTicketCount(1);
user.setTicketCount(1);
break;
case TWO_TICKET_ID:
purchaseManager.createTicket(user, ProductType.TWO_TICKET, Gateway.APPLE,
request.transactionId());
user.addTicketCount(2);
user.setTicketCount(2);
break;
case FIVE_TICKET_ID:
purchaseManager.createTicket(user, ProductType.FIVE_TICKET, Gateway.APPLE,
request.transactionId());
user.addTicketCount(5);
user.setTicketCount(5);
break;
default:
throw new PurchaseException(NOT_FOUND_TRANSACTION_EXCEPTION);
Expand Down Expand Up @@ -206,7 +206,7 @@ public GoogleSubscriptionGetResponse verifyGoogleSubscriptionTransaction(Long us
case ConstantUtil.GOOGLE_PURCHASE_SUBSCRIPTION_ACTIVE -> {
final Purchase subscribe =
purchaseManager.createSubscribe(user, Gateway.GOOGLE, request.orderId());
user.addTicketCount(3);
user.setTicketCount(3);
subscribe.setTransactionId(request.orderId());
}
}
Expand Down Expand Up @@ -259,7 +259,7 @@ public GoogleTicketGetResponse verifyGoogleTicketTransaction(Long userId,
Purchase ticket =
purchaseManager.createTicket(user, getProductType(request.productId()),
Gateway.GOOGLE, request.orderId());
user.addTicketCount(getTicketAmount(request.productId()) * request.quantity());
user.setTicketCount(getTicketAmount(request.productId()) * request.quantity());
ticket.setTransactionId(inAppResponse.getBody().orderId());
} else {
throw new GoogleBadRequestException(GOOGLE_INAPP_BAD_REQUEST_EXCEPTION);
Expand Down Expand Up @@ -304,7 +304,7 @@ public void appleNotification(AppleNotificationRequest request) {
purchaseManager.changeSubscriptionStatus(payloadVO);
break;
case APPLE_NOTIFICATION_REFUND:
System.out.println("dd");
purchaseManager.refundAppleInApp(payloadVO);
break;
case APPLE_NOTIFICATION_TEST:
return;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/yello/server/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public static User of(SignUpRequest signUpRequest, School group) {
.group(group)
.groupAdmissionYear(signUpRequest.groupAdmissionYear())
.email(signUpRequest.email())
.deviceToken(Objects.equals(signUpRequest.deviceToken(), "") ? null : signUpRequest.deviceToken())
.deviceToken(Objects.equals(signUpRequest.deviceToken(), "") ? null
: signUpRequest.deviceToken())
.subscribe(Subscribe.NORMAL)
.ticketCount(0)
.build();
Expand Down Expand Up @@ -167,7 +168,7 @@ public void increaseRecommendPoint() {
}

public void plusPoint(Integer point) {
if (this.getSubscribe() == Subscribe.NORMAL) {
if (this.getSubscribe()==Subscribe.NORMAL) {
this.point += point;
return;
}
Expand All @@ -194,7 +195,7 @@ public void setSubscribe(Subscribe subscribe) {
this.subscribe = subscribe;
}

public void addTicketCount(int ticketCount) {
public void setTicketCount(int ticketCount) {
this.ticketCount += ticketCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
public record VoteAvailableResponse(
Boolean isPossible,
Integer point,
String createdAt
String createdAt,
Integer friendStatus
) {

public static VoteAvailableResponse of(User user, Cooldown cooldown) {
public static VoteAvailableResponse of(User user, Cooldown cooldown, Integer friendStatus) {
return VoteAvailableResponse.builder()
.isPossible(cooldown.isPossible())
.point(user.getPoint())
.createdAt(toDateFormattedString(cooldown.getCreatedAt()))
.friendStatus(friendStatus)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yello.server.domain.vote.service;

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

void makeGreetingVote(User user);

List<FriendShuffleResponse> getShuffledFriends(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import static com.yello.server.global.common.ErrorCode.DUPLICATE_VOTE_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.INVALID_VOTE_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_POINT_EXCEPTION;
import static com.yello.server.global.common.ErrorCode.LACK_USER_EXCEPTION;
import static com.yello.server.global.common.factory.WeightedRandomFactory.randomPoint;
import static com.yello.server.global.common.util.ConstantUtil.KEYWORD_HINT_POINT;
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_DEFAULT;
import static com.yello.server.global.common.util.ConstantUtil.NAME_HINT_POINT;
import static com.yello.server.global.common.util.ConstantUtil.NO_FRIEND_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.VOTE_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_FEMALE;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_MALE;

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

@Override
public int useNameHint(User sender, Vote vote) {
if (sender.getPoint() < NAME_HINT_POINT && sender.getSubscribe() == Subscribe.NORMAL) {
if (sender.getPoint() < NAME_HINT_POINT && sender.getSubscribe()==Subscribe.NORMAL) {
throw new VoteForbiddenException(LACK_POINT_EXCEPTION);
}

if (vote.getNameHint() != NAME_HINT_DEFAULT) {
if (vote.getNameHint()!=NAME_HINT_DEFAULT) {
throw new VoteNotFoundException(INVALID_VOTE_EXCEPTION);
}

final ThreadLocalRandom random = ThreadLocalRandom.current();
int randomIndex = random.nextInt(2);
vote.checkNameIndexOf(randomIndex);

if (sender.getSubscribe() == Subscribe.NORMAL) {
if (sender.getSubscribe()==Subscribe.NORMAL) {
sender.minusPoint(NAME_HINT_POINT);
return randomIndex;
}
Expand Down Expand Up @@ -160,24 +163,35 @@ public void makeGreetingVote(User user) {
voteRepository.save(createFirstVote(sender, user, greetingQuestion));
}

private boolean isDuplicatedVote(int index, List<VoteAnswer> voteAnswers) {
return index > 0 && voteAnswers.get(index - 1).questionId()
.equals(voteAnswers.get(index).questionId());
}

private List<FriendShuffleResponse> getShuffledFriends(User user) {
@Override
public List<FriendShuffleResponse> getShuffledFriends(User user) {
List<String> uuidList = Arrays.asList(YELLO_FEMALE, YELLO_MALE);
final List<Friend> friends = friendRepository.findAllByUserIdNotIn(user.getId(), uuidList);

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

if (friends.size()==NO_FRIEND_COUNT) {
throw new FriendException(LACK_USER_EXCEPTION);
}

if (friends.size() > NO_FRIEND_COUNT && friends.size() < RANDOM_COUNT) {
return friendList.stream()
.map(FriendShuffleResponse::of)
.toList();
}

return friendList.stream()
.map(FriendShuffleResponse::of)
.limit(RANDOM_COUNT)
.toList();
}

private boolean isDuplicatedVote(int index, List<VoteAnswer> voteAnswers) {
return index > 0 && voteAnswers.get(index - 1).questionId()
.equals(voteAnswers.get(index).questionId());
}

private List<String> getShuffledKeywords(Question question) {
final List<Keyword> keywords = question.getKeywordList();
List<Keyword> keywordList = new ArrayList<>(keywords);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.yello.server.global.common.util.ConstantUtil.CHECK_FULL_NAME;
import static com.yello.server.global.common.util.ConstantUtil.COOL_DOWN_TIME;
import static com.yello.server.global.common.util.ConstantUtil.MINUS_TICKET_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.NO_FRIEND_COUNT;
import static com.yello.server.global.common.util.ConstantUtil.RANDOM_COUNT;

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

final List<Friend> friends = friendRepository.findAllByUserId(user.getId());
if (friends.size() < RANDOM_COUNT) {
if (friends.size()==NO_FRIEND_COUNT) {
throw new FriendException(LACK_USER_EXCEPTION);
}

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

if (friends.size() < RANDOM_COUNT) {
if (friends.size()==NO_FRIEND_COUNT) {
throw new FriendException(LACK_USER_EXCEPTION);
}

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

return VoteAvailableResponse.of(user, cooldown);
if (friends.size() > NO_FRIEND_COUNT && friends.size() < RANDOM_COUNT) {
return VoteAvailableResponse.of(user, cooldown, 0);
}

return VoteAvailableResponse.of(user, cooldown, 1);
}

@Transactional
Expand Down Expand Up @@ -195,7 +200,7 @@ public RevealFullNameResponse revealFullName(Long userId, Long voteId) {
}

vote.checkNameIndexOf(CHECK_FULL_NAME);
sender.addTicketCount(MINUS_TICKET_COUNT);
sender.setTicketCount(MINUS_TICKET_COUNT);

return RevealFullNameResponse.of(vote.getSender());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum ErrorCode {
REQUEST_VALIDATION_EXCEPTION(BAD_REQUEST, "잘못된 요청입니다."),
YELLOID_REQUIRED_EXCEPTION(BAD_REQUEST, "쿼리 스트링에 yelloId를 포함해야 합니다."),
OAUTH_ACCESS_TOKEN_REQUIRED_EXCEPTION(BAD_REQUEST, "소셜 액세스 토큰이 없습니다."),
LACK_USER_EXCEPTION(BAD_REQUEST, "친구가 4명 이하입니다."),
LACK_USER_EXCEPTION(BAD_REQUEST, "친구가 부족합니다."),
SIGNIN_FIELD_REQUIRED_EXCEPTION(BAD_REQUEST, "회원가입에 필요한 값이 없습니다."),
FIELD_REQUIRED_EXCEPTION(BAD_REQUEST, "필요한 값이 없습니다."),
INVALID_VOTE_EXCEPTION(BAD_REQUEST, "이미 공개한 투표입니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class ConstantUtil {
public static final String APPLE_NOTIFICATION_TEST = "TEST";
public static final String APPLE_SUBTYPE_AUTO_RENEW_DISABLED = "AUTO_RENEW_DISABLED";
public static final String APPLE_SUBTYPE_VOLUNTARY = "VOLUNTARY";
public static final int REFUND_ONE_TICKET = 1;
public static final int REFUND_TWO_TICKET = 2;
public static final int REFUND_FIVE_TICKET = 5;
public static final int NO_FRIEND_COUNT = 0;


private ConstantUtil() {
Expand Down
Loading
Loading