feat: Restructure notifications - Staff and Customer separation (WIP)

## 🎯 Phase 1: Backend + Frontend Structure

### Backend Changes

**NotificationsController.php:**
-  Added `/notifications/staff/events` endpoint
-  Added `/notifications/customer/events` endpoint
-  Created `get_all_events()` helper method
-  Added `recipient_type` field to all events
-  Filter events by recipient (staff vs customer)

### Frontend Changes

**Main Notifications Page:**
-  Restructured to show cards for Staff, Customer, Activity Log
-  Entry point with clear separation
-  Modern card-based UI

**Staff Notifications:**
-  Created `/settings/notifications/staff` route
-  Moved Channels.tsx → Staff/Channels.tsx
-  Moved Events.tsx → Staff/Events.tsx
-  Updated Staff/Events to use `/notifications/staff/events`
-  Fixed import paths

### Structure

```
Settings → Notifications
├── Staff Notifications (admin alerts)
│   ├── Channels (Email, Push)
│   ├── Events (Orders, Products, Customers)
│   └── Templates
└── Customer Notifications (customer emails)
    ├── Channels (Email, Push, SMS)
    ├── Events (Orders, Shipping, Account)
    └── Templates
```

---

**Next:** Customer notifications page + routes
This commit is contained in:
dwindown
2025-11-11 19:00:52 +07:00
parent 90407dcfc8
commit 7c0605d379
5 changed files with 1001 additions and 25 deletions

View File

@@ -147,6 +147,24 @@ class NotificationsController {
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/notifications/staff/events
register_rest_route($this->namespace, '/' . $this->rest_base . '/staff/events', [
[
'methods' => 'GET',
'callback' => [$this, 'get_staff_events'],
'permission_callback' => [$this, 'check_permission'],
],
]);
// GET /woonoow/v1/notifications/customer/events
register_rest_route($this->namespace, '/' . $this->rest_base . '/customer/events', [
[
'methods' => 'GET',
'callback' => [$this, 'get_customer_events'],
'permission_callback' => [$this, 'check_permission'],
],
]);
}
/**
@@ -290,6 +308,169 @@ class NotificationsController {
return new WP_REST_Response($events, 200);
}
/**
* Get staff notification events (admin/staff recipient)
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_staff_events(WP_REST_Request $request) {
$all_events = $this->get_all_events();
// Filter events where default recipient is 'admin' or 'staff'
$staff_events = [];
foreach ($all_events as $category => $events) {
$filtered = array_filter($events, function($event) {
$first_channel = reset($event['channels']);
return in_array($first_channel['recipient'] ?? 'admin', ['admin', 'staff']);
});
if (!empty($filtered)) {
$staff_events[$category] = array_values($filtered);
}
}
return new WP_REST_Response($staff_events, 200);
}
/**
* Get customer notification events (customer recipient)
*
* @param WP_REST_Request $request Request object
* @return WP_REST_Response
*/
public function get_customer_events(WP_REST_Request $request) {
$all_events = $this->get_all_events();
// Filter events where default recipient is 'customer'
$customer_events = [];
foreach ($all_events as $category => $events) {
$filtered = array_filter($events, function($event) {
$first_channel = reset($event['channels']);
return ($first_channel['recipient'] ?? 'admin') === 'customer';
});
if (!empty($filtered)) {
$customer_events[$category] = array_values($filtered);
}
}
return new WP_REST_Response($customer_events, 200);
}
/**
* Get all events (internal helper)
*
* @return array
*/
private function get_all_events() {
// Get saved settings
$settings = get_option('woonoow_notification_settings', []);
// Define all events
$events = [
'orders' => [
[
'id' => 'order_placed',
'label' => __('Order Placed', 'woonoow'),
'description' => __('When a new order is placed', 'woonoow'),
'category' => 'orders',
'wc_email' => 'new_order',
'enabled' => true,
'recipient_type' => 'staff',
'channels' => $settings['order_placed']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']],
],
[
'id' => 'order_processing',
'label' => __('Order Processing', 'woonoow'),
'description' => __('When order status changes to processing', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_processing_order',
'enabled' => true,
'recipient_type' => 'customer',
'channels' => $settings['order_processing']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
[
'id' => 'order_completed',
'label' => __('Order Completed', 'woonoow'),
'description' => __('When order is marked as completed', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_completed_order',
'enabled' => true,
'recipient_type' => 'customer',
'channels' => $settings['order_completed']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
[
'id' => 'order_cancelled',
'label' => __('Order Cancelled', 'woonoow'),
'description' => __('When order is cancelled', 'woonoow'),
'category' => 'orders',
'wc_email' => 'cancelled_order',
'enabled' => true,
'recipient_type' => 'staff',
'channels' => $settings['order_cancelled']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']],
],
[
'id' => 'order_refunded',
'label' => __('Order Refunded', 'woonoow'),
'description' => __('When order is refunded', 'woonoow'),
'category' => 'orders',
'wc_email' => 'customer_refunded_order',
'enabled' => true,
'recipient_type' => 'customer',
'channels' => $settings['order_refunded']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
],
'products' => [
[
'id' => 'low_stock',
'label' => __('Low Stock Alert', 'woonoow'),
'description' => __('When product stock is low', 'woonoow'),
'category' => 'products',
'wc_email' => 'low_stock',
'enabled' => true,
'recipient_type' => 'staff',
'channels' => $settings['low_stock']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']],
],
[
'id' => 'out_of_stock',
'label' => __('Out of Stock Alert', 'woonoow'),
'description' => __('When product is out of stock', 'woonoow'),
'category' => 'products',
'wc_email' => 'no_stock',
'enabled' => true,
'recipient_type' => 'staff',
'channels' => $settings['out_of_stock']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'admin'], 'push' => ['enabled' => false, 'recipient' => 'admin']],
],
],
'customers' => [
[
'id' => 'new_customer',
'label' => __('New Customer', 'woonoow'),
'description' => __('When a new customer registers', 'woonoow'),
'category' => 'customers',
'wc_email' => 'customer_new_account',
'enabled' => true,
'recipient_type' => 'customer',
'channels' => $settings['new_customer']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
[
'id' => 'customer_note',
'label' => __('Customer Note Added', 'woonoow'),
'description' => __('When a note is added to customer order', 'woonoow'),
'category' => 'customers',
'wc_email' => 'customer_note',
'enabled' => true,
'recipient_type' => 'customer',
'channels' => $settings['customer_note']['channels'] ?? ['email' => ['enabled' => false, 'recipient' => 'customer'], 'push' => ['enabled' => false, 'recipient' => 'customer']],
],
],
];
// Allow addons to add custom events
return apply_filters('woonoow_notification_events', $events);
}
/**
* Update event settings
*