diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/mission/MissionValidator.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/mission/MissionValidator.kt index 0d07140a1..7c2be4019 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/mission/MissionValidator.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/mission/MissionValidator.kt @@ -97,6 +97,13 @@ class MissionValidator : Validator { validateControlPlan(control) } + validateInfractions(control, isMissionEnded) + } + + private fun validateInfractions( + control: EnvActionControlEntity, + isMissionEnded: Boolean, + ) { val sumOfNbTarget = control.infractions?.sumOf { infraction -> infraction.nbTarget } if (sumOfNbTarget != 0 && sumOfNbTarget != null && (control.actionNumberOfControls != null && sumOfNbTarget > control.actionNumberOfControls)) { throw BackendUsageException( diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/reporting/ReportingValidator.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/reporting/ReportingValidator.kt index 7d73bb907..57b789f62 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/reporting/ReportingValidator.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/reporting/ReportingValidator.kt @@ -24,12 +24,6 @@ class ReportingValidator : Validator { "Le trigramme \"ouvert par\" doit avoir 3 lettres", ) } - if (reporting.reportingSources.isEmpty()) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "Une source du signalement est obligatoire", - ) - } if (reporting.validityTime == 0) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, @@ -42,13 +36,30 @@ class ReportingValidator : Validator { "Un sous-thème est obligatoire", ) } + if (reporting.targetType === TargetTypeEnum.OTHER && reporting.description === null) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "La description de la cible est obligatoire", + ) + } + validateReportingSources(reporting) + } + + private fun validateReportingSources(reporting: ReportingEntity) { + if (reporting.reportingSources.isEmpty()) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "Une source du signalement est obligatoire", + ) + } reporting.reportingSources.forEach { source -> + val errorMessage = "La source du signalement est invalide" when (source.sourceType) { SourceTypeEnum.SEMAPHORE -> { if (source.semaphoreId === null || source.controlUnitId !== null || source.sourceName !== null) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, - "La source du signalement est invalide", + errorMessage, ) } } @@ -57,7 +68,7 @@ class ReportingValidator : Validator { if (source.semaphoreId !== null || source.controlUnitId === null || source.sourceName !== null) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, - "La source du signalement est invalide", + errorMessage, ) } } @@ -66,18 +77,11 @@ class ReportingValidator : Validator { if (source.semaphoreId !== null || source.controlUnitId !== null || source.sourceName === null) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, - "La source du signalement est invalide", + errorMessage, ) } } } } - - if (reporting.targetType === TargetTypeEnum.OTHER && reporting.description === null) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "La description de la cible est obligatoire", - ) - } } } diff --git a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/vigilance_area/VigilanceAreaValidator.kt b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/vigilance_area/VigilanceAreaValidator.kt index 619806c60..247b0f7e8 100644 --- a/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/vigilance_area/VigilanceAreaValidator.kt +++ b/backend/src/main/kotlin/fr/gouv/cacem/monitorenv/domain/validators/vigilance_area/VigilanceAreaValidator.kt @@ -19,64 +19,68 @@ class VigilanceAreaValidator : Validator { logger.info("Validating vigilance area: ${vigilanceArea.id}") if (!vigilanceArea.isDraft) { - if (vigilanceArea.geom === null) { + validatePublishedVigilanceArea(vigilanceArea) + } + } + + private fun validatePublishedVigilanceArea(vigilanceArea: VigilanceAreaEntity) { + if (vigilanceArea.geom === null) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "La géométrie est obligatoire", + ) + } + if (vigilanceArea.comments === null) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "Un commentaire est obligatoire", + ) + } + if (vigilanceArea.createdBy !== null && vigilanceArea.createdBy.length != NB_CHAR_MAX) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "Le trigramme \"créé par\" doit avoir 3 lettres", + ) + } + if (vigilanceArea.themes?.isEmpty() == true) { + throw BackendUsageException(BackendUsageErrorCode.UNVALID_PROPERTY, "Un thème est obligatoire") + } + if (!vigilanceArea.isAtAllTimes) { + if (vigilanceArea.startDatePeriod === null) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, - "La géométrie est obligatoire", + "La date de début est obligatoire", ) } - if (vigilanceArea.comments === null) { + if (vigilanceArea.endDatePeriod === null) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, - "Un commentaire est obligatoire", + "La date de fin est obligatoire", ) } - if (vigilanceArea.createdBy !== null && vigilanceArea.createdBy.length != NB_CHAR_MAX) { + if (vigilanceArea.frequency === null) { throw BackendUsageException( BackendUsageErrorCode.UNVALID_PROPERTY, - "Le trigramme \"créé par\" doit avoir 3 lettres", + "La fréquence est obligatoire", ) } - if (vigilanceArea.themes?.isEmpty() == true) { - throw BackendUsageException(BackendUsageErrorCode.UNVALID_PROPERTY, "Un thème est obligatoire") + if (vigilanceArea.frequency !== FrequencyEnum.NONE && vigilanceArea.endingCondition === null) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "La condition de fin est obligatoire", + ) + } + if (vigilanceArea.endingCondition === EndingConditionEnum.END_DATE && vigilanceArea.endingOccurrenceDate === null) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "La date de fin de l'occurence est obligatoire", + ) } - if (!vigilanceArea.isAtAllTimes) { - if (vigilanceArea.startDatePeriod === null) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "La date de début est obligatoire", - ) - } - if (vigilanceArea.endDatePeriod === null) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "La date de fin est obligatoire", - ) - } - if (vigilanceArea.frequency === null) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "La fréquence est obligatoire", - ) - } - if (vigilanceArea.frequency !== FrequencyEnum.NONE && vigilanceArea.endingCondition === null) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "La condition de fin est obligatoire", - ) - } - if (vigilanceArea.endingCondition === EndingConditionEnum.END_DATE && vigilanceArea.endingOccurrenceDate === null) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "La date de fin de l'occurence est obligatoire", - ) - } - if (vigilanceArea.endingCondition === EndingConditionEnum.OCCURENCES_NUMBER && (vigilanceArea.endingOccurrencesNumber === null || vigilanceArea.endingOccurrencesNumber == 0)) { - throw BackendUsageException( - BackendUsageErrorCode.UNVALID_PROPERTY, - "Le nombre d'occurence est obligatoire", - ) - } + if (vigilanceArea.endingCondition === EndingConditionEnum.OCCURENCES_NUMBER && (vigilanceArea.endingOccurrencesNumber === null || vigilanceArea.endingOccurrencesNumber == 0)) { + throw BackendUsageException( + BackendUsageErrorCode.UNVALID_PROPERTY, + "Le nombre d'occurence est obligatoire", + ) } } } diff --git a/frontend/src/features/Mission/useCases/saveMission.ts b/frontend/src/features/Mission/useCases/saveMission.ts index 9d0cf36b6..05b16cc47 100644 --- a/frontend/src/features/Mission/useCases/saveMission.ts +++ b/frontend/src/features/Mission/useCases/saveMission.ts @@ -101,7 +101,7 @@ export const saveMission = reportings }) } else if ('data' in response.error) { - if (response.error.data?.code === ApiErrorCode.CHILD_ALREADY_ATTACHED) { + if (response.error.data?.type === ApiErrorCode.CHILD_ALREADY_ATTACHED) { throw Error('Le signalement est déjà rattaché à une mission') } if (response.error.data?.code === ApiErrorCode.UNVALID_PROPERTY) { diff --git a/frontend/src/features/Reportings/useCases/saveReporting.ts b/frontend/src/features/Reportings/useCases/saveReporting.ts index d7339a6a8..2e9235d6d 100644 --- a/frontend/src/features/Reportings/useCases/saveReporting.ts +++ b/frontend/src/features/Reportings/useCases/saveReporting.ts @@ -69,7 +69,7 @@ export const saveReporting = dispatch(updateMapInteractionListeners(MapInteractionListenerEnum.NONE)) dispatch(reportingActions.deleteSelectedReporting(values.id)) } else if ('data' in response.error) { - if (response.error.data?.code === ApiErrorCode.CHILD_ALREADY_ATTACHED) { + if (response.error.data?.type === ApiErrorCode.CHILD_ALREADY_ATTACHED) { throw Error('Le signalement est déjà rattaché à une mission') } if (response.error.data?.code === ApiErrorCode.UNVALID_PROPERTY) {