Skip to content

Commit

Permalink
[Missions] API publique ne peut pas écrire sur les envActions (#1078)
Browse files Browse the repository at this point in the history
resolves #879 
Pas de changement coté Fish nécessaire. Il n'est cependant plus nécessaire de renvoyer les envActions.
  • Loading branch information
thoomasbro authored Jan 4, 2024
2 parents 17c8fb1 + 7c87f30 commit 9af76b6
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 185 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
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.mission.envAction.EnvActionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionNoteEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionSurveillanceEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.EnvActionControlEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IDepartmentAreaRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IFacadeAreasRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository

@UseCase
class CreateOrUpdateEnvActions(
private val departmentRepository: IDepartmentAreaRepository,
private val facadeRepository: IFacadeAreasRepository,
private val missionRepository: IMissionRepository,
) {
@Throws(IllegalArgumentException::class)
fun execute(
mission: MissionEntity,
envActions: List<EnvActionEntity>?,
): MissionEntity {
val envActionsToSave =
envActions?.map {
when (it.actionType) {
ActionTypeEnum.CONTROL -> {
(it as EnvActionControlEntity).copy(
facade =
(it.geom ?: mission.geom)?.let { geom ->
facadeRepository.findFacadeFromGeometry(geom)
},
department =
(it.geom ?: mission.geom)?.let { geom ->
departmentRepository.findDepartmentFromGeometry(
geom,
)
},
)
}
ActionTypeEnum.SURVEILLANCE -> {
val surveillance = it as EnvActionSurveillanceEntity
/*
When coverMissionZone is true, use mission geometry in priority, fall back to action geometry.
When coverMissionZone is not true, prioritize the other way around.
Ideally the fallbacks should not be needed, but if coverMissionZone is true and the mission geom
is null, or if coverMissionZone is false and the action geom is null, then rather that nothing,
better use the geometry that is available, if any.
*/
val geometry =
if (surveillance.coverMissionZone == true) {
(mission.geom ?: surveillance.geom)
} else {
(surveillance.geom ?: mission.geom)
}
surveillance.copy(
facade =
geometry?.let { geom ->
facadeRepository.findFacadeFromGeometry(geom)
},
department =
geometry?.let { geom ->
departmentRepository.findDepartmentFromGeometry(
geom,
)
},
)
}
ActionTypeEnum.NOTE -> {
(it as EnvActionNoteEntity).copy()
}
}
}

val missionToSave =
mission.copy(
envActions = envActionsToSave,
)
val savedMission = missionRepository.save(missionToSave)

if (savedMission.mission.id == null) {
throw IllegalArgumentException("Mission id is null")
}

return savedMission.mission
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.mission.*
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionNoteEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionSurveillanceEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.EnvActionControlEntity
import fr.gouv.cacem.monitorenv.domain.repositories.IDepartmentAreaRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IFacadeAreasRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.events.UpdateMissionEvent
Expand All @@ -17,7 +12,6 @@ import org.springframework.context.ApplicationEventPublisher

@UseCase
class CreateOrUpdateMission(
private val departmentRepository: IDepartmentAreaRepository,
private val facadeRepository: IFacadeAreasRepository,
private val missionRepository: IMissionRepository,
private val eventPublisher: ApplicationEventPublisher,
Expand All @@ -30,67 +24,13 @@ class CreateOrUpdateMission(
): MissionEntity {
require(mission != null) { "No mission to create or update" }

val envActions =
mission.envActions?.map {
when (it.actionType) {
ActionTypeEnum.CONTROL -> {
(it as EnvActionControlEntity).copy(
facade =
(it.geom ?: mission.geom)?.let { geom ->
facadeRepository.findFacadeFromGeometry(geom)
},
department =
(it.geom ?: mission.geom)?.let { geom ->
departmentRepository.findDepartmentFromGeometry(
geom,
)
},
)
}
ActionTypeEnum.SURVEILLANCE -> {
val surveillance = it as EnvActionSurveillanceEntity
/*
When coverMissionZone is true, use mission geometry in priority, fall back to action geometry.
When coverMissionZone is not true, prioritize the other way around.
Ideally the fallbacks should not be needed, but if coverMissionZone is true and the mission geom
is null, or if coverMissionZone is false and the action geom is null, then rather that nothing,
better use the geometry that is available, if any.
*/
val geometry =
if (surveillance.coverMissionZone == true) {
(mission.geom ?: surveillance.geom)
} else {
(surveillance.geom ?: mission.geom)
}
surveillance.copy(
facade =
geometry?.let { geom ->
facadeRepository.findFacadeFromGeometry(geom)
},
department =
geometry?.let { geom ->
departmentRepository.findDepartmentFromGeometry(
geom,
)
},
)
}
ActionTypeEnum.NOTE -> {
(it as EnvActionNoteEntity).copy()
}
}
}

var facade: String? = null

if (mission.geom != null) {
facade = facadeRepository.findFacadeFromGeometry(mission.geom)
}
val facade = if (mission.geom != null) { facadeRepository.findFacadeFromGeometry(mission.geom) } else { null }
val storedMission = if (mission.id != null) { missionRepository.findById(mission.id) } else { null }

val missionToSave =
mission.copy(
facade = facade,
envActions = envActions,
envActions = storedMission?.envActions,
)
val savedMission = missionRepository.save(missionToSave)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import org.slf4j.LoggerFactory
import java.util.UUID

@UseCase
class CreateOrUpdateMissionWithAttachedReporting(
class CreateOrUpdateMissionWithActionsAndAttachedReporting(
private val createOrUpdateMission: CreateOrUpdateMission,
private val createOrUpdateEnvActions: CreateOrUpdateEnvActions,
private val missionRepository: IMissionRepository,
private val reportingRepository: IReportingRepository,
) {
private val logger =
LoggerFactory.getLogger(CreateOrUpdateMissionWithAttachedReporting::class.java)
LoggerFactory.getLogger(CreateOrUpdateMissionWithActionsAndAttachedReporting::class.java)

@Throws(IllegalArgumentException::class)
fun execute(
Expand All @@ -40,6 +41,11 @@ class CreateOrUpdateMissionWithAttachedReporting(
val savedMission = createOrUpdateMission.execute(mission)
require(savedMission.id != null) { "The mission id is null" }

createOrUpdateEnvActions.execute(
savedMission,
mission.envActions,
)

attachedReportingIds.forEach {
val reporting = reportingRepository.findById(it)
if (reporting.reporting.missionId != null &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitEnt
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.entities.mission.envAction.EnvActionEntity
import org.locationtech.jts.geom.MultiPolygon
import java.time.ZonedDateTime

Expand All @@ -22,7 +21,6 @@ data class CreateOrUpdateMissionDataInput(
val endDateTimeUtc: ZonedDateTime? = null,
val missionSource: MissionSourceEnum,
val isClosed: Boolean,
val envActions: List<EnvActionEntity>? = null,
val hasMissionOrder: Boolean,
val isUnderJdp: Boolean,
val isGeometryComputedFromControls: Boolean,
Expand All @@ -43,7 +41,6 @@ data class CreateOrUpdateMissionDataInput(
isClosed = this.isClosed,
isDeleted = false,
missionSource = this.missionSource,
envActions = this.envActions,
hasMissionOrder = this.hasMissionOrder,
isUnderJdp = this.isUnderJdp,
isGeometryComputedFromControls = this.isGeometryComputedFromControls,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import java.time.ZonedDateTime
@RequestMapping("/bff/v1/missions")
@Tag(description = "API Missions", name = "BFF.Missions")
class Missions(
private val createOrUpdateMissionWithAttachedReporting:
CreateOrUpdateMissionWithAttachedReporting,
private val createOrUpdateMissionWithActionsAndAttachedReporting:
CreateOrUpdateMissionWithActionsAndAttachedReporting,
private val getFullMissions: GetFullMissions,
private val getFullMissionById: GetFullMissionById,
private val deleteMission: DeleteMission,
Expand All @@ -32,7 +32,7 @@ class Missions(
createMissionDataInput: CreateOrUpdateMissionDataInput,
): MissionDataOutput {
val createdMission =
createOrUpdateMissionWithAttachedReporting.execute(
createOrUpdateMissionWithActionsAndAttachedReporting.execute(
mission = createMissionDataInput.toMissionEntity(),
attachedReportingIds = createMissionDataInput.attachedReportingIds,
envActionsAttachedToReportingIds =
Expand Down Expand Up @@ -128,7 +128,7 @@ class Missions(
if ((updateMissionDataInput.id != null) && (missionId != updateMissionDataInput.id)) {
throw java.lang.IllegalArgumentException("missionId doesn't match with request param")
}
return createOrUpdateMissionWithAttachedReporting.execute(
return createOrUpdateMissionWithActionsAndAttachedReporting.execute(
mission = updateMissionDataInput.toMissionEntity(),
attachedReportingIds = updateMissionDataInput.attachedReportingIds,
envActionsAttachedToReportingIds =
Expand Down
Loading

0 comments on commit 9af76b6

Please sign in to comment.