Skip to content

Commit

Permalink
tech: refactor (extract method)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault committed Jan 22, 2025
1 parent 6045ffd commit 70fec30
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ class MissionValidator : Validator<MissionEntity> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ class ReportingValidator : Validator<ReportingEntity> {
"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,
Expand All @@ -42,13 +36,30 @@ class ReportingValidator : Validator<ReportingEntity> {
"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,
)
}
}
Expand All @@ -57,7 +68,7 @@ class ReportingValidator : Validator<ReportingEntity> {
if (source.semaphoreId !== null || source.controlUnitId === null || source.sourceName !== null) {
throw BackendUsageException(
BackendUsageErrorCode.UNVALID_PROPERTY,
"La source du signalement est invalide",
errorMessage,
)
}
}
Expand All @@ -66,18 +77,11 @@ class ReportingValidator : Validator<ReportingEntity> {
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",
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,64 +19,68 @@ class VigilanceAreaValidator : Validator<VigilanceAreaEntity> {
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",
)
}
}
}
Expand Down

0 comments on commit 70fec30

Please sign in to comment.