Les groupes n'ont plus de liste d'employés car la récupération d'un groupe depuis la base de donnée causait une boucle infinie.

This commit is contained in:
FyloZ 2021-03-19 22:29:00 -04:00
parent 582236b72e
commit 6ea65f6519
4 changed files with 17 additions and 183 deletions

View File

@ -15,6 +15,7 @@ import javax.persistence.*
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size
import kotlin.jvm.Transient
private const val EMPLOYEE_ID_NULL_MESSAGE = "Un numéro d'employé est requis"
@ -136,14 +137,7 @@ data class EmployeeGroup(
@Column(name = "permission")
@Fetch(FetchMode.SUBSELECT)
val permissions: MutableSet<EmployeePermission> = mutableSetOf(),
@OneToMany(mappedBy = "group")
@field:JsonIgnore
val employees: MutableSet<Employee> = mutableSetOf()
) : NamedModel {
@JsonProperty("employeeCount")
fun getEmployeeCount() = employees.size - 1 // -1 removes the default employee
}
) : NamedModel
open class EmployeeGroupSaveDto(
@field:NotBlank(message = GROUP_NAME_NULL_MESSAGE)
@ -327,9 +321,8 @@ fun employeeGroup(
id: Long? = null,
name: String = "name",
permissions: MutableSet<EmployeePermission> = mutableSetOf(),
employees: MutableSet<Employee> = mutableSetOf(),
op: EmployeeGroup.() -> Unit = {}
) = EmployeeGroup(id, name, permissions, employees).apply(op)
) = EmployeeGroup(id, name, permissions).apply(op)
fun employeeGroupSaveDto(
name: String = "name",

View File

@ -107,18 +107,20 @@ class GroupsController(groupService: EmployeeGroupServiceImpl) :
@PutMapping("{groupId}/{employeeId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun addEmployeeToGroup(@PathVariable groupId: Long, @PathVariable employeeId: Long): ResponseEntity<Void> {
service.addEmployeeToGroup(groupId, employeeId)
return ResponseEntity
.noContent()
.build()
// service.addEmployeeToGroup(groupId, employeeId)
// return ResponseEntity
// .noContent()
// .build()
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() // TODO Go to employee controller
}
@DeleteMapping("{groupId}/{employeeId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun removeEmployeeFromGroup(@PathVariable groupId: Long, @PathVariable employeeId: Long): ResponseEntity<Void> {
service.removeEmployeeFromGroup(groupId, employeeId)
return ResponseEntity
.noContent()
.build()
// service.removeEmployeeFromGroup(groupId, employeeId)
// return ResponseEntity
// .noContent()
// .build()
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build() // TODO Go to employee controller
}
}

View File

@ -66,23 +66,6 @@ interface EmployeeGroupService :
/** Sets the default group cookie for the given HTTP [response]. */
fun setResponseDefaultGroup(groupId: Long, response: HttpServletResponse)
/** Adds the employee with the given [employeeId] to the group with the given [groupId]. */
fun addEmployeeToGroup(groupId: Long, employeeId: Long)
/**
* Adds a given [employee] to a given [group].
*
* If the [employee] is already in the [group], nothing will be done.
* If the [employee] is already in a group, it will be removed from it.
*/
fun addEmployeeToGroup(group: EmployeeGroup, employee: Employee)
/** Removes the employee with the given [employeeId] from the group with the given [groupId]. */
fun removeEmployeeFromGroup(groupId: Long, employeeId: Long)
/** Removes a given [employee] from the given [group]. */
fun removeEmployeeFromGroup(group: EmployeeGroup, employee: Employee)
}
interface EmployeeUserDetailsService : UserDetailsService {
@ -240,7 +223,7 @@ class EmployeeGroupServiceImpl(
EmployeeGroupService {
override fun existsByName(name: String): Boolean = repository.existsByName(name)
override fun getEmployeesForGroup(id: Long): Collection<Employee> =
getById(id).employees
employeeService.getByGroup(getById(id))
@Transactional
override fun save(entity: EmployeeGroup): EmployeeGroup {
@ -255,8 +238,7 @@ class EmployeeGroupServiceImpl(
EmployeeGroup(
entity.id,
if (name.isNotBlank()) entity.name else persistedGroup.name,
if (permissions.isNotEmpty()) entity.permissions else persistedGroup.permissions,
persistedGroup.employees
if (permissions.isNotEmpty()) entity.permissions else persistedGroup.permissions
)
})
}
@ -286,34 +268,6 @@ class EmployeeGroupServiceImpl(
"$defaultGroupCookieName=${defaultGroupUser.id}; Max-Age=${defaultGroupCookieMaxAge}; Path=/api; HttpOnly; Secure; SameSite=strict"
)
}
override fun addEmployeeToGroup(groupId: Long, employeeId: Long) {
addEmployeeToGroup(getById(groupId), employeeService.getById(employeeId))
}
@Transactional
override fun addEmployeeToGroup(group: EmployeeGroup, employee: Employee) {
if (employee.group == group) return
if (employee.group != null) removeEmployeeFromGroup(employee.group!!, employee)
group.employees.add(employee)
employee.group = group
update(group)
employeeService.update(employee)
}
override fun removeEmployeeFromGroup(groupId: Long, employeeId: Long) =
removeEmployeeFromGroup(getById(groupId), employeeService.getById(employeeId))
@Transactional
override fun removeEmployeeFromGroup(group: EmployeeGroup, employee: Employee) {
if (employee.group == null || employee.group != group) return
group.employees.removeIf { it.id == employee.id }
employee.group = null
update(group)
employeeService.update(employee)
}
}
@Service

