diff --git a/src/app/modules/organizations/data-quality/data-quality.component.html b/src/app/modules/organizations/data-quality/data-quality.component.html index 2832dc46..5cd58da5 100644 --- a/src/app/modules/organizations/data-quality/data-quality.component.html +++ b/src/app/modules/organizations/data-quality/data-quality.component.html @@ -4,7 +4,11 @@ @if (type === 'goal') { } @else { - + } diff --git a/src/app/modules/organizations/data-quality/inventory/inventory-table.component.spec.ts b/src/app/modules/organizations/data-quality/inventory/inventory-table.component.spec.ts new file mode 100644 index 00000000..5f57076c --- /dev/null +++ b/src/app/modules/organizations/data-quality/inventory/inventory-table.component.spec.ts @@ -0,0 +1,53 @@ +import { TestBed } from '@angular/core/testing' +import { MatDialog } from '@angular/material/dialog' +import { of } from 'rxjs' +import type { Column, Rule } from '@seed/api' +import { ColumnService, DataQualityService, LabelService, OrganizationService } from '@seed/api' +import { DataQualityInventoryTableComponent } from './inventory-table.component' +import { FormModalComponent } from './modal/form-modal.component' + +const column = (column_name: string, display_name: string, table_name: Column['table_name']) => + ({ column_name, display_name, table_name, derived_column: null }) as Column + +describe('DataQualityInventoryTableComponent', () => { + const setup = async () => { + const open = jasmine.createSpy('open').and.returnValue({ afterClosed: () => of(null) }) + const propertyColumns = [column('custom_id_1', 'Custom ID 1', 'PropertyState'), column('custom_id_1', 'Custom ID 1 (Tax Lot)', 'TaxLotState')] + const taxLotColumns = [column('custom_id_1', 'Custom ID 1', 'TaxLotState'), column('custom_id_1', 'Custom ID 1 (Property)', 'PropertyState')] + await TestBed.configureTestingModule({ + imports: [DataQualityInventoryTableComponent], + providers: [ + { provide: MatDialog, useValue: { open } }, + { provide: OrganizationService, useValue: { currentOrganization$: of({ id: 1 }) } }, + { provide: ColumnService, useValue: { propertyColumns$: of(propertyColumns), taxLotColumns$: of(taxLotColumns) } }, + { provide: LabelService, useValue: { labels$: of([]) } }, + { provide: DataQualityService, useValue: {} }, + ], + }) + .overrideComponent(DataQualityInventoryTableComponent, { set: { template: '', imports: [] } }) + .compileComponents() + + const fixture = TestBed.createComponent(DataQualityInventoryTableComponent) + fixture.componentInstance.currentRules = [] + fixture.componentInstance.type = 'taxlots' + fixture.detectChanges() + return { component: fixture.componentInstance, open } + } + + it('ignores related columns when looking up field names', async () => { + const { component } = await setup() + component.type = 'properties' + expect(component.getFieldName('custom_id_1')).toBe('Custom ID 1') + component.type = 'taxlots' + expect(component.getFieldName('custom_id_1')).toBe('Custom ID 1') + }) + + it('edits rules for the current tab after switching tabs', async () => { + const { component, open } = await setup() + component.type = 'properties' + component.editRule({ id: 1, field: 'custom_id_1' } as Rule) + const [modal, config] = open.calls.mostRecent().args as [unknown, { data: { tableName: string } }] + expect(modal).toBe(FormModalComponent) + expect(config.data.tableName).toBe('PropertyState') + }) +}) diff --git a/src/app/modules/organizations/data-quality/inventory/inventory-table.component.ts b/src/app/modules/organizations/data-quality/inventory/inventory-table.component.ts index bc621cc5..1d82d845 100644 --- a/src/app/modules/organizations/data-quality/inventory/inventory-table.component.ts +++ b/src/app/modules/organizations/data-quality/inventory/inventory-table.component.ts @@ -6,7 +6,6 @@ import type { MatDialogRef } from '@angular/material/dialog' import { MatDialog } from '@angular/material/dialog' import { MatPaginator } from '@angular/material/paginator' import { MatTableDataSource } from '@angular/material/table' -import { ActivatedRoute } from '@angular/router' import { combineLatest, filter, Subject, switchMap, takeUntil, tap } from 'rxjs' import type { Rule } from '@seed/api' import { ColumnService, DataQualityService, LabelService, OrganizationService } from '@seed/api' @@ -24,9 +23,10 @@ import { FormModalComponent } from './modal/form-modal.component' }) export class DataQualityInventoryTableComponent implements AfterViewInit, OnChanges, OnDestroy, OnInit { @Input() currentRules: Rule[] + // Passed from the parent: the route component is reused across tabs, so a route snapshot here goes stale + @Input() type: InventoryTypeGoal @Output() getRules = new EventEmitter() @ViewChild(MatPaginator) paginator!: MatPaginator - private _route = inject(ActivatedRoute) private _organizationService = inject(OrganizationService) private _dataQualityService = inject(DataQualityService) private _columnService = inject(ColumnService) @@ -37,7 +37,6 @@ export class DataQualityInventoryTableComponent implements AfterViewInit, OnChan private _orgId: number rulesDataSource = new MatTableDataSource([]) rulesColumns = ['enabled', 'field', 'severity', 'criteria', 'label', 'actions'] - type = this._route.snapshot.paramMap.get('type') as InventoryTypeGoal propertyColumnsLookup: Record = {} taxlotColumnsLookup: Record = {} isOverflowing = false @@ -69,10 +68,11 @@ export class DataQualityInventoryTableComponent implements AfterViewInit, OnChan .pipe(takeUntil(this._unsubscribeAll$)) .subscribe(([organization, propertyColumns, taxLotColumns, labels]) => { this._orgId = organization.id - for (const col of propertyColumns) { + // The columns endpoint also returns related columns; skip them so they don't overwrite same-named fields + for (const col of propertyColumns.filter((c) => c.table_name === 'PropertyState')) { this.propertyColumnsLookup[col.column_name] = col.display_name } - for (const col of taxLotColumns) { + for (const col of taxLotColumns.filter((c) => c.table_name === 'TaxLotState')) { this.taxlotColumnsLookup[col.column_name] = col.display_name } for (const label of labels) { diff --git a/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.spec.ts b/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.spec.ts new file mode 100644 index 00000000..df78ec24 --- /dev/null +++ b/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.spec.ts @@ -0,0 +1,60 @@ +import { TestBed } from '@angular/core/testing' +import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog' +import { of } from 'rxjs' +import type { Column, Rule } from '@seed/api' +import { DataQualityService, LabelService } from '@seed/api' +import { FormModalComponent } from './form-modal.component' + +const column = (overrides: Partial): Column => ({ derived_column: null, table_name: 'PropertyState', ...overrides }) as Column + +describe('FormModalComponent (data quality inventory rule)', () => { + const setup = async (rule: Rule | null = null) => { + const postRule = jasmine.createSpy('postRule').and.returnValue(of({})) + const putRule = jasmine.createSpy('putRule').and.returnValue(of({})) + const columns = [ + column({ column_name: 'site_eui', display_name: 'Site EUI' }), + column({ column_name: 'EUI Target Dif', display_name: 'EUI Target Dif', derived_column: 7 }), + column({ column_name: 'jurisdiction_tax_lot_id', display_name: 'Tax Lot ID', table_name: 'TaxLotState' }), + ] + await TestBed.configureTestingModule({ + imports: [FormModalComponent], + providers: [ + { + provide: MAT_DIALOG_DATA, + useValue: { rule, orgId: 1, columns$: of(columns), tableName: 'PropertyState', currentRules: [] }, + }, + { provide: MatDialogRef, useValue: { close: jasmine.createSpy('close') } }, + { provide: DataQualityService, useValue: { postRule, putRule } }, + { provide: LabelService, useValue: { labels$: of([]) } }, + ], + }) + .overrideComponent(FormModalComponent, { set: { template: '', imports: [] } }) + .compileComponents() + + const fixture = TestBed.createComponent(FormModalComponent) + fixture.detectChanges() + return { component: fixture.componentInstance, postRule, putRule } + } + + it('only offers columns from the rule table', async () => { + const { component } = await setup() + expect(component.columns.map((c) => c.column_name)).toEqual(['site_eui', 'EUI Target Dif']) + }) + + it('flags rules on derived columns so the backend evaluates the formula', async () => { + const { component, postRule } = await setup() + component.form.patchValue({ field: 'EUI Target Dif', condition: 'range', data_type: 0, max: 0, severity: 2, status_label: 3 }) + component.onSubmit() + const { rule } = postRule.calls.mostRecent().args[0] as { rule: Rule } + expect(rule.for_derived_column).toBeTrue() + }) + + it('clears the flag when an existing derived rule is moved to a regular column', async () => { + const existing = { id: 9, field: 'EUI Target Dif', for_derived_column: true, condition: 'range', table_name: 'PropertyState' } as Rule + const { component, putRule } = await setup(existing) + component.form.patchValue({ field: 'site_eui' }) + component.onSubmit() + const { rule } = putRule.calls.mostRecent().args[0] as { rule: Rule } + expect(rule.for_derived_column).toBeFalse() + }) +}) diff --git a/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.ts b/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.ts index 3087e87a..312e9a9f 100644 --- a/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.ts +++ b/src/app/modules/organizations/data-quality/inventory/modal/form-modal.component.ts @@ -49,7 +49,7 @@ export class FormModalComponent implements OnDestroy, OnInit { data_type: new FormControl(null, Validators.required), enabled: new FormControl(true), field: new FormControl(null, Validators.required), - for_derived_column: new FormControl(false), // unused + for_derived_column: new FormControl(false), // hidden, set from the selected column on submit id: new FormControl(null), // hidden max: new FormControl(null), min: new FormControl(null), @@ -76,7 +76,8 @@ export class FormModalComponent implements OnDestroy, OnInit { this.watchForm() combineLatest([this.data.columns$, this._labelsService.labels$]).subscribe(([columns, labels]) => { - this.columns = columns + // Only offer columns that belong to this rule's table (the columns endpoint also returns related columns) + this.columns = columns.filter((column) => column.table_name === this.data.tableName) this.labels = labels for (const label of labels) { this.labelLookup[label.id] = label @@ -130,10 +131,22 @@ export class FormModalComponent implements OnDestroy, OnInit { return this.form.errors ? (Object.values(this.form.errors) as string[]) : null } + /* + * The backend only evaluates a derived column's formula when the rule is flagged `for_derived_column`; + * otherwise it looks the field up in extra_data, finds nothing, and the rule never fires. + * Keep the saved flag when the field isn't in the column list (e.g. columns not loaded yet). + */ + isDerivedField(field: string): boolean { + const column = this.columns?.find((c) => c.column_name === field) + if (!column) return this.form.value.for_derived_column ?? false + return Boolean(column.derived_column) + } + onSubmit() { + const rule = { ...this.form.value, for_derived_column: this.isDerivedField(this.form.value.field) } as Rule const fn = this.create - ? this._dataQualityService.postRule({ rule: this.form.value as Rule, orgId: this.data.orgId }) - : this._dataQualityService.putRule({ rule: this.form.value as Rule, id: this.data.rule.id, orgId: this.data.orgId }) + ? this._dataQualityService.postRule({ rule, orgId: this.data.orgId }) + : this._dataQualityService.putRule({ rule, id: this.data.rule.id, orgId: this.data.orgId }) fn.subscribe(() => { this.close()