Skip to content

Commit

Permalink
add envActionsAttachedToReportingIds to missionDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
thoomasbro committed Oct 27, 2023
1 parent b012f98 commit 4695f31
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionSourceEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionTypeEnum
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.EnvActionAttachedToReportingIds
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import fr.gouv.cacem.monitorenv.utils.mapOrElseEmpty
import io.hypersistence.utils.hibernate.type.array.ListArrayType
Expand All @@ -25,6 +26,7 @@ import org.hibernate.annotations.Type
import org.locationtech.jts.geom.MultiPolygon
import org.n52.jackson.datatype.jts.GeometryDeserializer
import org.n52.jackson.datatype.jts.GeometrySerializer
import java.util.UUID

@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator::class,
Expand Down Expand Up @@ -138,28 +140,52 @@ data class MissionModel(
}

fun toMissionDTO(objectMapper: ObjectMapper): MissionDTO {
val envActionsAttachedToReportingIds = attachedReportings
?.filter { it.attachedEnvAction != null }
?.fold(mutableListOf<EnvActionAttachedToReportingIds>()) { acc, reduced ->
if (reduced.attachedEnvAction?.id != null &&
reduced.id != null &&
acc.find { it.first == reduced.attachedEnvAction.id } != null
) {
acc.map {
if (it.first == reduced.attachedEnvAction?.id && reduced.id != null) {
it.copy(second = it.second.plus(reduced.id))
} else {
it
}
}
} else {
if (reduced.attachedEnvAction?.id != null && reduced.id != null) {
val newPair = Pair(reduced.attachedEnvAction.id, mutableListOf(reduced.id))
acc.add(newPair)
}
}
acc
}
?: listOf()
return MissionDTO(
mission = this.toMissionEntity(objectMapper),
attachedReportingIds =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc == null }
?.map { it.id as Int }
?: listOf(),
attachedReportings =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc == null }
?.map { it.toReportingDTO(objectMapper) }
?: listOf(),
detachedReportings =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc != null }
?.map { it.toReportingDTO(objectMapper) }
?: listOf(),
detachedReportingIds =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc != null }
?.map { it.id as Int }
?: listOf(),
mission = this.toMissionEntity(objectMapper),
attachedReportingIds =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc == null }
?.map { it.id as Int }
?: listOf(),
attachedReportings =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc == null }
?.map { it.toReportingDTO(objectMapper) }
?: listOf(),
detachedReportings =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc != null }
?.map { it.toReportingDTO(objectMapper) }
?: listOf(),
detachedReportingIds =
this.attachedReportings
?.filter { it.detachedFromMissionAtUtc != null }
?.map { it.id as Int }
?: listOf(),
envActionsAttachedToReportingIds = envActionsAttachedToReportingIds,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,15 @@ class JpaMissionRepositoryITests : AbstractDBTests() {

@Test
@Transactional
fun `findById Should return specified mission and associated env actions`() {
fun `findById Should return specified mission and associated env actions and associated envActionReportingIds`() {
// When
val missionDTO = jpaMissionRepository.findFullMissionById(34)
assertThat(missionDTO.mission.id).isEqualTo(34)
assertThat(missionDTO.mission.envActions).hasSize(2)
assertThat(
missionDTO.envActionsAttachedToReportingIds?.get(0)?.first,
).isEqualTo(UUID.fromString("b8007c8a-5135-4bc3-816f-c69c7b75d807"))
assertThat(missionDTO.envActionsAttachedToReportingIds?.get(0)?.second).isEqualTo(listOf(6))
}

@Test
Expand Down

0 comments on commit 4695f31

Please sign in to comment.