Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signalements – Revoir le mode d'archivage des alertes "FAR 48h" #3806

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Seafront } from '@constants/seafront'
import { ReportingCard } from '@features/Reporting/components/ReportingCard'
import { ReportingType } from '@features/Reporting/types'
import { afterAll, describe, expect, it } from '@jest/globals'
import { afterAll, describe, expect, it, jest } from '@jest/globals'
import { THEME, ThemeProvider } from '@mtes-mct/monitor-ui'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
Expand All @@ -12,15 +12,13 @@ import { VesselIdentifier } from '../../../../../domain/entities/vessel/types'

import type { PendingAlertReporting } from '@features/Reporting/types'

// @ts-ignore
jest.mock('../../../useCases/archiveReporting', () => () => ({ archiveReporting: jest.fn() }))
// @ts-ignore
jest.mock('../../../useCases/archiveReporting', () => ({ archiveReporting: jest.fn() }))
jest.mock('../../../../../hooks/useMainAppDispatch', () => ({ useMainAppDispatch: () => {} }))
jest.mock('../../../useCases/deleteReporting', () => ({ deleteReporting: jest.fn() }))

describe('ReportingCard()', () => {
afterAll(() => {
// Reset module registry to clear the mock
// @ts-ignore
jest.resetModules()
})

Expand Down
12 changes: 10 additions & 2 deletions frontend/src/features/Reporting/components/ReportingCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useMemo, useState } from 'react'
import styled from 'styled-components'

import { getFrenchOrdinal, getReportingActorLabel } from './utils'
import { PendingAlertValueType } from '../../../../domain/entities/alerts/types'
import { getDateTime } from '../../../../utils'
import { ReportingType, ReportingTypeCharacteristics } from '../../types'
import { archiveReporting } from '../../useCases/archiveReporting'
Expand Down Expand Up @@ -35,6 +36,9 @@ export function ReportingCard({
const reportingName = Object.values(ReportingTypeCharacteristics).find(
reportingType => reportingType.code === reporting.type
)?.name
const canBeArchived = !(
reporting.type === ReportingType.ALERT && reporting.value.type === PendingAlertValueType.MISSING_FAR_48_HOURS_ALERT
)
const alertDateTime = getDateTime(
reporting.type === ReportingType.ALERT ? reporting.validationDate : reporting.creationDate,
true
Expand All @@ -54,7 +58,7 @@ export function ReportingCard({
}, [reporting, reportingName])

const archive = () => {
dispatch(archiveReporting(reporting.id, reporting.type))
dispatch(archiveReporting(reporting))
}

const askForDeletionConfirmation = () => {
Expand Down Expand Up @@ -166,7 +170,11 @@ export function ReportingCard({
Icon={Icon.Archive}
iconSize={20}
onClick={archive}
title="Archiver ce signalement"
title={
canBeArchived
? 'Archiver ce signalement'
: `Ce signalement sera archivé sous la forme de 2 alertes "Absence de message FAR en 24h"`
}
/>
<StyledIconButton
accent={Accent.TERTIARY}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Seafront } from '@constants/seafront'
import { ReportingType } from '@features/Reporting/types'

import { PendingAlertValueType } from '../../../../../domain/entities/alerts/types'
import { VesselIdentifier } from '../../../../../domain/entities/vessel/types'

import type { PendingAlertReporting } from '@features/Reporting/types'

export const fortyHeightHourAlertReporting: PendingAlertReporting = {
creationDate: '2023-10-30T09:10:00Z',
externalReferenceNumber: '',
flagState: 'ES',
id: 12345,
infraction: {
infraction:
'Pêche maritime non autorisée dans les eaux territoriales francaise par capitaine de navire communautaire',
infractionCategory: 'FISHING',
natinfCode: 2610,
regulation: 'ART.L.945-2 §I AL.1, ART.L.945-5 1°,2°,3°,4° C.RUR'
},
internalReferenceNumber: 'FR04504564',
ircs: '',
isArchived: false,
isDeleted: false,
type: ReportingType.ALERT,
underCharter: null,
validationDate: '2023-10-30T15:08:05.845121Z',
value: {
dml: null,
natinfCode: 2610,
seaFront: Seafront.NAMO,
type: PendingAlertValueType.MISSING_FAR_48_HOURS_ALERT
},
vesselId: 1234568,
vesselIdentifier: VesselIdentifier.INTERNAL_REFERENCE_NUMBER,
vesselName: 'A VESSEL'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { fortyHeightHourAlertReporting } from '@features/Reporting/useCases/__tests__/__mocks__/dummyReporting'
import { archiveReporting } from '@features/Reporting/useCases/archiveReporting'
import * as deleteReporting from '@features/Reporting/useCases/deleteReporting'
import { describe, it, expect, jest, afterAll } from '@jest/globals'
import { dispatchProcessor } from '@store/__tests__/utils'

import { PendingAlertValueType } from '../../../../domain/entities/alerts/types'
import { VesselIdentifier } from '../../../../domain/entities/vessel/types'

jest.mock('../../reportingApi', () => jest.fn())
jest.mock('../deleteReporting', () => ({
__esModule: true,
deleteReporting: () => jest.fn()
}))
jest.spyOn(deleteReporting, 'deleteReporting')

describe('archiveReporting()', () => {
const INITIAL_STATE = {
vessel: {
selectedVesselIdentity: {
externalReferenceNumber: '',
flagState: '',
internalReferenceNumber: '',
vesselId: 1234568,
vesselIdentifier: VesselIdentifier.INTERNAL_REFERENCE_NUMBER,
vesselName: ''
}
}
}

afterAll(() => {
// Reset module registry to clear the mock
jest.resetModules()
})

it('Should delete reporting When alert is MISSING_FAR_48_HOURS_ALERT', async () => {
// When
dispatchProcessor(archiveReporting(fortyHeightHourAlertReporting), INITIAL_STATE)

// Then
expect(deleteReporting.deleteReporting).toHaveBeenCalled()
})

it('Should not delete reporting When the alert is not an MISSING_FAR_48_HOURS_ALERT', async () => {
// Given
const otherAlertReporting = {
...fortyHeightHourAlertReporting,
value: {
...fortyHeightHourAlertReporting.value,
type: PendingAlertValueType.MISSING_FAR_ALERT
}
}

// When
dispatchProcessor(archiveReporting(otherAlertReporting), INITIAL_STATE)

// Then
expect(deleteReporting.deleteReporting).toHaveBeenCalledTimes(0)
})
})
22 changes: 17 additions & 5 deletions frontend/src/features/Reporting/useCases/archiveReporting.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { ReportingType } from '@features/Reporting/types'
import { deleteReporting } from '@features/Reporting/useCases/deleteReporting'
import { renderVesselFeatures } from '@features/Vessel/useCases/renderVesselFeatures'
import { DisplayedErrorKey } from '@libs/DisplayedError/constants'

import { PendingAlertValueType } from '../../../domain/entities/alerts/types'
import { Vessel } from '../../../domain/entities/vessel/vessel'
import { displayOrLogError } from '../../../domain/use_cases/error/displayOrLogError'
import { removeVesselReporting } from '../../Vessel/slice'
import { reportingApi } from '../reportingApi'

import type { ReportingType } from '@features/Reporting/types'
import type { Reporting } from '@features/Reporting/types'
import type { MainAppThunk } from '@store'

export const archiveReporting =
(id: number, type: ReportingType): MainAppThunk<Promise<void>> =>
(reporting: Reporting.Reporting): MainAppThunk<Promise<void>> =>
async (dispatch, getState) => {
const { selectedVesselIdentity } = getState().vessel

try {
await dispatch(reportingApi.endpoints.archiveReporting.initiate(id)).unwrap()
if (
reporting.type === ReportingType.ALERT &&
reporting.value.type === PendingAlertValueType.MISSING_FAR_48_HOURS_ALERT
) {
await dispatch(deleteReporting(reporting.id, reporting.type))

return
}

await dispatch(reportingApi.endpoints.archiveReporting.initiate(reporting.id)).unwrap()

dispatch(
removeVesselReporting({
reportingType: type,
reportingType: reporting.type,
vesselFeatureId: Vessel.getVesselFeatureId(selectedVesselIdentity)
})
)
Expand All @@ -29,7 +41,7 @@ export const archiveReporting =
dispatch(
displayOrLogError(
error as Error,
() => archiveReporting(id, type),
() => archiveReporting(reporting),
true,
DisplayedErrorKey.VESSEL_SIDEBAR_ERROR
)
Expand Down
22 changes: 22 additions & 0 deletions frontend/src/store/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { jest } from '@jest/globals'

/**
* To be used to capture all dispatched actions.
*
* we could have more middleware functions being called within the
* use-case middleware, and we should be able to capture all of these events.
*/
export const dispatchProcessor = (action, initialState) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pouquoi ne pas suivre le nommage + doc officielle ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu pourrais me détailler ta proposition ? Tu veux dire faire un createSlice test ?

Note: Ici je ne veux pas descendre au niveau du fetch, pour ne pas avoir à gérer RTK.

const store = {
dispatch: jest.fn(fn => {
if (typeof fn === 'function') {
fn(store.dispatch, store.getState)
}
}),
getState: jest.fn(() => initialState)
}

action(store.dispatch, store.getState)

return store
}
Loading