Skip to content

Commit

Permalink
fix: add control plans to envaction saving usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault authored and maximeperraultdev committed Sep 25, 2024
1 parent 68c76e3 commit 60ad0eb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,6 @@ class MissionModel(
resource = controlUnitResourceModel,
mission = missionModel,
)

MissionControlResourceModel(
resource = controlUnitResourceModel,
mission = missionModel,
)
}
missionModel.controlResources?.addAll(missionControlUnitResourceModels)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionEntit
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException
import fr.gouv.cacem.monitorenv.domain.repositories.IEnvActionRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ControlPlanSubThemeModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ControlPlanTagModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.ControlPlanThemeModel
import fr.gouv.cacem.monitorenv.infrastructure.database.model.EnvActionModel
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanSubThemeRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanTagRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBControlPlanThemeRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBEnvActionRepository
import fr.gouv.cacem.monitorenv.infrastructure.database.repositories.interfaces.IDBMissionRepository
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -16,22 +22,43 @@ import java.util.UUID
class JpaEnvActionRepository(
private val idbEnvActionRepository: IDBEnvActionRepository,
private val idbMissionRepository: IDBMissionRepository,
private val idbControlPlanThemeRepository: IDBControlPlanThemeRepository,
private val idbControlPlanSubThemeRepository: IDBControlPlanSubThemeRepository,
private val idbControlPlanTagRepository: IDBControlPlanTagRepository,
private val objectMapper: ObjectMapper,
) : IEnvActionRepository {
override fun findById(id: UUID): EnvActionEntity? {
return idbEnvActionRepository.findByIdOrNull(id)?.toActionEntity(objectMapper)
}

override fun save(envAction: EnvActionEntity): EnvActionEntity {
envAction.missionId?.let {
val mission = idbMissionRepository.getReferenceById(it)
envAction.missionId?.let { missionId ->
val mission = idbMissionRepository.getReferenceById(missionId)
val controlPlanThemesReferenceModelMap: MutableMap<Int, ControlPlanThemeModel> = mutableMapOf()
val controlPlanTagssReferenceModelMap: MutableMap<Int, ControlPlanTagModel> = mutableMapOf()
val controlPlanSubThemesReferenceModelMap: MutableMap<Int, ControlPlanSubThemeModel> = mutableMapOf()


envAction.controlPlans?.forEach { controlPlan ->
controlPlan.tagIds?.forEach { tagId ->
controlPlanTagssReferenceModelMap[tagId] = idbControlPlanTagRepository.getReferenceById(tagId)
}
controlPlan.themeId?.let { themeId ->
controlPlanThemesReferenceModelMap[themeId] =
idbControlPlanThemeRepository.getReferenceById(themeId)
}
controlPlan.subThemeIds?.forEach { subthemeId ->
controlPlanSubThemesReferenceModelMap[subthemeId] =
idbControlPlanSubThemeRepository.getReferenceById(subthemeId)
}
}
return idbEnvActionRepository.save(
EnvActionModel.fromEnvActionEntity(
envAction,
mission = mission,
controlPlanThemesReferenceModelMap = mapOf(),
controlPlanTagsReferenceModelMap = mapOf(),
controlPlanSubThemesReferenceModelMap = mapOf(),
controlPlanThemesReferenceModelMap = controlPlanThemesReferenceModelMap,
controlPlanTagsReferenceModelMap = controlPlanTagssReferenceModelMap,
controlPlanSubThemesReferenceModelMap = controlPlanSubThemesReferenceModelMap,
mapper = objectMapper,
),
).toActionEntity(objectMapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import fr.gouv.cacem.monitorenv.domain.entities.mission.ActionCompletionEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.MonitorFishActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionControlPlanEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.monitorfish.MonitorFishMissionActionEntity
import fr.gouv.cacem.monitorenv.domain.mappers.EnvActionMapper
Expand All @@ -21,6 +22,7 @@ class EnvActionFixture {
endTime: ZonedDateTime? = null,
observationsByUnit: String? = null,
missionId: Int? = 1,
controlPlans: List<EnvActionControlPlanEntity>? = null
): EnvActionEntity {
return EnvActionMapper.getEnvActionEntityFromJSON(
mapper,
Expand All @@ -30,7 +32,7 @@ class EnvActionFixture {
actionStartDateTimeUtc = startTime,
completedBy = "John Doe",
completion = ActionCompletionEnum.COMPLETED,
controlPlans = listOf(),
controlPlans = controlPlans,
department = "Department X",
facade = "Facade Y",
geom = null,
Expand Down

0 comments on commit 60ad0eb

Please sign in to comment.