Skip to content

Commit

Permalink
Merge branch 'master' of github.com:openforis/arena into fix/validati…
Browse files Browse the repository at this point in the history
…on-feedback
  • Loading branch information
SteRiccio committed Sep 19, 2023
2 parents 7f78379 + 34800b7 commit 984e06a
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 123 deletions.
5 changes: 5 additions & 0 deletions core/survey/_survey/surveyInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const keys = {
published: ObjectUtils.keys.published,
authGroups: 'authGroups',
props: ObjectUtils.keys.props,
rdbInitialized: 'rdbInitialized',
// Props
collectUri: 'collectUri',
collectReport: 'collectReport',
Expand Down Expand Up @@ -52,6 +53,8 @@ export const status = {

export const getInfo = (survey) => (survey.info ? survey.info : survey) // backwards compatibility: survey info were associated to 'info' prop

export const isRdbInitialized = R.propOr(false, keys.rdbInitialized)

// ====== READ surveyInfo
export const { getId, getUuid, getProps, getPropsDraft, isPublished, getDescription, getDescriptions, getLabels } =
ObjectUtils
Expand Down Expand Up @@ -156,6 +159,8 @@ export const markDraft = R.assoc(keys.draft, true)

export const assocSrs = (srs) => ObjectUtils.setProp(keys.srs, srs)

export const assocRDBInitilized = R.assoc(keys.rdbInitialized)

// ====== UTILS

export const isValid = (surveyInfo) => surveyInfo && surveyInfo.id
Expand Down
4 changes: 3 additions & 1 deletion core/survey/nodeDefLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ export const getLayoutChildrenCompressed =
: // item in another row, can have the same x of the previous one
Math.min(itemPrev.x, xOriginal)

const prevYDiff = itemPrev.yOriginal - itemPrev.y

const y = sameRowOfPreviousItem
? // item can have the same y of the previous one
Math.min(itemPrev.y, yOriginal)
yOriginal - prevYDiff
: // item in another row, move it yPrev + hPrev
Math.min(itemPrev.y + itemPrev.h, yOriginal)

Expand Down
4 changes: 3 additions & 1 deletion core/survey/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const {
isDraft,
isValid,
isFromCollect,
isRdbInitialized,
getCollectUri,
getCollectReport,
getCollectNodeDefsInfoByPath,
Expand All @@ -135,7 +136,7 @@ export const {
export const { getAuthGroupByName, getAuthGroups, isAuthGroupAdmin, getAuthGroupAdmin } = SurveyInfo

// UPDATE
export const { assocAuthGroups, assocSrs, markDraft } = SurveyInfo
export const { assocAuthGroups, assocRDBInitilized, assocSrs, markDraft } = SurveyInfo

// ====== READ nodeDefs
export const {
Expand All @@ -149,6 +150,7 @@ export const {
getNodeDefChildren,
getNodeDefChildrenInOwnPage,
hasNodeDefChildrenEntities,
getNodeDefChildrenSorted,
getNodeDefChildByName,
getNodeDefSiblingByName,
getNodeDefByName,
Expand Down
2 changes: 1 addition & 1 deletion server/modules/auth/repository/authGroupRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export const deleteUserGroupBySurveyAndUser = async (surveyId, userUuid, client
JOIN
survey s
ON
s.id = $2
s.uuid = g.survey_uuid AND s.id = $2
WHERE
gu.user_uuid = $1
)
Expand Down
71 changes: 33 additions & 38 deletions server/modules/nodeDef/repository/nodeDefRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,44 +71,39 @@ export const insertNodeDef = async (surveyId, nodeDef, client = DB) =>
(row) => dbTransformCallback({ row, draft: true, advanced: true }) // Always loading draft when creating or updating a nodeDef
)

export const insertNodeDefsBatch = async ({ surveyId, nodeDefs, backup = false }, client = DB) =>
client.tx(async (tx) => {
const schema = getSurveyDBSchema(surveyId)
await tx.batch([
nodeDefs.map((nodeDef) =>
tx.none(
`
INSERT INTO ${schema}.node_def (
parent_uuid,
uuid,
type,
props,
props_draft,
props_advanced,
props_advanced_draft,
meta,
analysis,
virtual)
VALUES ($1, $2, $3,
$4::jsonb, $5::jsonb,
$6::jsonb, $7::jsonb,
$8,$9,$10)`,
[
NodeDef.getParentUuid(nodeDef),
nodeDef.uuid,
NodeDef.getType(nodeDef),
backup ? NodeDef.getProps(nodeDef) : {},
backup ? NodeDef.getPropsDraft(nodeDef) : NodeDef.getProps(nodeDef),
backup ? NodeDef.getPropsAdvanced(nodeDef) : {},
backup ? NodeDef.getPropsAdvancedDraft(nodeDef) : NodeDef.getPropsAdvanced(nodeDef),
NodeDef.getMeta(nodeDef),
NodeDef.isAnalysis(nodeDef),
NodeDef.isVirtual(nodeDef),
]
)
),
])
})
export const insertNodeDefsBatch = async ({ surveyId, nodeDefs, backup = false }, client = DB) => {
const schema = getSurveyDBSchema(surveyId)
return client.none(
DbUtils.insertAllQuery(
schema,
'node_def',
[
'parent_uuid',
'uuid',
'type',
'props',
'props_draft',
'props_advanced',
'props_advanced_draft',
'meta',
'analysis',
'virtual',
],
nodeDefs.map((nodeDef) => [
NodeDef.getParentUuid(nodeDef),
nodeDef.uuid,
NodeDef.getType(nodeDef),
backup ? NodeDef.getProps(nodeDef) : {},
backup ? NodeDef.getPropsDraft(nodeDef) : NodeDef.getProps(nodeDef),
backup ? NodeDef.getPropsAdvanced(nodeDef) : {},
backup ? NodeDef.getPropsAdvancedDraft(nodeDef) : NodeDef.getPropsAdvanced(nodeDef),
NodeDef.getMeta(nodeDef),
NodeDef.isAnalysis(nodeDef),
NodeDef.isVirtual(nodeDef),
])
)
)
}

// ============== READ

Expand Down
17 changes: 14 additions & 3 deletions server/modules/survey/manager/surveyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as User from '@core/user/user'
import * as ObjectUtils from '@core/objectUtils'
import * as Validation from '@core/validation/validation'
import * as PromiseUtils from '@core/promiseUtils'
import SystemError from '@core/systemError'

import { db } from '@server/db/db'
import { DBMigrator } from '@openforis/arena-server'
Expand All @@ -32,7 +33,6 @@ import * as UserManager from '@server/modules/user/manager/userManager'
import * as UserRepository from '@server/modules/user/repository/userRepository'
import * as SurveyRepositoryUtils from '../repository/surveySchemaRepositoryUtils'
import * as SurveyRepository from '../repository/surveyRepository'
import SystemError from '@core/systemError'

const assocSurveyInfo = (survey) => survey

Expand All @@ -44,6 +44,17 @@ const _fetchAndAssocSrss = async ({ surveyInfo }, client) => {
return Survey.assocSrs(srss)(surveyInfo)
}

const _fetchAndAssocRdbInitialized = async ({ surveyInfo }, client) => {
const surveyId = Survey.getId(surveyInfo)
const rdbInitialized = await SchemaRdbRepository.selectSchemaExists(surveyId, client)
return Survey.assocRDBInitilized(rdbInitialized)(surveyInfo)
}

const _fetchAndAssocAdditionalInfo = async ({ surveyInfo }, client) => {
let surveyInfoUpdated = await _fetchAndAssocSrss({ surveyInfo }, client)
return _fetchAndAssocRdbInitialized({ surveyInfo: surveyInfoUpdated }, client)
}

// ====== VALIDATION

export const validateNewSurvey = async ({ newSurvey }) => {
Expand Down Expand Up @@ -168,7 +179,7 @@ export const importSurvey = async (params, client = db) => {
surveyInfo
)

surveyInfo = await _fetchAndAssocSrss({ surveyInfo }, t)
surveyInfo = await _fetchAndAssocAdditionalInfo({ surveyInfo }, t)

await _addUserToSurveyAdmins({ user, surveyInfo }, t)

Expand All @@ -193,7 +204,7 @@ export const fetchSurveyById = async ({ surveyId, draft = false, validate = fals
])

let surveyInfoUpdated = Survey.assocAuthGroups(authGroups)(surveyInfo)
surveyInfoUpdated = await _fetchAndAssocSrss({ surveyInfo: surveyInfoUpdated }, client)
surveyInfoUpdated = await _fetchAndAssocAdditionalInfo({ surveyInfo: surveyInfoUpdated }, client)

const validation = validate ? await validateSurveyInfo(surveyInfoUpdated) : null

Expand Down
18 changes: 15 additions & 3 deletions server/modules/surveyRdb/repository/schemaRdbRepository.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import * as SchemaRdb from '@common/surveyRdb/schemaRdb'
import { Schemata } from '@common/model/db'

import { db } from '@server/db/db'

export const dropSchema = async (surveyId, client = db) =>
client.query(`DROP SCHEMA IF EXISTS ${SchemaRdb.getName(surveyId)} CASCADE`)
client.query(`DROP SCHEMA IF EXISTS ${Schemata.getSchemaSurveyRdb(surveyId)} CASCADE`)

export const createSchema = async (surveyId, client = db) =>
client.query(`CREATE SCHEMA ${SchemaRdb.getName(surveyId)}`)
client.query(`CREATE SCHEMA ${Schemata.getSchemaSurveyRdb(surveyId)}`)

export const selectSchemaExists = async (surveyId, client = db) => {
const result = await client.one(
`
SELECT COUNT(*) = 1 AS res
FROM information_schema.schemata
WHERE schema_name = $1
`,
[Schemata.getSchemaSurveyRdb(surveyId)]
)
return result.res
}
23 changes: 10 additions & 13 deletions webapp/components/Map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import React from 'react'
import { MapContainer, ScaleControl } from 'react-leaflet'
import PropTypes from 'prop-types'

import { Points } from '@openforis/arena-core'

import { ButtonSave } from '@webapp/components'

import i18n from '@core/i18n/i18nFactory'
Expand Down Expand Up @@ -35,7 +33,7 @@ const INITIAL_ZOOM_LEVEL = 3

export const Map = (props) => {
const { editable, layers, markerPoint, markerTitle, showOptions } = props
const { centerPositionLatLon, mapEventHandlers, markerPointUpdated, onMarkerPointUpdated, onSaveClick } =
const { centerPositionLatLon, markerPointUpdated, markerPointUpdatedToString, onMarkerPointUpdated, onSaveClick } =
useMap(props)

if (!centerPositionLatLon) {
Expand All @@ -47,12 +45,7 @@ export const Map = (props) => {
{editable && <div className="location-edit-info">{i18n.t('mapView.locationEditInfo')}</div>}

<MapContextProvider>
<MapContainer
center={centerPositionLatLon}
doubleClickZoom={false}
zoom={INITIAL_ZOOM_LEVEL}
eventHandlers={mapEventHandlers}
>
<MapContainer center={centerPositionLatLon} doubleClickZoom={false} zoom={INITIAL_ZOOM_LEVEL}>
<ScaleControl position="topleft" />
<MapLayersControl layers={layers} />
<MapMarker
Expand All @@ -61,9 +54,13 @@ export const Map = (props) => {
onPointUpdated={onMarkerPointUpdated}
title={markerTitle}
/>
{showOptions && <MapOptionsEditor />}
<KmlUploader />
<MapBaseLayerPeriodSelector />
{showOptions && (
<>
<MapOptionsEditor />
<KmlUploader />
<MapBaseLayerPeriodSelector />
</>
)}
<ShowZoomLevel />
{/* <WmtsComponent /> */}
</MapContainer>
Expand All @@ -74,7 +71,7 @@ export const Map = (props) => {
{markerPointUpdated && (
<div className="location-updated-label">
<label>
{i18n.t('mapView.locationUpdated')}:<span>{Points.toString(markerPointUpdated)}</span>
{i18n.t('mapView.locationUpdated')}:<span> {markerPointUpdatedToString}</span>
</label>
</div>
)}
Expand Down
9 changes: 6 additions & 3 deletions webapp/components/Map/MapMarker/MapMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { useMapMarker } from './useMapMarker'
export const MapMarker = (props) => {
const { editable, point, title } = props

const { markerEventHandlers, markerRef, pointLatLon } = useMapMarker(props)
const { markerEventHandlers, markerRef, pointLatLon, pointUpdated } = useMapMarker(props)

if (!pointLatLon) return null
if (!pointLatLon) {
// invalid location
return null
}

return (
<Marker draggable={editable} eventHandlers={markerEventHandlers} position={pointLatLon} ref={markerRef}>
<MapMarkerPopup point={point} title={title} />
<MapMarkerPopup point={pointUpdated ?? point} title={title} />
</Marker>
)
}
28 changes: 15 additions & 13 deletions webapp/components/Map/MapMarker/useMapMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export const useMapMarker = (props) => {

const [state, setState] = useState({
pointLatLon: null, // array with latitude and longitude values
pointUpdated: null,
})

const { pointLatLon } = state
const { pointLatLon, pointUpdated } = state
const pointSrs = point?.srs

const fromPointToLatLon = useCallback(
Expand All @@ -43,17 +44,18 @@ export const useMapMarker = (props) => {

const onPointUpdated = useCallback(
({ lat, lng }) => {
setState((statePrev) => ({
...statePrev,
pointLatLon: [lat, lng],
}))

let pointUpdated = PointFactory.createInstance({ x: lng, y: lat })

// transform updated location into a location with the same SRS as the marker position parameter
if (pointSrs && pointSrs !== pointUpdated.srs) {
pointUpdated = Points.transform(pointUpdated, pointSrs, srsIndex)
}
setState((statePrev) => ({
...statePrev,
pointLatLon: [lat, lng],
pointUpdated,
}))

onPointUpdatedProp(pointUpdated)
},
[onPointUpdatedProp, pointSrs, srsIndex]
Expand All @@ -72,14 +74,14 @@ export const useMapMarker = (props) => {
[onPointUpdated]
)

if (editable) {
useMapEvents({
dblclick(event) {
useMapEvents({
dblclick(event) {
if (editable) {
const { lat, lng } = event.latlng
onPointUpdated({ lat, lng })
},
})
}
}
},
})

return { markerEventHandlers, markerRef, pointLatLon }
return { markerEventHandlers, markerRef, pointLatLon, pointUpdated }
}
Loading

0 comments on commit 984e06a

Please sign in to comment.