Skip to content

Commit

Permalink
wip: add expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Jan 16, 2025
1 parent 5b2cc6b commit af2a296
Show file tree
Hide file tree
Showing 28 changed files with 153 additions and 63 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 @@ -49,7 +49,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,18 +15,20 @@ 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 {
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 }

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 @@ -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 @@ -10,6 +10,7 @@ class UpdateReportingDataInput(
val controlUnitId: Int? = null,
val authorTrigram: String,
val authorContact: String? = null,
val expirationDate: String? = null,
val title: String,
val description: String? = null,
val natinfCode: Int? = null,
Expand All @@ -21,6 +22,8 @@ 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,
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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 +181,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 @@ -122,6 +122,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ class ArchiveOutdatedReportingsUTests {
@Test
fun `execute Should archive outdated reportings`() {
// Given
given(reportingRepository.findUnarchivedReportings()).willReturn(
given(reportingRepository.findUnarchivedReportingsAfterNewVoyage()).willReturn(
listOf(
Pair(1, TwelveMilesFishingAlert("NAMO")),
Pair(2, ThreeMilesTrawlingAlert("NAMO")),
Pair(3, MissingFARAlert("NAMO")),
),
)
given(reportingRepository.findExpiredReportings()).willReturn(
listOf(4, 5),
)

// When
ArchiveOutdatedReportings(reportingRepository).execute()

// Then
verify(reportingRepository).archiveReportings(eq(listOf(2, 3)))
verify(reportingRepository).archiveReportings(eq(listOf(2, 3, 4, 5)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,25 @@ class JpaReportingRepositoryITests : AbstractDBTests() {
@Transactional
fun `findUnarchivedReportings Should return archive candidates`() {
// When
val reportings = jpaReportingRepository.findUnarchivedReportings()
val reportings = jpaReportingRepository.findUnarchivedReportingsAfterNewVoyage()

// Then
assertThat(reportings).hasSize(1)
assertThat(reportings.first().first).isEqualTo(1)
assertThat(reportings.first().second.type).isEqualTo(AlertTypeMapping.THREE_MILES_TRAWLING_ALERT)
}

@Test
@Transactional
fun `findExpiredReportings Should return expired reportings, hence archive candidates`() {
// When
val reportings = jpaReportingRepository.findExpiredReportings()

// Then
assertThat(reportings).hasSize(1)
assertThat(reportings.first()).isEqualTo(12)
}

@Test
@Transactional
fun `archiveReportings Should archive reportings`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FieldError, Select } from '@mtes-mct/monitor-ui'
import { useField } from 'formik'
import styled from 'styled-components'

import { operationalAlertTypes } from '../../../../constants'
import { OPERATIONAL_ALERTS } from '../../../../constants'
import { PendingAlertValueType } from '../../../../types'

import type { SilencedAlertFormValues } from '../types'
Expand All @@ -11,7 +11,7 @@ import type { Option } from '@mtes-mct/monitor-ui'
export function AlertTypeField() {
const [input, meta, helper] = useField<SilencedAlertFormValues['value']>('value')

const alertsAsOptions: Array<Option<PendingAlertValueType>> = operationalAlertTypes.map(alert => ({
const alertsAsOptions: Array<Option<PendingAlertValueType>> = OPERATIONAL_ALERTS.map(alert => ({
label: alert.name,
value: alert.code
}))
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/features/Alert/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,64 @@ export const COMMON_ALERT_TYPE_OPTION: Record<
PendingAlertValueType | 'PNO_LAN_WEIGHT_TOLERANCE_ALERT',
MenuItem<PendingAlertValueType | 'PNO_LAN_WEIGHT_TOLERANCE_ALERT'> & {
isOperationalAlert: boolean
isArchivedAfterDEPMessage: boolean

Check failure on line 19 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected interface keys to be in ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
nameWithAlertDetails?: Function
}
> = {
FRENCH_EEZ_FISHING_ALERT: {
code: PendingAlertValueType.FRENCH_EEZ_FISHING_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: false,

Check failure on line 26 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Pêche en ZEE française par un navire tiers'
},
MISSING_DEP_ALERT: {
code: PendingAlertValueType.MISSING_DEP_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: true,

Check failure on line 32 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Sortie sans émission de message "DEP"'
},
MISSING_FAR_48_HOURS_ALERT: {
code: PendingAlertValueType.MISSING_FAR_48_HOURS_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: false,

Check failure on line 38 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Non-emission de message "FAR" en 48h'
},
MISSING_FAR_ALERT: {
code: PendingAlertValueType.MISSING_FAR_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: true,

Check failure on line 44 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Non-emission de message "FAR"'
},
PNO_LAN_WEIGHT_TOLERANCE_ALERT: {
code: 'PNO_LAN_WEIGHT_TOLERANCE_ALERT',
isOperationalAlert: false,
isArchivedAfterDEPMessage: false,

Check failure on line 50 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Tolérance 10% non respectée',
nameWithAlertDetails: (percentOfTolerance, minimumWeightThreshold) =>
`Tolérance de ${percentOfTolerance}% non respectée, appliquée pour un poids minimum de ${minimumWeightThreshold}kg.`
},
RTC_FISHING_ALERT: {
code: PendingAlertValueType.RTC_FISHING_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: false,

Check failure on line 58 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Pêche en zone RTC'
},
SUSPICION_OF_UNDER_DECLARATION_ALERT: {
code: PendingAlertValueType.SUSPICION_OF_UNDER_DECLARATION_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: true,

Check failure on line 64 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: 'Suspicion de sous-déclaration'
},
THREE_MILES_TRAWLING_ALERT: {
code: PendingAlertValueType.THREE_MILES_TRAWLING_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: true,

Check failure on line 70 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: '3 milles - Chaluts'
},
TWELVE_MILES_FISHING_ALERT: {
code: PendingAlertValueType.TWELVE_MILES_FISHING_ALERT,
isOperationalAlert: true,
isArchivedAfterDEPMessage: false,

Check failure on line 76 in frontend/src/features/Alert/constants.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected object keys to be in insensitive ascending order. 'isArchivedAfterDEPMessage' should be before 'isOperationalAlert'
name: '12 milles - Pêche sans droits historiques'
}
}
Expand Down Expand Up @@ -129,6 +139,11 @@ export enum SilencedAlertPeriod {
TWO_HOURS = 'TWO_HOURS'
}

export const operationalAlertTypes = Object.keys(COMMON_ALERT_TYPE_OPTION)
export const OPERATIONAL_ALERTS = Object.keys(COMMON_ALERT_TYPE_OPTION)
.map(alertTypeName => COMMON_ALERT_TYPE_OPTION[alertTypeName])
.filter(alertType => alertType.isOperationalAlert)

export const ALERTS_ARCHIVED_AFTER_NEW_VOYAGE: string[] = Object.keys(COMMON_ALERT_TYPE_OPTION)
.map(alertTypeName => COMMON_ALERT_TYPE_OPTION[alertTypeName])
.filter(alertType => alertType.isArchivedAfterDEPMessage)
.map(alert => alert.code)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Reportings/Current/utils.sortByValidationOrCreationDateDesc()', () =>
type: ReportingType.ALERT,
underCharter: null,
validationDate: '2023-10-30T15:08:05.845121Z',
expirationDate: null,
value: {
dml: null,
natinfCode: 2610,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('ReportingCard()', () => {
type: ReportingType.ALERT,
underCharter: null,
validationDate: '2023-10-30T15:08:05.845121Z',
expirationDate: null,
value: {
dml: null,
natinfCode: 2610,
Expand Down
Loading

0 comments on commit af2a296

Please sign in to comment.