View File

@ -222,9 +222,10 @@ class EmployeeGroupServiceTest :
@Test
fun `getEmployeesForGroup() returns all employees in the given group`() {
val group = employeeGroup(id = 1L, employees = mutableSetOf(groupEmployee))
val group = employeeGroup(id = 1L)
doReturn(group).whenever(service).getById(group.id!!)
whenever(employeeService.getByGroup(group)).doReturn(listOf(groupEmployee))
val found = service.getEmployeesForGroup(group.id!!)
@ -286,122 +287,6 @@ class EmployeeGroupServiceTest :
assertTrue(found.secure)
}
// addEmployeeToGroup()
@Test
fun `addEmployeeToGroup() calls addEmployeeToGroup() with the group of the given groupId and the employee of the given employeeId`() {
whenever(employeeService.getById(groupEmployeeId)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
doAnswer { }.whenever(service).addEmployeeToGroup(entity, groupEmployee)
service.addEmployeeToGroup(entity.id!!, groupEmployeeId)
verify(service).addEmployeeToGroup(entity, groupEmployee)
}
@Test
fun `addEmployeeToGroup() calls update() and employeeService_update() with the updated entities`() {
val group = employeeGroup()
val employee = employee()
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
service.addEmployeeToGroup(group, employee)
verify(service).update(group)
verify(employeeService).update(employee)
assertTrue(group.employees.any { it.id == employee.id })
assertEquals(group, employee.group)
}
@Test
fun `addEmployeeToGroup() do nothing when the given employee is already in the given group`() {
val group = employeeGroup()
val employee = employee(group = group)
service.addEmployeeToGroup(group, employee)
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
@Test
fun `addEmployeeToGroup() remove previous group from the given employee and add it the the given group`() {
val group = employeeGroup(id = 0L)
val previousGroup = employeeGroup(id = 1L)
val employee = employee(group = previousGroup)
whenever(employeeService.update(employee)).doReturn(employee)
doReturn(group).whenever(service).update(group)
doReturn(group).whenever(service).update(previousGroup)
service.addEmployeeToGroup(group, employee)
verify(service).removeEmployeeFromGroup(previousGroup, employee)
verify(service).update(group)
verify(employeeService, times(2)).update(employee)
assertTrue(group.employees.any { it.id == employee.id })
assertEquals(group, employee.group)
}
// removeEmployeeFromGroup()
@Test
fun `removeEmployeeFromGroup() calls removeEmployeeFromGroup() with the group of the given group id and the employee of the given employee id`() {
whenever(employeeService.getById(groupEmployeeId)).doReturn(groupEmployee)
doReturn(entity).whenever(service).getById(entity.id!!)
doAnswer { it.arguments[0] }.whenever(service).update(any<EmployeeGroup>())
service.removeEmployeeFromGroup(entity.id!!, groupEmployeeId)
verify(service).removeEmployeeFromGroup(entity, groupEmployee)
}
@Test
fun `removeEmployeeFromGroup() calls update() and employeeService_update() with the updated entities`() {
val employee = employee()
val group = employeeGroup(employees = mutableSetOf(employee))
employee.group = group
whenever(employeeService.update(any<Employee>())).doReturn(employee)
doReturn(group).whenever(service).update(group)
service.removeEmployeeFromGroup(group, employee)
verify(service).update(group)
verify(employeeService).update(argThat<Employee> { this.group == null })
assertFalse(group.employees.contains(employee))
assertNull(employee.group)
}
@Test
fun `removeEmployeeFromGroup() do nothing when the given employee is not in the given group`() {
val employee = employee()
val group = employeeGroup(id = 0L)
val anotherGroup = employeeGroup(id = 1L, employees = mutableSetOf(employee))
employee.group = anotherGroup
service.removeEmployeeFromGroup(group, employee)
verify(service, times(0)).update(anotherGroup)
verify(employeeService, times(0)).update(employee)
}
@Test
fun `removeEmployeeFromGroup() do nothing when the given employee is not in a group`() {
val employee = employee()
val group = employeeGroup()
service.removeEmployeeFromGroup(group, employee)
verify(service, times(0)).update(group)
verify(employeeService, times(0)).update(employee)
}
// save()
@Test