Skip to content

Commit

Permalink
Refactor multiple code parts in prior notification
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Mar 25, 2024
1 parent aed0ad2 commit 871653c
Show file tree
Hide file tree
Showing 19 changed files with 512 additions and 451 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package fr.gouv.cnsp.monitorfish.domain.entities.prior_notification
import fr.gouv.cnsp.monitorfish.domain.entities.logbook.Catch
import fr.gouv.cnsp.monitorfish.domain.entities.logbook.LogbookTripGear
import fr.gouv.cnsp.monitorfish.domain.entities.logbook.LogbookTripSegment
import fr.gouv.cnsp.monitorfish.domain.entities.vessel.Vessel

data class PriorNotification(
val id: Long,
val expectedArrivalDate: String?,
val expectedLandingDate: String?,
val isVesselUnderCharter: Boolean?,
val notificationTypeLabel: String? = null,
val onboardCatches: List<Catch>,
val portLocode: String?,
Expand All @@ -20,18 +20,10 @@ data class PriorNotification(
val tripGears: List<LogbookTripGear>,
val tripSegments: List<LogbookTripSegment>,
val types: List<PriorNotificationType>,
val vesselId: Int,
val vesselExternalReferenceNumber: String?,
// ISO Alpha-2 country code
val vesselFlagCountryCode: String?,
val vesselInternalReferenceNumber: String?,
val vesselIrcs: String?,
val vessel: Vessel,
val vesselLastControlDate: String?,
val vesselLength: Double?,
val vesselMmsi: String?,
val vesselName: String?,
val vesselRiskFactor: Double?,
val vesselRiskFactorImpact: Double?,
val vesselRiskFactorProbability: Double?,
val vesselRiskFactorDetectability: Double?,
val vesselRiskFactor: Double?,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.gouv.cnsp.monitorfish.domain.use_cases.prior_notification

import fr.gouv.cnsp.monitorfish.config.UseCase
import fr.gouv.cnsp.monitorfish.domain.entities.port.Port
import fr.gouv.cnsp.monitorfish.domain.entities.prior_notification.PriorNotification
import fr.gouv.cnsp.monitorfish.domain.exceptions.CodeNotFoundException
import fr.gouv.cnsp.monitorfish.domain.filters.LogbookReportFilter
Expand All @@ -27,21 +28,13 @@ class GetPriorNotifications(
null
}

val seaFront = port?.latitude?.let { latitude ->
port.longitude?.let { longitude ->
val point = GeometryFactory().createPoint(Coordinate(longitude, latitude))
val seaFront = getSeaFrontFromPort(port)

facadeAreasRepository.findByIncluding(point).firstOrNull()?.facade
}
}

val reportingsCount = priorNotification.vesselId.let { vesselId ->
reportingRepository.findCurrentAndArchivedByVesselIdEquals(
vesselId,
// TODO Fix that.
fromDate = ZonedDateTime.now().minusYears(2),
).count()
}
val reportingsCount = reportingRepository.findCurrentAndArchivedByVesselIdEquals(
priorNotification.vessel.id,
// TODO Fix that.
fromDate = ZonedDateTime.now().minusYears(2),
).count()

priorNotification.copy(
portName = port?.name,
Expand All @@ -52,4 +45,14 @@ class GetPriorNotifications(

return priorNotifications
}

private fun getSeaFrontFromPort(port: Port?): String? {
if (port?.latitude == null || port.longitude == null) {
return null
}

val point = GeometryFactory().createPoint(Coordinate(port.longitude, port.latitude))

return facadeAreasRepository.findByIncluding(point).firstOrNull()?.facade
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fr.gouv.cnsp.monitorfish.infrastructure.api.bff
import fr.gouv.cnsp.monitorfish.domain.filters.LogbookReportFilter
import fr.gouv.cnsp.monitorfish.domain.use_cases.prior_notification.GetPriorNotificationTypes
import fr.gouv.cnsp.monitorfish.domain.use_cases.prior_notification.GetPriorNotifications
import fr.gouv.cnsp.monitorfish.infrastructure.api.input.LogbookReportFilterDataInput
import fr.gouv.cnsp.monitorfish.infrastructure.api.outputs.PriorNotificationDataOutput
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
Expand All @@ -21,9 +22,11 @@ class PriorNotificationController(
@GetMapping("")
@Operation(summary = "Get all prior notifications")
fun getAll(
@ModelAttribute filter: LogbookReportFilter,
@ModelAttribute filter: LogbookReportFilterDataInput,
): List<PriorNotificationDataOutput> {
return getPriorNotifications.execute(filter).map { PriorNotificationDataOutput.fromPriorNotification(it) }
return getPriorNotifications.execute(filter.toLogbookReportFilter()).map {
PriorNotificationDataOutput.fromPriorNotification(it)
}
}

@GetMapping("/types")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.input

import fr.gouv.cnsp.monitorfish.domain.filters.LogbookReportFilter

class LogbookReportFilterDataInput(
val flagStates: List<String>? = null,
val isLessThanTwelveMetersVessel: Boolean? = null,
val lastControlledAfter: String? = null,
val lastControlledBefore: String? = null,
val portLocodes: List<String>? = null,
val priorNotificationTypes: List<String>? = null,
val searchQuery: String? = null,
val specyCodes: List<String>? = null,
val tripSegmentSegments: List<String>? = null,
val tripGearCodes: List<String>? = null,
val willArriveAfter: String? = null,
val willArriveBefore: String? = null,
) {
fun toLogbookReportFilter() = LogbookReportFilter(
flagStates = this.flagStates,
isLessThanTwelveMetersVessel = this.isLessThanTwelveMetersVessel,
lastControlledAfter = this.lastControlledAfter,
lastControlledBefore = this.lastControlledBefore,
portLocodes = this.portLocodes,
priorNotificationTypes = this.priorNotificationTypes,
searchQuery = this.searchQuery,
specyCodes = this.specyCodes,
tripSegmentSegments = this.tripSegmentSegments,
tripGearCodes = this.tripGearCodes,
willArriveAfter = this.willArriveAfter,
willArriveBefore = this.willArriveBefore,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class PriorNotificationDataOutput(
val id: Long,
val expectedArrivalDate: String?,
val expectedLandingDate: String?,
val isVesselUnderCharter: Boolean?,
val notificationTypeLabel: String?,
val onBoardCatches: List<LogbookMessageCatchDataOutput>,
val portLocode: String?,
Expand All @@ -18,15 +17,8 @@ class PriorNotificationDataOutput(
val tripGears: List<LogbookMessageTripGearDataOutput>,
val tripSegments: List<LogbookMessageTripSegmentDataOutput>,
val types: List<PriorNotificationTypeDataOutput>,
val vesselId: Int?,
val vesselExternalReferenceNumber: String?,
val vesselFlagCountryCode: String?,
val vesselInternalReferenceNumber: String?,
val vesselIrcs: String?,
val vessel: VesselDataOutput,
val vesselLastControlDate: String?,
val vesselLength: Double?,
val vesselMmsi: String?,
val vesselName: String?,
val vesselRiskFactorImpact: Double?,
val vesselRiskFactorProbability: Double?,
val vesselRiskFactorDetectability: Double?,
Expand All @@ -48,7 +40,6 @@ class PriorNotificationDataOutput(
id = priorNotification.id,
expectedArrivalDate = priorNotification.expectedArrivalDate,
expectedLandingDate = priorNotification.expectedLandingDate,
isVesselUnderCharter = priorNotification.isVesselUnderCharter,
notificationTypeLabel = priorNotification.notificationTypeLabel,
onBoardCatches,
portLocode = priorNotification.portLocode,
Expand All @@ -60,15 +51,8 @@ class PriorNotificationDataOutput(
tripGears,
tripSegments,
types,
vesselId = priorNotification.vesselId,
vesselExternalReferenceNumber = priorNotification.vesselExternalReferenceNumber,
vesselFlagCountryCode = priorNotification.vesselFlagCountryCode,
vesselInternalReferenceNumber = priorNotification.vesselInternalReferenceNumber,
vesselIrcs = priorNotification.vesselIrcs,
vessel = VesselDataOutput.fromVessel(priorNotification.vessel),
vesselLastControlDate = priorNotification.vesselLastControlDate,
vesselLength = priorNotification.vesselLength,
vesselMmsi = priorNotification.vesselMmsi,
vesselName = priorNotification.vesselName,
vesselRiskFactorImpact = priorNotification.vesselRiskFactorImpact,
vesselRiskFactorProbability = priorNotification.vesselRiskFactorProbability,
vesselRiskFactorDetectability = priorNotification.vesselRiskFactorDetectability,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,15 @@ data class LogbookReportEntity(
id = id!!,
expectedArrivalDate = expectedArrivalDate,
expectedLandingDate = expectedLandingDate,
isVesselUnderCharter = vessel?.underCharter,
types = types,
onboardCatches = onboardCatches,
portLocode = portLocode,
purposeCode = landingCauseCode,
sentAt = reportDateTime.toString(),
tripGears = tripGears,
tripSegments = tripSegments,
vesselId = vessel!!.id,
vesselExternalReferenceNumber = vessel.externalReferenceNumber,
vesselFlagCountryCode = vessel.flagState,
vesselInternalReferenceNumber = vessel.internalReferenceNumber,
vesselIrcs = vessel.ircs,
vesselMmsi = vessel.mmsi,
vessel = vessel!!.toVessel(),
vesselLastControlDate = vesselRiskFactor?.lastControlDatetime?.toString(),
vesselLength = vessel.length,
vesselName = vessel.vesselName,
vesselRiskFactor = vesselRiskFactor?.riskFactor,
vesselRiskFactorDetectability = vesselRiskFactor?.detectabilityRiskFactor,
vesselRiskFactorImpact = vesselRiskFactor?.impactRiskFactor,
Expand Down
Loading

0 comments on commit 871653c

Please sign in to comment.