fix(chat): handle user object from API responses

- Updated sendMessage.fulfilled to extract user.name from nested user object
- Updated fetchChatHistory.fulfilled to transform API format to internal format
- API response: { user: { name: "..." }, user_id: "..." }
- Internal format: { senderName: "...", senderId: "..." }
- WebSocket messages already use user_name (unchanged)

Fixes 'Unknown' sender name for messages from API (send + fetch).
This commit is contained in:
Nuwan 2026-01-31 20:12:14 +05:30
parent 2b593cce93
commit 6dc6c431ed
1 changed files with 15 additions and 3 deletions

View File

@ -229,9 +229,21 @@ const sessionChatSlice = createSlice({
state.messagesByChannel[channel] = [];
}
// Transform API response format to internal format
// API returns: { user: { name: "..." }, user_id: "...", ... }
// Internal format: { senderName: "...", senderId: "...", ... }
const transformedMessages = messages.map(m => ({
id: m.id,
senderId: m.user_id,
senderName: m.user?.name || 'Unknown',
message: m.message,
createdAt: m.created_at,
channel: m.channel
}));
// Deduplicate messages
const existingIds = new Set(state.messagesByChannel[channel].map(m => m.id));
const newMessages = messages.filter(m => !existingIds.has(m.id));
const newMessages = transformedMessages.filter(m => !existingIds.has(m.id));
// Prepend new messages (oldest first for pagination)
state.messagesByChannel[channel] = [...newMessages, ...state.messagesByChannel[channel]];
@ -285,8 +297,8 @@ const sessionChatSlice = createSlice({
if (index !== -1) {
messages[index] = {
id: realMessage.id,
senderId: realMessage.sender_id,
senderName: realMessage.sender_name,
senderId: realMessage.user_id,
senderName: realMessage.user?.name || 'Unknown',
message: realMessage.message,
createdAt: realMessage.created_at,
channel: realMessage.channel