1import { apiFetch } from '../lib/api';
4 * Universal notification function
5 * Sends notifications to backend which:
7 * - Appears in header notification bell
8 * - Sends to Slack if configured
10 * @param {Object} params
11 * @param {string} params.title - Notification title
12 * @param {string} params.message - Notification message
13 * @param {string} params.type - Type: 'info', 'success', 'warning', 'error'
14 * @param {number} [params.user_id] - Optional specific user ID
15 * @param {number} [params.customer_id] - Optional customer ID
16 * @returns {Promise<boolean>} Success status
18export async function notify({ title, message, type = 'info', user_id = null, customer_id = null }) {
20 const token = localStorage.getItem('token');
22 console.warn('No auth token for notification');
26 const res = await apiFetch('/notifications', {
29 'Content-Type': 'application/json',
30 Authorization: `Bearer ${token}`
32 body: JSON.stringify({
42 console.error('Failed to send notification:', await res.text());
46 // Dispatch event to refresh notification badge in header
47 window.dispatchEvent(new CustomEvent('notificationCreated'));
51 console.error('Error sending notification:', err);
57 * Quick notification helpers for common scenarios
59export const notifySuccess = (title, message) => notify({ title, message, type: 'success' });
60export const notifyError = (title, message) => notify({ title, message, type: 'error' });
61export const notifyWarning = (title, message) => notify({ title, message, type: 'warning' });
62export const notifyInfo = (title, message) => notify({ title, message, type: 'info' });
65 * Notification helper for action success
67export const notifyActionSuccess = (action, resource) =>
68 notifySuccess(`${action} Successful`, `${resource} has been ${action.toLowerCase()} successfully`);
71 * Notification helper for action errors
73export const notifyActionError = (action, resource, error) =>
74 notifyError(`${action} Failed`, `Failed to ${action.toLowerCase()} ${resource}: ${error}`);
77 * Notification helper for permission denied
79export const notifyPermissionDenied = (action, resource) =>
80 notifyWarning('Permission Denied', `You do not have permission to ${action.toLowerCase()} ${resource}`);