Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create user email sender #51

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void sendEmailFromTemplate(UserDTO user, String templateName, String titl
LOG.debug("Email doesn't exist for user '{}'", user);
return;
}
Locale locale = Locale.forLanguageTag(user.getLangKey());
Locale locale = Locale.forLanguageTag(user.getLangKey() != null ? user.getLangKey() : "en");
Context context = new Context(locale);
context.setVariable(USER, user);
context.setVariable(BASE_URL, flowInquiryProperties.getMail().getBaseUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -165,4 +166,11 @@ public LocalDateTime getLastLoginTime() {
(timezone != null) ? ZoneId.of(timezone) : ZoneId.of("America/Los_Angeles");
return lastLoginTime.atZone(ZoneOffset.UTC).withZoneSameInstant(userZone).toLocalDateTime();
}

@PrePersist
public void prePersist() {
if (isDeleted == null) {
isDeleted = Boolean.FALSE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import io.flowinquiry.modules.usermanagement.service.dto.UserDTO;
import io.flowinquiry.modules.usermanagement.service.dto.UserHierarchyDTO;
import io.flowinquiry.modules.usermanagement.service.dto.UserKey;
import io.flowinquiry.modules.usermanagement.service.event.CreatedUserEvent;
import io.flowinquiry.modules.usermanagement.service.event.DeleteUserEvent;
import io.flowinquiry.modules.usermanagement.service.mapper.UserMapper;
import io.flowinquiry.query.QueryDTO;
import io.flowinquiry.security.Constants;
import io.flowinquiry.security.SecurityUtils;
import io.flowinquiry.utils.Random;
import jakarta.persistence.EntityNotFoundException;
Expand Down Expand Up @@ -95,6 +95,7 @@ public Optional<User> completePasswordReset(String newPassword, String key) {
.map(
user -> {
user.setPassword(passwordEncoder.encode(newPassword));
user.setStatus(UserStatus.ACTIVE);
user.setResetKey(null);
user.setResetDate(null);
return user;
Expand Down Expand Up @@ -157,21 +158,7 @@ private boolean removeNonActivatedUser(User existingUser) {
}

public UserDTO createUser(UserDTO userDTO) {
User user = new User();
user.setFirstName(userDTO.getFirstName());
user.setLastName(userDTO.getLastName());
if (userDTO.getManagerId() != null) {
user.setManager(User.builder().id(userDTO.getManagerId()).build());
}
if (userDTO.getEmail() != null) {
user.setEmail(userDTO.getEmail().toLowerCase());
}
user.setImageUrl(userDTO.getImageUrl());
if (userDTO.getLangKey() == null) {
user.setLangKey(Constants.DEFAULT_LANGUAGE); // default language
} else {
user.setLangKey(userDTO.getLangKey());
}
User user = userMapper.toEntity(userDTO);
String encryptedPassword = passwordEncoder.encode(Random.generatePassword());
user.setPassword(encryptedPassword);
user.setResetKey(Random.generateResetKey());
Expand All @@ -193,7 +180,9 @@ public UserDTO createUser(UserDTO userDTO) {
}
userRepository.save(user);
LOG.debug("Created Information for User: {}", user);
return userMapper.toDto(user);
UserDTO savedUser = userMapper.toDto(user);
eventPublisher.publishEvent(new CreatedUserEvent(this, savedUser));
return savedUser;
}

/**
Expand Down Expand Up @@ -364,7 +353,6 @@ public UserHierarchyDTO getOrgChart() {
return rootUser;
}

// Handle case with no users
return null;
}

Expand All @@ -380,9 +368,7 @@ public UserHierarchyDTO getUserHierarchyWithSubordinates(Long userId) {

// Fetch subordinates
List<UserHierarchyDTO> subordinates = userRepository.findAllSubordinates(userId);

userHierarchy.setSubordinates(subordinates);

return userHierarchy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.flowinquiry.modules.usermanagement.service.event;

import io.flowinquiry.modules.usermanagement.service.dto.UserDTO;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class CreatedUserEvent extends ApplicationEvent {
private UserDTO user;

public CreatedUserEvent(Object source, UserDTO user) {
super(source);
this.user = user;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.flowinquiry.modules.usermanagement.service.listener;

import io.flowinquiry.modules.collab.service.MailService;
import io.flowinquiry.modules.usermanagement.service.event.CreatedUserEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class CreatedUserEventListener {

private final MailService mailService;

public CreatedUserEventListener(MailService mailService) {
this.mailService = mailService;
}

@Async("auditLogExecutor")
@Transactional
@EventListener
public void onCreatedUserEvent(CreatedUserEvent event) {
mailService.sendCreationEmail(event.getUser());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.flowinquiry.modules.usermanagement.web.rest;

import io.flowinquiry.modules.collab.service.MailService;
import io.flowinquiry.modules.fss.service.StorageService;
import io.flowinquiry.modules.usermanagement.AuthoritiesConstants;
import io.flowinquiry.modules.usermanagement.domain.User;
Expand Down Expand Up @@ -57,17 +56,12 @@ public class PublicUserController {
private final UserRepository userRepository;
private final UserService userService;
private final StorageService storageService;
private final MailService mailService;

public PublicUserController(
UserService userService,
UserRepository userRepository,
StorageService storageService,
MailService mailService) {
UserService userService, UserRepository userRepository, StorageService storageService) {
this.userService = userService;
this.userRepository = userRepository;
this.storageService = storageService;
this.mailService = mailService;
}

/**
Expand Down Expand Up @@ -196,7 +190,6 @@ public ResponseEntity<UserDTO> createUser(@Valid @RequestBody UserDTO userDTO)
throw new EmailAlreadyUsedException();
} else {
UserDTO newUser = userService.createUser(userDTO);
mailService.sendCreationEmail(newUser);
return ResponseEntity.created(new URI("/api/users/" + newUser.getEmail()))
.body(newUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
defaultValue="PENDING">
<constraints nullable="false" />
</column>
<column name="is_deleted" type="BOOLEAN" valueBoolean="false">
<column name="is_deleted" type="BOOLEAN"
defaultValueBoolean="false">
<constraints nullable="false" />
</column>
<column name="lang_key" type="VARCHAR(10)" />
Expand Down
Loading