diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/repositories/v2/mission/IEnvMissionRepository.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/repositories/v2/mission/IEnvMissionRepository.kt index c0d8dee1e..0b99107fe 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/repositories/v2/mission/IEnvMissionRepository.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/repositories/v2/mission/IEnvMissionRepository.kt @@ -1,8 +1,8 @@ package fr.gouv.dgampa.rapportnav.domain.repositories.v2.mission import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity -import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.inputs.CreateOrUpdateMissionDataInput +import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv interface IEnvMissionRepository { - fun createMission(input: CreateOrUpdateMissionDataInput): MissionEntity? + fun createMission(mission: MissionEnv): MissionEntity? } diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/CreateEnvMission.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/CreateEnvMission.kt index 67ab41769..664390d7e 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/CreateEnvMission.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/use_cases/mission/CreateEnvMission.kt @@ -5,7 +5,7 @@ import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.controlResources.Le import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity import fr.gouv.dgampa.rapportnav.domain.repositories.v2.controlUnit.IEnvControlUnitRepository import fr.gouv.dgampa.rapportnav.domain.repositories.v2.mission.IEnvMissionRepository -import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.inputs.CreateOrUpdateMissionDataInput +import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv import org.slf4j.LoggerFactory @UseCase @@ -15,8 +15,7 @@ class CreateEnvMission( ) { private val logger = LoggerFactory.getLogger(CreateEnvMission::class.java) - - fun execute(input: CreateOrUpdateMissionDataInput, controlUnitIds: List?): MissionEntity? { + fun execute(mission: MissionEnv, controlUnitIds: List?): MissionEntity? { try { if (controlUnitIds !== null && controlUnitIds.isNotEmpty()) { val controlUnits = monitorEnvControlUnitRepo.findAll() @@ -25,9 +24,9 @@ class CreateEnvMission( ?.filter { it.id in controlUnitIds } ?: emptyList() - input.controlUnits = matchedControlUnits + mission.controlUnits = matchedControlUnits - return monitorEnvRepo.createMission(input) + return monitorEnvRepo.createMission(mission) } throw Exception("CreateEnvMission : controlUnit is empty for this user") diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/model/v2/MissionEnv.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/model/v2/MissionEnv.kt new file mode 100644 index 000000000..09b644168 --- /dev/null +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/model/v2/MissionEnv.kt @@ -0,0 +1,62 @@ +package fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2 + +import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionSourceEnum +import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.MissionTypeEnum +import fr.gouv.dgampa.rapportnav.domain.entities.mission.env.controlResources.LegacyControlUnitEntity +import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity +import org.locationtech.jts.geom.MultiPolygon +import java.time.ZonedDateTime + +data class MissionEnv( + val id: Int? = null, + val missionTypes: List, + var controlUnits: List = listOf(), + val openBy: String? = null, + val completedBy: String? = null, + val observationsCacem: String? = null, + val observationsCnsp: String? = null, + val facade: String? = null, + val geom: MultiPolygon? = null, + val startDateTimeUtc: ZonedDateTime, + val endDateTimeUtc: ZonedDateTime? = null, + val missionSource: MissionSourceEnum, + val hasMissionOrder: Boolean, + val isUnderJdp: Boolean = false, + val isGeometryComputedFromControls: Boolean = false, +) { + + companion object { + fun fromMissionEntity(mission: MissionEntity): MissionEnv { + return MissionEnv( + id = mission.id, + missionSource = mission.missionSource, + startDateTimeUtc = mission.startDateTimeUtc, + endDateTimeUtc = mission.endDateTimeUtc, + openBy = mission.openBy, + controlUnits = mission.controlUnits, + hasMissionOrder = mission.hasMissionOrder, + missionTypes = mission.missionTypes, + ) + } + } + fun toMissionEntity(): MissionEntity { + return MissionEntity( + id = this.id, + missionTypes = this.missionTypes, + controlUnits = this.controlUnits, + openBy = this.openBy, + completedBy = this.completedBy, + observationsCacem = this.observationsCacem, + observationsCnsp = this.observationsCnsp, + facade = this.facade, + geom = this.geom, + startDateTimeUtc = this.startDateTimeUtc, + endDateTimeUtc = this.endDateTimeUtc, + isDeleted = false, + missionSource = this.missionSource, + hasMissionOrder = this.hasMissionOrder, + isUnderJdp = this.isUnderJdp, + isGeometryComputedFromControls = this.isGeometryComputedFromControls, + ) + } +} diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionController.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionController.kt index 0545d54a9..b92a334ab 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionController.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionController.kt @@ -21,7 +21,6 @@ class MissionController( private val getMission: GetMission, private val getControlUnitsForUser: GetControlUnitsForUser, private val fakeMissionData: FakeMissionData, - private val createMission: CreateEnvMission ) { private val logger = LoggerFactory.getLogger(MissionController::class.java) @@ -66,17 +65,4 @@ class MissionController( throw Exception(e) } } - - @MutationMapping - fun create(@Argument input: CreateOrUpdateMissionDataInput): MissionEntity? { - try { - return createMission.execute( - input, - controlUnitIds = getControlUnitsForUser.execute() - ) - } catch (e: Exception) { - logger.error("MissionController (ULAM v2) - failed to create mission from MonitorEnv", e) - throw Exception(e) - } - } } diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionRestController.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionRestController.kt new file mode 100644 index 000000000..e92df14f9 --- /dev/null +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/api/bff/v2/MissionRestController.kt @@ -0,0 +1,44 @@ +package fr.gouv.dgampa.rapportnav.infrastructure.api.bff.v2 + +import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.CreateEnvMission +import fr.gouv.dgampa.rapportnav.domain.use_cases.user.GetControlUnitsForUser +import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv +import io.swagger.v3.oas.annotations.Operation +import org.slf4j.LoggerFactory +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/api/v2/missions") +class MissionRestController( + private val createEnvMission: CreateEnvMission, + private val getControlUnitsForUser: GetControlUnitsForUser, +) { + + private val logger = LoggerFactory.getLogger(MissionRestController::class.java) + + @PostMapping("") + @Operation(summary = "Create a new MonitorEnv mission") + fun create( + @RequestBody body: MissionEnv + ): MissionEnv? { + try { + val mission = createEnvMission.execute( + mission = body, + controlUnitIds = getControlUnitsForUser.execute() + ) + + if (mission != null) { + return MissionEnv.fromMissionEntity(mission) + } + + throw RuntimeException("Error - can't create new MonitorEnv mission") + } + catch (e: Exception) { + logger.error("Error while creating MonitorEnv mission : ", e) + return null + } + } +} diff --git a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/monitorenv/v2/APIEnvMissionRepositoryV2.kt b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/monitorenv/v2/APIEnvMissionRepositoryV2.kt index 8676ca292..c94398444 100644 --- a/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/monitorenv/v2/APIEnvMissionRepositoryV2.kt +++ b/backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/infrastructure/monitorenv/v2/APIEnvMissionRepositoryV2.kt @@ -5,7 +5,7 @@ import com.fasterxml.jackson.module.kotlin.readValue import fr.gouv.dgampa.rapportnav.config.HttpClientFactory import fr.gouv.dgampa.rapportnav.domain.entities.mission.v2.env.MissionEntity import fr.gouv.dgampa.rapportnav.domain.repositories.v2.mission.IEnvMissionRepository -import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.inputs.CreateOrUpdateMissionDataInput +import fr.gouv.dgampa.rapportnav.infrastructure.api.bff.model.v2.MissionEnv import fr.gouv.dgampa.rapportnav.infrastructure.monitorenv.v2.outputs.MissionDataOutput import fr.gouv.dgampa.rapportnav.infrastructure.utils.GsonSerializer import org.n52.jackson.datatype.jts.JtsModule @@ -28,9 +28,11 @@ class APIEnvMissionRepositoryV2( private val client = clientFactory.create(); - private val host = System.getenv("MONITORENV_HOST") + // private val host = System.getenv("MONITORENV_HOST") - override fun createMission(input: CreateOrUpdateMissionDataInput): MissionEntity? { + private val host = "https://monitorenv.din.developpement-durable.gouv.fr" + + override fun createMission(mission: MissionEnv): MissionEntity? { val url = "$host/api/v1/missions"; logger.info("Sending POST request for Env mission creation URL: $url") return try { @@ -38,7 +40,7 @@ class APIEnvMissionRepositoryV2( val request = HttpRequest .newBuilder() .uri(URI.create(url)) - .method("POST", HttpRequest.BodyPublishers.ofString(gson.toJson(input))) + .method("POST", HttpRequest.BodyPublishers.ofString(gson.toJson(mission))) .header("Content-Type", "application/json") .build();