<commit_before>import {get, keyBy} from 'lodash';
import {IArticle} from 'superdesk-api';
import {getLabelForFieldId} from '../../helpers/getLabelForFieldId';
import {getTypeForFieldId} from '../../helpers/getTypeForFieldId';

const ARTICLE_HEADER_FIELDS = new Set<keyof IArticle>([
    'keywords',
    'genre',
    'anpa_take_key',
    'place',
    'language',
    'priority',
    'urgency',
    'anpa_category',
    'subject',
    'company_codes',
    'ednote',
    'authors',
]);

const ARTICLE_COMMON_FIELDS = new Set<keyof IArticle>([
    'slugline',
]);

ContentProfileFields.$inject = ['$scope', 'content', 'vocabularies', 'metadata'];
export default function ContentProfileFields($scope, content, vocabularies, metadata) {
    this.model = $scope.editing.form;
    this.fieldsById = {};
    this.fieldsBySection = {};

    const getOrder = (field) => get(this.model.editor[field], 'order') || 99;
    const isEnabled = (field) => get(this.model.editor[field], 'enabled', false);

    Promise.all([
        content.getCustomFields(),
        content.getTypeMetadata(this.model._id),
        vocabularies.getVocabularies(),
    ]).then((res) => {
        const [customFields, typeMetadata, vocabulariesCollection] = res;

        const label = (id) => getLabelForFieldId(id, vocabulariesCollection);

        const getArticleHeaderFields = (customVocabulariesForArticleHeader) => {
            const articleHeaderFields = new Set();

            ARTICLE_HEADER_FIELDS.forEach((id) => {
                articleHeaderFields.add(id);
            });

            customVocabulariesForArticleHeader.forEach((filteredCustomField) => {
                articleHeaderFields.add(filteredCustomField._id);
            });

            return articleHeaderFields;
        };

        const getArticleCommonFields = (customTextAndDateVocabularies) => {
            const articleCommonFields = new Set();

            ARTICLE_COMMON_FIELDS.forEach((id) => {
                articleCommonFields.add(id);
            });

            customTextAndDateVocabularies.forEach((filteredCustomField) => {
                articleCommonFields.add(filteredCustomField._id);
            });

            return articleCommonFields;
        };

        const initFields = (customVocabulariesForArticleHeader, customTextAndDateVocabularies) => {
            this.articleHeaderFields = getArticleHeaderFields(customVocabulariesForArticleHeader);
            this.articleCommonFields = getArticleCommonFields(customTextAndDateVocabularies);

            this.enabled = Object.keys(this.model.editor)
                .filter(isEnabled)
                .sort((a, b) => getOrder(a) - getOrder(b));

            updateSections();
        };

        this.model.schema = angular.extend({}, content.contentProfileSchema);
        this.model.editor = angular.extend({}, content.contentProfileEditor);

        this.model.schema = angular.extend({}, typeMetadata.schema);
        this.model.editor = angular.extend({}, typeMetadata.editor);
        this.fieldsById = keyBy(customFields, '_id');

        metadata.getAllCustomVocabulariesForArticleHeader(
            this.model.editor,
            this.model.schema,
        ).then(({customVocabulariesForArticleHeader, customTextAndDateVocabularies}) => {
            initFields(customVocabulariesForArticleHeader, customTextAndDateVocabularies);
        });

        const formatKey = (key: string) => ({
            key: key,
            name: this.model.editor[key].field_name || label(key),
            type: getTypeForFieldId(key, vocabulariesCollection),
        });

        const updateSections = () => {
            this.sections = {
                header: {
                    enabled: [],
                    available: [],
                },
                content: {
                    enabled: [],
                    available: [],
                },
            };

            this.enabled.forEach((key, index) => {
                const headerField = this.articleHeaderFields.has(key) || this.articleCommonFields.has(key);
                const section = this.model.editor[key].section || (headerField ? 'header' : 'content');

                this.sections[section].enabled.push(key);
                this.model.editor[key].order = index + 1; // keep order in sync
            });

            Object.keys(this.model.editor)
                .filter((key) => !isEnabled(key))
                .forEach((key) => {
                    const headerField = this.articleHeaderFields.has(key) || this.articleCommonFields.has(key);
                    const contentField = this.articleCommonFields.has(key) || !headerField;

                    if (headerField) {
                        this.sections.header.available.push(formatKey(key));
                    }

                    if (contentField) {
                        this.sections.content.available.push(formatKey(key));
                    }

                    this.model.editor[key].order = null;
                });
        };<commit_msg>Keep required value of editor fields in sync with the schema fields for Content Profiles. SDNTB-615 (#3198)<commit_after>import {get, keyBy} from 'lodash';
import {IArticle} from 'superdesk-api';
import {getLabelForFieldId} from '../../helpers/getLabelForFieldId';
import {getTypeForFieldId} from '../../helpers/getTypeForFieldId';

const ARTICLE_HEADER_FIELDS = new Set<keyof IArticle>([
    'keywords',
    'genre',
    'anpa_take_key',
    'place',
    'language',
    'priority',
    'urgency',
    'anpa_category',
    'subject',
    'company_codes',
    'ednote',
    'authors',
]);

const ARTICLE_COMMON_FIELDS = new Set<keyof IArticle>([
    'slugline',
]);

ContentProfileFields.$inject = ['$scope', 'content', 'vocabularies', 'metadata'];
export default function ContentProfileFields($scope, content, vocabularies, metadata) {
    this.model = $scope.editing.form;
    this.fieldsById = {};
    this.fieldsBySection = {};

    const getOrder = (field) => get(this.model.editor[field], 'order') || 99;
    const isEnabled = (field) => get(this.model.editor[field], 'enabled', false);

    Promise.all([
        content.getCustomFields(),
        content.getTypeMetadata(this.model._id),
        vocabularies.getVocabularies(),
    ]).then((res) => {
        const [customFields, typeMetadata, vocabulariesCollection] = res;

        const label = (id) => getLabelForFieldId(id, vocabulariesCollection);

        const getArticleHeaderFields = (customVocabulariesForArticleHeader) => {
            const articleHeaderFields = new Set();

            ARTICLE_HEADER_FIELDS.forEach((id) => {
                articleHeaderFields.add(id);
            });

            customVocabulariesForArticleHeader.forEach((filteredCustomField) => {
                articleHeaderFields.add(filteredCustomField._id);
            });

            return articleHeaderFields;
        };

        const getArticleCommonFields = (customTextAndDateVocabularies) => {
            const articleCommonFields = new Set();

            ARTICLE_COMMON_FIELDS.forEach((id) => {
                articleCommonFields.add(id);
            });

            customTextAndDateVocabularies.forEach((filteredCustomField) => {
                articleCommonFields.add(filteredCustomField._id);
            });

            return articleCommonFields;
        };

        const initFields = (customVocabulariesForArticleHeader, customTextAndDateVocabularies) => {
            this.articleHeaderFields = getArticleHeaderFields(customVocabulariesForArticleHeader);
            this.articleCommonFields = getArticleCommonFields(customTextAndDateVocabularies);

            this.enabled = Object.keys(this.model.editor)
                .filter(isEnabled)
                .sort((a, b) => getOrder(a) - getOrder(b));

            updateSections();
        };

        this.model.schema = angular.extend({}, content.contentProfileSchema);
        this.model.editor = angular.extend({}, content.contentProfileEditor);

        this.model.schema = angular.extend({}, typeMetadata.schema);
        this.model.editor = angular.extend({}, typeMetadata.editor);
        this.fieldsById = keyBy(customFields, '_id');

        metadata.getAllCustomVocabulariesForArticleHeader(
            this.model.editor,
            this.model.schema,
        ).then(({customVocabulariesForArticleHeader, customTextAndDateVocabularies}) => {
            initFields(customVocabulariesForArticleHeader, customTextAndDateVocabularies);
        });

        const formatKey = (key: string) => ({
            key: key,
            name: this.model.editor[key].field_name || label(key),
            type: getTypeForFieldId(key, vocabulariesCollection),
        });

        const updateSections = () => {
            this.sections = {
                header: {
                    enabled: [],
                    available: [],
                },
                content: {
                    enabled: [],
                    available: [],
                },
            };

            this.enabled.forEach((key, index) => {
                const headerField = this.articleHeaderFields.has(key) || this.articleCommonFields.has(key);
                const section = this.model.editor[key].section || (headerField ? 'header' : 'content');

                this.sections[section].enabled.push(key);
                this.model.editor[key].order = index + 1; // keep order in sync
                // keep required value of editor fields in sync with the schema fields SDNTB-615
                this.model.editor[key].required = this.model.schema[key].required;
            });

            Object.keys(this.model.editor)
                .filter((key) => !isEnabled(key))
                .forEach((key) => {
                    const headerField = this.articleHeaderFields.has(key) || this.articleCommonFields.has(key);
                    const contentField = this.articleCommonFields.has(key) || !headerField;

                    if (headerField) {
                        this.sections.header.available.push(formatKey(key));
                    }

                    if (contentField) {
                        this.sections.content.available.push(formatKey(key));
                    }

                    this.model.editor[key].order = null;
                });
        };
{'fraction_non_alphanumeric': 0.12193236714975846, 'fraction_numerical': 0.0015458937198067632, 'mean_word_length': 2.185903354878424, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3140306', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2734, 'n_words': 447}