Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
"CEJST",
"eeej",
"EPSG",
"euis",
"FEMP",
"falsey",
"greenbutton",
"kbtu",
"movened",
"NMEC",
"overlaycontainer",
"SRID",
"sqft",
"Syncr",
"ubids",
"unpair",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"@jsverse/transloco": "^7.5.1",
"ag-grid-angular": "^33.1.1",
"ag-grid-community": "^33.1.1",
"chart.js": "^4.5.1",
"chartjs-plugin-annotation": "^3.1.0",
"crypto-es": "^2.1.0",
"cspell": "^8.17.3",
"file-saver": "^2.0.5",
Expand Down
52 changes: 52 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions src/@seed/api/goal/goal.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { HttpErrorResponse } from '@angular/common/http'
import { HttpClient } from '@angular/common/http'
import { inject, Injectable } from '@angular/core'
import type { Observable } from 'rxjs'
import { BehaviorSubject, catchError, map, take, tap } from 'rxjs'
import { OrganizationService } from '@seed/api/organization'
import { ErrorService } from '@seed/services'
import { SnackBarService } from 'app/core/snack-bar/snack-bar.service'
import type { CycleGoal, Goal, GoalsResponse, PortfolioSummary, weightedEUIsResponse } from './goal.types'

@Injectable({ providedIn: 'root' })
export class GoalService {
private _httpClient = inject(HttpClient)
private _organizationService = inject(OrganizationService)
private _snackBar = inject(SnackBarService)
private _errorService = inject(ErrorService)
private _goals = new BehaviorSubject<Goal[]>([])
private _portfolioSummary = new BehaviorSubject<PortfolioSummary>(undefined)
orgId: number

goals$ = this._goals.asObservable()

constructor() {
this._organizationService.currentOrganization$
.pipe(
tap(({ org_id }) => {
this.get(org_id)
this.orgId = org_id
}),
)
.subscribe()
}

get(orgId: number) {
const url = `/api/v3/goals/?organization_id=${orgId}`
this._httpClient
.get<GoalsResponse>(url)
.pipe(
take(1),
map(({ goals }) => goals),
tap((goals) => {
this._goals.next(goals)
}),
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, 'Error fetching goals')
}),
)
.subscribe()
}

getPortfolioSummary(goalId: number, cycleGoalId: number, orgId: number): Observable<PortfolioSummary> {
const url = `/api/v3/goals/${goalId}/cycles/${cycleGoalId}/portfolio_summary?organization_id=${orgId}`
return this._httpClient.get<PortfolioSummary>(url).pipe(
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, `Error fetching summary: ${error.message}`)
}),
)
}

getWeightedEUIs(goalId: number, orgId: number): Observable<weightedEUIsResponse> {
const url = `/api/v3/goals/${goalId}/get_weighted_euis/?organization_id=${orgId}`
return this._httpClient.get<weightedEUIsResponse>(url).pipe(
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, `Error fetching summary: ${error.message}`)
}),
)
}

editGoal(goalId: number, editedGoal, orgId: number): Observable<Goal> {
const url = `/api/v3/goals/${goalId}/?organization_id=${orgId}`
return this._httpClient.put<Goal>(url, editedGoal).pipe(
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, `Error fetching summary: ${error.message}`)
}),
)
}

createGoal(newGoal, orgId: number): Observable<Goal> {
const url = `/api/v3/goals/?organization_id=${orgId}`
return this._httpClient.post<Goal>(url, { ...newGoal, organization: orgId }).pipe(
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, `Error fetching summary: ${error.message}`)
}),
)
}

createCycleGoal(goalId: number, cycleId: number, annual_report_id: string, annual_report_name: string): Observable<CycleGoal> {
const url = `/api/v3/goals/${goalId}/cycles/?organization_id=${this.orgId}`
return this._httpClient
.post<CycleGoal>(url, {
current_cycle: cycleId,
salesforce_annual_report_id: annual_report_id,
salesforce_annual_report_name: annual_report_name,
})
.pipe(
catchError((error: HttpErrorResponse) => {
return this._errorService.handleError(error, `Error fetching summary: ${error.message}`)
}),
)
}
}
Loading