Skip to content

Commit

Permalink
Add expiration date to reporting form
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Jan 17, 2025
1 parent af2a296 commit 335bc6b
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ interface ReportingRepository {

fun update(
reportingId: Int,
expirationDate: ZonedDateTime?,
updatedInfractionSuspicion: InfractionSuspicion,
): Reporting

fun update(
reportingId: Int,
expirationDate: ZonedDateTime?,
updatedObservation: Observation,
): Reporting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ArchiveOutdatedReportings(private val reportingRepository: ReportingReposi
val filteredReportingIdsToArchive =
reportingCandidatesToArchive.filter {
it.second.type == AlertTypeMapping.MISSING_FAR_ALERT ||
it.second.type == AlertTypeMapping.THREE_/MILES_TRAWLING_ALERT ||
it.second.type == AlertTypeMapping.THREE_MILES_TRAWLING_ALERT ||
it.second.type == AlertTypeMapping.MISSING_DEP_ALERT ||
it.second.type == AlertTypeMapping.SUSPICION_OF_UNDER_DECLARATION_ALERT
}.map { it.first }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class UpdateReporting(
val currentReporting = reportingRepository.findById(reportingId)
val controlUnits = getAllLegacyControlUnits.execute()
logger.info("Updating reporting id $reportingId for vessel id ${currentReporting.vesselId}")
val expirationDate = updatedInfractionSuspicionOrObservation.expirationDate ?: currentReporting.expirationDate

require(currentReporting.type != ReportingType.ALERT) {
"The edited reporting must be an INFRACTION_SUSPICION or an OBSERVATION"
Expand Down Expand Up @@ -53,10 +54,15 @@ class UpdateReporting(
when (nextReporting) {
is InfractionSuspicion ->
reportingRepository.update(
reportingId,
nextReporting,
reportingId = reportingId,
expirationDate = expirationDate,
updatedInfractionSuspicion = nextReporting,
)
is Observation -> reportingRepository.update(reportingId, nextReporting)
is Observation -> reportingRepository.update(
reportingId = reportingId,
expirationDate = expirationDate,
updatedObservation = nextReporting,
)
else -> throw IllegalArgumentException(
"The new reporting type must be an INFRACTION_SUSPICION or an OBSERVATION",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package fr.gouv.cnsp.monitorfish.domain.use_cases.reporting

import fr.gouv.cnsp.monitorfish.domain.entities.reporting.ReportingActor
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.ReportingType
import java.time.ZonedDateTime

class UpdatedInfractionSuspicionOrObservation(
val reportingActor: ReportingActor,
val type: ReportingType,
val controlUnitId: Int? = null,
val authorTrigram: String,
val authorContact: String? = null,
val expirationDate: ZonedDateTime? = null,
val title: String,
val description: String? = null,
val natinfCode: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package fr.gouv.cnsp.monitorfish.infrastructure.api.input
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.ReportingActor
import fr.gouv.cnsp.monitorfish.domain.entities.reporting.ReportingType
import fr.gouv.cnsp.monitorfish.domain.use_cases.reporting.UpdatedInfractionSuspicionOrObservation
import java.time.ZonedDateTime

class UpdateReportingDataInput(
val reportingActor: ReportingActor,
val type: ReportingType,
val controlUnitId: Int? = null,
val authorTrigram: String,
val authorContact: String? = null,
val expirationDate: String? = null,
val expirationDate: ZonedDateTime? = null,
val title: String,
val description: String? = null,
val natinfCode: Int? = null,
Expand All @@ -22,7 +23,6 @@ class UpdateReportingDataInput(
controlUnitId = this.controlUnitId,
authorTrigram = this.authorTrigram,
authorContact = this.authorContact,
// TODO Finish this adding and get it from the use-case
expirationDate = this.expirationDate,
title = this.title,
description = this.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ data class ReportingEntity(
flagState = flagState,
creationDate = creationDate,
validationDate = validationDate,
expirationDate = expirationDate,
value = ReportingMapper.getReportingValueFromJSON(mapper, value, type),
isArchived = isArchived,
isDeleted = isDeleted,
Expand Down Expand Up @@ -120,6 +121,7 @@ data class ReportingEntity(
flagState = reporting.flagState,
creationDate = reporting.creationDate,
validationDate = reporting.validationDate,
expirationDate = reporting.expirationDate,
value = mapper.writeValueAsString(reporting.value),
isArchived = false,
isDeleted = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ class JpaReportingRepository(
@Transactional
override fun update(
reportingId: Int,
expirationDate: ZonedDateTime?,
updatedInfractionSuspicion: InfractionSuspicion,
): Reporting {
dbReportingRepository.update(
reportingId,
mapper.writeValueAsString(updatedInfractionSuspicion),
ReportingType.INFRACTION_SUSPICION.toString(),
id = reportingId,
value = mapper.writeValueAsString(updatedInfractionSuspicion),
type = ReportingType.INFRACTION_SUSPICION.toString(),
expirationDate = expirationDate?.toInstant(),
)

return dbReportingRepository.findById(reportingId).get().toReporting(mapper)
Expand All @@ -56,12 +58,14 @@ class JpaReportingRepository(
@Transactional
override fun update(
reportingId: Int,
expirationDate: ZonedDateTime?,
updatedObservation: Observation,
): Reporting {
dbReportingRepository.update(
reportingId,
mapper.writeValueAsString(updatedObservation),
ReportingType.OBSERVATION.toString(),
id = reportingId,
value = mapper.writeValueAsString(updatedObservation),
type = ReportingType.OBSERVATION.toString(),
expirationDate = expirationDate?.toInstant(),
)

return dbReportingRepository.findById(reportingId).get().toReporting(mapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import java.time.Instant
import java.time.ZonedDateTime

@DynamicUpdate
interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
Expand Down Expand Up @@ -168,7 +169,8 @@ interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
UPDATE reportings
SET
value = CAST(:value AS JSONB),
type = CAST(:type AS reporting_type)
type = CAST(:type AS reporting_type),
expiration_date = :expirationDate
WHERE id = :id
""",
nativeQuery = true,
Expand All @@ -177,5 +179,6 @@ interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
id: Int,
value: String,
type: String,
expirationDate: Instant?,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class UpdateReportingUTests {
isDeleted = false,
)
given(reportingRepository.findById(any())).willReturn(reporting)
given(reportingRepository.update(any(), isA<InfractionSuspicion>())).willReturn(reporting)
given(reportingRepository.update(any(), anyOrNull(), isA<InfractionSuspicion>())).willReturn(reporting)
given(getReportingWithDMLAndSeaFront.execute(any(), anyOrNull())).willReturn(
InfractionSuspicion(
reportingActor = reportingActor,
Expand Down Expand Up @@ -237,6 +237,7 @@ class UpdateReportingUTests {
@Test
fun `execute Should update the reporting`() {
// Given
val expectedExpirationDate = ZonedDateTime.now().plusDays(2)
val observation =
Observation(
reportingActor = ReportingActor.UNIT,
Expand All @@ -257,12 +258,13 @@ class UpdateReportingUTests {
flagState = CountryCode.FR,
creationDate = ZonedDateTime.now(),
validationDate = ZonedDateTime.now(),
expirationDate = ZonedDateTime.now(),
value = observation as ReportingValue,
isArchived = false,
isDeleted = false,
)
given(reportingRepository.findById(any())).willReturn(reporting)
given(reportingRepository.update(any(), isA<Observation>())).willReturn(reporting)
given(reportingRepository.update(any(), anyOrNull(), isA<Observation>())).willReturn(reporting)
given(
getReportingWithDMLAndSeaFront.execute(any(), anyOrNull()),
).willReturn(observation.copy(description = "Test 2"))
Expand All @@ -279,6 +281,7 @@ class UpdateReportingUTests {
type = ReportingType.OBSERVATION,
controlUnitId = 1,
authorTrigram = "LTH",
expirationDate = expectedExpirationDate,
title = "A reporting",
description = "Test 2",
natinfCode = 1234,
Expand All @@ -287,7 +290,7 @@ class UpdateReportingUTests {

// Then
argumentCaptor<Observation>().apply {
verify(reportingRepository).update(any(), capture())
verify(reportingRepository).update(any(), eq(expectedExpirationDate), capture())

assertThat(allValues.first().description).isEqualTo("Test 2")
}
Expand Down Expand Up @@ -319,7 +322,7 @@ class UpdateReportingUTests {
isDeleted = false,
)
given(reportingRepository.findById(any())).willReturn(reporting)
given(reportingRepository.update(any(), isA<Observation>())).willReturn(reporting)
given(reportingRepository.update(any(), anyOrNull(), isA<Observation>())).willReturn(reporting)
given(getReportingWithDMLAndSeaFront.execute(any(), anyOrNull())).willReturn(
Observation(
reportingActor = ReportingActor.UNIT,
Expand Down Expand Up @@ -349,7 +352,7 @@ class UpdateReportingUTests {

// Then
argumentCaptor<Observation>().apply {
verify(reportingRepository).update(any(), capture())
verify(reportingRepository).update(any(), anyOrNull(), capture())

assertThat(allValues.first().description).isEqualTo("Test 2")
assertThat(allValues.first().natinfCode).isNull()
Expand Down Expand Up @@ -383,7 +386,7 @@ class UpdateReportingUTests {
isDeleted = false,
)
given(reportingRepository.findById(any())).willReturn(reporting)
given(reportingRepository.update(any(), isA<InfractionSuspicion>())).willReturn(reporting)
given(reportingRepository.update(any(), anyOrNull(), isA<InfractionSuspicion>())).willReturn(reporting)
given(getReportingWithDMLAndSeaFront.execute(any(), anyOrNull())).willReturn(
InfractionSuspicion(
reportingActor = ReportingActor.UNIT,
Expand Down Expand Up @@ -415,7 +418,7 @@ class UpdateReportingUTests {

// Then
argumentCaptor<InfractionSuspicion>().apply {
verify(reportingRepository).update(any(), capture())
verify(reportingRepository).update(any(), anyOrNull(), capture())

assertThat(allValues.first().dml).isEqualTo("DML 56")
assertThat(allValues.first().seaFront).isEqualTo("NAMO")
Expand Down Expand Up @@ -447,7 +450,7 @@ class UpdateReportingUTests {
isDeleted = false,
)
given(reportingRepository.findById(any())).willReturn(reporting)
given(reportingRepository.update(any(), isA<Observation>())).willReturn(reporting)
given(reportingRepository.update(any(), anyOrNull(), isA<Observation>())).willReturn(reporting)
given(getReportingWithDMLAndSeaFront.execute(any(), anyOrNull())).willReturn(
(reporting.value as Observation).copy(controlUnitId = 1),
)
Expand All @@ -472,7 +475,7 @@ class UpdateReportingUTests {

// Then
argumentCaptor<Observation>().apply {
verify(reportingRepository).update(any(), capture())
verify(reportingRepository).update(any(), anyOrNull(), capture())
}
assertThat(returnedReporting.first.flagState).isEqualTo(CountryCode.FR)
}
Expand Down Expand Up @@ -502,7 +505,7 @@ class UpdateReportingUTests {
isDeleted = false,
)
given(reportingRepository.findById(any())).willReturn(reporting)
given(reportingRepository.update(any(), isA<InfractionSuspicion>())).willReturn(reporting)
given(reportingRepository.update(any(), anyOrNull(), isA<InfractionSuspicion>())).willReturn(reporting)
given(getReportingWithDMLAndSeaFront.execute(any(), anyOrNull())).willReturn(
InfractionSuspicion(
reportingActor = ReportingActor.OPS,
Expand Down Expand Up @@ -533,7 +536,7 @@ class UpdateReportingUTests {

// Then
argumentCaptor<InfractionSuspicion>().apply {
verify(reportingRepository).update(any(), capture())
verify(reportingRepository).update(any(), anyOrNull(), capture())

assertThat(allValues.first().type.toString()).isEqualTo(ReportingType.INFRACTION_SUSPICION.name)
assertThat(allValues.first().dml).isEqualTo("DML 17")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LoggingFormatterUTests {
"\"controlUnitId\":null," +
"\"authorTrigram\":\"LTH\"," +
"\"authorContact\":null," +
"\"expirationDate\":null," +
"\"title\":\"A title\"," +
"\"description\":null," +
"\"natinfCode\":123456}",
Expand Down Expand Up @@ -104,6 +105,7 @@ class LoggingFormatterUTests {
"\"flagState\":\"FR\"," +
"\"creationDate\":\"2019-01-18T07:19:45.000000045Z\"," +
"\"validationDate\":null," +
"\"expirationDate\":null," +
"\"value\":{\"type\":\"INFRACTION_SUSPICION\"," +
"\"natinfCode\":123456," +
"\"type\":\"INFRACTION_SUSPICION\"," +
Expand Down Expand Up @@ -144,6 +146,7 @@ class LoggingFormatterUTests {
"\"controlUnitId\":null," +
"\"authorTrigram\":\"LTH\"," +
"\"authorContact\":null," +
"\"expirationDate\":null," +
"\"title\":\"A title\"," +
"\"description\":null," +
"\"natinfCode\":123456}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class JpaReportingRepositoryITests : AbstractDBTests() {
)

// When
val reporting = jpaReportingRepository.update(7, updatedReporting)
val reporting = jpaReportingRepository.update(7, null, updatedReporting)

// Then
assertThat(reporting.internalReferenceNumber).isEqualTo("ABC000042310")
Expand All @@ -371,6 +371,7 @@ class JpaReportingRepositoryITests : AbstractDBTests() {
@Transactional
fun `update Should update a given Observation`() {
// Given
val updatedExpirationDate = ZonedDateTime.now()
val updatedReporting =
Observation(
ReportingActor.UNIT,
Expand All @@ -382,7 +383,7 @@ class JpaReportingRepositoryITests : AbstractDBTests() {
)

// When
val reporting = jpaReportingRepository.update(8, updatedReporting)
val reporting = jpaReportingRepository.update(8, updatedExpirationDate, updatedReporting)

// Then
assertThat(reporting.internalReferenceNumber).isEqualTo("ABC000597493")
Expand All @@ -409,7 +410,7 @@ class JpaReportingRepositoryITests : AbstractDBTests() {
)

// When
val reporting = jpaReportingRepository.update(7, updatedReporting)
val reporting = jpaReportingRepository.update(7, null, updatedReporting)

// Then
assertThat(reporting.internalReferenceNumber).isEqualTo("ABC000042310")
Expand Down
Loading

0 comments on commit 335bc6b

Please sign in to comment.