From 5f611ded438505ea85f56d05bb163bd43b88fe40 Mon Sep 17 00:00:00 2001 From: Loup Theron Date: Mon, 2 Oct 2023 17:18:39 +0200 Subject: [PATCH] Delete previous regulation before updating the next regulation to avoid constraint --- .../regulation/createOrUpdateRegulation.js | 28 --------------- .../regulation/createOrUpdateRegulation.ts | 35 +++++++++++++++++++ .../edit_regulation/EditRegulation.tsx | 2 +- infra/local/README.md | 13 ++----- 4 files changed, 39 insertions(+), 39 deletions(-) delete mode 100644 frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.js create mode 100644 frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.ts diff --git a/frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.js b/frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.js deleted file mode 100644 index 17863c059e..0000000000 --- a/frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.js +++ /dev/null @@ -1,28 +0,0 @@ -import Feature from 'ol/Feature' -import { - REGULATION_ACTION_TYPE, - getRegulatoryFeatureId, mapToRegulatoryFeatureObject -} from '../../../entities/regulation' -import updateRegulation from './updateRegulation' -import resetPreviousRegulation from './resetRegulation' - -const createOrUpdateRegulation = (processingRegulation, id, previousId) => dispatch => { - const featureObject = mapToRegulatoryFeatureObject({ - ...processingRegulation, - region: processingRegulation.region?.join(', ') - }) - - const feature = new Feature(featureObject) - feature.setId(getRegulatoryFeatureId(id)) - dispatch(updateRegulation(feature, REGULATION_ACTION_TYPE.UPDATE)) - - if (geometryHasChanged(previousId, id)) { - dispatch(resetPreviousRegulation(previousId, id)) - } -} - -function geometryHasChanged (previousId, id) { - return previousId && previousId !== id -} - -export default createOrUpdateRegulation diff --git a/frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.ts b/frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.ts new file mode 100644 index 0000000000..252fd3773a --- /dev/null +++ b/frontend/src/domain/use_cases/layer/regulation/createOrUpdateRegulation.ts @@ -0,0 +1,35 @@ +import Feature from 'ol/Feature' + +import resetPreviousRegulation from './resetRegulation' +import updateRegulation from './updateRegulation' +import { + getRegulatoryFeatureId, + mapToRegulatoryFeatureObject, + REGULATION_ACTION_TYPE +} from '../../../entities/regulation' + +export const createOrUpdateRegulation = (processingRegulation, id, previousId) => async dispatch => { + const featureObject = mapToRegulatoryFeatureObject({ + ...processingRegulation, + region: processingRegulation.region?.join(', ') + }) + + const feature = new Feature(featureObject) + feature.setId(getRegulatoryFeatureId(id)) + if (isGeometryModified(previousId, id)) { + /** + * We first need to reset the previous regulation as there is an UNIQUE CONSTRAINT (topic, zone) of the table. + * /!\ This constraint is only applied to the local (CROSS) regulations table. + */ + await dispatch(resetPreviousRegulation(previousId, id)) + } + + /** + * Then, we update the new regulation with the values of the previous one + */ + await dispatch(updateRegulation(feature, REGULATION_ACTION_TYPE.UPDATE)) +} + +function isGeometryModified(previousId, id) { + return previousId && previousId !== id +} diff --git a/frontend/src/features/Backoffice/edit_regulation/EditRegulation.tsx b/frontend/src/features/Backoffice/edit_regulation/EditRegulation.tsx index ea5510c0c4..1d04a098be 100644 --- a/frontend/src/features/Backoffice/edit_regulation/EditRegulation.tsx +++ b/frontend/src/features/Backoffice/edit_regulation/EditRegulation.tsx @@ -34,7 +34,7 @@ import { setRegulatoryTopics, setRegulatoryZoneMetadata } from '../../../domain/shared_slices/Regulatory' -import createOrUpdateRegulation from '../../../domain/use_cases/layer/regulation/createOrUpdateRegulation' +import { createOrUpdateRegulation } from '../../../domain/use_cases/layer/regulation/createOrUpdateRegulation' import getAllRegulatoryLayersByRegTerritory from '../../../domain/use_cases/layer/regulation/getAllRegulatoryLayersByRegTerritory' import getGeometryWithoutRegulationReference from '../../../domain/use_cases/layer/regulation/getGeometryWithoutRegulationReference' import showRegulatoryZone from '../../../domain/use_cases/layer/regulation/showRegulatoryZone' diff --git a/infra/local/README.md b/infra/local/README.md index 6c0d5d4fc8..9fa19c93df 100644 --- a/infra/local/README.md +++ b/infra/local/README.md @@ -28,18 +28,11 @@ COPY public.reglementation_peche (id, law_type, ## Reset a regulation from the local PostGIS regulation referential ``` -update prod.reglementation_peche set regulatory_references = null, topic = null, zones = null, facade = null, region = null, law_type = null where id = 10014; +insert into prod.regulations (id, topic, zone, region, law_type) VALUES (11053, 'Un topic de test', 'Zone de test', 'Bretagne', 'Reg. NAMO'); ``` ## Create a test regulatory entry ``` -insert into prod.reglementation_peche (law_type, facade, topic, zones, region, date_fermeture,date_ouverture, fishing_period, periodes, engins, engins_interdits,mesures_techniques, especes, species, quantites, taille,especes_interdites, autre_reglementation_especes,documents_obligatoires, autre_reglementation, regulatory_references, row_hash) select law_type, - facade, 'Topic de test', 'Zone de test', region, date_fermeture, - date_ouverture, fishing_period, periodes, engins, engins_interdits, - mesures_techniques, especes, species, quantites, taille, - especes_interdites, autre_reglementation_especes, - documents_obligatoires, autre_reglementation, - regulatory_references, row_hash from prod.reglementation_peche where id = 10294; - -``` \ No newline at end of file +insert into prod.regulations (geometry) select geometry from prod.regulations where id = 654; +```