| 42 | {{{ |
| 43 | @Override |
| 44 | @Transactional |
| 45 | public void createLog(Integer userId, LogType type) { |
| 46 | if (type == null) { |
| 47 | throw new IllegalArgumentException("Log type must not be null"); |
| 48 | } |
| 49 | |
| 50 | UserProfile profile = profileRepository |
| 51 | .findById(userId).orElseThrow(() -> new NoExistingCredentialsException("User does not exist")); |
| 52 | |
| 53 | String fullName = buildFullName(profile); |
| 54 | |
| 55 | String description = switch (type) { |
| 56 | case LOGIN -> "User %s logged in successfully.".formatted(fullName); |
| 57 | case REGISTRATION -> "User %s registered successfully.".formatted(fullName); |
| 58 | case LOGOUT -> "User %s logged out successfully.".formatted(fullName); |
| 59 | case CHANGE_ROLE -> "The role of user %s was changed successfully.".formatted(fullName); |
| 60 | case CHANGE_PASSWORD -> "User %s changed the password successfully.".formatted(fullName); |
| 61 | }; |
| 62 | |
| 63 | UserProfileLog log = new UserProfileLog(); |
| 64 | log.setUserProfile(profile); |
| 65 | log.setChangeDescription(description); |
| 66 | log.setChangedAt(LocalDateTime.now()); |
| 67 | userProfileLogRepository.save(log); |
| 68 | } |
| 69 | }}} |