Skip to content

Commit

Permalink
Merge pull request #214 from softeerbootcamp4th/fix/#211-fix-referral
Browse files Browse the repository at this point in the history
Fix/#211 fix referral
  • Loading branch information
wjddn2165 authored Aug 22, 2024
2 parents d82b4be + e96a8f1 commit 9ae4172
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import JGS.CasperEvent.global.jwt.repository.UserRepository;
import JGS.CasperEvent.global.util.AESUtils;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -35,6 +37,7 @@
@RequiredArgsConstructor
public class LotteryEventService {

private static final Logger log = LoggerFactory.getLogger(LotteryEventService.class);
private final UserRepository userRepository;
private final LotteryEventRepository lotteryEventRepository;
private final LotteryParticipantsRepository lotteryParticipantsRepository;
Expand Down Expand Up @@ -83,14 +86,7 @@ public LotteryParticipants registerUserIfNeed(BaseUser user, CasperBotRequestDto
participant = new LotteryParticipants(user);
lotteryParticipantsRepository.save(participant);

if (casperBotRequestDto.getReferralId() != null) {
String referralId = AESUtils.decrypt(casperBotRequestDto.getReferralId(), secretKey);
Optional<LotteryParticipants> referralParticipant =
lotteryParticipantsRepository.findByBaseUser(
userRepository.findById(referralId).orElse(null)
);
referralParticipant.ifPresent(LotteryParticipants::linkClickedCountAdded);
}
addReferralAppliedCount(casperBotRequestDto);

user.updateLotteryParticipants(participant);
userRepository.save(user);
Expand All @@ -99,6 +95,22 @@ public LotteryParticipants registerUserIfNeed(BaseUser user, CasperBotRequestDto
return participant;
}

private void addReferralAppliedCount(CasperBotRequestDto casperBotRequestDto) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
String encryptedReferralId = casperBotRequestDto.getReferralId();
if (encryptedReferralId == null) return;
try {
String referralId = AESUtils.decrypt(casperBotRequestDto.getReferralId(), secretKey);
Optional<LotteryParticipants> referralParticipant =
lotteryParticipantsRepository.findByBaseUser(
userRepository.findById(referralId).orElse(null)
);
referralParticipant.ifPresent(LotteryParticipants::linkClickedCountAdded);
} catch (Exception e) {
log.debug(e.getLocalizedMessage());
}

}

public LotteryEventResponseDto getLotteryEvent() {
LotteryEvent lotteryEvent = getEvent();
return LotteryEventResponseDto.of(lotteryEvent, LocalDateTime.now());
Expand Down

0 comments on commit 9ae4172

Please sign in to comment.