1
|
|
|
package com.osomapps.pt.user; |
2
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
4
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
5
|
|
|
import com.osomapps.pt.UnauthorizedException; |
6
|
|
|
import com.osomapps.pt.admin.program.AdminProgramAssignService; |
7
|
|
|
import com.osomapps.pt.goals.Goal; |
8
|
|
|
import com.osomapps.pt.goals.GoalRepository; |
9
|
|
|
import com.osomapps.pt.goals.InUserGoalRepository; |
10
|
|
|
import com.osomapps.pt.token.InUser; |
11
|
|
|
import com.osomapps.pt.token.InUserGoal; |
12
|
|
|
import com.osomapps.pt.token.InUserLogin; |
13
|
|
|
import com.osomapps.pt.token.InUserLoginRepository; |
14
|
|
|
import com.osomapps.pt.token.InUserLogoutRepository; |
15
|
|
|
import com.osomapps.pt.token.InUserRepository; |
16
|
|
|
import com.osomapps.pt.tokenemail.DataurlValidator; |
17
|
|
|
import com.osomapps.pt.tokenemail.NameValidator; |
18
|
|
|
import com.osomapps.pt.tokenemail.SendEmailService; |
19
|
|
|
import java.io.IOException; |
20
|
|
|
import java.time.LocalDateTime; |
21
|
|
|
import java.util.ArrayList; |
22
|
|
|
import java.util.HashMap; |
23
|
|
|
import java.util.List; |
24
|
|
|
import java.util.Map; |
25
|
|
|
import java.util.Optional; |
26
|
|
|
import java.util.stream.Collectors; |
27
|
|
|
import org.springframework.stereotype.Service; |
28
|
|
|
import org.springframework.validation.MapBindingResult; |
29
|
|
|
|
30
|
|
|
@Service |
31
|
|
|
public class UserService { |
32
|
|
|
private final InUserRepository inUserRepository; |
33
|
|
|
private final InUserLoginRepository inUserLoginRepository; |
34
|
|
|
private final InUserLogoutRepository inUserLogoutRepository; |
35
|
|
|
private final GoalRepository goalRepository; |
36
|
|
|
private final InUserGoalRepository inUserGoalRepository; |
37
|
|
|
private final DataurlValidator dataurlValidator; |
38
|
|
|
private final NameValidator nameValidator; |
39
|
|
|
private final SendEmailService sendEmailService; |
40
|
|
|
private final AdminProgramAssignService adminProgramAssignService; |
41
|
|
|
|
42
|
|
|
UserService( |
|
|
|
|
43
|
|
|
InUserRepository inUserRepository, |
44
|
|
|
InUserLoginRepository inUserLoginRepository, |
45
|
|
|
InUserLogoutRepository inUserLogoutRepository, |
46
|
|
|
GoalRepository goalRepository, |
47
|
|
|
InUserGoalRepository inUserGoalRepository, |
48
|
|
|
DataurlValidator dataurlValidator, |
49
|
|
|
NameValidator nameValidator, |
50
|
|
|
SendEmailService sendEmailService, |
51
|
|
|
AdminProgramAssignService adminProgramAssignService) { |
52
|
|
|
this.inUserRepository = inUserRepository; |
53
|
|
|
this.inUserLoginRepository = inUserLoginRepository; |
54
|
|
|
this.inUserLogoutRepository = inUserLogoutRepository; |
55
|
|
|
this.goalRepository = goalRepository; |
56
|
|
|
this.inUserGoalRepository = inUserGoalRepository; |
57
|
|
|
this.dataurlValidator = dataurlValidator; |
58
|
|
|
this.nameValidator = nameValidator; |
59
|
|
|
this.sendEmailService = sendEmailService; |
60
|
|
|
this.adminProgramAssignService = adminProgramAssignService; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public InUserLogin checkUserToken(String token) { |
64
|
|
|
final List<InUserLogin> inUserLogins = inUserLoginRepository.findByToken(token); |
65
|
|
|
if (inUserLogins.isEmpty()) { |
66
|
|
|
throw new UnauthorizedException("Token not found"); |
67
|
|
|
} else { |
68
|
|
|
if (!inUserLogoutRepository.findByToken(token).isEmpty()) { |
69
|
|
|
throw new UnauthorizedException("Invalid token"); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return inUserLogins.get(inUserLogins.size() - 1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private Optional<String> getUserName(InUser inUser) { |
76
|
|
|
final Optional<String> userName; |
77
|
|
|
if (inUser.getInUserFacebooks() == null || inUser.getInUserFacebooks().isEmpty()) { |
78
|
|
|
if (inUser.getInUserEmails() == null || inUser.getInUserEmails().isEmpty()) { |
79
|
|
|
userName = Optional.empty(); |
80
|
|
|
} else { |
81
|
|
|
userName = |
82
|
|
|
Optional.of( |
83
|
|
|
inUser.getInUserEmails() |
84
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
85
|
|
|
.getUser_name()); |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
userName = |
89
|
|
|
Optional.ofNullable( |
90
|
|
|
inUser.getInUserFacebooks() |
91
|
|
|
.get(inUser.getInUserFacebooks().size() - 1) |
92
|
|
|
.getUser_name()); |
93
|
|
|
} |
94
|
|
|
return userName; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private void setUserName(InUser inUser, String userName) { |
98
|
|
|
if (inUser.getInUserFacebooks() == null || inUser.getInUserFacebooks().isEmpty()) { |
99
|
|
|
if (inUser.getInUserEmails() != null && !inUser.getInUserEmails().isEmpty()) { |
100
|
|
|
String prevUserName = |
101
|
|
|
inUser.getInUserEmails() |
102
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
103
|
|
|
.getUser_name(); |
104
|
|
|
inUser.getInUserEmails() |
105
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
106
|
|
|
.setUser_name(userName); |
107
|
|
|
if (prevUserName == null) { |
108
|
|
|
new Thread( |
109
|
|
|
() -> { |
|
|
|
|
110
|
|
|
sendEmailService.send( |
111
|
|
|
inUser.getInUserEmails() |
112
|
|
|
.get(inUser.getInUserEmails().size() - 1)); |
113
|
|
|
}, |
114
|
|
|
"Send-email") |
115
|
|
|
.start(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
|
|
inUser.getInUserFacebooks() |
120
|
|
|
.get(inUser.getInUserFacebooks().size() - 1) |
121
|
|
|
.setUser_name(userName); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private Optional<String> getAvatar(InUser inUser) { |
126
|
|
|
if (inUser.getInUserFacebooks() == null || inUser.getInUserFacebooks().isEmpty()) { |
127
|
|
|
return Optional.empty(); |
128
|
|
|
} |
129
|
|
|
return Optional.ofNullable( |
130
|
|
|
inUser.getInUserFacebooks() |
131
|
|
|
.get(inUser.getInUserFacebooks().size() - 1) |
132
|
|
|
.getPicture_url()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private Optional<String> getEmail(InUser inUser) { |
136
|
|
|
if (inUser.getInUserEmails() == null || inUser.getInUserEmails().isEmpty()) { |
137
|
|
|
return Optional.empty(); |
138
|
|
|
} |
139
|
|
|
return Optional.of( |
140
|
|
|
inUser.getInUserEmails().get(inUser.getInUserEmails().size() - 1).getLogin()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
UserResponseDTO findOne(String token) { |
144
|
|
|
final InUser inUser = checkUserToken(token).getInUser(); |
145
|
|
|
return userToDto(inUser); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private UserResponseDTO userToDto(final InUser inUser) { |
149
|
|
|
final UserResponseDTO userResponse = new UserResponseDTO(); |
150
|
|
|
userResponse.setId(inUser.getId()); |
151
|
|
|
userResponse.setGender(inUser.getD_sex()); |
152
|
|
|
if (inUser.getAge() != null) { |
153
|
|
|
userResponse.setAge(inUser.getAge().longValue()); |
154
|
|
|
} |
155
|
|
|
if (inUser.getHeight() != null) { |
156
|
|
|
userResponse.setHeight(inUser.getHeight().longValue()); |
157
|
|
|
} |
158
|
|
|
if (inUser.getWeight() != null) { |
159
|
|
|
userResponse.setWeight(inUser.getWeight().longValue()); |
160
|
|
|
} |
161
|
|
|
if (inUser.getD_level() != null) { |
162
|
|
|
userResponse.setLevel(UserLevel.of(Integer.parseInt(inUser.getD_level()))); |
163
|
|
|
} |
164
|
|
|
userResponse.setGoals( |
165
|
|
|
inUser.getInUserGoals().stream() |
166
|
|
|
.map( |
167
|
|
|
inUserGoal -> { |
168
|
|
|
Map<String, Integer> map = null; |
169
|
|
|
try { |
170
|
|
|
map = |
171
|
|
|
inUserGoal.getGoal_value() == null |
172
|
|
|
? null |
173
|
|
|
: new ObjectMapper() |
174
|
|
|
.readValue( |
175
|
|
|
inUserGoal.getGoal_value(), |
176
|
|
|
new TypeReference< |
177
|
|
|
Map< |
178
|
|
|
String, |
179
|
|
|
Integer>>() {}); |
180
|
|
|
} catch (IOException ex) { |
|
|
|
|
181
|
|
|
} |
182
|
|
|
return new UserGoalResponseDTO() |
183
|
|
|
.setId(inUserGoal.getGoalId()) |
184
|
|
|
.setValues(map); |
185
|
|
|
}) |
186
|
|
|
.collect(Collectors.toList())); |
187
|
|
|
userResponse.setAvatar_dataurl(inUser.getAvatar_dataurl()); |
188
|
|
|
userResponse.setName(getUserName(inUser).orElse("?")); |
189
|
|
|
userResponse.setAvatar(getAvatar(inUser).orElse(null)); |
190
|
|
|
userResponse.setEmail(getEmail(inUser).orElse(null)); |
191
|
|
|
userResponse.setBirthday(inUser.getBirthday()); |
192
|
|
|
return userResponse; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
UserResponseDTO updateUser(String token, UserRequestDTO userRequest) { |
196
|
|
|
final InUserLogin inUserLogin = checkUserToken(token); |
197
|
|
|
final InUser inUser = inUserLogin.getInUser(); |
198
|
|
|
if (userRequest.getGender() != null) { |
199
|
|
|
inUser.setD_sex(userRequest.getGender()); |
200
|
|
|
} |
201
|
|
|
if (userRequest.getAge() != null) { |
202
|
|
|
inUser.setAge(userRequest.getAge().floatValue()); |
203
|
|
|
} |
204
|
|
|
if (userRequest.getHeight() != null) { |
205
|
|
|
inUser.setHeight(userRequest.getHeight().floatValue()); |
206
|
|
|
} |
207
|
|
|
if (userRequest.getWeight() != null) { |
208
|
|
|
inUser.setWeight(userRequest.getWeight().floatValue()); |
209
|
|
|
} |
210
|
|
|
if (userRequest.getLevel() != null) { |
211
|
|
|
inUser.setD_level(Integer.toString(userRequest.getLevel().getLevel())); |
212
|
|
|
} |
213
|
|
|
final MapBindingResult errors = |
214
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
215
|
|
|
dataurlValidator.validate(userRequest.getAvatar_dataurl(), errors); |
216
|
|
|
if (errors.hasErrors()) { |
217
|
|
|
throw new UnauthorizedException(errors.getAllErrors().get(0).getDefaultMessage()); |
218
|
|
|
} |
219
|
|
|
if (userRequest.getName() != null) { |
220
|
|
|
final MapBindingResult errorsName = |
221
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
222
|
|
|
nameValidator.validate(userRequest.getName(), errorsName); |
223
|
|
|
if (errorsName.hasErrors()) { |
224
|
|
|
throw new UnauthorizedException( |
225
|
|
|
errorsName.getAllErrors().get(0).getDefaultMessage()); |
226
|
|
|
} |
227
|
|
|
setUserName(inUser, userRequest.getName()); |
228
|
|
|
} |
229
|
|
View Code Duplication |
if (userRequest.getGoals() != null) { |
|
|
|
|
230
|
|
|
if (userRequest.getGoals().size() > 2) { |
231
|
|
|
throw new UnauthorizedException("Amount of goals must be not more than 2"); |
232
|
|
|
} |
233
|
|
|
inUserGoalRepository.deleteAll(inUser.getInUserGoals()); |
234
|
|
|
inUser.setInUserGoals(new ArrayList<>()); |
235
|
|
|
userRequest |
236
|
|
|
.getGoals() |
237
|
|
|
.forEach( |
238
|
|
|
(userGoalRequestDTO) -> { |
239
|
|
|
Goal goal = |
240
|
|
|
goalRepository |
241
|
|
|
.findById(userGoalRequestDTO.getId()) |
242
|
|
|
.orElse(null); |
243
|
|
|
if (goal == null) { |
244
|
|
|
throw new UnauthorizedException( |
245
|
|
|
"Goal with id " |
246
|
|
|
+ userGoalRequestDTO.getId() |
247
|
|
|
+ " not found"); |
248
|
|
|
} |
249
|
|
|
String value = null; |
250
|
|
|
try { |
251
|
|
|
value = |
252
|
|
|
new ObjectMapper() |
253
|
|
|
.writeValueAsString( |
254
|
|
|
userGoalRequestDTO.getValues()); |
255
|
|
|
} catch (IOException ex) { |
|
|
|
|
256
|
|
|
} |
257
|
|
|
inUser.getInUserGoals() |
258
|
|
|
.add( |
259
|
|
|
inUserGoalRepository.save( |
260
|
|
|
new InUserGoal() |
261
|
|
|
.setGoalId( |
262
|
|
|
userGoalRequestDTO.getId()) |
263
|
|
|
.setD_goal_title( |
264
|
|
|
goal.getDGoalTitle()) |
265
|
|
|
.setD_goal_title_2( |
266
|
|
|
goal.getDGoalTitle2()) |
267
|
|
|
.setGoal_value(value))); |
268
|
|
|
}); |
269
|
|
|
} |
270
|
|
|
inUser.setAvatar_dataurl(userRequest.getAvatar_dataurl()); |
271
|
|
|
inUser.setUpdated(LocalDateTime.now()); |
272
|
|
|
return userToDto(inUserRepository.save(adminProgramAssignService.assign(inUser))); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|