This commit is contained in:
NotII
2025-03-23 23:08:09 +00:00
parent 259898cef0
commit 64ee9b842e
4 changed files with 46 additions and 71 deletions

View File

@@ -107,8 +107,9 @@ export default function UnifiedNotifications() {
yesterday.setDate(yesterday.getDate() - 1);
const timestamp = yesterday.toISOString();
// Include timestamp filter to reduce load
const orderData = await clientFetch(`/orders?status=paid&limit=10&after=${timestamp}`);
// Use orderDate parameter instead of 'after' to avoid backend casting errors
// The error logs show that the 'after' parameter is being interpreted as 'orderId' incorrectly
const orderData = await clientFetch(`/orders?status=paid&limit=10&orderDate[gte]=${timestamp}`);
const orders: Order[] = orderData.orders || [];
// If this is the first fetch, just store the orders without notifications
@@ -174,41 +175,34 @@ export default function UnifiedNotifications() {
const fetchUnreadCounts = async () => {
try {
const authToken = getCookie("Authorization");
if (!authToken) return;
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
// Use clientFetch instead of direct API calls to leverage the API rewrite rules
// This will route through Next.js rewrites instead of calling the API directly
// Get vendor info from profile endpoint
const vendorResponse = await authAxios.get('/auth/me');
const vendorData = await clientFetch('/auth/me');
// Access correct property - the vendor ID is in vendor._id
const vendorId = vendorResponse.data.vendor?._id;
const vendorId = vendorData.vendor?._id;
if (!vendorId) {
console.error("Vendor ID not found in profile response:", vendorResponse.data);
console.error("Vendor ID not found in profile response:", vendorData);
return;
}
const response = await authAxios.get(`/chats/vendor/${vendorId}/unread`);
// Use clientFetch which will properly route through Next.js API rewrites
const response = await clientFetch(`/chats/vendor/${vendorId}/unread`);
// Check if there are new notifications and play sound if needed
if (!loading && response.data.totalUnread > previousUnreadTotal) {
if (!loading && response.totalUnread > previousUnreadTotal) {
playNotificationSound();
}
// Update chat state
setUnreadCounts(response.data);
setPreviousUnreadTotal(response.data.totalUnread);
// Update chat state - note that clientFetch already parses the JSON response
setUnreadCounts(response);
setPreviousUnreadTotal(response.totalUnread);
if (response.data.totalUnread > 0) {
const chatIds = Object.keys(response.data.chatCounts);
if (response.totalUnread > 0) {
const chatIds = Object.keys(response.chatCounts);
if (chatIds.length > 0) {
// Create a simplified metadata object with just needed info
@@ -219,9 +213,9 @@ export default function UnifiedNotifications() {
chatIds.map(async (chatId) => {
try {
// Use markAsRead=false to ensure we don't mark messages as read
const chatResponse = await authAxios.get(`/chats/${chatId}?markAsRead=false`);
const chatResponse = await clientFetch(`/chats/${chatId}?markAsRead=false`);
metadata[chatId] = {
buyerId: chatResponse.data.buyerId,
buyerId: chatResponse.buyerId,
};
} catch (error) {
console.error(`Error fetching chat ${chatId}:`, error);