Skip to content

Commit 909980b

Browse files
authored
Merge pull request #71 from solutionchallenge/fix/#70/fix-chat-flow
Fix/#70/fix-chat-flow
2 parents bd2e69b + 7a45f3a commit 909980b

File tree

22 files changed

+1091
-164
lines changed

22 files changed

+1091
-164
lines changed

src/api/auth/login.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ interface OAuthResponse {
66
refresh_token: string;
77
}
88

9+
interface RefreshReponse {
10+
access_token: string;
11+
}
12+
913
export const startGoogleLogin = (redirectUri: string) => {
1014
return `${import.meta.env.VITE_API_BASE_URL}/oauth/google/start?redirect=${encodeURIComponent(redirectUri)}`;
1115
};
@@ -18,11 +22,11 @@ export const exchangeGoogleCode = async (code: string, redirectUri: string) => {
1822
return response.response;
1923
};
2024

21-
export const refreshToken = async (refresh_token: string) => {
22-
const response = await http.post<OAuthResponse>("/auth/refresh", {
25+
export const refreshToken = async (refresh_token: string): Promise<string> => {
26+
const response = await http.post<RefreshReponse>("/auth/refresh", {
2327
refresh_token,
2428
});
25-
return response.response;
29+
return response.response.access_token;
2630
};
2731

2832
export const getUserInfo = async (): Promise<User> => {

src/api/chat/index.ts

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
import { http } from "../fetch";
2-
3-
export interface ChatEmotion {
4-
emotion: string;
5-
rate: number;
6-
}
7-
8-
export interface ChatSummary {
9-
emotions: ChatEmotion[];
10-
keywords: string[];
11-
recommendations: string[];
12-
text: string;
13-
title: string;
14-
}
2+
import { EmotionTypes } from "../onboarding/addition";
153

164
export interface Chat {
175
chat_duration: string;
@@ -25,6 +13,28 @@ export interface Chat {
2513
user_timezone: string;
2614
}
2715

16+
export interface ChatSummary {
17+
emotions: {
18+
emotion: EmotionTypes;
19+
rate: 0;
20+
}[];
21+
keywords: string[];
22+
recommendations: string[];
23+
text: string;
24+
title: string;
25+
main_topic: {
26+
begin_message_id: string;
27+
end_message_id: string;
28+
};
29+
topic_messages: History[];
30+
}
31+
32+
export interface ChatSummaryReponse {
33+
success: boolean;
34+
created: boolean;
35+
returning: ChatSummary;
36+
}
37+
2838
export interface ListChatsResponse {
2939
chats: Chat[];
3040
}
@@ -34,11 +44,17 @@ export const listChats = async () => {
3444
return response;
3545
};
3646

37-
export const getChatSummary = async (
47+
export const putChatSummary = async (
3848
session_id: string
3949
): Promise<ChatSummary> => {
40-
const { response } = await http.get<Chat>(`/chats/${session_id}`);
41-
return response.summary;
50+
const { response } = await http.put<ChatSummaryReponse>(
51+
`/chats/${session_id}/summary`
52+
);
53+
if (response.success) {
54+
return response.returning;
55+
} else {
56+
throw new Error("Failed to fetch chat summary.");
57+
}
4258
};
4359

4460
export const archiveChat = async (session_id: string): Promise<void> => {

src/api/fetch.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { refreshToken } from "./auth/login";
2+
13
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL;
24

3-
const baseFetch = async (url: RequestInfo, init?: RequestInit) => {
5+
const baseFetch = async (
6+
url: RequestInfo,
7+
init?: RequestInit,
8+
retry: boolean = true
9+
) => {
410
const accessToken = localStorage.getItem("access_token");
511

612
const res = await fetch(`${API_BASE_URL}${url}`, {
@@ -12,9 +18,30 @@ const baseFetch = async (url: RequestInfo, init?: RequestInit) => {
1218
"Content-Type": "application/json",
1319
},
1420
});
21+
1522
if (!res.ok) {
23+
if (res.status === 401 && retry) {
24+
try {
25+
const refresh_token = localStorage.getItem("refresh_token");
26+
if (refresh_token) {
27+
const newAccessToken = await refreshToken(refresh_token);
28+
if (newAccessToken) {
29+
localStorage.setItem("access_token", newAccessToken);
30+
return baseFetch(url, init, false);
31+
}
32+
}
33+
} catch (e) {
34+
console.error("Refresh token failed", e);
35+
}
36+
37+
localStorage.removeItem("access_token");
38+
localStorage.removeItem("refresh_token");
39+
window.location.href = "/login";
40+
return Promise.reject(new Error("Unauthorized, redirected to login"));
41+
}
1642
throw new Error(`HTTP error! status: ${res.status}`);
1743
}
44+
1845
const json = await res.json();
1946
return { response: json };
2047
};

src/api/report/chats.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export interface ChatResponse {
2626
started_date: string;
2727
summary: SummaryResponse;
2828
}
29-
[];
3029

3130
type History = {
3231
content: string;

src/api/test/diagnosis.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ interface DiagnosisResultRequest {
6161

6262
export const postDiagnosisResult = async (
6363
result: DiagnosisResultRequest
64-
): Promise<{ id: number; success: boolean }> => {
65-
const { response } = await http.post<{ id: number; success: boolean }>(
64+
): Promise<{ id: string; success: boolean }> => {
65+
const { response } = await http.post<{ id: string; success: boolean }>(
6666
"/diagnoses",
6767
result
6868
);
@@ -79,10 +79,10 @@ interface DiagnosisResultResponse {
7979
}
8080

8181
export const getDiagnosisResult = async (
82-
diagnosis_id: number
82+
diagnosis_id: string
8383
): Promise<DiagnosisResultResponse> => {
8484
const { response } = await http.get<DiagnosisResultResponse>(
85-
`/diagnosis-papers/${diagnosis_id}`
85+
`/diagnoses/${diagnosis_id}`
8686
);
8787
return response;
8888
};

0 commit comments

Comments
 (0)