init: SR System — Helpdesk & ITAM Portal
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
package com.srsystem.service;
|
||||
|
||||
import com.srsystem.dto.ChangePasswordRequest;
|
||||
import com.srsystem.dto.EmployeeSearchResult;
|
||||
import com.srsystem.dto.EmployeeSummary;
|
||||
import com.srsystem.entity.Employee;
|
||||
import com.srsystem.entity.enums.EmpStatus;
|
||||
import com.srsystem.exception.BusinessException;
|
||||
import com.srsystem.exception.ErrorCode;
|
||||
import com.srsystem.repository.DispatchHistoryRepository;
|
||||
import com.srsystem.repository.EmployeeRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class EmployeeService {
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final DispatchHistoryRepository dispatchHistoryRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public Page<Employee> findAll(EmpStatus status, Pageable pageable) {
|
||||
if (status != null) {
|
||||
return employeeRepository.findByEmpStatus(status, pageable);
|
||||
}
|
||||
return employeeRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
public Employee findById(Long empId) {
|
||||
return employeeRepository.findById(empId)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.EMPLOYEE_NOT_FOUND));
|
||||
}
|
||||
|
||||
public Employee findByLoginId(String loginId) {
|
||||
return employeeRepository.findByLoginId(loginId)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.EMPLOYEE_NOT_FOUND));
|
||||
}
|
||||
|
||||
public List<EmployeeSearchResult> searchByName(String keyword) {
|
||||
List<Employee> byName = employeeRepository.findByNameContainingIgnoreCase(keyword);
|
||||
List<EmployeeSearchResult> results = new ArrayList<>();
|
||||
Set<Long> seen = new HashSet<>();
|
||||
for (Employee e : byName) {
|
||||
if (seen.add(e.getEmpId())) {
|
||||
results.add(toSearchResult(e));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private EmployeeSearchResult toSearchResult(Employee e) {
|
||||
return new EmployeeSearchResult(
|
||||
e.getEmpId(),
|
||||
e.getName(),
|
||||
e.getEmail(),
|
||||
e.getDispatchStatus() != null ? e.getDispatchStatus().name() : null
|
||||
);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Employee create(Employee employee) {
|
||||
if (employeeRepository.findByLoginId(employee.getLoginId()).isPresent()) {
|
||||
throw new BusinessException(ErrorCode.DUPLICATE_LOGIN_ID);
|
||||
}
|
||||
|
||||
employee.setPassword(passwordEncoder.encode(employee.getPassword()));
|
||||
return employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Employee update(Long empId, Employee updateRequest) {
|
||||
Employee employee = findById(empId);
|
||||
|
||||
if (employee.getEmpStatus() == EmpStatus.RESIGNED) {
|
||||
throw new BusinessException(ErrorCode.RESIGNED_EMPLOYEE);
|
||||
}
|
||||
|
||||
if (!employee.getLoginId().equals(updateRequest.getLoginId())) {
|
||||
if (employeeRepository.findByLoginId(updateRequest.getLoginId()).isPresent()) {
|
||||
throw new BusinessException(ErrorCode.DUPLICATE_LOGIN_ID);
|
||||
}
|
||||
employee.setLoginId(updateRequest.getLoginId());
|
||||
}
|
||||
|
||||
employee.setName(updateRequest.getName());
|
||||
employee.setEmail(updateRequest.getEmail());
|
||||
employee.setPhone(updateRequest.getPhone());
|
||||
employee.setRoleCode(updateRequest.getRoleCode());
|
||||
employee.setEmpStatus(updateRequest.getEmpStatus());
|
||||
employee.setDispatchStatus(updateRequest.getDispatchStatus());
|
||||
employee.setJoinedDate(updateRequest.getJoinedDate());
|
||||
employee.setLeftDate(updateRequest.getLeftDate());
|
||||
|
||||
if (updateRequest.getPassword() != null && !updateRequest.getPassword().isBlank()) {
|
||||
employee.setPassword(passwordEncoder.encode(updateRequest.getPassword()));
|
||||
}
|
||||
|
||||
return employee;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Long empId) {
|
||||
Employee employee = findById(empId);
|
||||
|
||||
if (employee.getEmpStatus() == EmpStatus.RESIGNED) {
|
||||
throw new BusinessException(ErrorCode.RESIGNED_EMPLOYEE);
|
||||
}
|
||||
|
||||
employeeRepository.delete(employee);
|
||||
}
|
||||
|
||||
public EmployeeSummary toSummary(Employee employee) {
|
||||
return new EmployeeSummary(
|
||||
employee.getEmpId(),
|
||||
employee.getName(),
|
||||
employee.getEmail(),
|
||||
employee.getPhone(),
|
||||
employee.getRoleCode(),
|
||||
employee.getEmpStatus(),
|
||||
employee.getDispatchStatus(),
|
||||
employee.getJoinedDate(),
|
||||
employee.getLeftDate()
|
||||
);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void changePassword(Long empId, ChangePasswordRequest request) {
|
||||
Employee employee = findById(empId);
|
||||
if (!passwordEncoder.matches(request.currentPassword(), employee.getPassword())) {
|
||||
throw new BusinessException(ErrorCode.AUTH_INVALID_CREDENTIALS);
|
||||
}
|
||||
employee.setPassword(passwordEncoder.encode(request.newPassword()));
|
||||
employeeRepository.save(employee);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user