import { Injectable } from '@angular/core'; import { SpinnerService } from '@core/services/spinner.service'; import { AdHocReportingAPI } from '@core/typings/api/ad-hoc-reporting.typing'; import { APConfigAPI } from '@core/typings/api/ap-config.typing'; import { AdHocReportingUI } from '@core/typings/ui/ad-hoc-reporting.typing'; import { AdHocReportingService } from '@features/reporting/services/ad-hoc-reporting.service'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; import { AttachYCState, BaseYCService } from '@yourcause/common/state'; import { APConfigResources } from './ap-config.resources'; import { APConfigState } from './ap-config.state'; @AttachYCState(APConfigState) @Injectable({ providedIn: 'root' }) export class APConfigService extends BaseYCService { constructor ( private logger: LogService, private adHocReportingService: AdHocReportingService, private apConfigResources: APConfigResources, private notifier: NotifierService, private i18n: I18nService, private spinnerService: SpinnerService ) { super(); } get apConfig () { return this.get('apConfig'); } async setApConfig (force = false) { if (!this.apConfig || force) { const response = await this.fetchAPConfiguration(); this.set('apConfig', response); } } // AP column --> API Ad hoc column mapAPColumnsToAdHocColumns ( columns: APConfigAPI.APSettingsColumn[] ): AdHocReportingUI.ColumnImplementation[] { const adaptedColumns = columns.map((column) => { return { ...column, displayName: column.suppliedColumnName, userSavedFilterColumns: [], sortPriority: column.sortPriority, sortType: null, isChartAggregate: false, isChartGroupingColumn: false, isChartSubGroupingColumn: false, chartAggregateType: null, referenceFieldId: null }; }); const mockedExistingReport: AdHocReportingAPI.UserSavedReport = { id: null, name: '', description: '', reportOwnerClientUserId: null, accessType: AdHocReportingAPI.AdHocReportAccessType.MANAGE, forms: [], clientId: null, isUserOwnedReport: true, reportModelType: AdHocReportingAPI.AdHocReportModelType.Payment, reportType: AdHocReportingAPI.AdHocReportType.AdHoc, userSavedReportColumns: adaptedColumns, primaryFormId: null, useLogicalOperatorAnd: false, advancedFilterColumns: [], referenceFieldTableId: null, referenceFieldTableKey: '', standardComponentIsPublished: true }; const response = this.adHocReportingService.getReportColumnsById( null, mockedExistingReport ); return response; } async fetchAPConfiguration () { try { const response = await this.apConfigResources.fetchAPConfiguration(); return response; } catch (e) { this.logger.error(e); this.notifier.error( this.i18n.translate( 'CONFIG:textErrorFetchingAPConfig', {}, 'There was an error fetching Accounts Payable configuration' ) ); } } async updateAPConfig (payload: APConfigAPI.APReportSettings) { this.spinnerService.startSpinner(); try { await this.apConfigResources.updateAPConfiguration(payload); await this.setApConfig(true); this.spinnerService.stopSpinner(); this.notifier.success( this.i18n.translate( 'CONFIG:textSuccessfullyUpdatedAPConfig', {}, 'Successfully updated Accounts Payable configuration' ) ); } catch (e) { this.logger.error(e); this.spinnerService.stopSpinner(); this.notifier.error( this.i18n.translate( 'CONFIG:textErrorUpdatingAPConfig', {}, 'There was an error updating Accounts Payable configuration' ) ); } } async getDataForVendorIdExport ( isIndividual: boolean, payload: APConfigAPI.GetDataForVendorIdExport ) { try { if (isIndividual) { const response = await this.apConfigResources.getApplicantsForVendorIdExport(payload); return response.map((item) => { item['Individual ID'] = item.id; delete item.id; item['Vendor ID'] = item.vendorId; delete item.vendorId; return item; }); } else { const response = await this.apConfigResources.getOrgsForVendorIdExport(payload); return response.map((item) => { item['Registration ID'] = item.ein; delete item.ein; item['Organization ID'] = item.id; delete item.id; item['Vendor ID'] = item.vendorId; delete item.vendorId; return item; }); } } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'CONFIG:textThereWasAnErrorGettingDataForExport', {}, 'There was an error retrieving data for export' )); return null; } } async handleImportVendorIds ( isIndividual: boolean, contents: APConfigAPI.ImportVendorIdModalResponse[] ) { try { await this.apConfigResources.updateVendorIds( isIndividual ? APConfigAPI.VendorIdImportType.APPLICANT : APConfigAPI.VendorIdImportType.ORG, contents ); this.notifier.success(this.i18n.translate( 'CONFIG:textSuccessfullyImportedTheFile', {}, 'Successfully imported the file' )); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'CONFIG:textErrorImportingTheFile', {}, 'There was an error importing the file' )); } } async updateVendorId ( recordId: number, vendorId: string, isIndividual: boolean ) { try { await this.apConfigResources.updateVendorIds( isIndividual ? APConfigAPI.VendorIdImportType.APPLICANT : APConfigAPI.VendorIdImportType.ORG, [{ id: recordId, vendorId }] ); this.notifier.success(this.i18n.translate( 'CONFIG:textSuccessfullyUpdatedTheVendorID', {}, 'Successfully updated the vendor ID' )); } catch (e) { this.logger.error(e); this.notifier.error(this.i18n.translate( 'CONFIG:textErrorUpdatingVendorID', {}, 'There was an error updating the vendor ID' )); } } }