diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionId.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionId.kt index 1e85632a..e2ea2678 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionId.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionId.kt @@ -8,8 +8,25 @@ import fr.gouv.dgampa.rapportnav.domain.repositories.mission.crew.IMissionCrewRe class GetAgentsCrewByMissionId(private val agentCrewRepository: IMissionCrewRepository) { fun execute(missionId: Int, commentDefaultsToString: Boolean? = false): List { + val rolePriority = listOf( + "Commandant", + "Second capitaine", + "Second", + "Chef mécanicien", + "Second mécanicien", + "Mécanicien électricien", + "Mécanicien", + "Chef de quart", + "Maître d’équipage", + "Agent pont", + "Agent machine", + "Agent mécanicien", + "Électricien", + "Cuisinier", + ) + return agentCrewRepository.findByMissionId(missionId = missionId) .map { it.toMissionCrewEntity(commentDefaultsToString) } - .sortedBy { it.id } + .sortedBy { rolePriority.indexOf(it.role.title) } } } diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/database/model/mission/crew/MissionCrewModel.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/database/model/mission/crew/MissionCrewModel.kt index 89f0d34a..f89cf89c 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/database/model/mission/crew/MissionCrewModel.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/database/model/mission/crew/MissionCrewModel.kt @@ -19,7 +19,7 @@ class MissionCrewModel( var missionId: Int, @Column(name = "comment", nullable = true) - var comment: String?, + var comment: String? = null, @ManyToOne @JoinColumn(name = "agent_role_id") diff --git a/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionIdTest.kt b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionIdTest.kt new file mode 100644 index 00000000..f171cccf --- /dev/null +++ b/backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/mission/crew/GetAgentsCrewByMissionIdTest.kt @@ -0,0 +1,89 @@ +package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.crew + +import fr.gouv.dgampa.rapportnav.domain.repositories.mission.crew.IMissionCrewRepository +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.crew.GetAgentsCrewByMissionId +import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.crew.AgentModel +import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.crew.AgentRoleModel +import fr.gouv.dgampa.rapportnav.infrastructure.database.model.mission.crew.MissionCrewModel +import junit.framework.TestCase.assertEquals +import org.junit.jupiter.api.Test +import org.mockito.Mockito.`when` +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.mock.mockito.MockBean + +@SpringBootTest(classes = [GetAgentsCrewByMissionId::class]) +class GetAgentsCrewByMissionIdTest { + + @Autowired + private lateinit var getAgentsCrewByMissionId: GetAgentsCrewByMissionId + + @MockBean + private lateinit var agentCrewRepository: IMissionCrewRepository + + + @Test + fun `execute should return sorted list of crew members by role priority`() { + + val missionId = 1 + + val johnDoe = AgentModel( + firstName = "John", + lastName = "Doe", + id = 1 + ) + + val janeDoe = AgentModel( + firstName = "Jane", + lastName = "Doe", + id = 2 + ) + + val alfredDeMusset = AgentModel( + firstName = "Alfred", + lastName = "de Musset", + id = 3 + ) + + val guyDeMaupassant = AgentModel( + firstName = "Guy", + lastName = "de Maupassant", + id = 4 + ) + + val chefMecano = AgentRoleModel( + title = "Chef mécanicien", + id = 1 + ) + + val secondCapitaine = AgentRoleModel( + title = "Second capitaine", + id = 2 + ) + + val cuisinier = AgentRoleModel( + title = "Cuisinier", + id = 3 + ) + + val commandant = AgentRoleModel( + title = "Commandant", + id = 4 + ) + + val crewMembers = listOf( + MissionCrewModel(role = chefMecano, agent = janeDoe, missionId = missionId, id = 1), + MissionCrewModel(role = secondCapitaine, agent = johnDoe, missionId = missionId, id = 2), + MissionCrewModel(role = cuisinier, agent = alfredDeMusset, missionId = missionId, id = 3), + MissionCrewModel(role = commandant, agent = guyDeMaupassant, missionId = missionId, id = 4), + ) + + `when`(agentCrewRepository.findByMissionId(missionId)).thenReturn(crewMembers) + + val sortedCrew = getAgentsCrewByMissionId.execute(missionId, commentDefaultsToString = false) + + // Assert + val expectedRoles = listOf("Commandant", "Second capitaine", "Chef mécanicien", "Cuisinier") + assertEquals(expectedRoles, sortedCrew.map { it.role.title }) + } +}