Skip to content

Commit

Permalink
Create user email sender (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
haiphucnguyen authored Jan 4, 2025
1 parent dad9e96 commit 2dfc5cb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 30 deletions.
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

0 comments on commit 2dfc5cb

Please sign in to comment.