Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -107,6 +107,10 @@ public int[] getMonthlyActiveUserWitClub(String clubId) {
}

public List<String> getDailyRanking(int n) {
if (n <= 0) {
return Collections.emptyList();
}

List<ClubMetric> todayMetrics = clubMetricRepository.findAllByDate(LocalDate.now());
Map<String, Long> clubViewCount = todayMetrics.stream()
.collect(Collectors.groupingBy(ClubMetric::getClubId, Collectors.counting()));
Expand All @@ -130,6 +134,10 @@ public List<String> getDailyRanking(int n) {
}

public int[] getDailyActiveUser(int n) {
if (n <= 0) {
return new int[0];
}

LocalDate today = LocalDate.now();
LocalDate fromDate = today.minusDays(n);
List<ClubMetric> metrics = clubMetricRepository.findAllByDateAfter(fromDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class RecruitmentScheduler {

private final TaskScheduler taskScheduler;

private final Map<String, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();

private final ClubRepository clubRepository;
Expand All @@ -34,12 +35,12 @@ public void scheduleRecruitment(String clubId, LocalDateTime startDate,
// 모집 시작 스케줄링
ScheduledFuture<?> startFuture = taskScheduler.schedule(
() -> updateRecruitmentStatus(clubId, ClubRecruitmentStatus.OPEN),
Date.from(startDate.atZone(ZoneId.systemDefault()).toInstant()));
Date.from(startDate.atZone(ZoneId.of("Asia/Seoul")).toInstant()));

// 모집 종료 스케줄링
ScheduledFuture<?> endFuture = taskScheduler.schedule(
() -> updateRecruitmentStatus(clubId, ClubRecruitmentStatus.CLOSED),
Date.from(endDate.atZone(ZoneId.systemDefault()).toInstant()));
Date.from(endDate.atZone(ZoneId.of("Asia/Seoul")).toInstant()));

scheduledTasks.put(clubId, startFuture);
scheduledTasks.put(clubId, endFuture);
Expand All @@ -56,10 +57,13 @@ public void cancelScheduledTask(String clubId) {
public void updateRecruitmentStatus(String clubId, ClubRecruitmentStatus status) {
ObjectId objectId = ObjectIdConverter.convertString(clubId);
Club club = clubRepository.findClubById(objectId)
.orElseThrow(() -> new RestApiException(ErrorCode.CLUB_NOT_FOUND));
.orElseThrow(() -> new RestApiException(ErrorCode.CLUB_NOT_FOUND));

club.getClubRecruitmentInformation().updateRecruitmentStatus(status);
clubRepository.save(club);
}

public Map<String, ScheduledFuture<?>> getScheduledTasks() {
return scheduledTasks;
}
}
33 changes: 16 additions & 17 deletions backend/src/main/java/moadong/user/service/UserCommandService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mongodb.MongoWriteException;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Date;
import lombok.AllArgsConstructor;
import moadong.club.entity.Club;
import moadong.club.repository.ClubRepository;
Expand All @@ -26,8 +27,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@AllArgsConstructor
public class UserCommandService {
Expand All @@ -45,24 +44,24 @@ public void registerUser(UserRegisterRequest userRegisterRequest) {
String encodedPw = passwordEncoder.encode(userRegisterRequest.password());
User user = userRepository.save(userRegisterRequest.toUserEntity(encodedPw));
userInformationRepository.save(
userRegisterRequest.toUserInformationEntity(user.getId()));
userRegisterRequest.toUserInformationEntity(user.getId()));
createClub(user.getId());
} catch (MongoWriteException e) {
throw new RestApiException(ErrorCode.USER_ALREADY_EXIST);
}
}

public LoginResponse loginUser(UserLoginRequest userLoginRequest,
HttpServletResponse response) {
HttpServletResponse response) {
try {
Authentication authenticate = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(userLoginRequest.userId(),
userLoginRequest.password()));
new UsernamePasswordAuthenticationToken(userLoginRequest.userId(),
userLoginRequest.password()));
CustomUserDetails userDetails = (CustomUserDetails) authenticate.getPrincipal();
Club club = clubRepository.findClubByUserId(userDetails.getId())
.orElseThrow(() -> new RestApiException(ErrorCode.CLUB_NOT_FOUND));
.orElseThrow(() -> new RestApiException(ErrorCode.CLUB_NOT_FOUND));
User user = userRepository.findUserByUserId(userDetails.getUserId())
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));

