Skip to content

Commit

Permalink
[Reporting] Create specific output for getAllReportings
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Jan 17, 2025
1 parent 7ccf8a0 commit d1e2eb2
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import org.locationtech.jts.geom.Geometry
import java.time.Instant
import java.util.UUID
import java.util.*

interface IReportingRepository {
fun archiveOutdatedReportings(): Int
Expand Down Expand Up @@ -47,7 +48,7 @@ interface IReportingRepository {
targetTypes: List<TargetTypeEnum>?,
isAttachedToMission: Boolean?,
searchQuery: String?,
): List<ReportingDTO>
): List<ReportingsDTO>

fun findByControlUnitId(controlUnitId: Int): List<ReportingEntity>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import org.slf4j.LoggerFactory
import java.time.ZonedDateTime

Expand All @@ -27,7 +27,7 @@ class GetReportings(
targetTypes: List<TargetTypeEnum>?,
isAttachedToMission: Boolean?,
searchQuery: String?,
): List<ReportingDTO> {
): List<ReportingsDTO> {
logger.info("Attempt to get reportings with criteria")
val now = ZonedDateTime.now()
val reports =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos

import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingSourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.semaphore.SemaphoreEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.controlUnit.dtos.FullControlUnitDTO

data class ReportingSourceDTO(
val reportingSource: ReportingSourceEntity,
val semaphore: SemaphoreEntity?,
val controlUnit: FullControlUnitDTO?,
val controlUnit: ControlUnitEntity?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos

import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ControlStatusEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingEntity

data class ReportingsDTO(
val reporting: ReportingEntity,
val reportingSources: List<ReportingSourceDTO>,
val attachedMission: MissionEntity? = null,
) {
val controlStatus: ControlStatusEnum?
get() =
when {
this.reporting.attachedEnvActionId != null &&
this.attachedMission?.envActions
?.find { it.id == this.reporting.attachedEnvActionId }
?.actionType == ActionTypeEnum.SURVEILLANCE ->
ControlStatusEnum.SURVEILLANCE_DONE

this.reporting.attachedEnvActionId != null &&
this.attachedMission?.envActions
?.find { it.id == this.reporting.attachedEnvActionId }
?.actionType == ActionTypeEnum.CONTROL ->
ControlStatusEnum.CONTROL_DONE

this.reporting.attachedEnvActionId == null &&
this.reporting.isControlRequired == true ->
ControlStatusEnum.CONTROL_TO_BE_DONE

else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.reporti

import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingSourceDTO
import java.util.UUID
import java.util.*

data class ReportingSourceDataOutput(
val id: UUID?,
Expand All @@ -19,13 +19,16 @@ data class ReportingSourceDataOutput(
id = reportingSourceDTO.reportingSource.id,
reportingId = reportingSourceDTO.reportingSource.reportingId,
sourceType = reportingSourceDTO.reportingSource.sourceType,
controlUnitId = reportingSourceDTO.controlUnit?.controlUnit?.id,
controlUnitId = reportingSourceDTO.controlUnit?.id,
semaphoreId = reportingSourceDTO.semaphore?.id,
sourceName = reportingSourceDTO.reportingSource.sourceName,
displayedSource =
when (reportingSourceDTO.reportingSource.sourceType) {
SourceTypeEnum.SEMAPHORE -> reportingSourceDTO.semaphore?.unit ?: reportingSourceDTO.semaphore?.name
SourceTypeEnum.CONTROL_UNIT -> reportingSourceDTO.controlUnit?.controlUnit?.name
SourceTypeEnum.SEMAPHORE ->
reportingSourceDTO.semaphore?.unit
?: reportingSourceDTO.semaphore?.name

SourceTypeEnum.CONTROL_UNIT -> reportingSourceDTO.controlUnit?.name
SourceTypeEnum.OTHER -> reportingSourceDTO.reportingSource.sourceName
} ?: "",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.ControlStatusEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetDetailsEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs.reportings.ReportingSourceDataOutput.Companion.fromReportingSourceDTO
import org.locationtech.jts.geom.Geometry
import java.time.ZonedDateTime
import java.util.UUID
import java.util.*

// TODO(25/07/2024) : to delete ?
data class ReportingsDataOutput(
Expand Down Expand Up @@ -41,7 +41,7 @@ data class ReportingsDataOutput(
val isInfractionProven: Boolean,
) {
companion object {
fun fromReportingDTO(dto: ReportingDTO): ReportingsDataOutput {
fun fromReportingDTO(dto: ReportingsDTO): ReportingsDataOutput {
requireNotNull(dto.reporting.id) { "ReportingEntity.id cannot be null" }
return ReportingsDataOutput(
id = dto.reporting.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionsDTO
import fr.gouv.cacem.monitorenv.infrastructure.database.model.reportings.ReportingModel
import jakarta.persistence.*
import jakarta.persistence.CascadeType
import jakarta.persistence.OrderBy
import jakarta.persistence.PrePersist
import jakarta.persistence.PreUpdate
import jakarta.persistence.Table
import org.hibernate.Hibernate
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
Expand Down Expand Up @@ -273,6 +268,10 @@ class MissionModel(
return buildMissionEntity(mappedControlUnits, objectMapper)
}

fun toMissionEntityWithoutControlUnit(objectMapper: ObjectMapper): MissionEntity {
return buildMissionEntity(null, objectMapper)
}

fun toMissionDTO(objectMapper: ObjectMapper): MissionDTO {
val envActionsAttachedToReportingIds =
attachedReportings?.filter { it.attachedEnvAction != null }?.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,11 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingSourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingSourceDTO
import fr.gouv.cacem.monitorenv.infrastructure.database.model.reportings.ReportingModel
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EnumType
import jakarta.persistence.Enumerated
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
import jakarta.persistence.*
import org.hibernate.Hibernate
import org.hibernate.annotations.JdbcType
import org.hibernate.dialect.PostgreSQLEnumJdbcType
import java.util.UUID
import java.util.*

@Entity
@Table(name = "reportings_source")
Expand Down Expand Up @@ -61,7 +50,7 @@ class ReportingSourceModel(
ReportingSourceDTO(
reportingSource = this.toReportingSource(),
semaphore = semaphore?.toSemaphore(),
controlUnit = controlUnit?.toFullControlUnit(),
controlUnit = controlUnit?.toControlUnit(),
)

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetDetailsEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import fr.gouv.cacem.monitorenv.infrastructure.database.model.*
import io.hypersistence.utils.hibernate.type.json.JsonBinaryType
import jakarta.persistence.*
Expand Down Expand Up @@ -143,6 +144,23 @@ abstract class AbstractReportingModel(
isInfractionProven = isInfractionProven,
)

fun toReportingsDTO(objectMapper: ObjectMapper): ReportingsDTO {
val reporting = this.toReporting()
return ReportingsDTO(
reporting = reporting,
reportingSources = reportingSources.map { it.toReportingSourceDTO() },
attachedMission =
if (detachedFromMissionAtUtc == null && attachedToMissionAtUtc != null
) {
mission?.toMissionEntityWithoutControlUnit(
objectMapper,
)
} else {
null
},
)
}

fun toReportingDTO(objectMapper: ObjectMapper): ReportingDTO {
val reporting = this.toReporting()
return ReportingDTO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.exceptions.NotFoundException
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ReportingSourceModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ReportingsControlPlanSubThemeModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.reportings.AbstractReportingModel.Companion.fromReportingEntity
import fr.gouv.cacem.monitorenv.infrastructure.database.model.reportings.ReportingModel
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanSubThemeRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanThemeRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlUnitRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBEnvActionRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBMissionRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBReportingRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBSemaphoreRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.*
import org.apache.commons.lang3.StringUtils
import org.locationtech.jts.geom.Geometry
import org.springframework.dao.DataIntegrityViolationException
Expand All @@ -29,7 +24,7 @@ import org.springframework.orm.jpa.JpaObjectRetrievalFailureException
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional
import java.time.Instant
import java.util.UUID
import java.util.*

@Repository
class JpaReportingRepository(
Expand Down Expand Up @@ -89,7 +84,7 @@ class JpaReportingRepository(
targetTypes: List<TargetTypeEnum>?,
isAttachedToMission: Boolean?,
searchQuery: String?,
): List<ReportingDTO> {
): List<ReportingsDTO> {
val pageable =
if (pageNumber != null && pageSize != null) {
PageRequest.of(pageNumber, pageSize)
Expand All @@ -107,7 +102,7 @@ class JpaReportingRepository(
targetTypes = targetTypes,
isAttachedToMission = isAttachedToMission,
)
.map { it.toReportingDTO(mapper) }.filter { findBySearchQuery(it.reporting, searchQuery) }
.map { it.toReportingsDTO(mapper) }.filter { findBySearchQuery(it.reporting, searchQuery) }
}

private fun findBySearchQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.springframework.data.repository.CrudRepository
interface IDBDepartmentAreaRepository : CrudRepository<DepartmentAreaModel, Int> {
@Query(
value =
"""
"""
SELECT *
FROM departments_areas
WHERE insee_dep = :inseeCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.reportings

import com.nhaarman.mockitokotlin2.given
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReportingsDTO
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
Expand All @@ -24,7 +24,7 @@ class GetReportingsUTest {
fun `execute should retrieve all reportings with given criteria`(log: CapturedOutput) {
// Given
val now = ZonedDateTime.now()
val reportings = listOf(aReportingDTO(id = 1), aReportingDTO(id = 2), aReportingDTO(id = 3))
val reportings = listOf(aReportingsDTO(id = 1), aReportingsDTO(id = 2), aReportingsDTO(id = 3))
given(
reportingRepository.findAll(
pageNumber = null,
Expand Down Expand Up @@ -68,7 +68,7 @@ class GetReportingsUTest {
log: CapturedOutput,
) {
// Given
val reportings = listOf(aReportingDTO(id = 1), aReportingDTO(id = 2), aReportingDTO(id = 3))
val reportings = listOf(aReportingsDTO(id = 1), aReportingsDTO(id = 2), aReportingsDTO(id = 3))

val now = ZonedDateTime.now()
mockkStatic(ZonedDateTime::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures

import fr.gouv.cacem.monitorenv.domain.entities.VehicleTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingSourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.*
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingSourceDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import org.locationtech.jts.geom.MultiPolygon
import org.locationtech.jts.io.WKTReader
import java.time.ZonedDateTime
import java.util.UUID
import java.util.*

class ReportingFixture {
companion object {
Expand Down Expand Up @@ -73,6 +70,24 @@ class ReportingFixture {
)
}

fun aReportingsDTO(
id: Int? = 1,
attachedEnvActionId: UUID? = null,
reporting: ReportingEntity = aReporting(id, attachedEnvActionId = attachedEnvActionId),
): ReportingsDTO {
return ReportingsDTO(
reporting = reporting,
reportingSources =
listOf(
ReportingSourceDTO(
reportingSource = aReportingSourceSemaphore(),
semaphore = null,
controlUnit = null,
),
),
)
}

fun aReportingSourceSemaphore(
reportingId: Int? = null,
semaphoreId: Int = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.TargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.semaphore.SemaphoreEntity
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.ArchiveReportings
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.CreateOrUpdateReporting
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.DeleteReporting
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.DeleteReportings
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.GetReportingById
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.GetReportings
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.GetReportingsByIds
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.*
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingSourceDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.dtos.ReportingsDTO
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.events.UpdateReportingEvent
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture
import fr.gouv.cacem.monitorenv.domain.use_cases.reportings.fixtures.ReportingFixture.Companion.aReportingDTO
Expand All @@ -42,10 +37,7 @@ import org.springframework.context.annotation.Import
import org.springframework.http.MediaType
import org.springframework.test.context.bean.override.mockito.MockitoBean
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
Expand Down Expand Up @@ -326,7 +318,7 @@ class ReportingsITests {
"MULTIPOLYGON (((-61.0 14.0, -61.0 15.0, -60.0 15.0, -60.0 14.0, -61.0 14.0)))",
)
val reporting =
ReportingDTO(
ReportingsDTO(
reporting =
ReportingEntity(
id = 1,
Expand Down
Loading

0 comments on commit d1e2eb2

Please sign in to comment.