Skip to content

Commit

Permalink
fixed error shown accessing Records or Validation Report for unpublis…
Browse files Browse the repository at this point in the history
…hed survey from Collect (#3017)

Co-authored-by: Stefano Ricci <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 18, 2023
1 parent 270fa49 commit 475539e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 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
3 changes: 2 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 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 475539e

Please sign in to comment.