From 475539e152f4fcf0373d93a6e0162feef208127f Mon Sep 17 00:00:00 2001 From: Stefano Ricci <1219739+SteRiccio@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:23:34 +0200 Subject: [PATCH] fixed error shown accessing Records or Validation Report for unpublished survey from Collect (#3017) Co-authored-by: Stefano Ricci Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- core/survey/_survey/surveyInfo.js | 5 +++++ core/survey/survey.js | 3 ++- server/modules/survey/manager/surveyManager.js | 17 ++++++++++++++--- .../repository/schemaRdbRepository.js | 18 +++++++++++++++--- .../SurveyDefsLoader/SurveyDefsLoader.js | 6 +++++- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/core/survey/_survey/surveyInfo.js b/core/survey/_survey/surveyInfo.js index 0e68184bc3..d172347a32 100644 --- a/core/survey/_survey/surveyInfo.js +++ b/core/survey/_survey/surveyInfo.js @@ -17,6 +17,7 @@ export const keys = { published: ObjectUtils.keys.published, authGroups: 'authGroups', props: ObjectUtils.keys.props, + rdbInitialized: 'rdbInitialized', // Props collectUri: 'collectUri', collectReport: 'collectReport', @@ -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 @@ -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 diff --git a/core/survey/survey.js b/core/survey/survey.js index 495884225d..e1a7da15f8 100644 --- a/core/survey/survey.js +++ b/core/survey/survey.js @@ -121,6 +121,7 @@ export const { isDraft, isValid, isFromCollect, + isRdbInitialized, getCollectUri, getCollectReport, getCollectNodeDefsInfoByPath, @@ -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 { diff --git a/server/modules/survey/manager/surveyManager.js b/server/modules/survey/manager/surveyManager.js index 0c4f19461c..62e1c37cd2 100644 --- a/server/modules/survey/manager/surveyManager.js +++ b/server/modules/survey/manager/surveyManager.js @@ -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' @@ -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 @@ -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 }) => { @@ -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) @@ -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 diff --git a/server/modules/surveyRdb/repository/schemaRdbRepository.js b/server/modules/surveyRdb/repository/schemaRdbRepository.js index 7ae9178a97..bff3ef03ba 100644 --- a/server/modules/surveyRdb/repository/schemaRdbRepository.js +++ b/server/modules/surveyRdb/repository/schemaRdbRepository.js @@ -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 +} diff --git a/webapp/components/survey/SurveyDefsLoader/SurveyDefsLoader.js b/webapp/components/survey/SurveyDefsLoader/SurveyDefsLoader.js index e8a261865f..47642cd7a8 100644 --- a/webapp/components/survey/SurveyDefsLoader/SurveyDefsLoader.js +++ b/webapp/components/survey/SurveyDefsLoader/SurveyDefsLoader.js @@ -42,7 +42,11 @@ const SurveyDefsLoader = (props) => { return null } - if (!requirePublish || Survey.isPublished(surveyInfo) || Survey.isFromCollect(surveyInfo)) { + if ( + !requirePublish || + Survey.isPublished(surveyInfo) || + (Survey.isFromCollect(surveyInfo) && Survey.isRdbInitialized(surveyInfo)) + ) { return children }