Skip to content

Commit 320ebbc

Browse files
refactor: 임시 편지 조회 시 응답값 변경, 스케줄링 중복키 에러 해결 (WR9-125)
1 parent 4547e1d commit 320ebbc

File tree

6 files changed

+15
-30
lines changed

6 files changed

+15
-30
lines changed

src/main/java/io/crops/warmletter/domain/letter/dto/response/LetterDraftResponse.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,22 @@
33
import io.crops.warmletter.domain.letter.enums.Category;
44
import io.crops.warmletter.domain.letter.enums.FontType;
55
import io.crops.warmletter.domain.letter.enums.PaperType;
6-
import io.crops.warmletter.domain.letter.enums.Status;
76
import lombok.*;
87

9-
import java.time.LocalDateTime;
108

119
@Getter
1210
@Builder
1311
@AllArgsConstructor
1412
public class LetterDraftResponse {
1513

1614
private final Long letterId;
17-
private final Long writerId;
15+
private final Long matchingId;
1816
private final Long receiverId;
1917
private final Long parentLetterId;
2018
private final String title;
2119
private final String content;
2220
private final Category category;
2321
private final PaperType paperType;
2422
private final FontType fontType;
25-
private final Status status;
26-
private final LocalDateTime deliveryStartedAt;
27-
private final LocalDateTime deliveryCompletedAt;
28-
private final Long matchingId;
2923

3024
}

src/main/java/io/crops/warmletter/domain/letter/dto/response/LetterResponse.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,17 @@ public static LetterResponse fromDeliveryLetter(Letter letter) {
115115
}
116116

117117
// 임시 저장(draft) 변환 메서드 추가
118-
public static LetterResponse fromDraftLetter(LetterDraftResponse draft, String zipCode) {
118+
public static LetterResponse fromDraftLetter(LetterDraftResponse draft) {
119119
return builder()
120120
.letterId(draft.getLetterId())
121-
.writerId(draft.getWriterId())
121+
.matchingId(draft.getMatchingId())
122122
.receiverId(draft.getReceiverId())
123123
.parentLetterId(draft.getParentLetterId())
124-
.zipCode(zipCode)
125124
.title(draft.getTitle())
126125
.content(draft.getContent())
127126
.category(draft.getCategory())
128127
.paperType(draft.getPaperType())
129128
.fontType(draft.getFontType())
130-
.status(draft.getStatus())
131-
.deliveryStartedAt(draft.getDeliveryStartedAt())
132-
.deliveryCompletedAt(draft.getDeliveryCompletedAt())
133-
.matchingId(draft.getMatchingId())
134129
.build();
135130
}
136131
}

