Skip to content

Commit

Permalink
Merge pull request #277 from NREL/issue-163-258
Browse files Browse the repository at this point in the history
Issue 163 258 270
  • Loading branch information
rmroot authored Oct 10, 2024
2 parents 62bb602 + feb5d64 commit 4e4be1d
Show file tree
Hide file tree
Showing 28 changed files with 433 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { UserIdbService } from 'src/app/indexed-db/user-idb.service';
import { PreAssessmentSetupService } from 'src/app/setup-wizard/pre-visit/pre-assessment-setup/pre-assessment-setup.service';
import { OnSiteVisitIdbService } from 'src/app/indexed-db/on-site-visit-idb.service';
import { AssessmentIdbService } from 'src/app/indexed-db/assessment-idb.service';
import { EnergyEquipmentIdbService } from 'src/app/indexed-db/energy-equipment-idb.service';

describe('FacilitySettingsComponent', () => {
let component: FacilitySettingsComponent;
Expand All @@ -31,6 +32,7 @@ describe('FacilitySettingsComponent', () => {
let onsiteVisitIdbService: Partial<OnSiteVisitIdbService> = {};
let preAassessmentSetupService: Partial<PreAssessmentSetupService> = {};
let assessmentIdbService: Partial<AssessmentIdbService> = {};
let energyEquipmentIdbService: Partial<EnergyEquipmentIdbService> = {};

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -44,7 +46,8 @@ describe('FacilitySettingsComponent', () => {
{ provide: UserIdbService, useValue: userIdbService},
{ provide: OnSiteVisitIdbService, useValue: onsiteVisitIdbService},
{ provide: PreAssessmentSetupService, useValue: preAassessmentSetupService },
{ provide: AssessmentIdbService, useValue: assessmentIdbService }
{ provide: AssessmentIdbService, useValue: assessmentIdbService },
{ provide: EnergyEquipmentIdbService, useValue: energyEquipmentIdbService }
]
})
.compileComponents();
Expand Down
12 changes: 12 additions & 0 deletions src/app/indexed-db/energy-equipment-idb.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs';
import { IdbEnergyEquipment } from '../models/energyEquipment';
import { NgxIndexedDBService } from 'ngx-indexed-db';
import { guidType } from '../shared/constants/guidTypes';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -57,4 +58,15 @@ export class EnergyEquipmentIdbService {
let energyEquipments: Array<IdbEnergyEquipment> = this.energyEquipments.getValue();
return energyEquipments.find(_energyEquipment => { return _energyEquipment.guid == guid });
}

getByOtherGuid(guid: string, idType: guidType): Array<IdbEnergyEquipment> {
let energyEquipments: Array<IdbEnergyEquipment> = this.energyEquipments.getValue();
if (idType == 'company') {
return energyEquipments.filter(_energyEquipment => { return _energyEquipment.companyId == guid });
} else if (idType == 'facility') {
return energyEquipments.filter(_energyEquipment => { return _energyEquipment.facilityId == guid });
} else {
return [];
}
}
}
4 changes: 4 additions & 0 deletions src/app/indexed-db/energy-opportunity-idb.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class EnergyOpportunityIdbService {
return energyOpportunities.filter(opportunity => {
return opportunity.facilityId == guid;
});
} else if (idType == 'assessment') {
return energyOpportunities.filter(opportunity => {
return opportunity.assessmentId == guid;
});
} else {
return [];
}
Expand Down
17 changes: 14 additions & 3 deletions src/app/models/energyEquipment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getNewIdbEntry, IdbEntry } from "./idbEntry"
import { EquipmentType, EquipmentTypes } from "../shared/constants/equipmentTypes"
import { UtilityType } from "../shared/constants/utilityTypes"
import { UtilityOptions, UtilityType } from "../shared/constants/utilityTypes"
import { UnitSettings } from "./unitSettings";

export interface IdbEnergyEquipment extends IdbEntry {
userId: string,
Expand All @@ -22,10 +23,18 @@ export interface IdbEnergyEquipment extends IdbEntry {
efficiency: number,
numberOfEquipment: number,
annualEnergyUse: number,
facilityUtilityUnit: string;
annualEnergyUseByUtility: number;
}

export function getNewIdbEnergyEquipment(userId: string, companyId: string, facilityId: string): IdbEnergyEquipment {
export function getNewIdbEnergyEquipment(userId: string, companyId: string, facilityId: string,
facilityUnitSettings: UnitSettings
): IdbEnergyEquipment {
let idbEntry: IdbEntry = getNewIdbEntry();
let facilityUtilityUnit: string = 'kWh';
if (facilityUnitSettings['includeElectricity']) {
facilityUtilityUnit = facilityUnitSettings['electricityUnit'];
}
return {
...idbEntry,
userId: userId,
Expand All @@ -46,6 +55,8 @@ export function getNewIdbEnergyEquipment(userId: string, companyId: string, faci
efficiency: 0.5,
numberOfEquipment: 1,
notes: undefined,
annualEnergyUse: 0
annualEnergyUse: 0,
facilityUtilityUnit: facilityUtilityUnit,
annualEnergyUseByUtility: 0
}
}
22 changes: 19 additions & 3 deletions src/app/models/energyOpportunity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IdbEntry, getNewIdbEntry } from "./idbEntry";
import { UtilityOptions, UtilityType } from "../shared/constants/utilityTypes";
import { UtilityEnergyUse } from "./utilityEnergyUses";

export interface IdbEnergyOpportunity extends IdbEntry {
name: string,
Expand All @@ -8,15 +10,27 @@ export interface IdbEnergyOpportunity extends IdbEntry {
assessmentId: string,
energySavings: number,
implementationCost: number,
opportunityType: string,
utilityType: UtilityType,
utilityTypes: Array<UtilityType>,
energyUnit: string,
costSavings: number,
notes: string,
includeSavings: boolean,
includeNote: boolean
}

export function getNewIdbEnergyOpportunity(userId: string, companyId: string, facilityId: string, assessmentId: string): IdbEnergyOpportunity {
export function getNewIdbEnergyOpportunity(userId: string, companyId: string, facilityId: string,
assessmentId: string, utilityEnergyUses: Array<UtilityEnergyUse>): IdbEnergyOpportunity {
let idbEntry: IdbEntry = getNewIdbEntry();
let utilityTypes: Array<UtilityType> = utilityEnergyUses
.filter(use => use.include)
.map(use => use.utilityType);
let utilityType: UtilityType = utilityTypes?.[0];
let energyUnit: string = 'MMBtu';
if (utilityType) {
let energyUse = utilityEnergyUses.find(use => use.utilityType === utilityType);
energyUnit = energyUse.energyUnit;
}
return {
...idbEntry,
name: 'New Opportunity',
Expand All @@ -26,7 +40,9 @@ export function getNewIdbEnergyOpportunity(userId: string, companyId: string, fa
assessmentId: assessmentId,
energySavings: undefined,
implementationCost: undefined,
opportunityType: undefined,
utilityType: utilityType,
utilityTypes: utilityTypes,
energyUnit: energyUnit,
costSavings: undefined,
notes: undefined,
includeSavings: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('AddNebsModalComponent', () => {

};
let energyOpportunityIdbService: Partial<EnergyOpportunityIdbService> = {
getByGuid: () => { return getNewIdbEnergyOpportunity('', '', '', '') }
getByGuid: () => { return getNewIdbEnergyOpportunity('', '', '', '', null) }

};
let keyPerformanceMetricImpactsIdbService: Partial<KeyPerformanceMetricImpactsIdbService> = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { IdbEnergyEquipment } from 'src/app/models/energyEquipment';
import { getDefaultUnitSettings } from 'src/app/models/unitSettings';
import { CompanyIdbService } from 'src/app/indexed-db/company-idb.service';
import { IdbCompany } from 'src/app/models/company';
import { EnergyOpportunityIdbService } from 'src/app/indexed-db/energy-opportunity-idb.service';

describe('AssessmentDetailsFormComponent', () => {
let component: AssessmentDetailsFormComponent;
Expand Down Expand Up @@ -53,6 +54,8 @@ describe('AssessmentDetailsFormComponent', () => {
let companyIdbService: Partial<CompanyIdbService> = {
selectedCompany: new BehaviorSubject<IdbCompany>(null)
};

let energyOpportunityIdbService: Partial<EnergyOpportunityIdbService> = {};
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FontAwesomeModule, FormsModule, RouterTestingModule, HelperPipesModule],
Expand All @@ -64,6 +67,7 @@ describe('AssessmentDetailsFormComponent', () => {
{ provide: ContactIdbService, useValue: contactIdbService },
{ provide: EnergyEquipmentIdbService, useValue: energyEquipmentIdbService },
{ provide: CompanyIdbService, useValue: companyIdbService },
{ provide: EnergyOpportunityIdbService, useValue: energyOpportunityIdbService }
]
})
.compileComponents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { UtilityEnergyUse } from 'src/app/models/utilityEnergyUses';
import { UnitSettings } from 'src/app/models/unitSettings';
import { FacilityIdbService } from 'src/app/indexed-db/facility-idb.service';
import { ConvertValue } from 'src/app/shared/conversions/convertValue';
import { IdbEnergyOpportunity } from 'src/app/models/energyOpportunity';
import { EnergyOpportunityIdbService } from 'src/app/indexed-db/energy-opportunity-idb.service';
import { AssessmentEnergyOpportunitiesFormService } from '../assessment-energy-opportunities-form/assessment-energy-opportunities-form.service';

@Component({
selector: 'app-assessment-details-form',
Expand Down Expand Up @@ -48,13 +51,18 @@ export class AssessmentDetailsFormComponent {

convertValue = new ConvertValue();

assessmentEnergyOpportunities: Array<IdbEnergyOpportunity>;

constructor(
private assessmentIdbService: AssessmentIdbService,
private contactIdbService: ContactIdbService,
private setupWizardService: SetupWizardService,
private energyEquipmentIdbService: EnergyEquipmentIdbService,
private companyIdbService: CompanyIdbService,
private facilityIdbService: FacilityIdbService,
private energyOpportunityIdbService: EnergyOpportunityIdbService,
private assessmentEnergyOpportunitiesFormService: AssessmentEnergyOpportunitiesFormService,

) { }

ngOnInit() {
Expand Down Expand Up @@ -98,7 +106,15 @@ export class AssessmentDetailsFormComponent {
await this.calculateEnergyUseCost();
}

updateEnergyOpportunities() {
this.assessmentEnergyOpportunities = this.energyOpportunityIdbService.getByOtherGuid(
this.assessment.guid, 'assessment');
this.assessmentEnergyOpportunitiesFormService.updateEnergyOpportunityFromAssessment(
this.assessmentEnergyOpportunities, this.assessment.utilityEnergyUses);
}

async calculateEnergyUseCost() {
this.updateEnergyOpportunities();
let use = 0, cost = 0;
this.assessment.utilityTypes.forEach(utilityType => {
let utilityEnergyUse: UtilityEnergyUse = this.assessment.utilityEnergyUses.find(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export class AssessmentEnergyOpportunitiesFormComponent {

async addEnergyOpportunity() {
this.isAddNew = true;
let newOpportunity: IdbEnergyOpportunity = getNewIdbEnergyOpportunity(this.assessment.userId, this.assessment.companyId, this.assessment.facilityId, this.assessment.guid);
let newOpportunity: IdbEnergyOpportunity = getNewIdbEnergyOpportunity(this.assessment.userId, this.assessment.companyId,
this.assessment.facilityId, this.assessment.guid, this.assessment.utilityEnergyUses);
let assessmentEnergyOpportunities: Array<IdbEnergyOpportunity> = this.energyOpportunities.filter(prj => {
return prj.assessmentId == this.assessment.guid;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Injectable } from '@angular/core';
import { EnergyOpportunityIdbService } from 'src/app/indexed-db/energy-opportunity-idb.service';
import { IdbEnergyOpportunity } from 'src/app/models/energyOpportunity';
import { UnitSettings } from 'src/app/models/unitSettings';
import { UtilityEnergyUse } from 'src/app/models/utilityEnergyUses';
import { UtilityType } from 'src/app/shared/constants/utilityTypes';
import { ConvertValue } from 'src/app/shared/conversions/convertValue';

@Injectable({
Expand All @@ -12,7 +15,8 @@ export class AssessmentEnergyOpportunitiesFormService {

constructor(private energyOpportunityIdbService: EnergyOpportunityIdbService) { }

async updateEnergyOpportunityEnergyUse(energyOpportunities: Array<IdbEnergyOpportunity>, companyEnergyUnit: string) {
async updateEnergyOpportunityEnergyUseFromCompany(energyOpportunities: Array<IdbEnergyOpportunity>,
companyEnergyUnit: string) {
for (const energyOpportunity of energyOpportunities) {
// Update energy opportunity energy savings
if (companyEnergyUnit === 'MMBtu') {
Expand All @@ -29,4 +33,33 @@ export class AssessmentEnergyOpportunitiesFormService {
await this.energyOpportunityIdbService.asyncUpdate(energyOpportunity);
}
}

async updateEnergyOpportunityFromAssessment(energyOpportunities: Array<IdbEnergyOpportunity>,
utilityEnergyUses: Array<UtilityEnergyUse>) {
for (const energyOpportunity of energyOpportunities) {
let utilityTypes: Array<UtilityType> = utilityEnergyUses
.filter(use => use.include)
.map(use => use.utilityType);
energyOpportunity.utilityTypes = utilityTypes;

let utilityType: UtilityType;
let energyUnit: string = 'MMBtu';

if (!utilityTypes.includes(energyOpportunity.utilityType)) {
utilityType = utilityTypes?.[0];
} else {
utilityType = energyOpportunity.utilityType;
}

if (utilityType) {
let energyUse = utilityEnergyUses.find(use => use.utilityType === utilityType);
energyUnit = energyUse.energyUnit;
}

energyOpportunity.utilityTypes = utilityTypes;
energyOpportunity.utilityType = utilityType;
energyOpportunity.energyUnit = energyUnit;
await this.energyOpportunityIdbService.asyncUpdate(energyOpportunity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,14 @@ <h6>
</div>
</div>
<div class="row">
<label class="col-sm-5 col-form-label" for="{{'opportunityType_'+energyOpportunity.guid}}">Opportunity
<label class="col-sm-5 col-form-label" for="{{'utilityType_'+energyOpportunity.guid}}">Utility
Type</label>
<div class="col-sm-7">
<select name="{{'opportunityType_'+energyOpportunity.guid}}" class="form-select"
id="{{'opportunityType_'+energyOpportunity.guid}}" [(ngModel)]="energyOpportunity.opportunityType"
(change)="saveEnergyOpportunity()">
<option [ngValue]="undefined">
Other
</option>
<option *ngFor="let opportunityType of opportunityTypes" [ngValue]="opportunityType.value">
{{opportunityType.label}}
<select name="{{'utilityType_'+energyOpportunity.guid}}" class="form-select"
id="{{'utilityType_'+energyOpportunity.guid}}" [(ngModel)]="energyOpportunity.utilityType"
(change)="changeUtilityType()">
<option *ngFor="let utilityType of energyOpportunity.utilityTypes" [ngValue]="utilityType">
{{utilityType}}
</option>
</select>
</div>
Expand Down Expand Up @@ -76,7 +73,7 @@ <h6>
<input name="{{'energySavings_'+energyOpportunity.guid}}" type="number" class="form-control"
[(ngModel)]="energyOpportunity.energySavings" id="{{'energySavings_'+energyOpportunity.guid}}"
(input)="saveEnergyOpportunity()">
<span class="input-group-text" [innerHTML]="(companyEnergyUnit | unitsDisplay) + '/yr'"></span>
<span class="input-group-text" [innerHTML]="(energyOpportunity.energyUnit| unitsDisplay) + '/yr'"></span>
</div>
</div>
</div>
Expand All @@ -91,6 +88,13 @@ <h6>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<button class="btn btn-outline-primary btn-sm border-0" (click)="calculateCostSavings()">
<fa-icon [icon]="faCalculator"></fa-icon> Calculate Savings
</button>
</div>
</div>
</ng-container>
<div class="row">
<div class="col-sm-12 col-form-label">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import { ChangeDetectorRef } from '@angular/core';
import { BootstrapService } from 'src/app/shared/shared-services/bootstrap.service';
import { CompanyIdbService } from 'src/app/indexed-db/company-idb.service';
import { IdbCompany } from 'src/app/models/company';
import { AssessmentIdbService } from 'src/app/indexed-db/assessment-idb.service';
import { FacilityIdbService } from 'src/app/indexed-db/facility-idb.service';
import { IdbAssessment } from 'src/app/models/assessment';
import { IdbFacility } from 'src/app/models/facility';

describe('EnergyOpportunitySetupFormComponent', () => {
let component: EnergyOpportunitySetupFormComponent;
Expand All @@ -28,7 +32,7 @@ describe('EnergyOpportunitySetupFormComponent', () => {
};
let energyOpportunityIdbService: Partial<EnergyOpportunityIdbService> = {
energyOpportunities: new BehaviorSubject<Array<IdbEnergyOpportunity>>([]),
getByGuid: () => { return getNewIdbEnergyOpportunity('', '', '', '') }
getByGuid: () => { return getNewIdbEnergyOpportunity('', '', '', '', []) }
};
let nonEnergyBenefitsIdbService: Partial<NonEnergyBenefitsIdbService> = {
nonEnergyBenefits: new BehaviorSubject<Array<IdbNonEnergyBenefit>>([])
Expand All @@ -40,6 +44,12 @@ describe('EnergyOpportunitySetupFormComponent', () => {
let companyIdbService: Partial<CompanyIdbService> = {
selectedCompany: new BehaviorSubject<IdbCompany>(null)
};
let assessmentIdbService: Partial<AssessmentIdbService> = {
selectedAssessment: new BehaviorSubject<IdbAssessment>(null)
};
let facilityIdbService: Partial<FacilityIdbService> = {
selectedFacility: new BehaviorSubject<IdbFacility>(null)
};
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FontAwesomeModule, RouterTestingModule, FormsModule],
Expand All @@ -52,7 +62,9 @@ describe('EnergyOpportunitySetupFormComponent', () => {
{ provide: LocalStorageDataService, useValue: localStorageDataService },
{ provide: ChangeDetectorRef, useValue: cd },
{ provide: BootstrapService, useValue: bootstrapService },
{ provide: CompanyIdbService, useValue: companyIdbService }
{ provide: CompanyIdbService, useValue: companyIdbService },
{ provide: AssessmentIdbService, useValue: assessmentIdbService },
{ provide: FacilityIdbService, useValue: facilityIdbService }
]

})
Expand Down
Loading

0 comments on commit 4e4be1d

Please sign in to comment.