-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserCommandService.java
More file actions
402 lines (338 loc) · 16.6 KB
/
UserCommandService.java
File metadata and controls
402 lines (338 loc) · 16.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.kustacks.kuring.user.application.service;
import com.kustacks.kuring.auth.token.JwtTokenProvider;
import com.kustacks.kuring.common.annotation.UseCase;
import com.kustacks.kuring.common.exception.InvalidStateException;
import com.kustacks.kuring.common.exception.NotFoundException;
import com.kustacks.kuring.common.exception.code.ErrorCode;
import com.kustacks.kuring.common.properties.ServerProperties;
import com.kustacks.kuring.common.utils.generator.NicknameGenerator;
import com.kustacks.kuring.message.application.service.exception.FirebaseSubscribeException;
import com.kustacks.kuring.message.application.service.exception.FirebaseUnSubscribeException;
import com.kustacks.kuring.notice.domain.CategoryName;
import com.kustacks.kuring.notice.domain.DepartmentName;
import com.kustacks.kuring.user.application.port.in.UserCommandUseCase;
import com.kustacks.kuring.user.application.port.in.dto.UserAcademicEventNotificationCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserBookmarkCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserCategoriesSubscribeCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserDecreaseQuestionCountCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserDepartmentsSubscribeCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserFeedbackCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserLoginCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserLoginResult;
import com.kustacks.kuring.user.application.port.in.dto.UserLogoutCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserPasswordModifyCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserSignupCommand;
import com.kustacks.kuring.user.application.port.in.dto.UserSubscribeCompareResult;
import com.kustacks.kuring.user.application.port.in.dto.UserWithdrawCommand;
import com.kustacks.kuring.user.application.port.out.RootUserCommandPort;
import com.kustacks.kuring.user.application.port.out.RootUserQueryPort;
import com.kustacks.kuring.user.application.port.out.UserCommandPort;
import com.kustacks.kuring.user.application.port.out.UserEventPort;
import com.kustacks.kuring.user.application.port.out.UserQueryPort;
import com.kustacks.kuring.user.domain.RootUser;
import com.kustacks.kuring.user.domain.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static com.kustacks.kuring.message.application.service.FirebaseSubscribeService.ALL_DEVICE_SUBSCRIBED_TOPIC;
import static com.kustacks.kuring.user.domain.RootUser.ROOT_USER_EXTRA_QUESTION_COUNT;
@Slf4j
@UseCase
@Transactional
@RequiredArgsConstructor
class UserCommandService implements UserCommandUseCase {
private final UserCommandPort userCommandPort;
private final UserQueryPort userQueryPort;
private final RootUserCommandPort rootUserCommandPort;
private final RootUserQueryPort rootUserQueryPort;
private final UserEventPort userEventPort;
private final ServerProperties serverProperties;
private final PasswordEncoder passwordEncoder;
private final JwtTokenProvider jwtTokenProvider;
@Override
public void editSubscribeCategories(UserCategoriesSubscribeCommand command) {
UserSubscribeCompareResult<CategoryName> compareResults =
this.editSubscribeCategoryList(command.userToken(), command.categories());
editUserCategoryList(
command.userToken(),
compareResults.savedNameList(),
compareResults.deletedNameList()
);
}
@Override
public void editSubscribeDepartments(UserDepartmentsSubscribeCommand command) {
UserSubscribeCompareResult<DepartmentName> compareResults
= this.editSubscribeDepartmentList(command.userToken(), command.departments());
editDepartmentNameList(
command.userToken(),
compareResults.savedNameList(),
compareResults.deletedNameList()
);
}
@Override
public void updateAcademicEventNotification(UserAcademicEventNotificationCommand command) {
User user = findUserByToken(command.userToken());
user.updateAcademicNotificationEnabled(command.enabled());
}
@Override
public void saveFeedback(UserFeedbackCommand command) {
User findUser = findUserByToken(command.userToken());
findUser.addFeedback(command.content());
}
@Override
public void saveBookmark(UserBookmarkCommand command) {
User user = findUserByToken(command.userToken());
user.addBookmark(command.articleId());
}
@Override
public void decreaseQuestionCount(UserDecreaseQuestionCountCommand command) {
User findUser = findUserByToken(command.userId());
try {
checkUserLoginIdAndDecreaseCount(findUser, command.email());
} catch (IllegalStateException e) {
throw new InvalidStateException(ErrorCode.QUESTION_COUNT_NOT_ENOUGH);
}
}
@Override
public void signupUser(UserSignupCommand userSignupCommand) {
String nickname = createNickname();
RootUser rootUser = new RootUser(
userSignupCommand.email(),
passwordEncoder.encode(userSignupCommand.password()),
nickname
);
rootUserQueryPort.findDeletedRootUserByEmail(userSignupCommand.email())
.ifPresentOrElse(
deletedRootUser -> {
deletedRootUser.reactive(rootUser.getPassword());
log.info("[RootUser name : {}] 재가입 완료", rootUser.getNickname());
},
() -> {
checkDuplicateEmailAndSave(rootUser);
log.info("[RootUser name : {}] 가입 완료", rootUser.getNickname());
}
);
}
@Override
public UserLoginResult login(UserLoginCommand userLoginCommand) {
User user = findUserByToken(userLoginCommand.fcmToken());
RootUser rootUser = findRootUserByEmailAndPasswordOrThrow(userLoginCommand.email(), userLoginCommand.password());
checkUserIsNotLoggedIn(user);
syncQuestionCount(rootUser, user);
user.login(rootUser.getId());
String token = jwtTokenProvider.createUserToken(userLoginCommand.email());
return new UserLoginResult(token);
}
@Override
public void withdraw(UserWithdrawCommand userWithdrawCommand) {
RootUser rootUser = findRootUserByEmailOrThrow(userWithdrawCommand.email());
//회원탈퇴 하기 전에 로그인되어 있는 모든 기기들 로그아웃 처리
logoutAllLoggedInUser(rootUser);
rootUserCommandPort.deleteRootUser(rootUser);
log.info("[RootUserId : {}] 삭제 완료", rootUser.getId());
}
@Override
public void changePassword(UserPasswordModifyCommand userPasswordModifyCommand) {
RootUser rootUser = findRootUserByEmailOrThrow(userPasswordModifyCommand.email());
rootUser.modifyPassword(passwordEncoder.encode(userPasswordModifyCommand.password()));
}
@Override
public void logout(UserLogoutCommand userLogoutCommand) {
User user = findUserByToken(userLogoutCommand.fcmToken());
RootUser rootUser = findRootUserByEmailOrThrow(userLogoutCommand.email());
checkUserMatchesRootUser(user, rootUser);
syncQuestionCount(rootUser, user);
user.logout();
}
private void checkUserLoginIdAndDecreaseCount(User user, String email) {
rootUserQueryPort.findRootUserByEmail(email).ifPresentOrElse(
rootuser -> {
if (matchLoginUserId(user, rootuser)) {
rootuser.decreaseQuestionCount();
}
},
user::decreaseQuestionCount
);
}
private void logoutAllLoggedInUser(RootUser rootUser) {
userQueryPort.findByLoggedInUserId(rootUser.getId())
.forEach(User::logout);
}
private void checkUserIsNotLoggedIn(User user) {
if (user.isLoggedIn()) {
throw new InvalidStateException(ErrorCode.USER_ALREADY_LOGIN);
}
}
private void checkUserMatchesRootUser(User user, RootUser rootUser) {
if (!matchLoginUserId(user, rootUser)) {
throw new InvalidStateException(ErrorCode.USER_MISMATCH_DEVICE);
}
}
private boolean matchLoginUserId(User user, RootUser rootuser) {
return user.matchLoginUserId(rootuser.getId());
}
private void syncQuestionCount(RootUser rootUser, User tokenUser) {
// minQuestionCount = Min(이메일 계정에 남아있는 횟수 - 이메일 계정 추가 횟수, 기기에 남은 횟수)
int fcmUserQuestionCount = Math.min(rootUser.getQuestionCount() - ROOT_USER_EXTRA_QUESTION_COUNT, tokenUser.getQuestionCount());
int emailUserQuestionCount = fcmUserQuestionCount + ROOT_USER_EXTRA_QUESTION_COUNT;
if (fcmUserQuestionCount < 0) {
fcmUserQuestionCount = 0;
}
if (emailUserQuestionCount < 0) {
emailUserQuestionCount = 0;
}
tokenUser.updateQuestionCount(fcmUserQuestionCount);
rootUser.updateQuestionCount(emailUserQuestionCount);
}
private void checkDuplicateEmailAndSave(RootUser rootUser) {
if (rootUserQueryPort.existRootUserByEmail(rootUser.getEmail())) {
throw new InvalidStateException(ErrorCode.EMAIL_DUPLICATE);
}
rootUserCommandPort.saveRootUser(rootUser);
}
private String createNickname() {
final int BATCH_SIZE = 5;
while (true) {
List<String> candidateNicknames = new ArrayList<>();
for (int i = 0; i < BATCH_SIZE; i++) {
candidateNicknames.add(NicknameGenerator.generateNickname());
}
List<String> usingNicknames = rootUserQueryPort.findUsingNicknamesIn(candidateNicknames);
List<String> availableNicknames = selectAvailableNicknames(candidateNicknames, usingNicknames);
// 사용 가능한 닉네임이 있으면 랜덤으로 하나 선택
if (!availableNicknames.isEmpty()) {
return availableNicknames.get(0);
}
}
}
private List<String> selectAvailableNicknames(List<String> candidateNicknames, List<String> usingNicknames) {
return candidateNicknames.stream()
.filter(candidateNickname -> !usingNicknames.contains(candidateNickname))
.toList();
}
private UserSubscribeCompareResult<CategoryName> editSubscribeCategoryList(
String userToken,
List<String> newCategoryStringNames
) {
User user = findUserByToken(userToken);
List<CategoryName> newCategoryNames = convertToEnumList(newCategoryStringNames);
List<CategoryName> savedCategoryNames = user.filteringNewCategoryName(newCategoryNames);
List<CategoryName> deletedCategoryNames = user.filteringOldCategoryName(newCategoryNames);
return new UserSubscribeCompareResult<>(savedCategoryNames, deletedCategoryNames);
}
private void editUserCategoryList(
String userToken,
List<CategoryName> savedCategoryNames,
List<CategoryName> deletedCategoryNames
) throws FirebaseSubscribeException, FirebaseUnSubscribeException {
subscribeUserCategory(userToken, savedCategoryNames);
unsubscribeUserCategory(userToken, deletedCategoryNames);
}
private UserSubscribeCompareResult<DepartmentName> editSubscribeDepartmentList(
String userToken,
List<String> departments
) {
User user = findUserByToken(userToken);
List<DepartmentName> newDepartmentNames = convertHostPrefixToEnum(departments);
List<DepartmentName> savedDepartmentNames = user.filteringNewDepartmentName(newDepartmentNames);
List<DepartmentName> deletedDepartmentNames = user.filteringOldDepartmentName(newDepartmentNames);
return new UserSubscribeCompareResult<>(savedDepartmentNames, deletedDepartmentNames);
}
private void subscribeUserCategory(
String token,
List<CategoryName> newCategoryNames
) throws FirebaseSubscribeException {
for (CategoryName newCategoryName : newCategoryNames) {
userEventPort.subscribeEvent(token, newCategoryName.getName());
this.subscribeCategory(token, newCategoryName);
log.debug("구독 성공 = {}", newCategoryName.getName());
}
}
private void unsubscribeUserCategory(
String token,
List<CategoryName> removeCategoryNames
) throws FirebaseUnSubscribeException {
for (CategoryName removeCategoryName : removeCategoryNames) {
userEventPort.unsubscribeEvent(token, removeCategoryName.getName());
this.unsubscribeCategory(token, removeCategoryName);
log.debug("구독 취소 = {}", removeCategoryName.getName());
}
}
private void editDepartmentNameList(
String userToken,
List<DepartmentName> savedDepartmentNames,
List<DepartmentName> deletedDepartmentNames
) throws FirebaseSubscribeException, FirebaseUnSubscribeException {
subscribeDepartment(userToken, savedDepartmentNames);
unsubscribeDepartment(userToken, deletedDepartmentNames);
}
private void subscribeCategory(String userToken, CategoryName categoryName) {
User user = findUserByToken(userToken);
user.subscribeCategory(categoryName);
}
private void unsubscribeCategory(String userToken, CategoryName categoryName) {
User user = findUserByToken(userToken);
user.unsubscribeCategory(categoryName);
}
private void subscribeDepartment(
String userToken,
List<DepartmentName> newDepartmentNames
) {
for (DepartmentName newDepartmentName : newDepartmentNames) {
userEventPort.subscribeEvent(userToken, newDepartmentName.getName());
this.subscribeDepartment(userToken, newDepartmentName);
log.debug("구독 성공 = {}", newDepartmentName.getName());
}
}
private void unsubscribeDepartment(
String userToken,
List<DepartmentName> removeDepartmentNames
) {
for (DepartmentName removeDepartmentName : removeDepartmentNames) {
userEventPort.unsubscribeEvent(userToken, removeDepartmentName.getName());
this.unsubscribeDepartment(userToken, removeDepartmentName);
log.debug("구독 취소 = {}", removeDepartmentName.getName());
}
}
private void subscribeDepartment(String userToken, DepartmentName newDepartmentName) {
User user = findUserByToken(userToken);
user.subscribeDepartment(newDepartmentName);
}
private void unsubscribeDepartment(String userToken, DepartmentName removeDepartmentName) {
User user = findUserByToken(userToken);
user.unsubscribeDepartment(removeDepartmentName);
}
private User findUserByToken(String token) {
Optional<User> optionalUser = userQueryPort.findByToken(token);
if (optionalUser.isEmpty()) {
optionalUser = Optional.of(userCommandPort.save(new User(token)));
userEventPort.subscribeEvent(token, serverProperties.ifDevThenAddSuffix(ALL_DEVICE_SUBSCRIBED_TOPIC));
}
return optionalUser.orElseThrow(() -> new NotFoundException(ErrorCode.USER_NOT_FOUND));
}
private List<CategoryName> convertToEnumList(List<String> categories) {
return categories.stream()
.map(CategoryName::fromStringName)
.toList();
}
private List<DepartmentName> convertHostPrefixToEnum(List<String> departments) {
return departments.stream()
.map(DepartmentName::fromHostPrefix)
.toList();
}
private RootUser findRootUserByEmailOrThrow(String email) {
return rootUserQueryPort.findRootUserByEmail(email)
.orElseThrow(() -> new NotFoundException(ErrorCode.ROOT_USER_NOT_FOUND));
}
private RootUser findRootUserByEmailAndPasswordOrThrow(String email, String password) {
RootUser rootUser = findRootUserByEmailOrThrow(email);
if (!passwordEncoder.matches(password, rootUser.getPassword())) {
throw new NotFoundException(ErrorCode.ROOT_USER_MISMATCH_PASSWORD);
}
return rootUser;
}
}