Skip to content

Commit

Permalink
[Reporting] Archive reporting with endpoint /�reporting/archive
Browse files Browse the repository at this point in the history
  • Loading branch information
claire2212 committed Jan 28, 2025
1 parent 5b53880 commit a9b508d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ VALUES (1, 2300001, 'VEHICLE', NULL, '[
]',
'0104000020E610000001000000010100000045035E406AC113C00CF3FE4C1D354840', 'NAMO', 'Description 1',
'INFRACTION_SUSPICION', 'Rejets illicites', '{"Jet de déchet","Carénage sauvage"}', 'ACTION TAKEN', true, true,
now() - INTERVAL '3 days', 24, false, false, NULL, NULL, NULL, NULL, NULL, 19, false,
now() - INTERVAL '3 days', 24, false, false, NULL, NULL, NULL, NULL, NULL, 105, false,
now() - INTERVAL '3 days');
INSERT INTO public.reportings
VALUES (2, 2300002, 'VEHICLE', 'VESSEL', '[
Expand All @@ -23,7 +23,7 @@ VALUES (2, 2300002, 'VEHICLE', 'VESSEL', '[
]',
'0104000020E610000001000000010100000062C9C715311E13C050CB31D53D4F4840', 'NAMO', 'Description 2',
'INFRACTION_SUSPICION', 'Police des mouillages', '{ZMEL}', 'ACTION TAKEN', true, true,
now() - INTERVAL '2 days', 2, false, false, NULL, NULL, NULL, NULL, NULL, 12, false, now() - INTERVAL '2 days');
now() - INTERVAL '2 days', 2, false, false, 'RST', NULL, NULL, NULL, NULL, 113, false, now() - INTERVAL '2 days');
INSERT INTO public.reportings
VALUES (3, 2300003, 'VEHICLE', 'VESSEL', '[
{
Expand All @@ -33,7 +33,7 @@ VALUES (3, 2300003, 'VEHICLE', 'VESSEL', '[
]',
'0106000020E610000001000000010300000001000000040000006E15B8857C090CC02C07754424784840552A202082FC09C0C0FD120D667A484025BF296025E00BC0805DC2889C7F48406E15B8857C090CC02C07754424784840',
'NAMO', 'Description 3', 'INFRACTION_SUSPICION', 'Police des mouillages', '{ZMEL}', 'ACTION TAKEN', true, true,
now() - INTERVAL '1 hour', 1, false, false, NULL, NULL, NULL, NULL, NULL, 12, false, now() - INTERVAL '1 hour');
now() - INTERVAL '1 hour', 1, false, false, 'DEF', NULL, NULL, NULL, NULL, 109, false, now() - INTERVAL '1 hour');
INSERT INTO public.reportings
VALUES (4, 2300004, 'INDIVIDUAL', NULL, '[
{
Expand All @@ -44,7 +44,7 @@ VALUES (4, 2300004, 'INDIVIDUAL', NULL, '[
'MED',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque ultrices risus ac arcu pellentesque, et tempor justo tempor. Pellentesque sem nisl, tempor id augue non, interdum sollicitudin felis.',
'OBSERVATION', 'Pêche à pied', '{Braconnage}', NULL, true, true, now() - INTERVAL '3 hour', 4, false, false,
NULL, NULL, NULL, NULL, NULL, 18, false, now() - INTERVAL '3 hour');
'ABC', NULL, NULL, NULL, NULL, 110, false, now() - INTERVAL '3 hour');
INSERT INTO public.reportings
VALUES (5, 2300005, 'VEHICLE', 'VESSEL', '[
{
Expand All @@ -53,10 +53,10 @@ VALUES (5, 2300005, 'VEHICLE', 'VESSEL', '[
}
]',
'0104000020E6100000010000000101000000417927B8BBBBD73F06D9D38A46E24840', 'Guadeloupe',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'OBSERVATION', 'Pêche à pied',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'OBSERVATION','Pêche à pied',
'{Braconnage}', NULL,
true, true,
now() - INTERVAL '1 hour', 6, false, false, 'CDA', NULL, NULL, NULL, NULL, 18, false,
now() - INTERVAL '1 hour', 6, false, false, 'CDA', NULL, NULL, NULL, NULL, 108, false,
now() - INTERVAL '1 hour');
INSERT INTO public.reportings
VALUES (6, 2300006, 'COMPANY', NULL, '[
Expand All @@ -68,7 +68,7 @@ VALUES (6, 2300006, 'COMPANY', NULL, '[
'0104000020E6100000010000000101000000A22CD736208DF9BF242A543717D44540', 'Guadeloupe',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'INFRACTION_SUSPICION', 'Police des mouillages',
'{ZMEL}', NULL, true, true, now() - INTERVAL '75 minutes', 6, false, false, 'ABC', 34,
now() - INTERVAL '15 minutes', NULL, 'b8007c8a-5135-4bc3-816f-c69c7b75d807', 12, false,
now() - INTERVAL '15 minutes', NULL, 'b8007c8a-5135-4bc3-816f-c69c7b75d807', 100, false,
now() - INTERVAL '60 minutes');
INSERT INTO public.reportings
VALUES (7, 2300007, 'COMPANY', NULL, '[
Expand All @@ -80,7 +80,7 @@ VALUES (7, 2300007, 'COMPANY', NULL, '[
'0104000020E6100000010000000101000000BDAE0F9A19C010C068D780A5708E4740', 'NAMO',
'Lorem LoremLorem ipsum dolor sit amet, consectetur adipiscing elit.', 'OBSERVATION', NULL, NULL, NULL, true,
true, now() - INTERVAL '90 minutes', 6, false, false, 'DEF', 34, now() - INTERVAL '25 minutes', NULL, NULL,
NULL, false, now() - INTERVAL '60 minutes');
111, false, now() - INTERVAL '60 minutes');
INSERT INTO public.reportings
VALUES (8, 2300008, 'COMPANY', NULL, '[
{
Expand All @@ -91,13 +91,13 @@ VALUES (8, 2300008, 'COMPANY', NULL, '[
'0104000020E61000000100000001010000005BE1449141C0F5BF89869C29BA034740', 'NAMO',
'Lorem LoremLorem ipsum dolor sit amet, consectetur adipiscing elit.', 'INFRACTION_SUSPICION', NULL, NULL, NULL,
true, true, now() - INTERVAL '90 minutes', 6, false, false, 'GHI', 38, now() - INTERVAL '25 minutes', NULL,
NULL, NULL, false, now() - INTERVAL '60 minutes');
NULL, 20, false, now() - INTERVAL '60 minutes');

/* reporting linked to a surveillance */


INSERT INTO reportings (id, reporting_id, target_type, vehicle_type, target_details, geom, sea_front, description,
report_type, theme, sub_themes, action_taken, is_control_required, has_no_unit_available,
report_type, control_plan_theme_id, action_taken, is_control_required, has_no_unit_available,
created_at, validity_time, is_deleted, mission_id, attached_to_mission_at_utc,
detached_from_mission_at_utc, attached_env_action_id, open_by, updated_at_utc)
VALUES (9, 2300009, 'COMPANY', NULL, '[
Expand All @@ -106,7 +106,7 @@ VALUES (9, 2300009, 'COMPANY', NULL, '[
"vesselName": "Mr le gérant"
}
]', ST_GeomFromText('MULTIPOINT((-4.40757547 48.65546589))', 4326), 'NAMO',
'Lorem LoremLorem ipsum dolor sit amet, consectetur adipiscing elit.', 'OBSERVATION', NULL, NULL, NULL, true,
'Lorem LoremLorem ipsum dolor sit amet, consectetur adipiscing elit.', 'OBSERVATION', 106, NULL, true,
true, now() - INTERVAL '90 minutes', 6, false, 53, now() - INTERVAL '25 minutes', NULL,
'9969413b-b394-4db4-985f-b00743ffb833', 'GHI', now() - INTERVAL '60 minutes'),
/* reporting linked to a control */
Expand All @@ -115,7 +115,7 @@ VALUES (9, 2300009, 'COMPANY', NULL, '[
"operatorName": ""
}
]', ST_GeomFromText('MULTIPOINT((-3.91241571 48.67428686))', 4326), 'NAMO',
'Lorem LoremLorem ipsum dolor sit amet, consectetur adipiscing elit.', 'INFRACTION_SUSPICION', NULL, NULL, NULL,
'Lorem LoremLorem ipsum dolor sit amet, consectetur adipiscing elit.', 'INFRACTION_SUSPICION', 104, NULL,
true, true, now() - INTERVAL '90 minutes', 6, false, 53, now() - INTERVAL '25 minutes', NULL,
'3480657f-7845-4eb4-aa06-07b174b1da45', 'GHI', now() - INTERVAL '60 minutes'),
/* reporting linked to a surveillance */
Expand All @@ -124,7 +124,7 @@ VALUES (9, 2300009, 'COMPANY', NULL, '[
"operatorName": ""
}
]', ST_GeomFromText('MULTIPOINT((-4.76689484 48.52102012))', 4326), 'NAMO', 'La description du signalement',
'OBSERVATION', NULL, NULL, NULL, true, true, now() - INTERVAL '90 minutes', 6, false, 53,
'OBSERVATION', 102, NULL, true, true, now() - INTERVAL '90 minutes', 6, false, 53,
now() - INTERVAL '25 minutes', NULL, '9969413b-b394-4db4-985f-b00743ffb833', 'GHI',
now() - INTERVAL '60 minutes')
;
Expand Down Expand Up @@ -156,16 +156,28 @@ CREATE SEQUENCE IF NOT EXISTS reportings_2023_seq;
SELECT setval('reportings_2023_seq', (SELECT max(id) FROM reportings), true);

