From 0827e2fbd593bcc607de63e410b80cb1fc5900d1 Mon Sep 17 00:00:00 2001 From: danjov Date: Wed, 10 Jan 2024 11:39:27 +0100 Subject: [PATCH] Use .NET API for editing stratigraphies --- CHANGELOG.md | 1 + src/api-legacy/__init__.py | 1 - src/api-legacy/main.py | 3 - src/api-legacy/v1/borehole/profile/patch.py | 190 ------------------ .../v1/borehole/profile/producer.py | 49 ----- src/client/src/api-lib/index.js | 8 +- src/client/src/api/fetchApiV2.js | 12 ++ .../components/profileInfo/api/index.js | 33 +-- 8 files changed, 33 insertions(+), 264 deletions(-) delete mode 100644 src/api-legacy/v1/borehole/profile/patch.py delete mode 100644 src/api-legacy/v1/borehole/profile/producer.py diff --git a/CHANGELOG.md b/CHANGELOG.md index d42511274..01587947a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changed - Updated layer management to use the .NET API. +- Update stratigraphy management to use the .NET API. ## v2.0.506 - 2023-12-21 diff --git a/src/api-legacy/__init__.py b/src/api-legacy/__init__.py index 3d93452f4..a9bacb176 100644 --- a/src/api-legacy/__init__.py +++ b/src/api-legacy/__init__.py @@ -36,7 +36,6 @@ # Profiles's ACTION Handlers from bms.v1.borehole.profile.viewer import ProfileViewerHandler -from bms.v1.borehole.profile.producer import ProfileProducerHandler # Profiles layers's ACTION Handlers from bms.v1.borehole.profile.layer.viewer import ProfileLayerViewerHandler diff --git a/src/api-legacy/main.py b/src/api-legacy/main.py index d09f6db87..ac4c024dc 100644 --- a/src/api-legacy/main.py +++ b/src/api-legacy/main.py @@ -146,7 +146,6 @@ async def close(application): # Profile handlers ProfileViewerHandler, - ProfileProducerHandler, # Layer handlers ProfileLayerViewerHandler, @@ -221,11 +220,9 @@ async def close(application): # Profile handlers (r'/api/v1/borehole/profile', ProfileViewerHandler), - (r'/api/v1/borehole/profile/edit', ProfileProducerHandler), # Profile Layer handlers (r'/api/v1/borehole/profile/layer', ProfileLayerViewerHandler), - (r'/api/v1/borehole/profile/layer/edit', ProfileProducerHandler), # Other handlers (r'/api/v1/borehole/codes', CodeListHandler), diff --git a/src/api-legacy/v1/borehole/profile/patch.py b/src/api-legacy/v1/borehole/profile/patch.py deleted file mode 100644 index 7849a53e0..000000000 --- a/src/api-legacy/v1/borehole/profile/patch.py +++ /dev/null @@ -1,190 +0,0 @@ -# -*- coding: utf-8 -*- -from bms.v1.action import Action -from bms.v1.exceptions import ( - PatchAttributeException -) -import traceback - - -class PatchProfile(Action): - - async def execute(self, id, field, value, user_id): - try: - # Updating character varing, boolean fields - if field in [ - 'date', 'date_spud', 'date_fin', 'date_abd' - ]: - - column = None - - if field == 'date': - column = 'date_sty' - - elif field == 'date_abd': - column = 'casng_date_abd_sty' - - if value == '': - value = None - - await self.conn.execute(""" - UPDATE bdms.stratigraphy - SET - %s = to_date($1, 'YYYY-MM-DD'), - update_sty = now(), - updater_sty = $2 - WHERE id_sty = $3 - """ % column, value, user_id, id) - - elif field in ['primary']: - # Boolean values - - column = None - - if field == 'primary': - column = 'primary_sty' - - if value not in [True, False]: - raise Exception( - f"Value of field {field} is not a boolean" - ) - - if value: - # Reset all others stratigraphy to false - - id_bho = await self.conn.fetchval(""" - SELECT - id_bho_fk - FROM - bdms.stratigraphy - WHERE - id_sty = $1 - """, id) - - await self.conn.execute(""" - UPDATE bdms.stratigraphy - SET - %s = FALSE, - update_sty = now(), - updater_sty = $1 - WHERE id_bho_fk = $2 - """ % column, user_id, id_bho) - - await self.conn.execute(""" - UPDATE bdms.stratigraphy - SET - %s = $1, - update_sty = now(), - updater_sty = $2 - WHERE id_sty = $3 - """ % column, value, user_id, id) - - elif field in [ - 'name', 'fill_name', 'notes' - ]: - # Text values - - column = None - - if field in ['name', 'fill_name']: - column = 'name_sty' - - elif field == 'notes': - column = 'notes_sty' - - await self.conn.execute(""" - UPDATE bdms.stratigraphy - SET - %s = $1, - update_sty = now(), - updater_sty = $2 - WHERE id_sty = $3 - """ % column, value, user_id, id) - - elif field in [ - 'kind' - ]: - - column = None - schema = field - - if field == 'kind': - column = 'kind_id_cli' - schema = 'layer_kind' - - # Check if domain is extracted from the correct schema - if value is not None and schema != (await self.conn.fetchval(""" - SELECT - schema_cli - FROM - bdms.codelist - WHERE - id_cli = $1 - """, value)): - raise Exception( - "Attribute id %s not part of schema %s" % - ( - value, schema - ) - ) - - await self.conn.execute(""" - UPDATE bdms.stratigraphy - SET - %s = $1, - update_sty = now(), - updater_sty = $2 - WHERE id_sty = $3 - """ % column, value, user_id, id) - - elif field in [ - 'fill_casing' - ]: - column = None - schema = field - - if field == 'fill_casing': - column = 'fill_casng_id_sty_fk' - - if value is not None and value != 0: - # Check that the casing and the filling borehole is the same - fill_borehole_id = await self.conn.fetchval(""" - SELECT - id_bho_fk - FROM - bdms.stratigraphy - WHERE - id_sty = $1 - """, id) - - casng_borehole_id = await self.conn.fetchval(""" - SELECT - id_bho_fk - FROM - bdms.stratigraphy - WHERE - id_sty = $1 - """, value) - - if casng_borehole_id != fill_borehole_id: - raise Exception( - "Casing and filling borehole must be the same" - ) - - await self.conn.execute(""" - UPDATE bdms.stratigraphy - SET - %s = $1 - WHERE id_sty = $2 - """ % column, value, id) - - else: - raise PatchAttributeException(field) - - return None - - except PatchAttributeException as bmsx: - raise bmsx - - except Exception: - print(traceback.print_exc()) - raise Exception("Error while updating stratigraphy") diff --git a/src/api-legacy/v1/borehole/profile/producer.py b/src/api-legacy/v1/borehole/profile/producer.py deleted file mode 100644 index 603b337e9..000000000 --- a/src/api-legacy/v1/borehole/profile/producer.py +++ /dev/null @@ -1,49 +0,0 @@ -# -*- coding: utf-8 -*- -from bms.v1.borehole.profile.patch import PatchProfile -from bms.v1.handlers import Producer - - -class ProfileProducerHandler(Producer): - async def execute(self, request): - action = request.pop('action', None) - - if action in [ - 'PATCH' - ]: - - async with self.pool.acquire() as conn: - - exe = None - - id_bho = None - - if action in [ - 'PATCH', - ]: - # Get Borehole id - id_bho = await conn.fetchval(""" - SELECT - id_bho_fk - FROM - bdms.stratigraphy - WHERE - id_sty = $1; - """, request['id']) - - # Lock check - await self.check_lock( - id_bho, self.user, conn - ) - - if action == 'PATCH': - exe = PatchProfile(conn) - request['user_id'] = self.user['id'] - - request.pop('lang', None) - - if exe is not None: - return ( - await exe.execute(**request) - ) - - raise Exception("Action '%s' unknown" % action) diff --git a/src/client/src/api-lib/index.js b/src/client/src/api-lib/index.js index b9f6c9319..d9eb68183 100644 --- a/src/client/src/api-lib/index.js +++ b/src/client/src/api-lib/index.js @@ -83,12 +83,7 @@ import { getLayers, } from "./actions/stratigraphy"; -import { - getProfile, - getProfiles, - getProfileLayers, - patchProfile, -} from "./actions/profile"; +import { getProfile, getProfiles, getProfileLayers } from "./actions/profile"; import { loadDomains, patchCodeConfig } from "./actions/domains"; @@ -167,7 +162,6 @@ export { getProfile, getProfiles, getProfileLayers, - patchProfile, loadDomains, patchCodeConfig, getWms, diff --git a/src/client/src/api/fetchApiV2.js b/src/client/src/api/fetchApiV2.js index 5f7db8956..06ac647b5 100644 --- a/src/client/src/api/fetchApiV2.js +++ b/src/client/src/api/fetchApiV2.js @@ -171,6 +171,10 @@ export const deleteFaciesDescription = async id => { export const fetchUsers = async () => await fetchApiV2("user", "GET"); // stratigraphy +export const fetchStratigraphy = async id => { + return await fetchApiV2(`stratigraphy/${id}`, "GET"); +}; + export const copyStratigraphy = async id => { return await fetchApiV2(`stratigraphy/copy?id=${id}`, "POST"); }; @@ -190,6 +194,14 @@ export const addBedrock = async id => { return await fetchApiV2(`stratigraphy/addbedrock?id=${id}`, "POST"); }; +export const updateStratigraphy = async stratigraphy => { + // remove derived objects + delete stratigraphy.createdBy; + delete stratigraphy.updatedBy; + + return await fetchApiV2("stratigraphy", "PUT", stratigraphy); +}; + // Enable using react-query outputs across the application. // eslint-disable-next-line react-hooks/rules-of-hooks diff --git a/src/client/src/commons/form/profile/components/profileInfo/api/index.js b/src/client/src/commons/form/profile/components/profileInfo/api/index.js index ec5a34bae..99148d1cc 100644 --- a/src/client/src/commons/form/profile/components/profileInfo/api/index.js +++ b/src/client/src/commons/form/profile/components/profileInfo/api/index.js @@ -1,4 +1,8 @@ -import { getProfile, patchProfile } from "../../../../../../api-lib/index"; +import { getProfile } from "../../../../../../api-lib/index"; +import { + fetchStratigraphy, + updateStratigraphy, +} from "../../../../../../api/fetchApiV2"; let data = []; export const getData = async id => { @@ -17,19 +21,20 @@ export const getData = async id => { return data; }; -let isSendProfile = false; export const sendProfile = async (id, attribute, value) => { - await patchProfile(id, attribute, value) - .then(response => { - if (response.data.success) { - isSendProfile = true; - } else { - alert(response.data.message); - } - }) - .catch(function (error) { - console.error(error); - }); + let success = false; + await fetchStratigraphy(id).then(async stratigraphy => { + if (stratigraphy) { + stratigraphy[attribute] = value; + await updateStratigraphy(stratigraphy).then(response => { + if (response) { + success = true; + } + }); + } else { + alert("Failed to get stratigraphy data for update."); + } + }); - return isSendProfile; + return success; };