UNPKG

@traien/n8n-nodes-espocrm

Version:
197 lines 7.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamicFieldsHelper = void 0; const MetadataService_1 = require("../services/MetadataService"); class DynamicFieldsHelper { static async generateFieldSelector(entityType, parameterName = 'field', displayName = 'Field', description = 'Select a field') { const fieldDefs = await MetadataService_1.MetadataService.getFieldDefs.call(this, this, entityType); const options = Object.keys(fieldDefs).map((fieldName) => { const fieldDef = fieldDefs[fieldName]; return { name: fieldDef.label ? `${fieldDef.label} (${fieldName})` : fieldName, value: fieldName, }; }); options.sort((a, b) => a.name.localeCompare(b.name)); return { displayName, name: parameterName, type: 'options', options, default: '', description, required: true, }; } static async generateValueInput(entityType, fieldName, parameterName = 'value', displayName = 'Value') { const fieldDefs = await MetadataService_1.MetadataService.getFieldDefs.call(this, this, entityType); const fieldDef = fieldDefs[fieldName]; if (!fieldDef) { return { displayName, name: parameterName, type: 'string', default: '', description: 'Value for the field', }; } const fieldType = MetadataService_1.MetadataService.mapFieldTypeToN8n(fieldDef.type, fieldDef); const baseProperties = { displayName, name: parameterName, description: `Value for the ${fieldDef.label || fieldName} field`, }; if (fieldType === 'options') { if (fieldDef.type === 'multiEnum' || fieldDef.type === 'array' || fieldDef.type === 'checklist') { return { ...baseProperties, type: 'multiOptions', options: MetadataService_1.MetadataService.generateFieldOptions(fieldDefs, fieldName), default: [], }; } else { return { ...baseProperties, type: 'options', options: MetadataService_1.MetadataService.generateFieldOptions(fieldDefs, fieldName), default: '', }; } } else if (fieldType === 'boolean') { return { ...baseProperties, type: 'boolean', default: false, }; } else if (fieldType === 'number') { const typeOptions = {}; if (fieldDef.min !== undefined) { typeOptions.minValue = fieldDef.min; } if (fieldDef.max !== undefined) { typeOptions.maxValue = fieldDef.max; } return { ...baseProperties, type: 'number', default: 0, typeOptions: Object.keys(typeOptions).length > 0 ? typeOptions : undefined, }; } else if (fieldType === 'dateTime') { return { ...baseProperties, type: 'dateTime', default: '', }; } else if (fieldType === 'json') { return { ...baseProperties, type: 'json', default: '{}', typeOptions: { alwaysOpenEditWindow: true, }, }; } else if (MetadataService_1.MetadataService.isRelationshipField(fieldDef.type)) { if (fieldDef.entity) { return { ...baseProperties, type: 'string', default: '', description: `ID of the related ${fieldDef.entity} record`, placeholder: fieldDef.type === 'linkMultiple' ? 'Comma-separated IDs' : 'Record ID', }; } else { return { ...baseProperties, type: 'string', default: '', description: 'ID of the related record', }; } } else { const typeOptions = {}; if (fieldDef.type === 'text' || fieldDef.type === 'wysiwyg') { typeOptions.rows = 4; } if (fieldDef.type === 'password') { typeOptions.password = true; } if (fieldDef.type === 'url') { typeOptions.validationRegex = '^(https?|ftp):\\/\\/[^\\s/$.?#].[^\\s]*$'; typeOptions.validationMessage = 'Please enter a valid URL'; } if (fieldDef.type === 'email') { typeOptions.validationRegex = '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$'; typeOptions.validationMessage = 'Please enter a valid email address'; } if (fieldDef.type === 'varchar' && fieldDef.maxLength) { typeOptions.maxLength = fieldDef.maxLength; } return { ...baseProperties, type: 'string', default: '', typeOptions: Object.keys(typeOptions).length > 0 ? typeOptions : undefined, }; } } static async generateEntityTypeSelector(parameterName = 'entityType', displayName = 'Entity Type', description = 'Select an entity type') { const entityList = await MetadataService_1.MetadataService.getEntityList.call(this, this); const options = entityList.map((entityType) => ({ name: entityType, value: entityType, })); options.sort((a, b) => a.name.localeCompare(b.name)); return { displayName, name: parameterName, type: 'options', options, default: '', description, required: true, }; } static async generateRelationshipFilterSelector(entityType, linkName, parameterName = 'relationFilter', displayName = 'Relation Filter') { const links = await MetadataService_1.MetadataService.getEntityLinks.call(this, this, entityType); const linkDef = links[linkName]; if (!linkDef || !linkDef.entity) { return { displayName, name: parameterName, type: 'string', default: '', description: 'Filter for related records', }; } const relatedEntityType = linkDef.entity; const relatedFields = await MetadataService_1.MetadataService.getFieldDefs.call(this, this, relatedEntityType); const options = Object.keys(relatedFields).map((fieldName) => { const fieldDef = relatedFields[fieldName]; return { name: fieldDef.label ? `${fieldDef.label} (${fieldName})` : fieldName, value: fieldName, }; }); options.sort((a, b) => a.name.localeCompare(b.name)); return { displayName, name: parameterName, type: 'options', options, default: '', description: `Select field from related ${relatedEntityType} to filter on`, }; } } exports.DynamicFieldsHelper = DynamicFieldsHelper; //# sourceMappingURL=DynamicFieldsHelper.js.map