diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/reportings/CreateOrUpdateReporting.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/reportings/CreateOrUpdateReporting.kt index e0f4ca11ae..66e318542a 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/reportings/CreateOrUpdateReporting.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/use_cases/reportings/CreateOrUpdateReporting.kt @@ -31,8 +31,7 @@ class CreateOrUpdateReporting( var attachedToMissionAtUtc: ZonedDateTime? = null var detachedFromMissionAtUtc: ZonedDateTime? = null - if (reporting.missionId != null) { - // TO CHECK if the date is with or without UTC + if (reporting.missionId != null && reporting.attachedToMissionAtUtc == null) { attachedToMissionAtUtc = ZonedDateTime.now() detachedFromMissionAtUtc = null } diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/interfaces/IDBReportingRepository.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/interfaces/IDBReportingRepository.kt index 9126b2523b..53b41303c0 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/interfaces/IDBReportingRepository.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/infrastructure/database/repositories/interfaces/IDBReportingRepository.kt @@ -38,11 +38,11 @@ interface IDBReportingRepository : JpaRepository { """ UPDATE reportings SET - mission_id = :missionId, - attached_to_mission_at_utc = CASE WHEN mission_id is null AND id in (:reportingIds) THEN NOW() ELSE attached_to_mission_at_utc END, - detached_from_mission_at_utc = CASE WHEN id not in (:reportingIds) THEN NOW() ELSE CAST(null as timestamp ) END - WHERE id in (:reportingIds) - """, + mission_id = :missionId, + attached_to_mission_at_utc = CASE WHEN (mission_id IS NULL OR mission_id = (:missionId)) AND id IN (:reportingIds) THEN NOW() ELSE attached_to_mission_at_utc END, + detached_from_mission_at_utc = CASE WHEN id NOT IN (:reportingIds) THEN NOW() ELSE NULL END + WHERE id in (:reportingIds) OR (mission_id = :missionId AND detached_from_mission_at_utc IS NULL) + """, nativeQuery = true, ) fun attachReportingsToMission(reportingIds: List, missionId: Int)