Skip to content

Commit

Permalink
✨ [Feature] AgendaProfile이 없는 유저 검색시 구분을 위해 함수 분리 #1002 (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
AreSain authored Sep 5, 2024
1 parent b8ae238 commit 97682d8
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import gg.agenda.api.user.agendaprofile.controller.response.AgendaProfileInfoDetailsResDto;
import gg.agenda.api.user.agendaprofile.controller.response.AttendedAgendaListResDto;
import gg.agenda.api.user.agendaprofile.controller.response.CurrentAttendAgendaListResDto;
import gg.agenda.api.user.agendaprofile.controller.response.IntraProfileResDto;
import gg.agenda.api.user.agendaprofile.controller.response.MyAgendaProfileDetailsResDto;
import gg.agenda.api.user.agendaprofile.service.AgendaProfileFindService;
import gg.agenda.api.user.agendaprofile.service.AgendaProfileService;
Expand Down Expand Up @@ -109,12 +110,18 @@ public ResponseEntity<List<CurrentAttendAgendaListResDto>> getCurrentAttendAgend
return ResponseEntity.ok(currentAttendAgendaList);
}

@GetMapping("/{intraId}")
public ResponseEntity<AgendaProfileDetailsResDto> agendaProfileDetails(@PathVariable String intraId,
HttpServletResponse response) {
@GetMapping("{intraId}")
public ResponseEntity<AgendaProfileDetailsResDto> agendaProfileDetails(@PathVariable String intraId) {
AgendaProfile profile = agendaProfileFindService.findAgendaProfileByIntraId(intraId);
AgendaProfileDetailsResDto resDto = AgendaProfileDetailsResDto.toDto(profile);
return ResponseEntity.ok(resDto);
}

@GetMapping("/intra/{intraId}")
public ResponseEntity<IntraProfileResDto> intraProfileDetails(@PathVariable String intraId,
HttpServletResponse response) {
IntraProfile intraProfile = intraProfileUtils.getIntraProfile(intraId, response);
AgendaProfileDetailsResDto resDto = AgendaProfileDetailsResDto.toDto(profile, intraProfile);
IntraProfileResDto resDto = IntraProfileResDto.toDto(intraProfile);
return ResponseEntity.ok(resDto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package gg.agenda.api.user.agendaprofile.controller.response;

import java.net.URL;
import java.util.List;

import org.mapstruct.Mapper;

import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraAchievement;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfile;
import gg.data.agenda.AgendaProfile;
import gg.data.agenda.type.Coalition;
import gg.data.agenda.type.Location;
Expand All @@ -23,31 +16,24 @@ public class AgendaProfileDetailsResDto {
private String userGithub;
private Coalition userCoalition;
private Location userLocation;
private URL imageUrl;
private List<IntraAchievement> achievements;

@Builder
public AgendaProfileDetailsResDto(String userIntraId, String userContent, String userGithub,
Coalition userCoalition, Location userLocation, URL imageUrl,
List<IntraAchievement> achievements) {
Coalition userCoalition, Location userLocation) {
this.userIntraId = userIntraId;
this.userContent = userContent;
this.userGithub = userGithub;
this.userCoalition = userCoalition;
this.userLocation = userLocation;
this.imageUrl = imageUrl;
this.achievements = achievements;
}

public static AgendaProfileDetailsResDto toDto(AgendaProfile profile, IntraProfile intraProfile) {
public static AgendaProfileDetailsResDto toDto(AgendaProfile profile) {
return AgendaProfileDetailsResDto.builder()
.userIntraId(profile.getIntraId())
.userContent(profile.getContent())
.userGithub(profile.getGithubUrl())
.userCoalition(profile.getCoalition())
.userLocation(profile.getLocation())
.imageUrl(intraProfile.getImageUrl())
.achievements(intraProfile.getAchievements())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gg.agenda.api.user.agendaprofile.controller.response;

import java.net.URL;
import java.util.List;

import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraAchievement;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraProfile;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
public class IntraProfileResDto {
private String intraId;
private URL imageUrl;
private List<IntraAchievement> achievements;

@Builder
public IntraProfileResDto(String intraId, URL imageUrl, List<IntraAchievement> achievements) {
this.intraId = intraId;
this.imageUrl = imageUrl;
this.achievements = achievements;
}

public static IntraProfileResDto toDto(IntraProfile intraProfile) {
return IntraProfileResDto.builder()
.intraId(intraProfile.getIntraId())
.imageUrl(intraProfile.getImageUrl())
.achievements(intraProfile.getAchievements())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;

import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraAchievement;
import gg.agenda.api.user.agendaprofile.service.intraprofile.IntraImage;
Expand All @@ -18,6 +20,7 @@
import gg.auth.FortyTwoAuthUtil;
import gg.utils.cookie.CookieUtil;
import gg.utils.exception.custom.AuthenticationException;
import gg.utils.exception.custom.NotExistException;
import gg.utils.external.ApiUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -42,7 +45,7 @@ public IntraProfile getIntraProfile(HttpServletResponse response) {
intraProfileResponseValidation(intraProfileResponse);
IntraImage intraImage = intraProfileResponse.getImage();
List<IntraAchievement> intraAchievements = intraProfileResponse.getAchievements();
return new IntraProfile(intraImage.getLink(), intraAchievements);
return new IntraProfile(intraProfileResponse.getLogin(), intraImage.getLink(), intraAchievements);
} catch (Exception e) {
log.error("42 Intra Profile API 호출 실패", e);
cookieUtil.deleteCookie(response, "refresh_token");
Expand All @@ -56,11 +59,14 @@ public IntraProfile getIntraProfile(String intraId, HttpServletResponse response
intraProfileResponseValidation(intraProfileResponse);
IntraImage intraImage = intraProfileResponse.getImage();
List<IntraAchievement> intraAchievements = intraProfileResponse.getAchievements();
return new IntraProfile(intraImage.getLink(), intraAchievements);
return new IntraProfile(intraProfileResponse.getLogin(), intraImage.getLink(), intraAchievements);
} catch (Exception e) {
if (e instanceof NotExistException) {
throw new NotExistException(AUTH_NOT_FOUND);
}
log.error("42 Intra Profile API 호출 실패", e);
cookieUtil.deleteCookie(response, "refresh_token");
throw new AuthenticationException(AUTH_NOT_FOUND);
throw new AuthenticationException(AUTH_NOT_VALID);
}
}

Expand All @@ -70,7 +76,10 @@ private IntraProfileResponse requestIntraProfile(String requestUrl) {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
return apiUtil.apiCall(requestUrl, IntraProfileResponse.class, headers, HttpMethod.GET);
} catch (Exception e) {
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
throw new NotExistException(AUTH_NOT_FOUND);
}
String accessToken = fortyTwoAuthUtil.refreshAccessToken();
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);
Expand All @@ -82,6 +91,9 @@ private void intraProfileResponseValidation(IntraProfileResponse intraProfileRes
if (Objects.isNull(intraProfileResponse)) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
if (Objects.isNull(intraProfileResponse.getLogin())) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
if (Objects.isNull(intraProfileResponse.getImage())) {
throw new AuthenticationException(AUTH_NOT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraProfile {
private String intraId;

private URL imageUrl;

private List<IntraAchievement> achievements;

@Builder
public IntraProfile(URL imageUrl, List<IntraAchievement> achievements) {
public IntraProfile(String intraId, URL imageUrl, List<IntraAchievement> achievements) {
this.intraId = intraId;
this.imageUrl = imageUrl;
this.achievements = achievements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class IntraProfileResponse {
String login;

IntraImage image;

List<IntraAchievement> achievements;

@Builder
public IntraProfileResponse(IntraImage image, List<IntraAchievement> achievements) {
public IntraProfileResponse(String login, IntraImage image, List<IntraAchievement> achievements) {
this.login = login;
this.image = image;
this.achievements = achievements;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ void getAgendaListSuccess() {
int nonOfficialSize = 6;
List<Agenda> agendas = new ArrayList<>();
IntStream.range(0, officialSize).forEach(i -> agendas.add(Agenda.builder().isOfficial(true)
.deadline(LocalDateTime.now().plusDays(i + 3)).build()));
.deadline(LocalDateTime.now().plusDays(i + 5)).build()));
IntStream.range(0, nonOfficialSize).forEach(i -> agendas.add(Agenda.builder().isOfficial(false)
.deadline(LocalDateTime.now().plusDays(i + 3)).build()));
.deadline(LocalDateTime.now().plusDays(i + 2)).build()));
when(agendaRepository.findAllByStatusIs(AgendaStatus.OPEN)).thenReturn(agendas);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void beforeEach() {
void test() throws Exception {
//given
URL url = new URL("http://localhost:8080");
IntraProfile intraProfile = new IntraProfile(url, List.of());
IntraProfile intraProfile = new IntraProfile(user.getIntraId(), url, List.of());
Mockito.when(intraProfileUtils.getIntraProfile(any(HttpServletResponse.class)))
.thenReturn(intraProfile);
AgendaProfile agendaProfile = agendaMockData.createAgendaProfile(user, SEOUL);
Expand Down Expand Up @@ -148,7 +148,7 @@ void beforeEach() {
void getAgendaProfileSuccess() throws Exception {
//given
URL url = new URL("http://localhost:8080");
IntraProfile intraProfile = new IntraProfile(url, List.of());
IntraProfile intraProfile = new IntraProfile(user.getIntraId(), url, List.of());
AgendaProfile agendaProfile = agendaMockData.createAgendaProfile(user, SEOUL);
agendaMockData.createTicket(agendaProfile);
Mockito.when(intraProfileUtils.getIntraProfile(any(String.class), any(HttpServletResponse.class)))
Expand All @@ -174,7 +174,7 @@ void getAgendaProfileSuccess() throws Exception {
void getAgendaProfileFailedWithInvalidIntraId() throws Exception {
//given
URL url = new URL("http://localhost:8080");
IntraProfile intraProfile = new IntraProfile(url, List.of());
IntraProfile intraProfile = new IntraProfile(user.getIntraId(), url, List.of());
HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
Mockito.when(intraProfileUtils.getIntraProfile(res)).thenReturn(intraProfile);
AgendaProfile agendaProfile = agendaMockData.createAgendaProfile(user, SEOUL);
Expand Down
1 change: 1 addition & 0 deletions gg-auth/src/main/java/gg/auth/FortyTwoAuthUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private OAuth2AuthorizedClient requestNewClient(OAuth2AuthorizedClient client, C
params.add("refresh_token", client.getRefreshToken().getTokenValue());
params.add("client_id", registration.getClientId());
params.add("client_secret", registration.getClientSecret());
params.add("redirect_uri", registration.getRedirectUri());

List<Map<String, Object>> responseBody = apiUtil.apiCall(
registration.getProviderDetails().getTokenUri(),
Expand Down
3 changes: 2 additions & 1 deletion gg-utils/src/main/java/gg/utils/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public enum ErrorCode {
INVALID_CHECKLIST(400, "RE001", "잘못된 요청 데이터입니다."),

// agenda
AUTH_NOT_VALID(401, "AG001", "인증이 유효하지 않습니다."),
AGENDA_TEAM_FULL(400, "AG101", "팀이 꽉 찼습니다."),
LOCATION_NOT_VALID(400, "AG102", "유효하지 않은 지역입니다."),
AGENDA_AWARD_EMPTY(400, "AG103", "시상 정보가 없습니다."),
Expand All @@ -216,7 +217,7 @@ public enum ErrorCode {
TEAM_LEADER_FORBIDDEN(403, "AG207", "팀장이 아닙니다."),
AGENDA_TEAM_FORBIDDEN(403, "AG208", "일정에 참여한 팀이 있습니다."),
AGENDA_MODIFICATION_FORBIDDEN(403, "AG209", "개최자만 일정을 수정할 수 있습니다."),
AUTH_NOT_FOUND(404, "AG301", "42 정보가 만료되었습니다."),
AUTH_NOT_FOUND(404, "AG301", "42 정보를 찾을 수 없습니다."),
TICKET_NOT_FOUND(404, "AG302", "해당 티켓이 존재하지 않습니다."),
AGENDA_NOT_FOUND(404, "AG303", "해당 일정이 존재하지 않습니다."),
NOT_SETUP_TICKET(404, "AG304", "티켓 신청이 되어있지 않습니다."),
Expand Down

0 comments on commit 97682d8

Please sign in to comment.