import { Component, EventEmitter, Input, Output } from '@angular/core'; import { SpinnerService } from '@core/services/spinner.service'; import { AddOrgUI } from '@core/typings/ui/add-org.typing'; import { AddOrganizationService } from '@features/add-organization/add-organization.service'; import { ClientSettingsService } from '@features/client-settings/client-settings.service'; import { NonprofitService } from '@features/nonprofit/nonprofit.service'; import { AddressFormatterService, BucketSearchResultObj, DebounceFactory, EinService, OrganizationEligibleForGivingStatus, SearchFilter, SearchResult, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { from, map } from 'rxjs'; @Component({ selector: 'gc-search-npo-autocomplete', templateUrl: './search-npo-autocomplete.component.html', styleUrls: ['./search-npo-autocomplete.component.scss'] }) export class SearchNpoAutocompleteComponent { @Input() placeholder = ''; @Input() charityBucketId: string; @Input() clientId: number; @Input() processorIsYourCause: boolean; @Output() onOrgSelect = new EventEmitter(); formGroup: TypeSafeFormGroup<{ searchTerm: string }>; results: any[]; counter = 0; debounce: DebounceFactory; hasInternational = this.clientSettingsService.clientSettings.hasInternational; searchFilters: SearchFilter[] = []; constructor ( public fb: TypeSafeFormBuilder, private einService: EinService, private addressFormatter: AddressFormatterService, private nonprofitService: NonprofitService, private spinnerService: SpinnerService, private clientSettingsService: ClientSettingsService, private addOrgService: AddOrganizationService ) { this.formGroup = fb.group({ searchTerm: '' }); this.debounce = DebounceFactory.create((term: string) => { return this.doSearch(term); }, 500); this.getDisplay = this.getDisplay.bind(this); } async orgSelected (org: SearchResult) { if (org) { this.spinnerService.startSpinner(); const adaptedOrg = await this.adaptedOrg(org); this.spinnerService.stopSpinner(); this.formGroup.get('searchTerm').setValue(''); this.addOrgService.setOriginalSearchResponse(org); this.setSelectionType(adaptedOrg); this.onOrgSelect.emit(adaptedOrg); } } setSelectionType (adaptedOrg: SearchResult) { if (adaptedOrg.document.eligibleForGivingStatusId === OrganizationEligibleForGivingStatus.ELIGIBLE) { this.addOrgService.setSelectionType(AddOrgUI.SelectionType.VETTED_ORG); } else { this.addOrgService.setSelectionType(AddOrgUI.SelectionType.UNVETTED_ORG); } } async getOrgId (org: SearchResult): Promise { let charityId; if (org.document && org.document.nonprofitGuid) { charityId = await this.nonprofitService.getIdByNonprofitGuid(org.document.nonprofitGuid); return +charityId; } return +org.document.id; } async adaptedOrg (org: SearchResult): Promise { const id = '' + await this.getOrgId(org); let addressString = ''; let address1; let address2; let city; let stateProvRegCode; let country; let postalCode; let eligibleForGivingStatusId = org.document.eligibleForGivingStatusId; if (org.document.nonprofitGuid) { const { nonprofitDetail } = await this.nonprofitService.getNonprofitAdditionalDataByGuid( org.document.nonprofitGuid ); if (nonprofitDetail) { const address = this.nonprofitService.getNonprofitAddressFields( nonprofitDetail ); address1 = address.address1; address2 = address.address2; city = address.city; stateProvRegCode = address.stateProvRegCode; country = address.country; postalCode = address.postalCode; addressString = this.addressFormatter.format( address, true ); eligibleForGivingStatusId = nonprofitDetail.eligibleForGivingStatusId; } } return SearchResult.construct({ text: org.document.name, document: { ...org.document, id, name: org.document.name, classification: org.document.classification, addressString, iconURL: org.document.iconURL, displayName: org.document.name, address1: address1 || org.document.address1, address2: address2 || org.document.address2, city: city || org.document.city, postalCode: postalCode || org.document.postalCode, country: country || org.document.country, stateProvRegCode: stateProvRegCode || org.document.stateProvRegCode, nonprofitGuid: org.document.nonprofitGuid, eligibleForGivingStatusId, parentName: org.document.parentName, parentGuid: org.document.parentGuid, registrationId: org.document.registrationId } }); } adaptBucketSearchOrg (org: BucketSearchResultObj) { return SearchResult.construct({ text: org.name, document: { id: '' + org.id, name: org.name, registrationId: org.registrationNumber, classification: null, addressString: org.location.address, iconURL: org.imageUrl, displayName: org.name, address1: null, city: null, postalCode: null, country: null, stateProvRegCode: null, stateProvRegId: null, nonprofitGuid: org.uniqueGuid, eligibleForGivingStatusId: null // Will fetch this if they actually select the org } }); } doSearch (term: string) { this.searchFilters = []; if (this.charityBucketId) { return from( this.nonprofitService.searchNonprofitsWithinBucket( term, 1, 5, this.clientId, this.charityBucketId ) ).pipe(map((val) => { return val.data ? val.data.map(result => this.adaptBucketSearchOrg(result)) : []; })); } else { // if international, searchFilters stay as is, if not, add filter for domestic if (!this.hasInternational) { this.searchFilters = [ ...this.clientSettingsService.domesticRegAuthFilters ]; } if (this.processorIsYourCause) { this.searchFilters = [ ...this.searchFilters, { filterName: 'EligibleForGivingStatusId', filterValue: '' + OrganizationEligibleForGivingStatus.ELIGIBLE }, { filterName: 'EligibleForGivingStatusId', filterValue: '' + OrganizationEligibleForGivingStatus.INFO_REQUIRED } ]; } return from( this.nonprofitService.searchPublicNonprofits( term, this.searchFilters, 1, 5 ) ).pipe(map((val) => { return val.results ? val.results.map(result => SearchResult.construct(result)) : []; })); } } getDisplay (result: SearchResult) { return `${result.document.remittanceName || result.document.displayName || result.document.name}: ${this.einService.formatEin(result.document.registrationId)}`; } }