feat: soft delete for interests and roles
A super admin can delete an interest or a role. Deleted rows leave every list, picker and filter, keep their names on existing leads, and keep their names reserved. A role held by any user, and the protected roles, cannot be deleted. Deletes are audited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0f83e9cc08
commit
0b201a51ca
25 files changed
+765
-9
No files matched your search
+5
@@ -24,6 +24,11 @@ public class InterestController {
|
||||
return interestService.save(interest);
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public void delete(@RequestBody Long id) {
|
||||
interestService.delete(id);
|
||||
}
|
||||
|
||||
@PostMapping("/findByIds")
|
||||
public List<Interest> findByIds(@RequestBody InterestFindByIdsRequest body) {
|
||||
return interestService.findByIds(body.companyId(), body.ids());
|
||||
|
||||
+5
@@ -4,6 +4,8 @@ import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@@ -21,4 +23,7 @@ public class Interest {
|
||||
private boolean active = true;
|
||||
|
||||
private int sortOrder;
|
||||
|
||||
/** Soft delete: the row stays so tagged leads keep resolving the name, and the name stays reserved. */
|
||||
private LocalDateTime deletedAt;
|
||||
}
|
||||
+2
@@ -13,5 +13,7 @@ public interface InterestRepository extends JpaRepository<Interest, Long> {
|
||||
|
||||
boolean existsByNameIgnoreCase(String name);
|
||||
|
||||
boolean existsByNameIgnoreCaseAndDeletedAtIsNotNull(String name);
|
||||
|
||||
List<Interest> findByIdIn(Collection<Long> ids);
|
||||
}
|
||||
+18
-2
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -16,6 +17,7 @@ import java.util.List;
|
||||
public class InterestService {
|
||||
private final InterestRepository interestRepository;
|
||||
|
||||
/** Every row, deleted ones included (flagged by deletedAt): lead views resolve a deleted interest's name through this list. Consumers filter what they offer. */
|
||||
public List<Interest> findByCompany(long companyId) {
|
||||
return interestRepository.findAllByOrderBySortOrderAscNameAsc();
|
||||
}
|
||||
@@ -39,7 +41,7 @@ public class InterestService {
|
||||
// Create: names are unique across the one shared list
|
||||
interest.setCompanyId(null);
|
||||
if (interestRepository.existsByNameIgnoreCase(trimmedName)) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
throw nameTaken(trimmedName);
|
||||
}
|
||||
return interestRepository.saveAndFlush(interest);
|
||||
} else {
|
||||
@@ -48,7 +50,7 @@ public class InterestService {
|
||||
interest.setCompanyId(existing.getCompanyId());
|
||||
if (!existing.getName().equalsIgnoreCase(trimmedName)
|
||||
&& interestRepository.existsByNameIgnoreCase(trimmedName)) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
throw nameTaken(trimmedName);
|
||||
}
|
||||
return interestRepository.saveAndFlush(interest);
|
||||
}
|
||||
@@ -57,6 +59,20 @@ public class InterestService {
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Long id) {
|
||||
Interest interest = interestRepository.findById(id)
|
||||
.filter(i -> i.getDeletedAt() == null)
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||
interest.setDeletedAt(LocalDateTime.now());
|
||||
interestRepository.save(interest);
|
||||
}
|
||||
|
||||
private ResponseStatusException nameTaken(String name) {
|
||||
return new ResponseStatusException(HttpStatus.CONFLICT,
|
||||
interestRepository.existsByNameIgnoreCaseAndDeletedAtIsNotNull(name) ? "INTEREST_NAME_DELETED" : "DUPLICATE_NAME");
|
||||
}
|
||||
|
||||
public List<Interest> findByIds(long companyId, List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return List.of();
|
||||
|
||||
Reference in new issue
Block a user