Skip to content

Commit

Permalink
Signalements – Ajouter une date de validité du signalement (#4024)
Browse files Browse the repository at this point in the history
## Linked issues

- Resolve #3788

----

- [ ] Tests E2E (Cypress)
  • Loading branch information
louptheron authored Jan 17, 2025
2 parents eab6ba9 + dbbc36f commit c217dd9
Show file tree
Hide file tree
Showing 34 changed files with 238 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ data class Reporting(
val flagState: CountryCode,
val creationDate: ZonedDateTime,
val validationDate: ZonedDateTime? = null,
val expirationDate: ZonedDateTime? = null,
val value: ReportingValue,
val isArchived: Boolean,
val isDeleted: Boolean,
Expand Down
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 All @@ -49,7 +51,9 @@ interface ReportingRepository {
fromDate: ZonedDateTime,
): List<Reporting>

fun findUnarchivedReportings(): List<Pair<Int, AlertType>>
fun findUnarchivedReportingsAfterNewVoyage(): List<Pair<Int, AlertType>>

fun findExpiredReportings(): List<Int>

fun archive(id: Int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ArchiveOutdatedReportings(private val reportingRepository: ReportingReposi
@Scheduled(fixedDelay = 300000, initialDelay = 6000)
@Transactional
fun execute() {
val reportingCandidatesToArchive = reportingRepository.findUnarchivedReportings()
val reportingCandidatesToArchive = reportingRepository.findUnarchivedReportingsAfterNewVoyage()
val expiredReportingsToArchive = reportingRepository.findExpiredReportings()

val filteredReportingIdsToArchive =
reportingCandidatesToArchive.filter {
Expand All @@ -25,8 +26,9 @@ class ArchiveOutdatedReportings(private val reportingRepository: ReportingReposi
it.second.type == AlertTypeMapping.SUSPICION_OF_UNDER_DECLARATION_ALERT
}.map { it.first }

logger.info("Found ${filteredReportingIdsToArchive.size} reportings to archive.")
val numberOfArchivedReportings = reportingRepository.archiveReportings(filteredReportingIdsToArchive)
logger.info("Found ${filteredReportingIdsToArchive.size} reportings alerts to archive.")
logger.info("Found ${expiredReportingsToArchive.size} expired reportings to archive.")
val numberOfArchivedReportings = reportingRepository.archiveReportings(filteredReportingIdsToArchive + expiredReportingsToArchive)

logger.info("Archived $numberOfArchivedReportings reportings")
}
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 @@ -18,6 +18,7 @@ class CreateReportingDataInput(
val flagState: CountryCode,
val creationDate: ZonedDateTime,
val validationDate: ZonedDateTime? = null,
val expirationDate: ZonedDateTime? = null,
val value: InfractionSuspicionOrObservationType,
) {
fun toReporting() =
Expand All @@ -32,6 +33,7 @@ class CreateReportingDataInput(
flagState = this.flagState,
creationDate = this.creationDate,
validationDate = this.validationDate,
expirationDate = this.expirationDate,
isDeleted = false,
isArchived = false,
value = this.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +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: ZonedDateTime? = null,
val title: String,
val description: String? = null,
val natinfCode: Int? = null,
Expand All @@ -21,6 +23,7 @@ class UpdateReportingDataInput(
controlUnitId = this.controlUnitId,
authorTrigram = this.authorTrigram,
authorContact = this.authorContact,
expirationDate = this.expirationDate,
title = this.title,
description = this.description,
natinfCode = this.natinfCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ReportingDataOutput(
val flagState: CountryCode,
val creationDate: ZonedDateTime,
val validationDate: ZonedDateTime? = null,
val expirationDate: ZonedDateTime? = null,
val value: ReportingValueDataOutput,
val isArchived: Boolean,
val isDeleted: Boolean,
Expand Down Expand Up @@ -58,6 +59,7 @@ class ReportingDataOutput(
flagState = reporting.flagState,
creationDate = reporting.creationDate,
validationDate = reporting.validationDate,
expirationDate = reporting.expirationDate,
value = value,
isArchived = reporting.isArchived,
isDeleted = reporting.isDeleted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ data class ReportingEntity(
val creationDate: ZonedDateTime,
@Column(name = "validation_date", nullable = true)
val validationDate: ZonedDateTime? = null,
@Column(name = "expiration_date", nullable = true)
val expirationDate: ZonedDateTime? = null,
@Type(JsonBinaryType::class)
@Column(name = "value", nullable = false, columnDefinition = "jsonb")
val value: String,
Expand All @@ -73,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 @@ -118,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 Expand Up @@ -168,7 +172,7 @@ class JpaReportingRepository(
dbReportingRepository.archiveReporting(id)
}

override fun findUnarchivedReportings(): List<Pair<Int, AlertType>> {
override fun findUnarchivedReportingsAfterNewVoyage(): List<Pair<Int, AlertType>> {
return dbReportingRepository.findAllUnarchivedAfterDEPLogbookMessage().map { result ->
Pair(
result[0] as Int,
Expand All @@ -181,6 +185,10 @@ class JpaReportingRepository(
}
}

override fun findExpiredReportings(): List<Int> {
return dbReportingRepository.findExpiredReportings()
}

override fun archiveReportings(ids: List<Int>): Int {
return dbReportingRepository.archiveReportings(ids)
}
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 @@ -122,6 +123,21 @@ interface DBReportingRepository : CrudRepository<ReportingEntity, Int> {
)
fun findAllUnarchivedAfterDEPLogbookMessage(): List<Array<Any>>

@Query(
value = """
SELECT
id
FROM
reportings
WHERE
archived is false AND
deleted is false AND
NOW() > expiration_date
""",
nativeQuery = true,
)
fun findExpiredReportings(): List<Int>

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query(
value = """
Expand Down Expand Up @@ -153,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 @@ -162,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
@@ -0,0 +1,2 @@
ALTER TABLE public.reportings
ADD COLUMN expiration_date TIMESTAMP WITH TIME ZONE
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ INSERT INTO reportings (id, archived, creation_date, deleted, external_reference

INSERT INTO reportings (id, archived, creation_date, deleted, external_reference_number, flag_state, internal_reference_number, ircs, latitude, longitude, type, validation_date, value, vessel_id, vessel_identifier, vessel_name) VALUES (11, false, NOW() AT TIME ZONE 'UTC' - INTERVAL '15 days', false, 'EXTIMM101', 'FR', 'CFR101', 'IRCS101', NULL, NULL, 'INFRACTION_SUSPICION', NULL, '{"authorContact":"Jean Bon (0623456789)","authorTrigram":"LTH","controlUnitId":10336,"description":"Une description d''infraction.","dml":"DML 29","natinfCode":27689,"reportingActor":"OPS","seaFront":"NAMO","title":"Suspicion d''infraction 10","type":"INFRACTION_SUSPICION"}', 101, 'INTERNAL_REFERENCE_NUMBER', 'VIVA ESPANA');

INSERT INTO reportings (id, archived, creation_date, deleted, external_reference_number, flag_state, internal_reference_number, ircs, latitude, longitude, type, validation_date, value, vessel_id, vessel_identifier, vessel_name) VALUES (12, false, NOW() AT TIME ZONE 'UTC' - INTERVAL '20 days', false, 'EXTIMM115', 'FR', 'CFR115', 'IRCS115', NULL, NULL, 'INFRACTION_SUSPICION', NULL, '{"authorContact":"Jean Bon (0623456789)","authorTrigram":"LTH","controlUnitId":10336,"description":"Une description d''infraction.","dml":"DML 29","natinfCode":27689,"reportingActor":"OPS","seaFront":"NAMO","title":"Suspicion d''infraction 11","type":"INFRACTION_SUSPICION"}', 115, 'INTERNAL_REFERENCE_NUMBER', 'DOS FIN');
INSERT INTO reportings (id, archived, creation_date, deleted, external_reference_number, flag_state, internal_reference_number, ircs, latitude, longitude, type, validation_date, expiration_date, value, vessel_id, vessel_identifier, vessel_name) VALUES (12, false, NOW() AT TIME ZONE 'UTC' - INTERVAL '20 days', false, 'EXTIMM115', 'FR', 'CFR115', 'IRCS115', NULL, NULL, 'INFRACTION_SUSPICION', NULL, NOW() AT TIME ZONE 'UTC' - INTERVAL '2 days', '{"authorContact":"Jean Bon (0623456789)","authorTrigram":"LTH","controlUnitId":10336,"description":"Une description d''infraction.","dml":"DML 29","natinfCode":27689,"reportingActor":"OPS","seaFront":"NAMO","title":"Suspicion d''infraction 11","type":"INFRACTION_SUSPICION"}', 115, 'INTERNAL_REFERENCE_NUMBER', 'DOS FIN');

INSERT INTO reportings (id, archived, creation_date, deleted, external_reference_number, flag_state, internal_reference_number, ircs, latitude, longitude, type, validation_date, value, vessel_id, vessel_identifier, vessel_name) VALUES (13, false, NOW() AT TIME ZONE 'UTC' - INTERVAL '25 days', false, 'EXTIMM115', 'FR', 'CFR115', 'IRCS115', NULL, NULL, 'INFRACTION_SUSPICION', NULL, '{"authorContact":"Jean Bon (0623456789)","authorTrigram":"LTH","controlUnitId":10336,"description":"Une description d''infraction.","dml":"DML 29","natinfCode":27689,"reportingActor":"OPS","seaFront":"NAMO","title":"Suspicion d''infraction 212","type":"INFRACTION_SUSPICION"}', 115, 'INTERNAL_REFERENCE_NUMBER', 'DOS FIN');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@
"longitude": null,
"type": "INFRACTION_SUSPICION",
"validation_date": null,
"expiration_date:sql": "NOW() AT TIME ZONE 'UTC' - INTERVAL '2 days'",
"value:jsonb": {
"authorContact": "Jean Bon (0623456789)",
"authorTrigram": "LTH",
Expand Down
Loading

0 comments on commit c217dd9

Please sign in to comment.