Repository navigation
Expand file tree
/
Copy pathAdminMentorApplicationService.java
More file actions
150 lines (127 loc) · 6.89 KB
/
Copy pathAdminMentorApplicationService.java
File metadata and controls
150 lines (127 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.example.solidconnection.admin.service;
import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_ALREADY_EXISTS;
import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_APPLICATION_NOT_FOUND;
import static com.example.solidconnection.common.exception.ErrorCode.USER_NOT_FOUND;
import com.example.solidconnection.admin.dto.MentorApplicationCountResponse;
import com.example.solidconnection.admin.dto.MentorApplicationHistoryResponse;
import com.example.solidconnection.admin.dto.MentorApplicationRejectRequest;
import com.example.solidconnection.admin.dto.MentorApplicationSearchCondition;
import com.example.solidconnection.admin.dto.MentorApplicationSearchResponse;
import com.example.solidconnection.common.discord.DiscordNotificationType;
import com.example.solidconnection.common.discord.DiscordNotifier;
import com.example.solidconnection.common.discord.DiscordReviewMarker;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.mentor.domain.Mentor;
import com.example.solidconnection.mentor.domain.MentorApplication;
import com.example.solidconnection.mentor.domain.MentorApplicationStatus;
import com.example.solidconnection.mentor.repository.MentorApplicationRepository;
import com.example.solidconnection.mentor.repository.MentorRepository;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import com.example.solidconnection.university.domain.HostUniversity;
import com.example.solidconnection.university.repository.HostUniversityRepository;
import java.util.List;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Service
public class AdminMentorApplicationService {
private final MentorApplicationRepository mentorApplicationRepository;
private final HostUniversityRepository hostUniversityRepository;
private final SiteUserRepository siteUserRepository;
private final MentorRepository mentorRepository;
private final DiscordNotifier discordNotifier;
@Transactional(readOnly = true)
public Page<MentorApplicationSearchResponse> searchMentorApplications(
MentorApplicationSearchCondition mentorApplicationSearchCondition,
Pageable pageable
) {
return mentorApplicationRepository.searchMentorApplications(mentorApplicationSearchCondition, pageable);
}
@Transactional
public void approveMentorApplication(Long mentorApplicationId) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));
SiteUser siteUser = siteUserRepository.findById(mentorApplication.getSiteUserId())
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
validateUserCanCreateMentor(siteUser.getId());
mentorApplication.approve();
siteUser.becomeMentor();
Mentor mentor = new Mentor(
siteUser.getId(),
mentorApplication.getUniversityId(),
mentorApplication.getTermId()
);
mentorRepository.save(mentor);
publishReviewResult(mentorApplicationId, siteUser.getNickname(), DiscordReviewMarker.APPROVED.getValue());
}
private void validateUserCanCreateMentor(long siteUserId) {
if (mentorRepository.existsBySiteUserId(siteUserId)) {
throw new CustomException(MENTOR_ALREADY_EXISTS);
}
}
@Transactional
public void rejectMentorApplication(
Long mentorApplicationId,
MentorApplicationRejectRequest request
) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));
mentorApplication.reject(request.rejectedReason());
SiteUser siteUser = siteUserRepository.findById(mentorApplication.getSiteUserId())
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
publishReviewResult(mentorApplicationId, siteUser.getNickname(), DiscordReviewMarker.REJECTED.getValue());
}
private void publishReviewResult(long mentorApplicationId, String applicantInfo, String marker) {
discordNotifier.markReviewResult(
DiscordNotificationType.MENTOR_APPLICATION,
mentorApplicationId,
applicantInfo,
marker
);
}
@Transactional(readOnly = true)
public MentorApplicationCountResponse getMentorApplicationCount() {
long approvedCount = mentorApplicationRepository.countByMentorApplicationStatus(MentorApplicationStatus.APPROVED);
long pendingCount = mentorApplicationRepository.countByMentorApplicationStatus(MentorApplicationStatus.PENDING);
long rejectedCount = mentorApplicationRepository.countByMentorApplicationStatus(MentorApplicationStatus.REJECTED);
return new MentorApplicationCountResponse(
approvedCount,
pendingCount,
rejectedCount
);
}
@Transactional
public void assignUniversity(
Long mentorApplicationId,
Long universityId
) {
MentorApplication mentorApplication = mentorApplicationRepository.findById(mentorApplicationId)
.orElseThrow(() -> new CustomException(MENTOR_APPLICATION_NOT_FOUND));
mentorApplication.validateCanAssignUniversity();
HostUniversity university = hostUniversityRepository.getHostUniversityById(universityId);
mentorApplication.assignUniversity(university.getId());
}
@Transactional(readOnly = true)
public List<MentorApplicationHistoryResponse> findMentorApplicationHistory(Long siteUserId) {
SiteUser siteUser = siteUserRepository.findById(siteUserId)
.orElseThrow(() -> new CustomException(USER_NOT_FOUND));
long totalCount = mentorApplicationRepository.countBySiteUserId(siteUser.getId());
List<MentorApplication> mentorApplications = mentorApplicationRepository.findTop5BySiteUserIdOrderByCreatedAtDesc(siteUser.getId());
return IntStream.range(0, mentorApplications.size())
.mapToObj(index -> {
MentorApplication app = mentorApplications.get(index);
return new MentorApplicationHistoryResponse(
app.getId(),
app.getMentorApplicationStatus(),
app.getRejectedReason(),
app.getCreatedAt(),
(int) totalCount - index
);
}).toList();
}
}