Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 4x 26x 26x 26x 15x 15x 2x 13x 3x 3x 4x 4x 4x 1x 4x 4x 4x 4x 3x 3x 1x 1x 4x 12x 12x 12x 9x 9x 3x 3x 4x 4x 4x 4x 3x 3x 5x 3x 5x 3x 1x 2x 2x 2x 2x | import { apiClient } from '@/lib/http-client';
import type { NOTIFICATION_TYPES } from '@/constants/notification';
import {
API_ENDPOINTS,
DEFAULT_NOTIFICATION_COUNTS,
ERROR_MESSAGES,
LOG_MESSAGES,
} from '@/constants/notification';
// Notification API response types
export interface NotificationCounts {
trade: number;
transaction: number;
total: number;
lastUpdated: string;
}
interface NotificationApiResponse {
data: NotificationCounts;
}
interface NotificationItem {
id: number;
documentId: string;
type: (typeof NOTIFICATION_TYPES)[keyof typeof NOTIFICATION_TYPES];
count: number;
label: string;
description?: string;
isActive: boolean;
lastUpdated: string;
createdAt: string;
updatedAt: string;
publishedAt: string;
locale: string;
}
interface NotificationListResponse {
data: NotificationItem[];
}
// Fetch notification badge counts from backend API (public access)
export const fetchNotificationCounts =
async (): Promise<NotificationCounts> => {
try {
console.log(LOG_MESSAGES.FETCHING_COUNTS);
const result: NotificationApiResponse = await apiClient.get(
API_ENDPOINTS.BADGE_COUNTS
);
console.log(LOG_MESSAGES.COUNTS_RESPONSE, result);
// Handle undefined or null response data
if (!result.data) {
return {
...DEFAULT_NOTIFICATION_COUNTS,
lastUpdated: new Date().toISOString(),
};
}
return result.data;
} catch (error) {
console.error(ERROR_MESSAGES.FETCH_COUNTS, error);
// Re-throw the error so it can be caught by hooks
throw error;
}
};
// Fetch notification badge counts with fallback for direct use
export const fetchNotificationCountsWithFallback =
async (): Promise<NotificationCounts> => {
try {
return await fetchNotificationCounts();
} catch (error) {
// Return default values on error for direct use
return {
...DEFAULT_NOTIFICATION_COUNTS,
lastUpdated: new Date().toISOString(),
};
}
};
// Fetch all notifications from backend API (public access)
export const fetchNotifications = async (): Promise<NotificationItem[]> => {
try {
console.log(LOG_MESSAGES.FETCHING_NOTIFICATIONS);
const result: NotificationListResponse = await apiClient.get(
API_ENDPOINTS.NOTIFICATIONS
);
console.log(LOG_MESSAGES.NOTIFICATIONS_RESPONSE, result);
return result.data;
} catch (error) {
console.error(ERROR_MESSAGES.FETCH_NOTIFICATIONS, error);
return [];
}
};
// Update notification count (requires authentication in production)
export const updateNotificationCount = async (
id: number,
count: number
): Promise<NotificationItem | null> => {
try {
console.log(LOG_MESSAGES.UPDATING_NOTIFICATION(id, count));
const result = await apiClient.put(`${API_ENDPOINTS.NOTIFICATIONS}/${id}`, {
data: {
count,
lastUpdated: new Date().toISOString(),
},
});
console.log(LOG_MESSAGES.UPDATE_RESPONSE, result);
return result.data;
} catch (error) {
console.error(ERROR_MESSAGES.UPDATE_NOTIFICATION, error);
return null;
}
};
// Reset all notification counts to 0
export const resetNotificationCounts = async (): Promise<boolean> => {
try {
console.log(LOG_MESSAGES.RESETTING_COUNTS);
// First get all notifications using direct API call to catch errors
const result: NotificationListResponse = await apiClient.get(
API_ENDPOINTS.NOTIFICATIONS
);
const notifications = result.data;
// Update each notification to count 0
const updatePromises = notifications.map(notification =>
updateNotificationCount(notification.id, 0)
);
const results = await Promise.all(updatePromises);
// Check if all updates were successful (no null results)
const allSuccessful = results.every(result => result !== null);
if (!allSuccessful) {
throw new Error(ERROR_MESSAGES.RESET_FAILED);
}
console.log(LOG_MESSAGES.RESET_SUCCESS);
return true;
} catch (error) {
console.error(ERROR_MESSAGES.RESET_COUNTS, error);
return false;
}
};
|