Skip to content

Commit

Permalink
merge sprint0
Browse files Browse the repository at this point in the history
  • Loading branch information
μ£Όμ–΄μ§„μ‚¬λž‘(eojinjoo) committed May 23, 2024
2 parents f5c45d8 + 28ced6b commit 1354218
Show file tree
Hide file tree
Showing 23 changed files with 869 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -13,6 +12,7 @@
import lombok.ToString;
import org.sopt.app.domain.enums.UserStatus;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PlaygroundAuthInfo {

@Getter
Expand Down Expand Up @@ -51,6 +51,7 @@ public static class PlaygroundMain {
@ToString
public static class PlaygroundProfile {

private Long memberId;
private String name;
private String profileImage;
private List<PlaygroundActivity> activities;
Expand All @@ -62,19 +63,6 @@ public static class PlaygroundProfile {
public static class PlaygroundActivity {

private String cardinalInfo;
private List<PlaygroundCardinalActivity> cardinalActivities;
}

@Getter
@Setter
@ToString
public static class PlaygroundCardinalActivity {

private Long id;
private Long generation;
private String team;
private String part;
private Boolean isProject;
}

@Getter
Expand Down Expand Up @@ -148,7 +136,7 @@ public static class MemberProfile {
public ActivityCardinalInfo getLatestActivity() {
return activities.stream()
.sorted(Comparator.comparing(ActivityCardinalInfo::getGeneration, Comparator.reverseOrder()))
.collect(Collectors.toList())
.toList()
.get(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.sopt.app.application.auth;

import io.jsonwebtoken.ExpiredJwtException;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
Expand All @@ -15,6 +14,8 @@
import org.sopt.app.interfaces.external.PlaygroundClient;
import org.sopt.app.presentation.auth.AppAuthRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException.BadRequest;

Expand All @@ -32,9 +33,8 @@ public class PlaygroundAuthService {

public PlaygroundAuthInfo.PlaygroundMain getPlaygroundInfo(String token) {
val member = this.getPlaygroundMember(token);
val playgroundProfile = this.getPlaygroundMemberProfile(token);
val generationList = playgroundProfile.getActivities().stream()
.map(activity -> activity.getCardinalActivities().get(0).getGeneration()).collect(Collectors.toList());
val playgroundProfile = this.getPlaygroundMemberProfile(token, member.getId());
val generationList = this.getMemberGenerationList(playgroundProfile);
member.setAccessToken(token);
member.setStatus(this.getStatus(generationList));
return member;
Expand All @@ -51,7 +51,7 @@ public AppAuthRequest.AccessTokenRequest getPlaygroundAccessToken(AppAuthRequest

private PlaygroundAuthInfo.PlaygroundMain getPlaygroundMember(String accessToken) {
Map<String, String> headers = createDefaultHeader();
headers.put("Authorization", accessToken);
headers.put(HttpHeaders.AUTHORIZATION, accessToken);
try {
return playgroundClient.getPlaygroundMember(headers);
} catch (ExpiredJwtException e) {
Expand All @@ -67,19 +67,15 @@ public PlaygroundAuthInfo.RefreshedToken refreshPlaygroundToken(AppAuthRequest.A
headers.put("x-request-from", requestFrom);
try {
return playgroundClient.refreshPlaygroundToken(headers, tokenRequest);
} catch (BadRequest badRequest) {
} catch (BadRequest | ExpiredJwtException badRequest) {
throw new UnauthorizedException(ErrorCode.INVALID_PLAYGROUND_TOKEN.getMessage());
} catch (ExpiredJwtException e) {
throw new UnauthorizedException(ErrorCode.INVALID_PLAYGROUND_TOKEN.getMessage());
}
}
}

public PlaygroundAuthInfo.MainView getPlaygroundUserForMainView(String accessToken) {
val playgroundProfile = this.getPlaygroundMemberProfile(accessToken);
public PlaygroundAuthInfo.MainView getPlaygroundUserForMainView(String accessToken, Long playgroundId) {
val playgroundProfile = this.getPlaygroundMemberProfile(accessToken, playgroundId);
val profileImage = playgroundProfile.getProfileImage() == null ? "" : playgroundProfile.getProfileImage();
val generationList = playgroundProfile.getActivities().stream()
.map(activity -> activity.getCardinalActivities().get(0).getGeneration()).collect(Collectors.toList());
Collections.sort(generationList, Collections.reverseOrder());
val generationList = this.getMemberGenerationList(playgroundProfile);
val mainViewUser = PlaygroundAuthInfo.MainViewUser.builder()
.status(this.getStatus(generationList))
.name(playgroundProfile.getName())
Expand All @@ -93,37 +89,50 @@ private UserStatus getStatus(List<Long> generationList) {
return generationList.contains(currentGeneration) ? UserStatus.ACTIVE : UserStatus.INACTIVE;
}

private PlaygroundAuthInfo.PlaygroundProfile getPlaygroundMemberProfile(String accessToken) {
private PlaygroundAuthInfo.PlaygroundProfile getPlaygroundMemberProfile(String accessToken, Long playgroundId) {
Map<String, String> headers = createDefaultHeader();
headers.put("Authorization", accessToken);
headers.put(HttpHeaders.AUTHORIZATION, accessToken);
try {
return playgroundClient.getPlaygroundMemberProfile(headers);
return playgroundClient.getPlaygroundMemberProfile(headers, playgroundId).get(0);
} catch (BadRequest e) {
throw new BadRequestException(ErrorCode.PLAYGROUND_PROFILE_NOT_EXISTS.getMessage());
} catch (ExpiredJwtException e) {
throw new UnauthorizedException(ErrorCode.INVALID_PLAYGROUND_TOKEN.getMessage());
}
}

public PlaygroundAuthInfo.UserActiveInfo getPlaygroundUserActiveInfo(String accessToken) {
val playgroundProfile = this.getPlaygroundMemberProfile(accessToken);
val generationList = playgroundProfile.getActivities().stream()
.map(activity -> activity.getCardinalActivities().get(0).getGeneration()).toList();
public PlaygroundAuthInfo.UserActiveInfo getPlaygroundUserActiveInfo(String accessToken, Long playgroundId) {
val playgroundProfile = this.getPlaygroundMemberProfile(accessToken, playgroundId);
val generationList = this.getMemberGenerationList(playgroundProfile);
val userStatus = this.getStatus(generationList);
return PlaygroundAuthInfo.UserActiveInfo.builder()
.status(userStatus)
.currentGeneration(currentGeneration)
.build();
}

private List<Long> getMemberGenerationList(PlaygroundAuthInfo.PlaygroundProfile playgroundProfile) {
return playgroundProfile.getActivities().stream()
.map(activity ->
{
try {
return Long.parseLong(activity.getCardinalInfo().split(",")[0]);
} catch (NumberFormatException e) {
throw new BadRequestException(ErrorCode.INVALID_PLAYGROUND_CARDINAL_INFO.getMessage());
}
})
.sorted(Collections.reverseOrder())
.toList();
}

// Header 생성 λ©”μ„œλ“œ
private Map<String, String> createDefaultHeader() {
return new HashMap<>(Map.of("content-type", "application/json;charset=UTF-8"));
return new HashMap<>(Map.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
}

public PlaygroundAuthInfo.ActiveUserIds getPlayGroundUserIds(String accessToken) {
Map<String, String> headers = createDefaultHeader();
headers.put("Authorization", accessToken);
headers.put(HttpHeaders.AUTHORIZATION, accessToken);
try {
return playgroundClient.getPlaygroundUserIds(headers, currentGeneration);
} catch (BadRequest e) {
Expand All @@ -133,14 +142,16 @@ public PlaygroundAuthInfo.ActiveUserIds getPlayGroundUserIds(String accessToken)
}
}

public List<PlaygroundAuthInfo.MemberProfile> getPlaygroundMemberProfiles(String accessToken, List<Long> memberIds) {
public List<PlaygroundAuthInfo.MemberProfile> getPlaygroundMemberProfiles(String accessToken,
List<Long> memberIds) {
Map<String, String> defaultHeader = createDefaultHeader();
defaultHeader.put("Authorization", accessToken);
defaultHeader.put(HttpHeaders.AUTHORIZATION, accessToken);
String stringifyIds = memberIds.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
try {
return playgroundClient.getMemberProfiles(defaultHeader, URLEncoder.encode(stringifyIds, StandardCharsets.UTF_8));
return playgroundClient.getMemberProfiles(defaultHeader,
URLEncoder.encode(stringifyIds, StandardCharsets.UTF_8));
} catch (BadRequest e) {
throw new BadRequestException(ErrorCode.PLAYGROUND_PROFILE_NOT_EXISTS.getMessage());
} catch (ExpiredJwtException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.sopt.app.domain.entity.Mission;
Expand Down Expand Up @@ -36,7 +35,7 @@ public List<MissionInfo.Completeness> findAllMission(Long userId) {
.build())
.sorted(Comparator.comparing(MissionInfo.Completeness::getLevel)
.thenComparing(MissionInfo.Completeness::getTitle))
.collect(Collectors.toList());
.toList();
}

private Boolean isCompletedMission(Long missionId, List<Stamp> completedStamps) {
Expand All @@ -60,7 +59,7 @@ public Mission uploadMission(MissionRequest.RegisterMissionRequest registerMissi
@Transactional(readOnly = true)
public List<Mission> getCompleteMission(Long userId) {
val stampList = stampRepository.findAllByUserId(userId);
val missionIdList = stampList.stream().map(Stamp::getMissionId).collect(Collectors.toList());
val missionIdList = stampList.stream().map(Stamp::getMissionId).toList();
return missionRepository.findMissionInOrderByLevelAndTitle(missionIdList);
}

Expand All @@ -69,11 +68,11 @@ public List<Mission> getIncompleteMission(Long userId) {

//전체 λ―Έμ…˜ μ‘°νšŒν•˜κΈ°
val missionList = missionRepository.findAllByDisplay(true);
val missionIdList = missionList.stream().map(Mission::getId).collect(Collectors.toList());
val missionIdList = missionList.stream().map(Mission::getId).toList();

//stampμ—μ„œ userId둜 λ‹¬μ„±ν•œ mission μ‘°νšŒν•˜κΈ°
val stampList = stampRepository.findAllByUserId(userId);
val completeMissionIdList = stampList.stream().map(Stamp::getMissionId).collect(Collectors.toList());
val completeMissionIdList = stampList.stream().map(Stamp::getMissionId).toList();

//두 리슀트 λΉ„κ΅ν•΄μ„œ 쀑볡값 제거
val inCompleteIdList = missionIdList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,26 @@ public void deleteAll() {
public List<SoptampPoint> createCurrentGenerationSoptampPointList(
List<SoptampUser> soptampUserList
) {
val soptampPointList = soptampUserList.stream().map(e ->
SoptampPoint
.builder()
.generation(currentGeneration)
.points(0L)
.soptampUserId(e.getId())
.build())
val soptampUserIdList = soptampUserList.stream().map(SoptampUser::getId).toList();

val prevSoptampUserIdList = soptampPointRepository.findAllBySoptampUserIdInAndGeneration(
soptampUserIdList, currentGeneration
).stream().map(SoptampPoint::getSoptampUserId).toList();

val newSoptampUserList = soptampUserList.stream()
.map(soptampUser -> prevSoptampUserIdList.contains(soptampUser.getId()) ? null : soptampUser)
.filter(x -> x != null)
.toList();

val soptampPointList = newSoptampUserList.stream().map(soptampUser ->
SoptampPoint
.builder()
.generation(currentGeneration)
.points(0L)
.soptampUserId(soptampUser.getId())
.build()
).toList();

soptampPointRepository.saveAll(soptampPointList);
return soptampPointList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class SoptampUserPlaygroundInfo {

private Long userId;
private Long playgroundId;
private String name;
private Long generation;
private String part;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static org.sopt.app.domain.enums.PlaygroundPart.findPlaygroundPart;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -227,6 +229,7 @@ public void initPoint(Long userId) {
soptampUserRepository.save(newSoptampUser);
}

@Transactional
public void initAllSoptampUserPoints() {
val soptampUserList = soptampUserRepository.findAll();
soptampUserList.forEach(SoptampUser::initTotalPoints);
Expand All @@ -237,41 +240,68 @@ public List<SoptampUser> getSoptampUserInfoList(List<Long> userIdList) {
return soptampUserRepository.findAllByUserIdIn(userIdList);
}

@Transactional
public List<SoptampUser> initAllCurrentGenerationSoptampUser(
List<SoptampUser> soptampUserList,
List<SoptampUserInfo.SoptampUserPlaygroundInfo> userInfoList
) {
val validatedSoptampUserList = validateNickname(soptampUserList);
validatedSoptampUserList.stream().forEach(e -> System.out.println(e));

soptampUserList.stream().forEach(soptampUser -> {
validatedSoptampUserList.stream().forEach(soptampUser -> {
val userInfo = userInfoList.stream()
.filter(e -> soptampUser.getUserId().equals(e.getUserId()))
.findFirst().get();
PlaygroundPart part = findPlaygroundPart(userInfo.getPart());
soptampUser.updateNicknameAndGenerationAndPart(
part.getSoptampNickname() + soptampUser.getNickname(),
soptampUser.updateGenerationAndPart(
userInfo.getGeneration(),
part
);
});
soptampUserRepository.saveAll(soptampUserList);
soptampUserRepository.saveAll(validatedSoptampUserList);

return soptampUserList;
return validatedSoptampUserList;
}

@Transactional
public List<SoptampUser> validateNickname(List<SoptampUser> soptampUserList) {
soptampUserList = soptampUserList.stream().sorted(Comparator.comparing(SoptampUser::getNickname))
.collect(Collectors.toList());
val nicknameList = soptampUserList.stream().map(SoptampUser::getNickname).collect(Collectors.toList());
val uniqueNicknameList = nicknameList.stream().distinct().collect(Collectors.toList());
// uniqueNickname map 생성
val nicknameMap = generateUniqueNicknameMap(
soptampUserList.stream().sorted(Comparator.comparing(SoptampUser::getNickname))
.map(SoptampUser::getNickname).toList());

// soptampUser 리슀트 userId κΈ°μ€€μœΌλ‘œ 쀑볡 λ‹‰λ„€μž„ μ•ŒνŒŒλ²³ λΆ€μ—¬
return updateUniqueNickname(soptampUserList, nicknameMap);
}

private HashMap<String, ArrayList<String>> generateUniqueNicknameMap(List<String> nicknameList) {
val nicknameMap = new HashMap<String, ArrayList<String>>();
val uniqueNicknameList = nicknameList.stream().distinct().collect(Collectors.toList());
uniqueNicknameList.stream().forEach(nickname -> {
val count = Collections.frequency(nicknameList, nickname);
val alphabetList = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(0, count).split(""));
// 동λͺ…이인 처리
if (count == 1) {
nicknameMap.put(nickname, new ArrayList<>(List.of()));
} else {
val alphabetList = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(0, count).split(""));
val changedList = alphabetList.stream().map(alphabet -> nickname + alphabet).toList();
nicknameMap.put(nickname, new ArrayList<>(changedList));
}
});
return soptampUserList;
return nicknameMap;
}

private List<SoptampUser> updateUniqueNickname(
List<SoptampUser> soptampUserList,
HashMap<String, ArrayList<String>> nicknameMap
) {
soptampUserList.stream().sorted(Comparator.comparing(SoptampUser::getUserId)).forEach(soptampUser -> {
val validatedNicknameList = nicknameMap.get(soptampUser.getNickname());
if (validatedNicknameList.size() > 0) {
val validatedNickname = validatedNicknameList.get(0);
validatedNicknameList.remove(0);
nicknameMap.put(soptampUser.getNickname(), validatedNicknameList);
soptampUser.updateNickname(validatedNickname);
}
});
return soptampUserList;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/sopt/app/common/response/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum ErrorCode {
// PLAYGROUND
PLAYGROUND_USER_NOT_EXISTS("ν”Œλ ˆμ΄κ·ΈλΌμš΄λ“œ μœ μ € 정보λ₯Ό κ°€μ Έμ˜¬ 수 μ—†μŠ΅λ‹ˆλ‹€."),
PLAYGROUND_PROFILE_NOT_EXISTS("ν”Œλ ˆμ΄κ·ΈλΌμš΄λ“œ ν”„λ‘œν•„μ„ λ“±λ‘ν•˜μ§€ μ•Šμ€ μœ μ €μž…λ‹ˆλ‹€."),
INVALID_PLAYGROUND_CARDINAL_INFO("ν”Œλ ˆμ΄κ·ΈλΌμš΄λ“œ ν™œλ™ 정보가 μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),

// OPERATION
OPERATION_PROFILE_NOT_EXISTS("운영 μ„œλΉ„μŠ€μ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” νšŒμ›μž…λ‹ˆλ‹€."),
Expand Down Expand Up @@ -66,6 +67,7 @@ public enum ErrorCode {
FRIENDSHIP_NOT_FOUND("ν•΄λ‹Ή μΉœκ΅¬κ΄€κ³„λŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€."),

// ADMIN
INVALID_APP_ADMIN_PASSWORD("잘λͺ»λœ μ•± μ–΄λ“œλ―Ό νŒ¨μŠ€μ›Œλ“œμž…λ‹ˆλ‹€."),
ADMIN_INIT_SOPTAMP_USER_FAILED("솝탬프 μœ μ € μ΄ˆκΈ°ν™”μ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."),

// S3
Expand Down
Loading

0 comments on commit 1354218

Please sign in to comment.