src/main/java/io/crops/warmletter/domain/letter/repository/LetterRepository.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,14 @@ Page<Letter> findDeliveredOrMyLettersByMatchingId(
5252

5353
@Query("SELECT new io.crops.warmletter.domain.letter.dto.response.LetterDraftResponse(" +
5454
"l.id, " +
55-
"l.writerId, " +
55+
"l.matchingId, " +
5656
"l.receiverId, " +
5757
"l.parentLetterId, " +
5858
"l.title, " +
5959
"l.content, " +
6060
"l.category, " +
6161
"l.paperType, " +
62-
"l.fontType, " +
63-
"l.status, " +
64-
"l.deliveryStartedAt, " +
65-
"l.deliveryCompletedAt, " +
66-
"l.matchingId) " +
62+
"l.fontType) " +
6763
"FROM Letter l LEFT JOIN LetterMatching m ON l.matchingId = m.id " +
6864
"WHERE l.writerId = :writerId AND l.status = :status")
6965
List<LetterDraftResponse> findDraftLettersWithMatching(@Param("writerId") Long writerId,

src/main/java/io/crops/warmletter/domain/letter/service/LetterService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ public LetterResponse createLetter(CreateLetterRequest request) {
7373
//부모편지 조회
7474
Letter parentLetter = letterRepository.findById(request.getParentLetterId()).orElseThrow(ParentLetterNotFoundException::new);
7575

76-
// Long matchingId = request.getMatchingId() != null ? request.getMatchingId() : parentLetter.getMatchingId(); 만약을 위해..
76+
Long matchingId = request.getMatchingId() != null ? request.getMatchingId() : parentLetter.getMatchingId();
7777

7878
//현재 계속 주고 받을 수 있는 상황이면 답장 가능
79-
boolean active = letterMatchingRepository.findById(request.getMatchingId()).orElseThrow(MatchingNotFoundException::new).isActive();
79+
boolean active = letterMatchingRepository.findById(matchingId).orElseThrow(MatchingNotFoundException::new).isActive();
8080

8181
if (active) {
8282
builder.receiverId(request.getReceiverId())
8383
.parentLetterId(request.getParentLetterId())
8484
.letterType(LetterType.DIRECT)
8585
.status(Status.IN_DELIVERY)
86-
.matchingId(request.getMatchingId());
86+
.matchingId(matchingId);
8787

8888
//첫편지면 matchingId 넣어줌 , 받는사람도 넣어줌.
8989
if(parentLetter.getParentLetterId() == null) {
90-
parentLetter.updateMatchingId(request.getMatchingId());
90+
parentLetter.updateMatchingId(matchingId);
9191
parentLetter.updateReceiverId(writerId);
9292
parentLetter.updateLetterType(LetterType.DIRECT);
9393
parentLetter.updateIsRead(true);
@@ -247,7 +247,7 @@ public List<LetterResponse> getLettersByStatus(String status) {
247247
// 임시 저장 편지이면서 상태가 SAVED인 편지 조회 (작성자 기준)
248248
return letterRepository.findDraftLettersWithMatching(currentUserId, Status.SAVED)
249249
.stream()
250-
.map(draft -> LetterResponse.fromDraftLetter(draft, authFacade.getZipCode()))
250+
.map(LetterResponse::fromDraftLetter)
251251
.collect(Collectors.toList());
252252
} else {
253253
throw new BusinessException(INVALID_INPUT_VALUE);

src/main/java/io/crops/warmletter/global/schedule/DeliverySchedule.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public void processDeliveryCompletion() {
4141
// lettersToComplete 조건을 만족하는 편지를 보낸 사람의 zipCode 조회
4242
List<LetterAlarmResponse> zipCodeData = letterRepository.findZipCodeByLettersToComplete(now);
4343
Map<Long, String> senderZipCodes = zipCodeData.stream()
44-
.collect(Collectors.toMap(LetterAlarmResponse::getWriterId, LetterAlarmResponse::getZipCode));
44+
.collect(Collectors.toMap(
45+
LetterAlarmResponse::getWriterId,
46+
LetterAlarmResponse::getZipCode,
47+
(existingZipCode, newZipCode) -> existingZipCode // 중복 키 발생 시 기존 값 사용
48+
));
4549

4650
if (!lettersToComplete.isEmpty()) {
4751
log.info("배송 완료 처리할 편지 수: {}", lettersToComplete.size());

src/test/java/io/crops/warmletter/domain/letter/service/LetterServiceTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,17 +1232,13 @@ void findDraftLetters() throws Exception {
12321232
Long currentUserId = 1L;
12331233
LetterDraftResponse letterDraftResponse = LetterDraftResponse.builder()
12341234
.letterId(200L)
1235-
.writerId(currentUserId)
12361235
.receiverId(2L)
12371236
.parentLetterId(50L)
12381237
.title("제목")
12391238
.content("내용")
12401239
.category(Category.CONSULT)
12411240
.paperType(PaperType.COMFORT)
12421241
.fontType(FontType.HIMCHAN)
1243-
.status(Status.SAVED)
1244-
.deliveryStartedAt(LocalDateTime.now())
1245-
.deliveryCompletedAt(LocalDateTime.now().plusHours(1))
12461242
.matchingId(300L)
12471243
.build();
12481244
List<LetterDraftResponse> letterDraftResponses = Collections.singletonList(letterDraftResponse);

0 commit comments

Comments
 (0)