Skip to content

Commit 729d9bf

Browse files
Fix 6.3 blog: correct AiService hrInsight() to use ApiResult<T> wrapper and map(r => r.value)
1 parent 0e89141 commit 729d9bf

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

blogs/series-6-ai-app-features/6.3-angular-ai-chat-widget.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ The AI endpoints don't fit the CRUD pattern in `BaseApiService<T>`. Create a foc
8989
import { Injectable, inject } from '@angular/core';
9090
import { HttpClient } from '@angular/common/http';
9191
import { Observable } from 'rxjs';
92+
import { map } from 'rxjs/operators';
9293
import { environment } from '../../../environments/environment';
9394

95+
interface ApiResult<T> {
96+
isSuccess: boolean;
97+
value: T;
98+
errors: string[];
99+
}
100+
94101
export interface AiChatResponse {
95102
reply: string;
96103
}
@@ -133,7 +140,9 @@ export class AiService {
133140
}
134141

135142
hrInsight(question: string): Observable<HrInsightResponse> {
136-
return this.http.post<HrInsightResponse>(`${this.apiUrl}/ai/hr-insight`, { question });
143+
return this.http.post<ApiResult<HrInsightResponse>>(`${this.apiUrl}/ai/hr-insight`, {
144+
question,
145+
}).pipe(map(r => r.value));
137146
}
138147

139148
nlEmployeeSearch(query: string): Observable<NlEmployeeFilter> {
@@ -152,6 +161,7 @@ export class AiService {
152161

153162
* **Does not extend `BaseApiService<T>`** — The AI endpoints are not CRUD endpoints. A focused service with four methods is self-documenting.
154163
* **Four methods, four endpoints**`chat`, `hrInsight`, `nlEmployeeSearch`, `semanticPositionSearch`. Each article in this series introduces one method; the full service is shown here for reference.
164+
* **`hrInsight` unwraps `ApiResult<T>`** — The `POST /ai/hr-insight` endpoint returns the standard `Result<HrInsightDto>` envelope (`{ isSuccess, value, errors }`) used by all MediatR-dispatched endpoints. The private `ApiResult<T>` interface models this shape, and `.pipe(map(r => r.value))` unwraps it so components receive a plain `HrInsightResponse`. The `chat` and `nlEmployeeSearch` endpoints return their payloads directly (no envelope), so no unwrapping is needed for those.
155165
* **`inject()` not constructor injection** — Consistent with every other service in this codebase.
156166

157167
**Add to the barrel export** in `src/app/services/api/index.ts`:

0 commit comments

Comments
 (0)