Skip to content

Commit

Permalink
[Hotfix] Fixe la multiplication des moyens dans les missions (#931)
Browse files Browse the repository at this point in the history
## Related Pull Requests & Issues

None

----

- [ ] Tests E2E (Cypress)
  • Loading branch information
ivangabriele authored Oct 30, 2023
2 parents 05ffc08 + 7e6aa55 commit 49d6150
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 476 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,4 @@ data class ControlUnitResourceEntity(

return result
}

fun toLegacyControlUnitEntity(): LegacyControlUnitResourceEntity {
return LegacyControlUnitResourceEntity(
id = requireNotNull(id),
name,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package fr.gouv.cacem.monitorenv.domain.entities.controlUnit
// TODO Replace this entity with `ControlUnitResourceEntity`. It's left for `MissionEntity.units` backward compatibility.
data class LegacyControlUnitResourceEntity(
val id: Int,
val controlUnitId: Int,
val name: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data class FullControlUnitResourceDTO(
fun toLegacyControlUnitResource(): LegacyControlUnitResourceEntity {
return LegacyControlUnitResourceEntity(
id = requireNotNull(controlUnitResource.id),
controlUnitId = requireNotNull(controlUnit.id),
name = controlUnitResource.name,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ data class ControlUnitResourceModel(
fun toLegacyControlUnitResource(): LegacyControlUnitResourceEntity {
return LegacyControlUnitResourceEntity(
id = requireNotNull(id),
controlUnitId = requireNotNull(controlUnit.id),
name,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.gouv.cacem.monitorenv.infrastructure.database.model

import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.ControlUnitResourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitResourceEntity
import jakarta.persistence.*

@Entity
Expand All @@ -19,16 +19,11 @@ data class MissionControlResourceModel(
@JoinColumn(name = "control_resource_id")
var resource: ControlUnitResourceModel,
) {
fun toControlUnitResource(): ControlUnitResourceEntity {
return ControlUnitResourceEntity(
id = resource.id,
baseId = requireNotNull(resource.base.id),
fun toLegacyControlUnitResource(): LegacyControlUnitResourceEntity {
return LegacyControlUnitResourceEntity(
id = requireNotNull(resource.id),
controlUnitId = requireNotNull(resource.controlUnit.id),
isArchived = resource.isArchived,
name = resource.name,
note = resource.note,
photo = byteArrayOf(),
type = resource.type,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ data class MissionModel(
) {
fun toMissionEntity(objectMapper: ObjectMapper): MissionEntity {
val controlUnits = controlUnits.mapOrElseEmpty { missionControlUnitModel ->
val controlUnitResources = controlResources.mapOrElseEmpty { it.toControlUnitResource() }
val controlUnitResources = controlResources.mapOrElseEmpty {
it.toLegacyControlUnitResource()
}.filter { it.controlUnitId == missionControlUnitModel.unit.id }

missionControlUnitModel.unit.toLegacyControlUnit().copy(
contact = missionControlUnitModel.contact,
resources = controlUnitResources.map { it.toLegacyControlUnitEntity() },
resources = controlUnitResources,
)
}

Expand Down Expand Up @@ -195,14 +197,14 @@ data class MissionModel(
missionModel.envActions?.add(EnvActionModel.fromEnvActionEntity(it, missionModel, mapper))
}

mission.controlUnits.map {
mission.controlUnits.map { controlUnit ->
val missionControlUnitModel = MissionControlUnitModel.fromLegacyControlUnit(
it,
controlUnit,
missionModel,
)
missionModel.controlUnits?.add(missionControlUnitModel)

val missionControlUnitResourceModels = it.resources.map { controlUnitResource ->
val missionControlUnitResourceModels = controlUnit.resources.map { controlUnitResource ->
val controlUnitResourceModel = requireNotNull(controlUnitResourceModelMap[controlUnitResource.id])

MissionControlResourceModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,15 @@ class JpaMissionRepository(
@Transactional
override fun save(mission: MissionEntity): MissionDTO {
// Extract all control units resources unique control unit resource IDs
val uniqueControlUnitResourceIds =
mission.controlUnits
.flatMap { controlUnit -> controlUnit.resources.map { it.id } }
.distinct()
val uniqueControlUnitResourceIds = mission.controlUnits
.flatMap { controlUnit -> controlUnit.resources.map { it.id } }
.distinct()
// Fetch all of them as models
val controlUnitResourceModels =
dbControlUnitResourceRepository.findAllById(uniqueControlUnitResourceIds)
val controlUnitResourceModels = dbControlUnitResourceRepository.findAllById(uniqueControlUnitResourceIds)
// Create an `[id] → ControlUnitResourceModel` map
val controlUnitResourceModelMap =
controlUnitResourceModels.associateBy { requireNotNull(it.id) }
val controlUnitResourceModelMap = controlUnitResourceModels.associateBy { requireNotNull(it.id) }

val missionModel =
MissionModel.fromMissionEntity(mission, mapper, controlUnitResourceModelMap)
val missionModel = MissionModel.fromMissionEntity(mission, mapper, controlUnitResourceModelMap)

return dbMissionRepository.save(missionModel).toMissionDTO(mapper)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ALTER TABLE public.missions_control_resources
ADD CONSTRAINT fk_missions_control_resources_control_resource_id_control_unit_resources
FOREIGN KEY (control_resource_id)
REFERENCES public.control_unit_resources(id);

-- Remove duplicates in `missions_control_resources` (both same `mission_id` and `control_resource_id`):

WITH missions_with_duplicate_resources AS (
SELECT
mission_id,
control_resource_id,
ARRAY_AGG(id) AS ids
FROM missions_control_resources
GROUP BY 1, 2
HAVING COUNT(*) > 1
),

missions_with_duplicate_resources_unnested AS (
SELECT
mission_id,
control_resource_id,
unnest(ids) AS id
FROM missions_with_duplicate_resources
),

missions_with_duplicate_resources_numbered AS (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY mission_id, control_resource_id ORDER BY id) AS rk
FROM missions_with_duplicate_resources_unnested
),

missions_control_resources_to_delete AS (
SELECT id
FROM missions_with_duplicate_resources_numbered
WHERE rk > 1
)

DELETE FROM missions_control_resources
WHERE id IN (SELECT id FROM missions_control_resources_to_delete)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ActionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ThemeEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.ActionTargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.EnvActionControlEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.FormalNoticeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.InfractionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.InfractionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.VesselSizeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.VesselTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.*
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingEntity
import fr.gouv.cacem.monitorenv.domain.entities.reporting.ReportingTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.reporting.SourceTypeEnum
Expand Down Expand Up @@ -196,6 +192,7 @@ class MissionsControllerITests {
listOf(
LegacyControlUnitResourceEntity(
id = 2,
controlUnitId = 1,
name =
"Ressource 2",
),
Expand Down Expand Up @@ -519,8 +516,8 @@ class MissionsControllerITests {
listOf(
LegacyControlUnitResourceEntity(
id = 2,
name =
"Ressource 2",
controlUnitId = 1,
name = "Ressource 2",
),
),
isArchived = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ApiLegacyControlUnitsControllerITests {
resources = listOf(
LegacyControlUnitResourceEntity(
id = 0,
controlUnitId = 4,
name = "Vedette",
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package fr.gouv.cacem.monitorenv.infrastructure.database.repositories
import fr.gouv.cacem.monitorenv.domain.entities.VehicleTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitEntity
import fr.gouv.cacem.monitorenv.domain.entities.controlUnit.LegacyControlUnitResourceEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.*
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.EnvActionNoteEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.EnvActionSurveillanceEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.ThemeEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.ActionTargetTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.EnvActionControlEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.FormalNoticeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.InfractionEntity
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.InfractionTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.VesselSizeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.VesselTypeEnum
import fr.gouv.cacem.monitorenv.domain.entities.mission.envAction.envActionControl.infraction.*
import fr.gouv.cacem.monitorenv.domain.use_cases.missions.dtos.MissionDTO
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.catchThrowable
Expand Down Expand Up @@ -114,14 +112,14 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
controlUnits =
listOf(
LegacyControlUnitEntity(
id = 10004,
name = "DPM – DDTM 35",
administration = "DDTM",
id = 10121,
name = "PAM Jeanne Barret",
administration = "DIRM / DM",
isArchived = false,
resources =
listOf(
resources = listOf(
LegacyControlUnitResourceEntity(
id = 8,
controlUnitId = 10121,
name = "PAM Jeanne Barret",
),
),
Expand All @@ -135,11 +133,12 @@ class JpaMissionRepositoryITests : AbstractDBTests() {

// Then
assertThat(newMissionCreated.mission.controlUnits).hasSize(1)
assertThat(newMissionCreated.mission.controlUnits.first().id).isEqualTo(10004)
assertThat(newMissionCreated.mission.controlUnits.first().name).isEqualTo("DPM – DDTM 35")
assertThat(newMissionCreated.mission.controlUnits.first().administration).isEqualTo("DDTM")
assertThat(newMissionCreated.mission.controlUnits.first().id).isEqualTo(10121)
assertThat(newMissionCreated.mission.controlUnits.first().name).isEqualTo("PAM Jeanne Barret")
assertThat(newMissionCreated.mission.controlUnits.first().administration).isEqualTo("DIRM / DM")
assertThat(newMissionCreated.mission.controlUnits.first().resources).hasSize(1)
assertThat(newMissionCreated.mission.controlUnits.first().resources.first().id).isEqualTo(8)
assertThat(newMissionCreated.mission.controlUnits.first().resources.first().controlUnitId).isEqualTo(10121)
assertThat(newMissionCreated.mission.controlUnits.first().resources.first().name)
.isEqualTo("PAM Jeanne Barret")
assertThat(newMissionCreated.mission.envActions).hasSize(3)
Expand Down Expand Up @@ -190,10 +189,10 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
name = "DPM – DDTM 35",
administration = "DDTM",
isArchived = false,
resources =
listOf(
resources = listOf(
LegacyControlUnitResourceEntity(
id = 8,
controlUnitId = 10004,
name = "PAM Jeanne Barret",
),
),
Expand All @@ -210,19 +209,19 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
controlUnits =
listOf(
LegacyControlUnitEntity(
id = 10004,
name = "DPM – DDTM 35",
administration = "DDTM",
id = 10002,
name = "DML 2A",
administration = "DIRM / DM",
isArchived = false,
resources =
listOf(
resources = listOf(
LegacyControlUnitResourceEntity(
id = 8,
name =
"PAM Jeanne Barret",
id = 3,
controlUnitId = 10002,
name = "Semi-rigide 1",
),
LegacyControlUnitResourceEntity(
id = 5,
controlUnitId = 10002,
name = "Voiture",
),
),
Expand All @@ -233,16 +232,16 @@ class JpaMissionRepositoryITests : AbstractDBTests() {

// Then
assertThat(newMissionUpdated.mission.controlUnits).hasSize(1)
assertThat(newMissionUpdated.mission.controlUnits.first().id).isEqualTo(10004)
assertThat(newMissionUpdated.mission.controlUnits.first().name).isEqualTo("DPM – DDTM 35")
assertThat(newMissionUpdated.mission.controlUnits.first().administration).isEqualTo("DDTM")
assertThat(newMissionUpdated.mission.controlUnits.first().id).isEqualTo(10002)
assertThat(newMissionUpdated.mission.controlUnits.first().name).isEqualTo("DML 2A")
assertThat(newMissionUpdated.mission.controlUnits.first().administration).isEqualTo("DIRM / DM")
assertThat(newMissionUpdated.mission.controlUnits.first().resources).hasSize(2)
assertThat(newMissionUpdated.mission.controlUnits.first().resources.first().id).isEqualTo(8)
assertThat(newMissionUpdated.mission.controlUnits.first().resources.first().name)
.isEqualTo("PAM Jeanne Barret")
assertThat(newMissionUpdated.mission.controlUnits.first().resources.first().id).isEqualTo(3)
assertThat(newMissionUpdated.mission.controlUnits.first().resources.first().controlUnitId).isEqualTo(10002)
assertThat(newMissionUpdated.mission.controlUnits.first().resources.first().name).isEqualTo("Semi-rigide 1")
assertThat(newMissionUpdated.mission.controlUnits.first().resources.last().id).isEqualTo(5)
assertThat(newMissionUpdated.mission.controlUnits.first().resources.last().name)
.isEqualTo("Voiture")
assertThat(newMissionUpdated.mission.controlUnits.first().resources.last().controlUnitId).isEqualTo(10002)
assertThat(newMissionUpdated.mission.controlUnits.first().resources.last().name).isEqualTo("Voiture")
}

@Test
Expand All @@ -265,10 +264,10 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
name = "DPM – DDTM 35",
administration = "DDTM",
isArchived = false,
resources =
listOf(
resources = listOf(
LegacyControlUnitResourceEntity(
id = 123456,
controlUnitId = 5,
name = "PAM Jeanne Barret",
),
),
Expand Down Expand Up @@ -297,8 +296,7 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
missionSource = MissionSourceEnum.MONITORENV,
hasMissionOrder = false,
isUnderJdp = false,
controlUnits =
listOf(
controlUnits = listOf(
LegacyControlUnitEntity(
id = 123456,
name = "PAM Jeanne Barret",
Expand Down Expand Up @@ -571,18 +569,18 @@ class JpaMissionRepositoryITests : AbstractDBTests() {
listOf(
LegacyControlUnitResourceEntity(
id = 3,
name =
"Semi-rigide 1",
controlUnitId = 10002,
name = "Semi-rigide 1",
),
LegacyControlUnitResourceEntity(
id = 4,
name =
"Semi-rigide 2",
controlUnitId = 10002,
name = "Semi-rigide 2",
),
LegacyControlUnitResourceEntity(
id = 5,
name =
"Voiture",
controlUnitId = 10002,
name = "Voiture",
),
),
),
Expand Down
4 changes: 0 additions & 4 deletions datascience/src/pipeline/data/dummy_area_notes.csv

This file was deleted.

8 changes: 0 additions & 8 deletions datascience/src/pipeline/data/dummy_bases.csv

This file was deleted.

Loading

0 comments on commit 49d6150

Please sign in to comment.