String accessToken = jwtProvider.generateAccessToken(userDetails.getUsername());
RefreshToken refreshToken = jwtProvider.generateRefreshToken(userDetails.getUsername());
Expand All @@ -80,24 +79,24 @@ public LoginResponse loginUser(UserLoginRequest userLoginRequest,

public void logoutUser(String refreshToken) {
User user = userRepository.findUserByRefreshToken_Token(refreshToken)
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));

user.updateRefreshToken(null);
userRepository.save(user);
}

public RefreshResponse refreshAccessToken(String refreshToken,
HttpServletResponse response) {
HttpServletResponse response) {
if (refreshToken.isBlank() ||
!jwtProvider.validateToken(refreshToken, jwtProvider.extractUsername(refreshToken))) {
!jwtProvider.validateToken(refreshToken, jwtProvider.extractUsername(refreshToken))) {
throw new RestApiException(ErrorCode.TOKEN_INVALID);
}
String userId = jwtProvider.extractUsername(refreshToken);
User user = userRepository.findUserByUserId(userId)
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));

if (!user.getRefreshToken().getToken().equals(refreshToken)
|| jwtProvider.isTokenExpired(refreshToken)) {
|| jwtProvider.isTokenExpired(refreshToken)) {
throw new RestApiException(ErrorCode.TOKEN_INVALID);
}
String accessToken = jwtProvider.generateAccessToken(userId);
Expand All @@ -112,10 +111,10 @@ public RefreshResponse refreshAccessToken(String refreshToken,
}

public void update(String userId,
UserUpdateRequest userUpdateRequest,
HttpServletResponse response) {
UserUpdateRequest userUpdateRequest,
HttpServletResponse response) {
User user = userRepository.findUserByUserId(userId)
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));
.orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_EXIST));
user.updateUserProfile(userUpdateRequest.encryptPassword(passwordEncoder));

userRepository.save(user);
Expand All @@ -127,7 +126,7 @@ public void update(String userId,

public String findClubIdByUserId(String userID) {
Club club = clubRepository.findClubByUserId(userID)
.orElseThrow(() -> new RestApiException(ErrorCode.CLUB_NOT_FOUND));
.orElseThrow(() -> new RestApiException(ErrorCode.CLUB_NOT_FOUND));
return club.getId();
}

Expand Down
17 changes: 17 additions & 0 deletions backend/src/test/java/moadong/club/fixture/ClubFixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package moadong.club.fixture;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import moadong.club.entity.Club;

public class ClubFixture {

public static Club createClub(String clubId, String name) {
Club club = mock(Club.class);
when(club.getId()).thenReturn(clubId);
when(club.getName()).thenReturn(name);
return club;
}

}
31 changes: 31 additions & 0 deletions backend/src/test/java/moadong/club/fixture/MetricFixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package moadong.club.fixture;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.time.LocalDate;
import moadong.club.entity.ClubMetric;

public class MetricFixture {

public static ClubMetric createClubMetric(LocalDate date) {
ClubMetric metric = mock(ClubMetric.class);
when(metric.getDate()).thenReturn(date);
return metric;
}

public static ClubMetric createClubMetric(String clubId, LocalDate date) {
ClubMetric metric = mock(ClubMetric.class);
when(metric.getClubId()).thenReturn(clubId);
when(metric.getDate()).thenReturn(date);
return metric;
}

public static ClubMetric createClubMetric(LocalDate date, String ip) {
ClubMetric metric = mock(ClubMetric.class);
when(metric.getDate()).thenReturn(date);
when(metric.getIp()).thenReturn(ip);
return metric;
}

}
Loading