UNPKG

@tasolutions/express-core

Version:
69 lines (62 loc) 3.04 kB
// fieldBuilderInterface.js const _ = require('lodash'); const { fieldsToExclude } = require('./constants'); const { createObjectSelectOptionInterface } = require('./createObjectSelectOptionInterface'); const { createCustomSelectOptionInterface } = require('./createCustomSelectOptionInterface'); const buildFieldListFromInterface = async (interfaceSchema, segment = '/') => { const fieldList = []; for (const key of Object.keys(interfaceSchema)) { if (fieldsToExclude.includes(key)) continue; const field = interfaceSchema[key]; const fieldObject = { index: field.index || 0, key: key, type: field.type || null, type_type: field.type_type || null, show: field.show || false, is_required: field.required || false, is_filter: field.filter || false, label: field.label ? field.label : _.startCase(_.toLower(key)), refresh_change_fields: field.refresh_change_fields || null, html: { placeholder: `Please input ${_.startCase(_.toLower(key))}`, disabled: false, read_only: false, col: 12, }, select_options: field.select_options_ref ? await createObjectSelectOptionInterface(field, segment) : null, db_type: 'Interface', max_length: field.max_length || null, array_fields: field.type == 'ARRAY' && field.array_fields ? await buildFieldListFromInterface(field.array_fields, segment) : null, }; if (field.select_options) { if (fieldObject.select_options) { const fieldSelectOptions = createCustomSelectOptionInterface(field.select_options, segment); Object.keys(fieldSelectOptions).forEach(optionKey => { if (fieldObject.select_options.hasOwnProperty(optionKey)) { if (optionKey !== 'api') { if (fieldSelectOptions[optionKey] !== null) { fieldObject.select_options[optionKey] = fieldSelectOptions[optionKey]; } } else { Object.keys(fieldSelectOptions.api).forEach(apiKey => { if (fieldObject.select_options.api.hasOwnProperty(apiKey)) { if (fieldSelectOptions.api[apiKey] !== null) { fieldObject.select_options.api[apiKey] = fieldSelectOptions.api[apiKey]; } } }); } } }); } else { fieldObject.select_options = createCustomSelectOptionInterface(field.select_options, segment); } } fieldList.push(fieldObject); } return fieldList; }; module.exports = { buildFieldListFromInterface, };