@tasolutions/express-core
Version:
All libs for express
116 lines (104 loc) • 4.83 kB
JavaScript
const _ = require('lodash');
const { fieldsToExclude } = require('./constants');
const buildFieldListSelect = async (Collection, segment = '/') => {
const schema = Collection.schema.paths;
const fieldList = [];
const fieldPromises = Object.keys(schema).map(async (key, index) => {
if (fieldsToExclude.includes(key)) return null;
const field = schema[key];
const fieldOption = field.options || {};
const label = _.startCase(_.toLower(fieldOption.label || key));
const fieldObject = {
index,
key: key,
type: null,
type_sub: null,
show: true,
is_required: field.isRequired || false,
is_filter: field.options?.filter || false,
label,
html: {
placeholder: `Please input ${label}`,
disabled: false,
read_only: false,
col: 12,
},
refresh_change_fields: fieldOption.refresh_change_fields || null,
select_options: null,
db_type: field.instance,
max_length: null,
};
if (fieldOption.enum && fieldOption.enum.length) {
fieldOption.field_type = 'SELECT';
fieldOption.select_options = {
api: null,
fields: [
{ key: 'key', label, type: 'TEXT' },
],
values: fieldOption.enum.map(value => {
return { key: value };
}),
field_submit: 'key',
field_display: 'key',
};
}
switch (fieldOption.field_type) {
case 'IMAGE':
fieldObject.max_length = 1;
break;
case 'SELECT':
if (!fieldOption.select_options) {
throw new Error(`'${key}' require select_option.`);
}
break;
}
switch (field.instance) {
case 'Array':
fieldObject.type = 'ARRAY';
if (field.caster && field.caster.options) {
const fieldCasterOptions = field.caster.options;
fieldObject.type = fieldCasterOptions.field_type || 'ARRAY';
fieldObject.type_sub = fieldCasterOptions.field_type_sub ? fieldCasterOptions.field_type_sub.toUpperCase() : (field.caster.instance === 'String' ? 'TEXT' : field.caster.instance.toUpperCase());
if (field.caster.instance === 'ObjectId' && !fieldCasterOptions.select_options) {
// fieldCasterOptions.select_options = await createObjectSelectOption(field.caster, segment);
fieldCasterOptions.field_type = 'SELECT';
fieldObject.type = 'SELECT';
} else {
fieldObject.select_options = fieldCasterOptions.select_options;
fieldObject.type = 'SELECT';
}
fieldObject.label = fieldCasterOptions.label || fieldObject.label;
fieldObject.select_options = fieldCasterOptions.select_options || null;
fieldObject.refresh_change_fields = fieldCasterOptions.refresh_change_fields || null;
fieldObject.max_length = fieldCasterOptions.max_length || 2;
}
break;
case 'ObjectId':
if (!fieldOption.select_options) {
// fieldObject.select_options = await createObjectSelectOption(field, segment);
fieldObject.type = 'SELECT';
fieldObject.max_length = 1;
} else {
fieldObject.select_options = fieldOption.select_options;
}
break;
default:
fieldObject.type_sub = fieldOption.field_type_sub || null;
fieldObject.select_options = fieldOption.select_options || null;
fieldObject.max_length = fieldOption.max_length || 1;
if (fieldObject.field_type === 'SELECT' && !fieldOption.select_option) {
throw new Error(`'${key}' requires select_option.`);
}
fieldObject.type = fieldOption.field_type ? fieldOption.field_type.toUpperCase() : (field.instance === 'String' ? 'TEXT' : field.instance.toUpperCase());
break;
}
fieldObject.type_sub = fieldObject.type_sub?.toUpperCase() || null;
return fieldObject;
});
const resolvedFieldList = await Promise.all(fieldPromises);
fieldList.push(...resolvedFieldList.filter(field => field !== null));
return fieldList;
};
module.exports = {
buildFieldListSelect,
};