Skip to content

Commit

Permalink
category and taxonomy extra props editor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SteRiccio committed Oct 25, 2024
1 parent b4082fb commit e4200bd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 74 deletions.
43 changes: 43 additions & 0 deletions core/survey/extraPropDefsUpdater.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as Validation from '@core/validation/validation'
import { validateExtraPropDef } from './extraPropDefValidator'

import { ExtraPropDef } from './extraPropDef'

const updateOrDeleteExtraDef = async ({ extraPropDefs, propName, extraPropDef, deleted = false }) => {
const extraPropDefsArray = ExtraPropDef.extraDefsToArray(extraPropDefs)
const itemExtraDefsArrayUpdated = [...extraPropDefsArray]
const oldItemIndex = itemExtraDefsArrayUpdated.findIndex((item) => ExtraPropDef.getName(item) === propName)

if (deleted) {
// remove old item
delete itemExtraDefsArrayUpdated[oldItemIndex]
} else {
if (oldItemIndex < 0) {
// add new extra def item
itemExtraDefsArrayUpdated.push(extraPropDef)
} else {
// update existing item
itemExtraDefsArrayUpdated[oldItemIndex] = extraPropDef
}
const validation = await validateExtraPropDef({
extraPropDef,
extraPropDefsArray: itemExtraDefsArrayUpdated,
})
if (!Validation.isValid(validation)) {
throw new Error('Invalid item extra def')
}
}
// prepare itemExtraDefs for storage
// - remove unnecessary information (uuid, name)
// - index stored object by extra def name
return itemExtraDefsArrayUpdated.reduce((acc, item, index) => {
const name = ExtraPropDef.getName(item)
const dataType = ExtraPropDef.getDataType(item)
acc[name] = ExtraPropDef.newItem({ dataType, index })
return acc
}, {})
}

export const ExtraPropDefsUpdater = {
updateOrDeleteExtraDef,
}
48 changes: 9 additions & 39 deletions server/modules/category/manager/categoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import * as ActivityLog from '@common/activityLog/activityLog'
import * as ObjectUtils from '@core/objectUtils'
import * as PromiseUtils from '@core/promiseUtils'
import * as StringUtils from '@core/stringUtils'
import * as Validation from '@core/validation/validation'

import * as Survey from '@core/survey/survey'
import * as Category from '@core/survey/category'
import * as CategoryLevel from '@core/survey/categoryLevel'
import * as CategoryItem from '@core/survey/categoryItem'
import { ExtraPropDef } from '@core/survey/extraPropDef'
import * as Validation from '@core/validation/validation'
import { validateExtraPropDef } from '@core/survey/extraPropDefValidator'
import { ExtraPropDefsUpdater } from '@core/survey/extraPropDefsUpdater'

import { db } from '@server/db/db'
import * as ActivityLogRepository from '@server/modules/activityLog/repository/activityLogRepository'
Expand Down Expand Up @@ -363,48 +363,18 @@ export const updateCategoryItemExtraDefItem = async (
client.tx(async (t) => {
const category = await _fetchCategory({ surveyId, categoryUuid }, t)

// validate new item extra def
let itemExtraDefsArrayUpdated = [...Category.getItemExtraDefsArray(category)]
// remove old item
const oldItemIndex = itemExtraDefsArrayUpdated.findIndex((item) => ExtraPropDef.getName(item) === name)

if (deleted) {
delete itemExtraDefsArrayUpdated[oldItemIndex]
} else {
if (oldItemIndex < 0) {
// add new extra def item
itemExtraDefsArrayUpdated.push(itemExtraDef)
} else {
// update existing item
itemExtraDefsArrayUpdated[oldItemIndex] = itemExtraDef
}
const validation = await validateExtraPropDef({
extraPropDef: itemExtraDef,
extraPropDefsArray: itemExtraDefsArrayUpdated,
})
if (!Validation.isValid(validation)) {
throw new Error('Invalid category item extra def')
}
}
const extraPropDefs = Category.getItemExtraDef(category)
const itemExtraDefsToStore = await ExtraPropDefsUpdater.updateOrDeleteExtraDef({
extraPropDefs,
propName: name,
extraPropDef: itemExtraDef,
deleted,
})
// update category items
if (deleted || name !== ExtraPropDef.getName(itemExtraDef)) {
await _updateCategoryItemsExtraDef({ surveyId, categoryUuid, name, itemExtraDef, deleted }, t)
}

// prepare itemExtraDefs for storage
// - remove unnecessary information (uuid, name)
// - index stored object by extra def name
const itemExtraDefsToStore = itemExtraDefsArrayUpdated.reduce(
(acc, item, index) => ({
...acc,
[ExtraPropDef.getName(item)]: ExtraPropDef.newItem({
dataType: ExtraPropDef.getDataType(item),
index,
}),
}),
{}
)

return updateCategoryProp(
{
user,
Expand Down
44 changes: 9 additions & 35 deletions server/modules/taxonomy/manager/taxonomyManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import * as R from 'ramda'
import * as ActivityLog from '@common/activityLog/activityLog'

import { ExtraPropDef } from '@core/survey/extraPropDef'
import { validateExtraPropDef } from '@core/survey/extraPropDefValidator'
import * as Taxonomy from '@core/survey/taxonomy'
import { ExtraPropDefsUpdater } from '@core/survey/extraPropDefsUpdater'
import * as Taxon from '@core/survey/taxon'
import * as Validation from '@core/validation/validation'
import * as Taxonomy from '@core/survey/taxonomy'

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

Expand Down Expand Up @@ -292,43 +291,18 @@ export const updateTaxonomyExtraPropDef = async (
client.tx(async (t) => {
const taxonomy = await TaxonomyRepository.fetchTaxonomyByUuid(surveyId, taxonomyUuid, true, t)

// validate new item extra def
let extraPropDefsArrayUpdated = [...Taxonomy.getExtraPropsDefsArray(taxonomy)]
// remove old item
extraPropDefsArrayUpdated = extraPropDefsArrayUpdated.filter((def) => ExtraPropDef.getName(def) !== propName)

if (!deleted) {
// add new extra def item
extraPropDefsArrayUpdated.push(extraPropDef)

const validation = await validateExtraPropDef({
extraPropDef,
extraPropDefsArray: extraPropDefsArrayUpdated,
})
if (!Validation.isValid(validation)) {
throw new Error('Invalid taxonomy item extra def')
}
}

const extraPropDefs = Taxonomy.getExtraPropsDefs(taxonomy)
const extraPropDefsToStore = await ExtraPropDefsUpdater.updateOrDeleteExtraDef({
extraPropDefs,
propName,
extraPropDef,
deleted,
})
// update category items
if (deleted || propName !== ExtraPropDef.getName(extraPropDef)) {
await _updateTaxaExtraDefProp({ surveyId, taxonomyUuid, propName, extraPropDef, deleted }, t)
}

// prepare extraPropDefs for storage
// - remove unnecessary information (uuid, name)
// - index stored object by extra def name
const extraPropDefsToStore = extraPropDefsArrayUpdated.reduce(
(acc, item, index) => ({
...acc,
[ExtraPropDef.getName(item)]: ExtraPropDef.newItem({
dataType: ExtraPropDef.getDataType(item),
index,
}),
}),
{}
)

return updateTaxonomyProp(
user,
surveyId,
Expand Down

0 comments on commit e4200bd

Please sign in to comment.