This commit is contained in:
NotII
2025-05-19 17:51:06 +01:00
parent 02e09003d5
commit 308a816736
6 changed files with 36 additions and 230 deletions

View File

@@ -54,6 +54,7 @@ export default function UnifiedNotifications() {
const [loading, setLoading] = useState(true);
const [activeTab, setActiveTab] = useState<string>("all");
const audioRef = useRef<HTMLAudioElement | null>(null);
const vendorIdRef = useRef<string | null>(null);
// Total notifications count
const totalNotifications = unreadCounts.totalUnread + newOrders.length;
@@ -72,6 +73,34 @@ export default function UnifiedNotifications() {
}
};
}, []);
// Get vendor ID from JWT token
const getVendorIdFromToken = () => {
if (vendorIdRef.current) {
return vendorIdRef.current;
}
const authToken = getCookie("Authorization") || "";
if (!authToken) {
throw new Error("No auth token found");
}
const tokenParts = authToken.split(".");
if (tokenParts.length !== 3) {
throw new Error("Invalid token format");
}
const payload = JSON.parse(atob(tokenParts[1]));
const vendorId = payload.id;
if (!vendorId) {
throw new Error("Vendor ID not found in token");
}
vendorIdRef.current = vendorId;
return vendorId;
};
// Function to play notification sound
const playNotificationSound = () => {
@@ -107,8 +136,6 @@ export default function UnifiedNotifications() {
yesterday.setDate(yesterday.getDate() - 1);
const timestamp = yesterday.toISOString();
// 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 || [];
@@ -175,19 +202,8 @@ export default function UnifiedNotifications() {
const fetchUnreadCounts = async () => {
try {
// 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 vendorData = await clientFetch('/auth/me');
// Access correct property - the vendor ID is in vendor._id
const vendorId = vendorData.vendor?._id;
if (!vendorId) {
console.error("Vendor ID not found in profile response:", vendorData);
return;
}
// Get vendor ID from token
const vendorId = getVendorIdFromToken();
// Use clientFetch which will properly route through Next.js API rewrites
const response = await clientFetch(`/chats/vendor/${vendorId}/unread`);