Skip to content

Commit

Permalink
Migrate last vessels apis to rtk
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Feb 3, 2025
1 parent 47a7df3 commit 80bcf37
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class VesselController(

@GetMapping("/find")
@Operation(summary = "Get vessel information and positions")
fun getVessel(
fun getVesselAndPositions(
@Parameter(description = "Vessel internal id")
@RequestParam(name = "vesselId")
vesselId: Int?,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/api/BackendApi.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ export namespace BackendApi {
export interface Meta {
response?: {
headers: Headers
status: number
}
}
85 changes: 0 additions & 85 deletions frontend/src/api/vessel.ts

This file was deleted.

24 changes: 19 additions & 5 deletions frontend/src/domain/use_cases/vessel/searchVessels.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { searchVesselsFromAPI } from '../../../api/vessel'
import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants'
import { Vessel } from '@features/Vessel/Vessel.types'
import { vesselApi } from '@features/Vessel/vesselApi'

import { setError } from '../../shared_slices/Global'

import type { MainAppThunk } from '@store'

/** @deprecated Use Redux RTK `searchVessels()` query. */
export const searchVessels = searched => dispatch =>
searchVesselsFromAPI(searched).catch(error => {
dispatch(setError(error))
})
export const searchVessels =
(searched: string): MainAppThunk<Promise<Vessel.VesselIdentity[] | undefined>> =>
async dispatch => {
try {
return await dispatch(
vesselApi.endpoints.searchVessels.initiate({ searched }, RTK_FORCE_REFETCH_QUERY_OPTIONS)
).unwrap()
} catch (error) {
dispatch(setError(error))

return undefined
}
}
11 changes: 9 additions & 2 deletions frontend/src/domain/use_cases/vessel/showVessel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getVesselFromAPI } from '@api/vessel'
import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants'
import { logbookActions } from '@features/Logbook/slice'
import { doNotAnimate } from '@features/Map/slice'
import { loadingVessel, resetLoadingVessel, setSelectedVessel, vesselSelectors } from '@features/Vessel/slice'
import { vesselApi } from '@features/Vessel/vesselApi'
import { DisplayedErrorKey } from '@libs/DisplayedError/constants'
import { captureMessage } from '@sentry/react'
import { omit } from 'lodash'
Expand Down Expand Up @@ -59,7 +60,13 @@ export const showVessel =
dispatch(addSearchedVessel(vesselIdentity))
}

const { isTrackDepthModified, vesselAndPositions } = await getVesselFromAPI(vesselIdentity, nextTrackRequest)
const { isTrackDepthModified, vesselAndPositions } = await dispatch(
vesselApi.endpoints.getVesselAndPositions.initiate(
{ trackRequest: nextTrackRequest, vesselIdentity },
RTK_FORCE_REFETCH_QUERY_OPTIONS
)
).unwrap()

try {
throwCustomErrorFromAPIFeedback(vesselAndPositions.positions, isTrackDepthModified, isFromUserAction)
} catch (error) {
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/domain/use_cases/vessel/showVesselTrack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants'
import { OPENLAYERS_PROJECTION, WSG84_PROJECTION } from '@features/Map/constants'
import { doNotAnimate } from '@features/Map/slice'
import { addVesselTrackShowed, resetLoadingVessel } from '@features/Vessel/slice'
import { getVesselCompositeIdentifier } from '@features/Vessel/utils'
import { vesselApi } from '@features/Vessel/vesselApi'
import { transform } from 'ol/proj'

import { getVesselPositionsFromAPI } from '../../../api/vessel'
import { getCustomOrDefaultTrackRequest, throwCustomErrorFromAPIFeedback } from '../../entities/vesselTrackDepth'
import { removeError, setError } from '../../shared_slices/Global'

Expand All @@ -30,7 +31,12 @@ export const showVesselTrack =
dispatch(doNotAnimate(!isFromUserAction))
dispatch(removeError())

const { isTrackDepthModified, positions } = await getVesselPositionsFromAPI(vesselIdentity, nextTrackRequest)
const { isTrackDepthModified, positions } = await dispatch(
vesselApi.endpoints.getVesselPositions.initiate(
{ trackRequest: nextTrackRequest, vesselIdentity },
RTK_FORCE_REFETCH_QUERY_OPTIONS
)
).unwrap()
try {
throwCustomErrorFromAPIFeedback(positions, isTrackDepthModified, isFromUserAction)
dispatch(removeError())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { RTK_FORCE_REFETCH_QUERY_OPTIONS } from '@api/constants'
import { animateToExtent, doNotAnimate } from '@features/Map/slice'
import {
resetLoadingVessel,
setSelectedVesselCustomTrackRequest,
updateSelectedVesselPositions,
updatingVesselTrackDepth
} from '@features/Vessel/slice'
import { vesselApi } from '@features/Vessel/vesselApi'

import { getVesselPositionsFromAPI } from '../../../api/vessel'
import { logbookActions } from '../../../features/Logbook/slice'
import { throwCustomErrorFromAPIFeedback } from '../../entities/vesselTrackDepth'
import { removeError, setError } from '../../shared_slices/Global'
Expand All @@ -30,7 +31,12 @@ export const updateSelectedVesselTrackRequest =

dispatchUpdatingVessel(dispatch, true)

const { isTrackDepthModified, positions } = await getVesselPositionsFromAPI(vesselIdentity, trackRequest)
const { isTrackDepthModified, positions } = await dispatch(
vesselApi.endpoints.getVesselPositions.initiate(
{ trackRequest, vesselIdentity },
RTK_FORCE_REFETCH_QUERY_OPTIONS
)
).unwrap()
throwCustomErrorFromAPIFeedback(positions, isTrackDepthModified, false)

dispatch(removeError())
Expand Down
86 changes: 84 additions & 2 deletions frontend/src/features/Vessel/vesselApi.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { monitorfishApi } from '@api/api'
import { RtkCacheTagType } from '@api/constants'
import { HttpStatusCode, RtkCacheTagType } from '@api/constants'
import { DisplayedErrorKey } from '@libs/DisplayedError/constants'
import { FrontendApiError } from '@libs/FrontendApiError'
import { getUrlOrPathWithQueryParams } from '@utils/getUrlOrPathWithQueryParams'
import { displayedErrorActions } from 'domain/shared_slices/DisplayedError'
import { displayOrLogError } from 'domain/use_cases/error/displayOrLogError'

import { getVesselIdentityPropsAsEmptyStringsWhenUndefined } from './utils'
import { getVesselIdentityFromLegacyVesselIdentity, getVesselIdentityPropsAsEmptyStringsWhenUndefined } from './utils'
import { Vessel } from './Vessel.types'

import type { TrackRequest } from '../../domain/entities/vessel/types'
import type { Meta } from '@api/BackendApi.types'
import type { VesselReportings } from '@features/Reporting/types'

const GET_VESSEL_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les informations de ce navire."
const GET_VESSEL_REPORTINGS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les signalements de ce navire."
const SEARCH_VESSELS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les navires correspondants à cette recherche."
const VESSEL_POSITIONS_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les informations du navire"

export const vesselApi = monitorfishApi.injectEndpoints({
endpoints: builder => ({
Expand All @@ -23,6 +26,85 @@ export const vesselApi = monitorfishApi.injectEndpoints({
transformErrorResponse: response => new FrontendApiError(GET_VESSEL_ERROR_MESSAGE, response)
}),

/**
* Get vessel information and positions.
*
* Transforms the response by reading the JSON body and setting
* `isTrackDepthModified` based on the response status.
*/
getVesselAndPositions: builder.query<
{ isTrackDepthModified: boolean; vesselAndPositions: Vessel.VesselAndPositions },
{ trackRequest: TrackRequest; vesselIdentity: Vessel.VesselIdentity }
>({
query: ({ trackRequest, vesselIdentity }) => {
const { externalReferenceNumber, internalReferenceNumber, ircs, vesselId, vesselIdentifier } =
getVesselIdentityPropsAsEmptyStringsWhenUndefined(getVesselIdentityFromLegacyVesselIdentity(vesselIdentity))
const trackDepth = trackRequest.trackDepth ?? ''
const afterDateTime = trackRequest.afterDateTime?.toISOString() ?? ''
const beforeDateTime = trackRequest.beforeDateTime?.toISOString() ?? ''

return {
method: 'GET',
params: {
afterDateTime,
beforeDateTime,
externalReferenceNumber,
internalReferenceNumber,
IRCS: ircs,
trackDepth,
vesselId,
vesselIdentifier
},
url: `/bff/v1/vessels/find`
}
},
transformErrorResponse: response => new FrontendApiError(VESSEL_POSITIONS_ERROR_MESSAGE, response),
transformResponse: async (baseQueryReturnValue: Vessel.VesselAndPositions, meta: Meta) => ({
isTrackDepthModified: meta?.response?.status === HttpStatusCode.ACCEPTED,
vesselAndPositions: baseQueryReturnValue
})
}),

/**
* Get vessel positions.
*
* Transforms the response by reading the JSON body and setting
* `isTrackDepthModified` based on the response status.
*/
getVesselPositions: builder.query<
{ isTrackDepthModified: boolean; positions: Vessel.VesselPosition[] },
{ trackRequest: TrackRequest; vesselIdentity: Vessel.VesselIdentity }
>({
query: ({ trackRequest, vesselIdentity }) => {
const { externalReferenceNumber, internalReferenceNumber, ircs, vesselIdentifier } =
getVesselIdentityPropsAsEmptyStringsWhenUndefined(getVesselIdentityFromLegacyVesselIdentity(vesselIdentity))
const trackDepth = trackRequest.trackDepth ?? ''
const afterDateTime = trackRequest.afterDateTime?.toISOString() ?? ''
const beforeDateTime = trackRequest.beforeDateTime?.toISOString() ?? ''

return {
method: 'GET',
// Pass query parameters
params: {
afterDateTime,
beforeDateTime,
externalReferenceNumber,
internalReferenceNumber,
IRCS: ircs,
trackDepth,
vesselIdentifier
},

url: `/bff/v1/vessels/positions`
}
},
transformErrorResponse: response => new FrontendApiError(VESSEL_POSITIONS_ERROR_MESSAGE, response),
transformResponse: async (baseQueryReturnValue: Vessel.VesselPosition[], meta: Meta) => ({
isTrackDepthModified: meta?.response?.status === HttpStatusCode.ACCEPTED,
positions: baseQueryReturnValue
})
}),

getVesselReportingsByVesselIdentity: builder.query<
VesselReportings,
{ fromDate: string; vesselIdentity: Vessel.VesselIdentity }
Expand Down

0 comments on commit 80bcf37

Please sign in to comment.