Skip to content

Commit

Permalink
Merge pull request #87 from Next-Room/feature/free-status
Browse files Browse the repository at this point in the history
[FEAT] 30일 무료체험 구현
  • Loading branch information
eunsol-an authored Nov 15, 2023
2 parents 74ea9ba + 5129dde commit f806a02
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ public class Subscription extends Timestamped {
@Enumerated(EnumType.STRING)
private SubscriptionPlan plan;
private LocalDate expiryDate;

public void updateStatus(UserStatus userStatus, LocalDate expiryDate, SubscriptionPlan plan) {
this.status = userStatus;
this.expiryDate = expiryDate;
this.plan = plan;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nextroom.nextRoomServer.service;

import static com.nextroom.nextRoomServer.enums.SubscriptionPlan.*;
import static com.nextroom.nextRoomServer.enums.UserStatus.*;
import static com.nextroom.nextRoomServer.exceptions.StatusCode.*;
import static com.nextroom.nextRoomServer.util.Timestamped.*;

Expand All @@ -12,12 +14,15 @@

import com.nextroom.nextRoomServer.domain.RefreshToken;
import com.nextroom.nextRoomServer.domain.Shop;
import com.nextroom.nextRoomServer.domain.Subscription;
import com.nextroom.nextRoomServer.dto.AuthDto;
import com.nextroom.nextRoomServer.dto.TokenDto;
import com.nextroom.nextRoomServer.exceptions.CustomException;
import com.nextroom.nextRoomServer.repository.RefreshTokenRepository;
import com.nextroom.nextRoomServer.repository.ShopRepository;
import com.nextroom.nextRoomServer.repository.SubscriptionRepository;
import com.nextroom.nextRoomServer.security.TokenProvider;
import com.nextroom.nextRoomServer.util.Timestamped;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand All @@ -30,6 +35,7 @@ public class AuthService {
private final PasswordEncoder passwordEncoder;
private final TokenProvider tokenProvider;
private final RefreshTokenRepository refreshTokenRepository;
private final SubscriptionRepository subscriptionRepository;

@Transactional
public AuthDto.SignUpResponseDto signUp(AuthDto.SignUpRequestDto request) {
Expand All @@ -38,6 +44,7 @@ public AuthDto.SignUpResponseDto signUp(AuthDto.SignUpRequestDto request) {
}

Shop shop = shopRepository.save(request.toShop(passwordEncoder));
createSubscription(shop);

return AuthDto.SignUpResponseDto.builder()
.adminCode(shop.getAdminCode())
Expand All @@ -46,6 +53,16 @@ public AuthDto.SignUpResponseDto signUp(AuthDto.SignUpRequestDto request) {
.modifiedAt(dateTimeFormatter(shop.getModifiedAt())).build();
}

private void createSubscription(Shop shop) {
Subscription subscription = Subscription.builder()
.shop(shop)
.status(FREE)
.plan(MINI)
.expiryDate(Timestamped.getToday().plusDays(30))
.build();
subscriptionRepository.save(subscription);
}

@Transactional
public AuthDto.LogInResponseDto login(@RequestBody AuthDto.LogInRequestDto request) {

Expand All @@ -56,7 +73,7 @@ public AuthDto.LogInResponseDto login(@RequestBody AuthDto.LogInRequestDto reque
String shopName = shopRepository.findByAdminCode(request.getAdminCode())
.orElseThrow(() -> new CustomException(TARGET_SHOP_NOT_FOUND)).getName();
AuthDto.LogInResponseDto response = AuthDto.LogInResponseDto.toLogInResponseDto(shopName, token);

RefreshToken refreshToken = RefreshToken.builder()
.key(authentication.getName())
.value(response.getRefreshToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nextroom.nextRoomServer.domain.Shop;
import com.nextroom.nextRoomServer.domain.Subscription;
import com.nextroom.nextRoomServer.dto.SubscriptionDto;
import com.nextroom.nextRoomServer.enums.EnumModel;
import com.nextroom.nextRoomServer.enums.SubscriptionPlan;
import com.nextroom.nextRoomServer.enums.UserStatus;
import com.nextroom.nextRoomServer.exceptions.CustomException;
import com.nextroom.nextRoomServer.repository.ShopRepository;
import com.nextroom.nextRoomServer.repository.SubscriptionRepository;
import com.nextroom.nextRoomServer.security.SecurityUtil;
import com.nextroom.nextRoomServer.util.Timestamped;

import lombok.RequiredArgsConstructor;

Expand All @@ -43,6 +46,7 @@ public void test() {
subscriptionRepository.save(entity);
}

@Transactional(readOnly = true)
public SubscriptionDto.SubscriptionInfoResponse getSubscriptionInfo() {
Long shopId = SecurityUtil.getRequestedShopId();
Subscription subscription = subscriptionRepository.findByShopId(shopId).orElseThrow(
Expand All @@ -51,14 +55,32 @@ public SubscriptionDto.SubscriptionInfoResponse getSubscriptionInfo() {
return new SubscriptionDto.SubscriptionInfoResponse(subscription);
}

@Transactional
public SubscriptionDto.UserStatusResponse getUserStatus() {
Long shopId = SecurityUtil.getRequestedShopId();
Subscription subscription = subscriptionRepository.findByShopId(shopId).orElseThrow(
() -> new CustomException(TARGET_SHOP_NOT_FOUND));

checkUserStatus(subscription);

return new SubscriptionDto.UserStatusResponse(subscription);
}

private void checkUserStatus(Subscription subscription) {
UserStatus status = subscription.getStatus();
LocalDate expiryDate = subscription.getExpiryDate();

if (status == FREE && expiryDate.isBefore(Timestamped.getToday())) {
subscription.updateStatus(HOLD, expiryDate.plusYears(1), null);
return;
}

if (status == HOLD && expiryDate.isBefore(Timestamped.getToday())) {
// subscriptionRepository.delete(subscription);
}
}

@Transactional(readOnly = true)
public List<SubscriptionDto.SubscriptionPlanResponse> getSubscriptionPlan() {
return toEnumValues(SubscriptionPlan.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nextroom.nextRoomServer.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Expand Down Expand Up @@ -28,4 +29,8 @@ public static String dateTimeFormatter(LocalDateTime localDateTime) {
return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}

public static LocalDate getToday() {
return LocalDate.now();
}

}

0 comments on commit f806a02

Please sign in to comment.