Skip to content

Commit ea6d1e8

Browse files
committed
deduplication of question entries
1 parent deecba1 commit ea6d1e8

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ui-services/history-ui-service/src/api/historyService.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ const mapToHistoryEntry = (payload: HistoryEntryPayload): HistoryEntry => {
102102
};
103103
};
104104

105+
function getEntryTimestamp(entry: HistoryEntry): number {
106+
return (
107+
entry.sessionEndedAt?.getTime() ??
108+
entry.updatedAt?.getTime() ??
109+
entry.createdAt?.getTime() ??
110+
0
111+
);
112+
}
113+
105114
export async function fetchHistoryEntries(
106115
options: {
107116
userId?: string;
@@ -139,7 +148,22 @@ export async function fetchHistoryEntries(
139148
}
140149

141150
const items = Array.isArray(data.items) ? data.items : [];
142-
return items.map(mapToHistoryEntry);
151+
const entries = items.map(mapToHistoryEntry);
152+
153+
const latestByQuestionId = new Map<string, HistoryEntry>();
154+
for (const entry of entries) {
155+
if (!entry.questionId) {
156+
continue;
157+
}
158+
const existing = latestByQuestionId.get(entry.questionId);
159+
if (!existing || getEntryTimestamp(entry) > getEntryTimestamp(existing)) {
160+
latestByQuestionId.set(entry.questionId, entry);
161+
}
162+
}
163+
164+
return Array.from(latestByQuestionId.values()).sort(
165+
(a, b) => getEntryTimestamp(b) - getEntryTimestamp(a),
166+
);
143167
}
144168

145169
export { HISTORY_SERVICE_BASE_URL };

0 commit comments

Comments
 (0)