import { Component, OnInit } from '@angular/core'; import { ReferenceFieldAPI } from '@core/typings/api/reference-fields.typing'; import { ReferenceFieldsUI } from '@core/typings/ui/reference-fields.typing'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { FormAudience } from '@features/configure-forms/form.typing'; import { CustomDataTablesService } from '@features/custom-data-tables/custom-data-table.service'; import { FileService, OrganizedError, SimpleStringMap, TypeaheadSelectOption, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { AnalyticsService, EventType } from '@yourcause/common/analytics'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; import { ApplicantTextValidator, BulkImportValidationClass, FormFieldsSimpleApplicantValidationModel, FormFieldsSimpleManagerValidationModel, FormFieldsWithMultipleApplicantValidationModel, FormFieldsWithOptionsAndMultipleApplicantValidationModel, FormFieldsWithOptionsApplicantValidationModel, ManagerAggregateValidator, ReferenceFieldsValidatorService } from '../services/reference-fields-validator.service'; import { ReferenceFieldsService, STANDARD_FIELD_PREFIX } from '../services/reference-fields.service'; interface BulkFormImportFormGroup { type: ReferenceFieldsUI.ReferenceFieldTypes; formAudience: FormAudience; } @Component({ selector: 'gc-bulk-form-field-import-modal', templateUrl: './bulk-form-field-import-modal.component.html', styleUrls: ['./bulk-form-field-import-modal.component.scss'] }) export class BulkFormFieldImportModalComponent extends YCModalComponent implements OnInit { ValidationModelByAudience = this.referenceFieldValidatorServce.getValidationModelByAudienceMap(); formFields: ReferenceFieldAPI.BulkCreateReferenceField[]; importValid: boolean; errors: OrganizedError[]; typeOptions = this.referenceFieldService.getReferenceFieldTypeOptions() .filter((opt) => { return ![ ReferenceFieldsUI.ReferenceFieldTypes.ExternalAPI, ReferenceFieldsUI.ReferenceFieldTypes.Table, ReferenceFieldsUI.ReferenceFieldTypes.Subset ].includes(opt.value); }); formGroup: TypeSafeFormGroup; formAudienceOptions: TypeaheadSelectOption[] = []; hasOptions = false; typeOptionsMap: SimpleStringMap = {}; constructor ( private formBuilder: TypeSafeFormBuilder, private fileService: FileService, private referenceFieldService: ReferenceFieldsService, private i18n: I18nService, private customDataTableService: CustomDataTablesService, private clientSettingsService: ClientSettingsService, private analyticsService: AnalyticsService, private referenceFieldValidatorServce: ReferenceFieldsValidatorService ) { super(); } get ValidationClass () { return this.ValidationModelByAudience[this.formGroup.value.formAudience][ this.formGroup.value.type ]; } ngOnInit () { this.setTypeOptionsMap(); this.formGroup = this.formBuilder.group({ type: [ReferenceFieldsUI.ReferenceFieldTypes.Checkbox], formAudience: [FormAudience.APPLICANT] }); this.setHasOptions(); this.setFormAudienceOptions(); } setTypeOptionsMap () { this.typeOptions.forEach((type) => { this.typeOptionsMap[type.value] = type.label; }); } setHasOptions () { this.hasOptions = this.referenceFieldService.doesTypeHaveOptions( this.formGroup.value.type ); this.setFormAudienceOptions(); if (this.formGroup.value.type === ReferenceFieldsUI.ReferenceFieldTypes.Aggregate) { setTimeout(() => { this.formGroup.get('formAudience').setValue(FormAudience.MANAGER); }); } } setFormAudienceOptions () { const isAggregate = this.formGroup.value.type === ReferenceFieldsUI.ReferenceFieldTypes.Aggregate; // Aggregate is only valid for GM fields this.formAudienceOptions = [ !isAggregate ? { label: this.i18n.translate( 'common:lblApplicant', {}, 'Applicant' ), value: FormAudience.APPLICANT } : undefined, { label: this.i18n.translate( 'GLOBAL:textGrantManager', {}, 'Grant manager' ), value: FormAudience.MANAGER }].filter((item) => !!item); } downloadErrors () { const csv = this.fileService.convertObjectArrayToCSVString(this.errors); this.fileService.downloadString(csv, 'text/csv', 'Import_Form_Field_Errors.csv'); this.analyticsService.emitEvent({ eventName: 'Download import ref fields errors', eventType: EventType.Click, extras: null }); } downloadGuids () { const csv = this.fileService.convertObjectArrayToCSVString( this.customDataTableService.customDataTables.map((table) => { return { name: table.name, id: table.guid }; }) ); this.fileService.downloadString(csv, 'text/csv', 'Custom_Data_Table_Ids.csv'); } onContentsChange ( rows: BulkImportValidationClass[] ) { const generatedKeys: string[] = []; this.formFields = rows.map((row) => { let type = this.formGroup.value.type; let key = (row as FormFieldsSimpleApplicantValidationModel).Key; const isRootZone = this.clientSettingsService.clientSettings.isRootClient; if (isRootZone) { if (key && !key?.startsWith(STANDARD_FIELD_PREFIX)) { key = STANDARD_FIELD_PREFIX + key; } } if (!key) { key = this.referenceFieldService.guessKey( row.Name, generatedKeys ); generatedKeys.push(key); } const supportsMultiple = ( row as FormFieldsWithMultipleApplicantValidationModel )['Supports multiple values'] || type === ReferenceFieldsUI.ReferenceFieldTypes.SelectBoxes || false; let dataTableId: number = null; const guid = ( row as FormFieldsWithOptionsApplicantValidationModel )['Custom Data Table Id'] ?? null; if (guid) { dataTableId = this.customDataTableService.getCDTIdFromGuid(guid); } let parentReferenceFieldKey = ( row as FormFieldsWithOptionsAndMultipleApplicantValidationModel )['Parent Form Field Key'] ?? null; let aggregateType: ReferenceFieldAPI.ReferenceFieldAggregateType; if (type === ReferenceFieldsUI.ReferenceFieldTypes.Aggregate) { parentReferenceFieldKey = ( row as ManagerAggregateValidator )['Form Field Key']; type = ReferenceFieldsUI.ReferenceFieldTypes.Number; aggregateType = this.referenceFieldService.adaptAggregateTypeFromImport( (row as any)['Aggregation type'] ); } const isEncrypted = (row as ApplicantTextValidator)['Encrypted'] ?? false; const isMasked = (row as ApplicantTextValidator)['Masked'] ?? false; const formatType = (row as ApplicantTextValidator)['Formatting'] || null; const isTableField = (row as ApplicantTextValidator)['Is table field'] ?? false; const formAudience = this.formGroup.value.formAudience; return { id: null, name: row.Name, description: row.Description, type, key, picklistId: dataTableId, parentReferenceFieldKey, supportsMultiple, category: null, formAudience, aggregateType, isEncrypted, isMasked, isTableField, formatType, singleResponse: formAudience === FormAudience.APPLICANT ? true : (row as FormFieldsSimpleManagerValidationModel)['Single response'] ?? false }; }); } onImport () { this.closeModal.emit(this.formFields); this.analyticsService.emitEvent({ eventName: 'Import reference fields save', eventType: EventType.Click, extras: null }); } }