Commit e441cb15 authored by Giannis Kopanas's avatar Giannis Kopanas Committed by Alexandros Georgantas

fix(applicationmanager): fix models retrieve confgi form db

parent 3a34792b
......@@ -4,6 +4,7 @@ const {
Book,
BookComponent,
BookComponentState,
ApplicationParameter,
} = require('editoria-data-model/src').models
const {
......@@ -64,6 +65,11 @@ const getDashBoardRules = async (_, args, ctx) => {
}
const getBookBuilderRules = async (_, args, ctx) => {
const bookBuilderAppConfig = await ApplicationParameter.query().where({
context: 'bookBuilder',
area: 'stages',
})
await ctx.connectors.UserLoader.model.userTeams.clear()
const book = await Book.find(args.id)
const bookComponents = await BookComponent.query().where({
......@@ -110,7 +116,7 @@ const getBookBuilderRules = async (_, args, ctx) => {
result.bookComponentStateRules = await Promise.all(
map(bookComponentState, async value => {
const data = await Promise.all(
map(config.bookBuilder.stages, async v => {
map(bookBuilderAppConfig[0].config, async v => {
const data = await executeMultipleAuthorizeRules(
ctx,
{
......
......@@ -9,14 +9,13 @@ const keys = require('lodash/keys')
const map = require('lodash/map')
const forEach = require('lodash/forEach')
const clone = require('lodash/clone')
const get = require('lodash/get')
const assign = require('lodash/assign')
const config = require('config')
const logger = require('@pubsweet/logger')
const pubsweetServer = require('pubsweet-server')
const { withFilter } = require('graphql-subscriptions')
const {
ApplicationParameter,
BookComponentState,
BookComponent,
BookComponentTranslation,
......@@ -70,8 +69,14 @@ const ingestWordFile = async (_, { files }, ctx) => {
const addBookComponent = async (_, args, ctx, info) => {
const { divisionId, bookId, componentType, title, uploading } = args.input
const bookBuilder = get(config, 'bookBuilder')
const workflowStages = get(bookBuilder, 'stages')
const applicationParameters = await ApplicationParameter.query().where({
context: 'bookBuilder',
area: 'stages',
})
const { config: workflowStages } = applicationParameters[0]
let bookComponentWorkflowStages
try {
......@@ -153,8 +158,13 @@ const addBookComponent = async (_, args, ctx, info) => {
}
const addBookComponents = async (_, { input }, ctx, info) => {
const bookBuilder = get(config, 'bookBuilder')
const workflowStages = get(bookBuilder, 'stages')
const applicationParameters = await ApplicationParameter.query().where({
context: 'bookBuilder',
area: 'stages',
})
const { config: workflowStages } = applicationParameters[0]
let bookComponentWorkflowStages
try {
......@@ -314,8 +324,12 @@ const updateWorkflowState = async (_, { input }, ctx) => {
try {
const { id, workflowStages } = input
const pubsub = await pubsubManager.getPubsub()
const bookBuilder = get(config, 'bookBuilder')
const lockTrackChanges = get(bookBuilder, 'lockTrackChangesWhenReviewing')
const applicationParameters = await ApplicationParameter.query().where({
context: 'bookBuilder',
area: 'lockTrackChangesWhenReviewing',
})
const { config: lockTrackChanges } = applicationParameters[0]
logger.info(
`Searching of book component state for the book component with id ${id}`,
......
const indexOf = require('lodash/indexOf')
const find = require('lodash/find')
const utils = require('../helpers/utils')
const config = require('config')
const { transaction } = require('objection')
const {
ApplicationParameter,
BookComponent,
Division,
Book,
......@@ -25,6 +25,11 @@ const updateBookComponentOrder = async (
Division,
Book,
async (BookComponent, Division, Book) => {
const applicationParameters = await ApplicationParameter.query().where({
context: 'bookBuilder',
area: 'divisions',
})
const { config: divisions } = applicationParameters[0]
const bookComponent = await BookComponent.findById(bookComponentId)
const sourceDivision = await Division.findById(bookComponent.divisionId)
const found = indexOf(sourceDivision.bookComponents, bookComponentId)
......@@ -62,7 +67,7 @@ const updateBookComponentOrder = async (
bookComponents: updatedTargetDivisionBookComponents,
},
)
const divisionConfig = find(config.bookBuilder.divisions, {
const divisionConfig = find(divisions, {
name: updatedDivision.label,
})
await BookComponent.query().patchAndFetchById(bookComponentId, {
......
import React from 'react'
import styled, { css, keyframes } from 'styled-components'
import { find, map } from 'lodash'
import { find, map, groupBy, forEach } from 'lodash'
import { th } from '@pubsweet/ui-toolkit'
import { Menu as UIMenu } from './Menu'
......@@ -234,13 +234,25 @@ const ComponentTypeMenu = ({
const division = find(divisions, { name: divisionType })
const options = map(division.allowedComponentTypes, componentType => ({
label: componentType,
value: componentType,
}))
const groupedOptions = groupBy(division.allowedComponentTypes, value =>
value.predefined ? 'predefined' : 'custom',
)
const options = []
forEach(groupedOptions, groupOption => {
options.push({
text: null,
children: map(groupOption, componentType => ({
label: componentType.title,
value: componentType.value,
})),
})
})
const handleChangeComponentType = value => {
onChange(value)
}
return (
<Menu
onChange={handleChangeComponentType}
......
......@@ -261,8 +261,8 @@ class Division extends React.Component {
addButtons = map(componentConfig.allowedComponentTypes, componentType => (
<AddComponentButton
add={this.onAddClick}
label={`add ${componentType}`}
type={componentType}
label={`add ${componentType.title}`}
type={componentType.value}
/>
))
}
......
......@@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import styled from 'styled-components'
import { th, override } from '@pubsweet/ui-toolkit'
import { Button } from '@pubsweet/ui'
import { forEach } from 'lodash'
// #region styled components
......@@ -246,8 +247,14 @@ class Menu extends React.Component {
optionLabel = value => {
const { options } = this.props
return options.find(option => option.value === value)
? options.find(option => option.value === value).label
const flatOption = []
forEach(options, value => {
if (value.children) flatOption.push(value.children)
if (!value.children) flatOption.push(value)
})
return flatOption.find(option => option.value === value)
? flatOption.find(option => option.value === value).label
: ''
}
......@@ -287,17 +294,37 @@ class Menu extends React.Component {
<OptionsContainer>
{open && (
<Options maxHeight={maxHeight} open={open}>
{options.map(option => (
<RenderOption
handleKeyPress={this.handleKeyPress}
handleSelect={this.handleSelect}
key={option.value}
label={option.label}
multi={multi}
selected={selected}
value={option.value}
/>
))}
{options.map(option => {
let groupedHeader = null
let groupedOptions = [option]
if (option.children) {
groupedOptions = option.children
groupedHeader = option.text ? (
<>
<span>{option.text}</span>
<hr />
</>
) : (
<hr />
)
}
return (
<>
{groupedHeader}
{groupedOptions.map(groupoption => (
<RenderOption
handleKeyPress={this.handleKeyPress}
handleSelect={this.handleSelect}
key={groupoption.value}
label={groupoption.label}
multi={multi}
selected={selected}
value={groupoption.value}
/>
))}
</>
)
})}
</Options>
)}
</OptionsContainer>
......
......@@ -53,6 +53,7 @@ class SecondRow extends Component {
workflowStages.length - 1 ===
findIndex(workflowStages, { label: title, type })
if (instanceConfig === 'UCP') {
debugger;
if (type === 'file_prep' && (value === -1 || value === 0)) {
if (
find(workflowStages, { type: 'edit' }).value === 0 ||
......
......@@ -75,6 +75,7 @@ const WorkflowList = ({
values={values}
/>
)
const items = map(stageItems, stageItem => {
const { type } = stageItem
const currentValueIndex = indexOf(
......
const uuid = require('uuid/v4')
const { dbCleaner } = require('pubsweet-server/test')
const config = require('config')
// const set = require('lodash/set')
const unset = require('lodash/unset')
const { Book, BookCollection, Division } = require('../src').models
const {
Book,
BookCollection,
Division,
ApplicationParameter,
} = require('../src').models
const divisionConfig = [
{
name: 'Frontmatter',
showNumberBeforeComponents: [],
allowedComponentTypes: [
{ value: 'component', title: 'Component', predefined: true },
],
defaultComponentType: 'component',
},
{
name: 'Body',
showNumberBeforeComponents: ['chapter'],
allowedComponentTypes: [
{ value: 'chapter', title: 'Chapter', predefined: true },
{ value: 'part', title: 'Part', predefined: true },
{ value: 'unnumbered', title: 'Unnumbered', predefined: true },
],
defaultComponentType: 'chapter',
},
{
name: 'Backmatter',
showNumberBeforeComponents: [],
allowedComponentTypes: [
{ value: 'component', title: 'Component', predefined: true },
],
defaultComponentType: 'component',
},
]
describe('Book', () => {
beforeEach(async () => {
......@@ -37,6 +68,12 @@ describe('Book', () => {
})
it('creates divisions on book creation based on the config', async () => {
await new ApplicationParameter({
context: 'bookBuilder',
area: 'divisions',
config: JSON.stringify(divisionConfig),
}).save()
const collection = await new BookCollection().save()
const book = await new Book({ collectionId: collection.id }).save()
......@@ -57,10 +94,11 @@ describe('Book', () => {
const actualPosition = book.divisions.indexOf(division.id)
expect(actualPosition).toBe(correctPosition)
})
await ApplicationParameter.query().del()
})
it('creates a default division on book creation if no config is found', async () => {
unset(config, 'bookBuilder')
const collection = await new BookCollection().save()
const book = await new Book({ collectionId: collection.id }).save()
const divisions = await Division.query().where('bookId', book.id)
......
......@@ -19,13 +19,12 @@
const { Model } = require('objection')
const uuid = require('uuid/v4')
const get = require('lodash/get')
const config = require('config')
const find = require('lodash/find')
const Base = require('../editoriaBase')
const { model: BookCollection } = require('../bookCollection')
const { model: Division } = require('../division')
const { model: ApplicationParameter } = require('../applicationParameter')
const { booleanDefaultFalse, id, string, year } = require('../helpers').schema
......@@ -91,7 +90,11 @@ class Book extends Base {
If no divisions in config, make a single default 'body' division.
Otherwise create the ones declared in the config.
*/
const divisions = get(config, 'bookBuilder.divisions')
const applicationParameters = await ApplicationParameter.query()
const { config: divisions } = find(applicationParameters, {
context: 'bookBuilder',
area: 'divisions',
})
if (!divisions) {
const division = await this.addDivision('body')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment