import { Component, OnInit } from '@angular/core'; import { Validators } from '@angular/forms'; import { PolicyService } from '@core/services/policy.service'; import { SpinnerService } from '@core/services/spinner.service'; import { StatusService } from '@core/services/status.service'; import { ProgramTypes } from '@core/typings/program.typing'; import { FormTypes, SimpleDataHubForm } from '@features/configure-forms/form.typing'; import { TableDataDownloadFormat, TypeaheadSelectOption, TypeSafeFormBuilder, TypeSafeFormGroup } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { YCModalComponent } from '@yourcause/common/modals'; import moment, { Moment } from 'moment'; import { DatahubService } from '../data-hub.service'; import { ExportModalResponse } from '../data-hub.typing'; interface ExportFormGroup { exportName: string; programIds: number[]; batchId: number; description: string; form: number; requestForm: SimpleDataHubForm; requestFormRevisionId: number; budgetIds: number[]; fromDate: Moment; toDate: Moment; paymentStatusIds: number[]; canSeeMaskedApplicantInfo: boolean; includeArchived: boolean; exportFileType: TableDataDownloadFormat; } @Component({ selector: 'gc-export-modal', templateUrl: './export-modal.component.html', styleUrls: ['./export-modal.component.scss'] }) export class ExportModalComponent extends YCModalComponent implements OnInit { formGroup: TypeSafeFormGroup; formTypes = FormTypes; modalHeader = ''; type: 'applications'|'awards'|'budgets'|'customForms'|'payments'|'programSummary'; programOptions: TypeaheadSelectOption[] = []; formOptions: TypeaheadSelectOption[] = []; budgetOptions: TypeaheadSelectOption[] = []; batchOptions: TypeaheadSelectOption[] = []; paymentStatusOptions: TypeaheadSelectOption[]; requestFormOptions: TypeaheadSelectOption[] = []; requestFormRevisionOptions: TypeaheadSelectOption[] = []; defaultFormSelectValue = this.i18n.translate( 'GLOBAL:textSelectAForm', {}, 'Select a form' ); isNominationForm = false; showArchiveOption = false; archivedOptionLabel: string; fileTypeOptions: TypeaheadSelectOption[] = [{ label: this.i18n.translate( 'common:textCSV', {}, 'CSV' ), value: TableDataDownloadFormat.CSV }, { label: this.i18n.translate( 'common:textExcel', {}, 'Excel' ), value: TableDataDownloadFormat.XLSX }]; constructor ( private formBuilder: TypeSafeFormBuilder, private i18n: I18nService, private dataHubService: DatahubService, private spinnerService: SpinnerService, private statusService: StatusService, private policyService: PolicyService ) { super(); } get canSeeMaskedApplicants () { return this.policyService.grantApplication.canSeeMaskedApplicants(); } async ngOnInit () { this.spinnerService.startSpinner(); const isForms = this.type === 'customForms'; const isBudgets = this.type === 'budgets'; const isPayments = this.type === 'payments'; const isAwards = this.type === 'awards'; if (isPayments) { await this.dataHubService.getBatches(); const map = this.statusService.paymentStatusMap; this.paymentStatusOptions = this.statusService.get('payment').map((status) => { return { label: map[status.id].translated, value: status.id }; }); } else if (isBudgets) { await this.dataHubService.getBudgets(); } else if (isForms) { await this.dataHubService.getForms(); } if (isAwards || isPayments) { await this.getPrograms(null, ProgramTypes.GRANT); } else { await this.getPrograms(); } this.setModalHeaderAndOptions(); this.formGroup = this.formBuilder.group({ exportName: ['', Validators.required], programIds: [[]], batchId: null, description: '', form: [ null, isForms ? Validators.required : undefined ], requestForm: null, requestFormRevisionId: null, budgetIds: [[]], fromDate: [ isPayments ? moment().subtract(1, 'year') : null, isPayments ? Validators.required : null ], toDate: [ isPayments ? moment() : null, isPayments ? Validators.required : null ], paymentStatusIds: [[]], canSeeMaskedApplicantInfo: false, includeArchived: false, exportFileType: TableDataDownloadFormat.CSV }); this.showArchiveOption = !isBudgets && !isForms; this.spinnerService.stopSpinner(); } setModalHeaderAndOptions () { switch (this.type) { case 'applications': this.modalHeader = this.i18n.translate( 'DATA_HUB:hdrApplicationExport', {}, 'Application Export' ); this.setProgramOptions(); break; case 'awards': this.modalHeader = this.i18n.translate( 'DATA_HUB:hdrAwardExport', {}, 'Award Export' ); this.setProgramOptions(); break; case 'customForms': this.modalHeader = this.i18n.translate( 'DATA_HUB:hdrCustomFormsExport', {}, 'Custom Forms Export' ); this.setFormOptions(); this.setProgramOptions(); break; case 'budgets': this.modalHeader = this.i18n.translate( 'DATA_HUB:hdrBudgetsExport', {}, 'Budget Export' ); this.setBudgetOptions(); break; case 'payments': this.modalHeader = this.i18n.translate( 'DATA_HUB:hdrPaymentsExport', {}, 'Payment Export' ); this.setBatchOptions(); this.setProgramOptions(); break; case 'programSummary': this.modalHeader = this.i18n.translate( 'DATA_HUB:hdrProgramSummaryExport', {}, 'Program Summary Export' ); this.setProgramOptions(); break; } switch (this.type) { case 'payments': this.archivedOptionLabel = this.i18n.translate( 'DATA_HUB:lblIncludeArchivedPayments', {}, 'Include archived payments' ); break; case 'applications': this.archivedOptionLabel = this.i18n.translate( 'DATA_HUB:lblIncludeArchivedApplications', {}, 'Include archived applications' ); break; case 'programSummary': this.archivedOptionLabel = this.i18n.translate( 'DATA_HUB:lblIncludeArchivedPrograms', {}, 'Include archived programs' ); break; case 'awards': this.archivedOptionLabel = this.i18n.translate( 'DATA_HUB:lblIncludeArchivedAwards', {}, 'Include archived awards' ); } } setProgramOptions () { this.programOptions = this.dataHubService.get('programList').map((prog) => { return { label: prog.name, value: prog.id }; }); } setBudgetOptions () { this.budgetOptions = this.dataHubService.get('budgetList').map((budget) => { return { label: budget.name, value: budget.id }; }); } setBatchOptions () { this.batchOptions = this.dataHubService.get('batchesList').map((batch) => { return { label: batch.name, value: batch.id }; }); } setFormOptions () { this.formOptions = this.dataHubService.get('formList').map((form) => { return { label: form.name, value: form.id }; }); } getPrograms (formId: number = null, type: ProgramTypes = null) { return this.dataHubService.getPrograms(formId, type); } async getRequestForms (nominationFormId: number) { const response = await this.dataHubService.getRequestForms(nominationFormId); const map = response.map((form) => { return { label: form.name, value: form }; }); return map; } requestFormChange () { if (this.formGroup.value.requestForm) { this.requestFormRevisionOptions = ( this.formGroup.value.requestForm.revisions as SimpleDataHubForm[] || [] ).map((revision) => { return { label: revision.name, value: revision.id }; }); } } async formChanged () { this.spinnerService.startSpinner(); const formId = this.formGroup.value.form; const found = this.dataHubService.get('formList').find((form) => { return +form.id === +formId; }); if (found && found.formType === FormTypes.NOMINATION) { this.isNominationForm = true; this.formGroup.get('requestForm').setValue(null); this.requestFormOptions = await this.getRequestForms(formId); } else { this.isNominationForm = false; } this.formGroup.get('programIds').setValue([]); this.dataHubService.clearProgramList(); await this.getPrograms(formId); this.spinnerService.stopSpinner(); } buildExport () { const form = this.formGroup.value; const canSeeMaskedApplicantInfo = this.canSeeMaskedApplicants && (this.type !== 'budgets') && (this.type !== 'programSummary') ? form.canSeeMaskedApplicantInfo : undefined; const emitPayload: ExportModalResponse = { nominationGrantFormId: form.requestForm?.id || null, nominationGrantFormRevisionId: form.requestFormRevisionId, formId : form.form, name: form.exportName, programIds: form.programIds, description: form.description, budgetIds: form.budgetIds, fromDate: form.fromDate?.format() ?? '', toDate: form.toDate?.format() ?? '', paymentBatchId: form.batchId, paymentStatusIds: form.paymentStatusIds, canSeeMaskedApplicantInfo, includeArchived: form.includeArchived, exportFileType: form.exportFileType }; this.closeModal.emit(emitPayload); } _onCancel () { this.closeModal.emit(); } }