INSERT INTO public.reportings_control_plan_sub_themes
VALUES (1, 63);
VALUES (1, 207);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (1, 208);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (2, 235);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (3, 220);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (4, 222);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (5, 218);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (6, 180);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (1, 27);
VALUES (6, 179);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (2, 83);
VALUES (7, 229);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (3, 83);
VALUES (8, 245);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (4, 24);
VALUES (9, 212);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (5, 24);
VALUES (10, 202);
INSERT INTO public.reportings_control_plan_sub_themes
VALUES (6, 83);
VALUES (11, 192);
5 changes: 2 additions & 3 deletions frontend/cypress/e2e/side_window/reporting/reportings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ context('Reportings', () => {
})

it('Reportings should be archived in Reportings Table', () => {
cy.intercept('PUT', '/bff/v1/reportings/5').as('archiveReporting')
cy.intercept('PUT', '/bff/v1/reportings/archive').as('archiveReporting')
cy.getDataCy('status-filter-Archivés').click()
cy.getDataCy('more-actions-reporting-5').scrollIntoView().click({ force: true })
cy.getDataCy('archive-reporting-5').scrollIntoView().click({ force: true })

cy.wait('@archiveReporting').then(({ response }) => {
expect(response && response.body.id).equal(5)
expect(response && response.body.isArchived).equal(true)
expect(response && response.statusCode).equal(204)
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { reportingActions } from '@features/Reportings/slice'
import { StyledButton, ButtonWithWiteBg, StyledDeleteButton, StyledFooter } from '@features/Reportings/style'
import { archiveReporting } from '@features/Reportings/useCases/archiveReporting'
import { reopenReporting } from '@features/Reportings/useCases/reopenReporting'
import { saveReporting } from '@features/Reportings/useCases/saveReporting'
import { getTimeLeft, isNewReporting } from '@features/Reportings/utils'
import { useAppDispatch } from '@hooks/useAppDispatch'
import { useAppSelector } from '@hooks/useAppSelector'
Expand All @@ -10,6 +9,7 @@ import { ReportingStatusEnum, type Reporting, getReportingStatus } from 'domain/
import { ReportingContext } from 'domain/shared_slices/Global'
import { useFormikContext } from 'formik'
import { isEmpty } from 'lodash'
import { useEffect, useState } from 'react'

type ReportingFooterProps = {
isAutoSaveEnabled: boolean
Expand All @@ -35,6 +35,19 @@ export function Footer({

const reportingStatus = getReportingStatus(values)

const [isFormValid, setIsFormValid] = useState(false)

useEffect(() => {
const checkFormValidity = async () => {
const validateFormResult = await validateForm()
setIsFormValid(isEmpty(validateFormResult))
}

checkFormValidity()
// we need to ccke values change to update the form validity
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values])

const handleReopen = () => {
const endOfValidity = getLocalizedDayjs(values?.createdAt ?? customDayjs().toISOString()).add(
values?.validityTime ?? 0,
Expand All @@ -59,15 +72,10 @@ export function Footer({
}

const handleArchive = async () => {
validateForm().then(async errors => {
if (!isEmpty(errors)) {
dispatch(reportingActions.setIsConfirmCancelDialogVisible(true))

return
}

dispatch(saveReporting({ ...values, isArchived: true }, reportingContext, true))
})
if (!values.id) {
return
}
dispatch(archiveReporting(Number(values.id), reportingContext, true))
}

if (isNewReporting(values.id)) {
Expand Down Expand Up @@ -96,7 +104,7 @@ export function Footer({
Rouvrir le signalement
</StyledButton>
) : (
<StyledButton Icon={Icon.Archive} onClick={handleArchive}>
<StyledButton disabled={!isFormValid} Icon={Icon.Archive} onClick={handleArchive}>
Archiver
</StyledButton>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArchiveModal } from '@features/commonComponents/Modals/Archive'
import { DeleteModal } from '@features/commonComponents/Modals/Delete'
import { archiveReportingFromTable } from '@features/Reportings/useCases/archiveReporting'
import { archiveReporting } from '@features/Reportings/useCases/archiveReporting'
import { deleteReporting } from '@features/Reportings/useCases/deleteReporting'
import { duplicateReporting } from '@features/Reportings/useCases/duplicateReporting'
import { editReportingInLocalStore } from '@features/Reportings/useCases/editReportingInLocalStore'
Expand Down Expand Up @@ -40,7 +40,7 @@ export function ButtonsGroupRow({ id }) {
return setIsArchiveModalOpen(true)
}

return dispatch(archiveReportingFromTable(id))
return dispatch(archiveReporting(id))
}

return setIsDeleteModalOpen(true)
Expand All @@ -57,7 +57,7 @@ export function ButtonsGroupRow({ id }) {
setIsArchiveModalOpen(false)
}

const confirmArchiveReporting = () => dispatch(archiveReportingFromTable(id))
const confirmArchiveReporting = () => dispatch(archiveReporting(id))

return (
<>
Expand Down
89 changes: 58 additions & 31 deletions frontend/src/features/Reportings/useCases/archiveReporting.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
import { reportingActions } from '@features/Reportings/slice'

import { reportingsAPI } from '../../../api/reportingsAPI'
import { setToast } from '../../../domain/shared_slices/Global'
import {
ReportingContext,
setReportingFormVisibility,
setToast,
VisibilityState
} from '../../../domain/shared_slices/Global'

import type { Reporting } from '../../../domain/entities/reporting'
import type { HomeAppThunk } from '@store/index'

export const archiveReportingFromTable = (id: number) => async (dispatch, getState) => {
const {
reporting: { activeReportingId, reportings }
} = getState()
try {
const isReportingExistInLocalStore = reportings[id] || undefined
let reportingToArchive = isReportingExistInLocalStore ? reportings[id].reporting : undefined
export const archiveReporting =
(
id: number,
context: ReportingContext = ReportingContext.SIDE_WINDOW,
closeReporting: boolean = false
): HomeAppThunk =>
async (dispatch, getState) => {
const {
reporting: { reportings }
} = getState()
try {
const isReportingExistInLocalStore = reportings[id]
const archiveResponse = await dispatch(reportingsAPI.endpoints.archiveReportings.initiate({ ids: [id] }))

if (id !== activeReportingId || !reportingToArchive) {
const { data: reporting } = await dispatch(reportingsAPI.endpoints.getReporting.initiate(id))
reportingToArchive = reporting
}
if ('error' in archiveResponse) {
throw Error("Erreur à l'archivage du signalement")
} else {
if (closeReporting) {
dispatch(reportingActions.deleteSelectedReporting(id))
dispatch(
setToast({
containerId: context === ReportingContext.MAP ? 'map' : 'sideWindow',
message: 'Le signalement a bien été archivé',
type: 'success'
})
)

dispatch(
setReportingFormVisibility({
context,
visibility: VisibilityState.NONE
})
)

return
}

dispatch(
setToast({
containerId: 'sideWindow',
message: 'Le signalement a bien été archivé',
type: 'success'
})
)
if (isReportingExistInLocalStore) {
const { data: reporting } = await dispatch(reportingsAPI.endpoints.getReporting.initiate(id))

const response = await dispatch(
reportingsAPI.endpoints.updateReporting.initiate({ ...(reportingToArchive as Reporting), isArchived: true })
)
if ('error' in response) {
throw Error("Erreur à l'archivage du signalement")
} else {
dispatch(
setToast({
containerId: 'sideWindow',
message: 'Le signalement a bien été archivé',
type: 'success'
})
)
if (isReportingExistInLocalStore) {
dispatch(reportingActions.setReporting({ ...reportings[id].reporting, reporting: response.data }))
if (reporting && reportings[id]) {
dispatch(reportingActions.setReporting({ ...reportings[id], reporting }))
}
}
}
} catch (error) {
dispatch(setToast({ containerId: 'sideWindow', message: error }))
}
} catch (error) {
dispatch(setToast({ containerId: 'sideWindow', message: error }))
}
}

0 comments on commit a9b508d

Please sign in to comment.