Skip to content

Commit 92b71fe

Browse files
authored
[fix] 지원자 상태 변경 오류
1 parent 1c00f30 commit 92b71fe

File tree

3 files changed

+127
-4
lines changed

3 files changed

+127
-4
lines changed

src/main/java/dmu/dasom/api/domain/applicant/service/ApplicantServiceImpl.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public class ApplicantServiceImpl implements ApplicantService {
3838
private final EmailService emailService;
3939
private final GoogleApiService googleApiService;
4040

41+
@Value("${google.spreadsheet.id}")
42+
private String spreadSheetId;
43+
4144
// 지원자 저장
4245
@Override
4346
public void apply(final ApplicantCreateRequestDto request) {
@@ -82,9 +85,20 @@ public ApplicantDetailsResponseDto getApplicant(final Long id) {
8285
@Override
8386
public ApplicantDetailsResponseDto updateApplicantStatus(final Long id, final ApplicantStatusUpdateRequestDto request) {
8487
final Applicant applicant = findById(id);
85-
88+
// 지원자 상태 변경
8689
applicant.updateStatus(request.getStatus());
87-
googleApiService.updateSheet(List.of(applicant));
90+
91+
// Google Sheets에서 학번(Student No)을 기준으로 사용자 존재 여부 확인
92+
int rowIndex = googleApiService.findRowIndexByStudentNo(spreadSheetId, "Sheet1", applicant.getStudentNo());
93+
if (rowIndex == -1) {
94+
// Google Sheets에 사용자 추가
95+
googleApiService.appendToSheet(List.of(applicant));
96+
log.info("지원자가 Google Sheets에 없어서 새로 추가되었습니다: {}", applicant.getStudentNo());
97+
} else {
98+
// Google Sheets에서 사용자 상태 업데이트
99+
googleApiService.updateSheet(List.of(applicant));
100+
log.info("Google Sheets에서 지원자 상태가 업데이트되었습니다: {}", applicant.getStudentNo());
101+
}
88102

89103
return applicant.toApplicantDetailsResponse();
90104
}

src/main/java/dmu/dasom/api/domain/google/service/GoogleApiService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public void updateSheet(List<Applicant> applicants) {
7878
processSheetsUpdate(applicants, false);
7979
}
8080

81-
private int findRowIndexByStudentNo(String spreadSheetId, String sheetName, String studentNo){
81+
public int findRowIndexByStudentNo(String spreadSheetId, String sheetName, String studentNo){
8282
try {
8383
List<List<Object>> rows = readSheet(spreadSheetId, sheetName + "!A:L"); // A열부터 L열까지 읽기
8484

8585
for (int i = 0; i < rows.size(); i++){
8686
List<Object> row = rows.get(i);
87-
if(!row.isEmpty() && row.get(2).equals(studentNo)){
87+
if(!row.isEmpty() && row.get(2).equals(studentNo)){ // 학번(Student No)이 3번째 열(A=0 기준)
8888
return i + 1;
8989
}
9090
}
@@ -175,4 +175,6 @@ public void processSheetsUpdate(List<Applicant> applicants, boolean isAppend) {
175175
}
176176
}
177177

178+
179+
178180
}

src/test/java/dmu/dasom/api/domain/applicant/ApplicantServiceTest.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dmu.dasom.api.domain.applicant.dto.ApplicantCreateRequestDto;
44
import dmu.dasom.api.domain.applicant.dto.ApplicantDetailsResponseDto;
55
import dmu.dasom.api.domain.applicant.dto.ApplicantResponseDto;
6+
import dmu.dasom.api.domain.applicant.dto.ApplicantStatusUpdateRequestDto;
67
import dmu.dasom.api.domain.applicant.entity.Applicant;
78
import dmu.dasom.api.domain.applicant.enums.ApplicantStatus;
89
import dmu.dasom.api.domain.applicant.repository.ApplicantRepository;
@@ -14,6 +15,7 @@
1415
import dmu.dasom.api.domain.google.service.GoogleApiService;
1516
import dmu.dasom.api.global.dto.PageResponse;
1617
import jakarta.mail.MessagingException;
18+
import org.junit.jupiter.api.BeforeEach;
1719
import org.junit.jupiter.api.DisplayName;
1820
import org.junit.jupiter.api.Test;
1921
import org.junit.jupiter.api.extension.ExtendWith;
@@ -23,6 +25,7 @@
2325
import org.springframework.data.domain.Page;
2426
import org.springframework.data.domain.PageImpl;
2527
import org.springframework.data.domain.PageRequest;
28+
import org.springframework.test.util.ReflectionTestUtils;
2629

2730
import java.time.LocalDateTime;
2831
import java.util.Collections;
@@ -47,6 +50,11 @@ class ApplicantServiceTest {
4750
@Mock
4851
private GoogleApiService googleApiService;
4952

53+
@BeforeEach
54+
void setup() {
55+
ReflectionTestUtils.setField(applicantService, "spreadSheetId", "test-spreadsheet-id");
56+
}
57+
5058
@Test
5159
@DisplayName("지원자 저장 - 성공")
5260
void apply_success() {
@@ -241,4 +249,103 @@ void getApplicantByStudentNo_fail() {
241249
assertEquals(ErrorCode.ARGUMENT_NOT_VALID, exception.getErrorCode());
242250
}
243251

252+
@Test
253+
@DisplayName("지원자 상태 변경 - Google Sheets에 없는 사용자 추가")
254+
void updateApplicantStatus_addToGoogleSheets() {
255+
// given
256+
Long applicantId = 1L;
257+
String testSpreadsheetId = "test-spreadsheet-id";
258+
259+
ApplicantStatusUpdateRequestDto request = mock(ApplicantStatusUpdateRequestDto.class);
260+
when(request.getStatus()).thenReturn(ApplicantStatus.INTERVIEW_PASSED);
261+
262+
// Mock 지원자 생성
263+
Applicant mockApplicant = Applicant.builder()
264+
.name("홍길동")
265+
.studentNo("20210000")
266+
.contact("010-1234-5678")
267+
268+
.grade(2)
269+
.reasonForApply("팀 활동 경험을 쌓고 싶습니다.")
270+
.activityWish("프로그래밍 스터디 참여")
271+
.isPrivacyPolicyAgreed(true)
272+
.status(ApplicantStatus.PENDING)
273+
.createdAt(LocalDateTime.now())
274+
.updatedAt(LocalDateTime.now())
275+
.build();
276+
277+
when(applicantRepository.findById(applicantId)).thenReturn(Optional.of(mockApplicant));
278+
279+
// 모든 인자를 Matchers로 설정
280+
when(googleApiService.findRowIndexByStudentNo(eq(testSpreadsheetId), eq("Sheet1"), eq("20210000")))
281+
.thenReturn(-1);
282+
283+
doNothing().when(googleApiService).appendToSheet(anyList());
284+
285+
// when
286+
applicantService.updateApplicantStatus(applicantId, request);
287+
288+
// then
289+
verify(applicantRepository).findById(applicantId);
290+
verify(googleApiService).appendToSheet(List.of(mockApplicant));
291+
}
292+
293+
@Test
294+
@DisplayName("지원자 상태 변경 - Google Sheets에서 상태 업데이트")
295+
void updateApplicantStatus_updateInGoogleSheets() {
296+
// given
297+
Long applicantId = 1L;
298+
299+
// 요청 DTO 설정
300+
ApplicantStatusUpdateRequestDto request = mock(ApplicantStatusUpdateRequestDto.class);
301+
when(request.getStatus()).thenReturn(ApplicantStatus.INTERVIEW_PASSED);
302+
303+
// Mock 지원자 생성
304+
Applicant mockApplicant = Applicant.builder()
305+
.name("홍길동")
306+
.studentNo("20210000")
307+
.contact("010-1234-5678")
308+
309+
.grade(2)
310+
.reasonForApply("팀 활동 경험을 쌓고 싶습니다.")
311+
.activityWish("프로그래밍 스터디 참여")
312+
.isPrivacyPolicyAgreed(true)
313+
.status(ApplicantStatus.PENDING)
314+
.createdAt(LocalDateTime.now())
315+
.updatedAt(LocalDateTime.now())
316+
.build();
317+
318+
// Repository와 GoogleApiService의 동작 모킹
319+
when(applicantRepository.findById(applicantId)).thenReturn(Optional.of(mockApplicant));
320+
when(googleApiService.findRowIndexByStudentNo(anyString(), eq("Sheet1"), eq("20210000"))).thenReturn(5); // Google Sheets에 있음
321+
doNothing().when(googleApiService).updateSheet(anyList());
322+
323+
// when
324+
applicantService.updateApplicantStatus(applicantId, request);
325+
326+
// then
327+
verify(applicantRepository).findById(applicantId);
328+
verify(googleApiService).updateSheet(List.of(mockApplicant));
329+
}
330+
331+
@Test
332+
@DisplayName("지원자 저장 - 덮어쓰기 (Google Sheets 연동 포함)")
333+
void apply_overwrite_withGoogleSheets() {
334+
// given
335+
ApplicantCreateRequestDto request = mock(ApplicantCreateRequestDto.class);
336+
when(request.getStudentNo()).thenReturn("20210000");
337+
338+
Applicant existingApplicant = mock(Applicant.class); // 기존 Applicant 객체 모킹
339+
when(applicantRepository.findByStudentNo("20210000")).thenReturn(Optional.of(existingApplicant));
340+
341+
when(request.getIsOverwriteConfirmed()).thenReturn(true);
342+
343+
// when
344+
applicantService.apply(request);
345+
346+
// then
347+
verify(existingApplicant).overwrite(request);
348+
verify(googleApiService).updateSheet(List.of(existingApplicant));
349+
}
350+
244351
}

0 commit comments

Comments
 (0)