MukeshKapoor25 commited on
Commit
d954b70
·
1 Parent(s): e7cfc57

docs(widgets): Add comprehensive request examples and improve API documentation

Browse files

- Create WIDGET_REQUEST_EXAMPLES.md with detailed request/response examples for all widget endpoints
- Add JSON request body examples for chart, table, and KPI widgets
- Include cURL examples for testing widget endpoints
- Document common parameters and their usage across widget types
- Add Body() examples to chart_widget_router.py endpoints for better API documentation
- Create add_request_examples.py script for automated documentation generation
- Improve developer experience with clear endpoint usage patterns and parameter descriptions

WIDGET_REQUEST_EXAMPLES.md ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Widget Request Body Examples
2
+
3
+ This document provides request body examples for all widget endpoints.
4
+
5
+ ## Chart Widgets
6
+
7
+ ### 1. Revenue Trend (12 Months)
8
+ **Endpoint:** `POST /api/v1/charts/revenue-trend`
9
+
10
+ ```json
11
+ {
12
+ "widget_id": "wid_revenue_trend_12m_001",
13
+ "chart_type": "line",
14
+ "months": 12,
15
+ "period_window": "last_12_months"
16
+ }
17
+ ```
18
+
19
+ ### 2. Gross Margin Trend (12 Months)
20
+ **Endpoint:** `POST /api/v1/charts/gross-margin-trend`
21
+
22
+ ```json
23
+ {
24
+ "widget_id": "wid_gross_margin_trend_12m_001",
25
+ "chart_type": "line",
26
+ "months": 12,
27
+ "period_window": "last_12_months"
28
+ }
29
+ ```
30
+
31
+ ### 3. Channel Mix
32
+ **Endpoint:** `POST /api/v1/charts/channel-mix`
33
+
34
+ ```json
35
+ {
36
+ "widget_id": "wid_channel_mix_001",
37
+ "chart_type": "donut",
38
+ "period_window": "last_12_months"
39
+ }
40
+ ```
41
+
42
+ ### 4. Top 5 SKUs
43
+ **Endpoint:** `POST /api/v1/charts/top-skus`
44
+
45
+ ```json
46
+ {
47
+ "widget_id": "wid_top_5_skus_001",
48
+ "chart_type": "bar",
49
+ "limit": 5,
50
+ "period_window": "last_12_months"
51
+ }
52
+ ```
53
+
54
+ ### 5. Inventory Status
55
+ **Endpoint:** `POST /api/v1/charts/inventory-status`
56
+
57
+ ```json
58
+ {
59
+ "widget_id": "wid_inventory_status_001",
60
+ "chart_type": "donut"
61
+ }
62
+ ```
63
+
64
+ ### 6. Top Selling Products (30 Days)
65
+ **Endpoint:** `POST /api/v1/charts/top-selling-products`
66
+
67
+ ```json
68
+ {
69
+ "widget_id": "wid_top_selling_products_30d_001",
70
+ "chart_type": "bar",
71
+ "window_days": 30,
72
+ "limit": 10
73
+ }
74
+ ```
75
+
76
+ ### 7. Staff Performance
77
+ **Endpoint:** `POST /api/v1/charts/staff-performance`
78
+
79
+ ```json
80
+ {
81
+ "widget_id": "wid_staff_performance_001",
82
+ "chart_type": "bar",
83
+ "window_days": 30,
84
+ "limit": 10
85
+ }
86
+ ```
87
+
88
+ ### 8. My Sales Trend (30 Days)
89
+ **Endpoint:** `POST /api/v1/charts/my-sales-trend`
90
+
91
+ ```json
92
+ {
93
+ "widget_id": "wid_personal_sales_trend_30d_001",
94
+ "chart_type": "line",
95
+ "window_days": 30
96
+ }
97
+ ```
98
+
99
+ ### 9. Top Products Sold by Me
100
+ **Endpoint:** `POST /api/v1/charts/my-top-products`
101
+
102
+ ```json
103
+ {
104
+ "widget_id": "wid_top_products_sold_by_me_001",
105
+ "chart_type": "bar",
106
+ "window_days": 30,
107
+ "limit": 5
108
+ }
109
+ ```
110
+
111
+ ### Universal Chart Endpoint
112
+ **Endpoint:** `POST /api/v1/charts/widget`
113
+
114
+ Can use any of the above widget_ids. Example:
115
+
116
+ ```json
117
+ {
118
+ "widget_id": "wid_revenue_trend_12m_001",
119
+ "chart_type": "line",
120
+ "months": 12,
121
+ "period_window": "last_12_months"
122
+ }
123
+ ```
124
+
125
+ ## Table Widgets
126
+
127
+ ### Universal Table Endpoint
128
+ **Endpoint:** `POST /api/v1/tables/widget`
129
+
130
+ ### 1. Recent Orders
131
+ ```json
132
+ {
133
+ "widget_id": "wid_recent_orders_001",
134
+ "limit": 10,
135
+ "offset": 0
136
+ }
137
+ ```
138
+
139
+ ### 2. Pending Orders
140
+ ```json
141
+ {
142
+ "widget_id": "wid_pending_orders_001",
143
+ "limit": 10,
144
+ "offset": 0
145
+ }
146
+ ```
147
+
148
+ ### 3. Low Stock Items
149
+ ```json
150
+ {
151
+ "widget_id": "wid_low_stock_items_001",
152
+ "limit": 10,
153
+ "offset": 0
154
+ }
155
+ ```
156
+
157
+ ### 4. Top Customers (30 Days)
158
+ ```json
159
+ {
160
+ "widget_id": "wid_top_customers_30d_001",
161
+ "window_days": 30,
162
+ "limit": 10,
163
+ "offset": 0
164
+ }
165
+ ```
166
+
167
+ ### 5. Refunded/Cancelled Orders
168
+ ```json
169
+ {
170
+ "widget_id": "wid_top_refunded_orders_001",
171
+ "limit": 10,
172
+ "offset": 0
173
+ }
174
+ ```
175
+
176
+ ### 6. Expiring Stock (Next 30 Days)
177
+ ```json
178
+ {
179
+ "widget_id": "wid_expiring_stock_001",
180
+ "expiry_within_days": 30,
181
+ "limit": 10,
182
+ "offset": 0
183
+ }
184
+ ```
185
+
186
+ ### 7. Products Needing Reorder
187
+ ```json
188
+ {
189
+ "widget_id": "wid_product_reorder_list_001",
190
+ "limit": 10,
191
+ "offset": 0
192
+ }
193
+ ```
194
+
195
+ ## KPI Widgets
196
+
197
+ ### Get All KPIs
198
+ **Endpoint:** `POST /api/v1/kpi/stats`
199
+
200
+ ```json
201
+ {
202
+ "period_window": "mtd"
203
+ }
204
+ ```
205
+
206
+ Options for `period_window`:
207
+ - `"mtd"` - Month to Date
208
+ - `"qtd"` - Quarter to Date
209
+ - `"ytd"` - Year to Date
210
+
211
+ ### Get Individual KPI
212
+ **Endpoint:** `POST /api/v1/kpi/stats/individual/{kpi_id}`
213
+
214
+ ```json
215
+ {
216
+ "period_window": "mtd"
217
+ }
218
+ ```
219
+
220
+ Available KPI IDs:
221
+ - `total_revenue` (widget: wid_total_revenue_001)
222
+ - `gross_margin_pct` (widget: wid_gross_margin_pct_001)
223
+ - `orders_count` (widget: wid_orders_count_001)
224
+ - `aov` (widget: wid_aov_001)
225
+ - `repeat_rate` (widget: wid_repeat_rate_001)
226
+ - `refund_rate` (widget: wid_refund_rate_001)
227
+
228
+ ## Common Parameters
229
+
230
+ ### Chart Widget Parameters
231
+ - `widget_id` (required): Unique widget identifier
232
+ - `chart_type` (optional): "line", "bar", "donut", "pie", "area"
233
+ - `period_window` (optional): "last_30_days", "last_12_months", "mtd", "qtd", "ytd"
234
+ - `months` (optional): Number of months for trend charts (default: 12)
235
+ - `window_days` (optional): Number of days for window-based charts (default: 30)
236
+ - `limit` (optional): Limit for top N results (default: 5)
237
+ - `branch_id` (optional): Filter by specific branch
238
+
239
+ ### Table Widget Parameters
240
+ - `widget_id` (required): Unique widget identifier
241
+ - `limit` (optional): Number of records to return (default: 10)
242
+ - `offset` (optional): Pagination offset (default: 0)
243
+ - `sort` (optional): Sort field
244
+ - `order` (optional): "asc" or "desc"
245
+ - `window_days` (optional): Number of days for time-based filters
246
+ - `expiry_within_days` (optional): Days until expiry for stock widgets
247
+ - `branch_id` (optional): Filter by specific branch
248
+
249
+ ### KPI Parameters
250
+ - `period_window` (required): "mtd", "qtd", or "ytd"
251
+
252
+ ## cURL Examples
253
+
254
+ ### Chart Widget
255
+ ```bash
256
+ curl -X 'POST' \
257
+ 'http://localhost:9100/api/v1/charts/revenue-trend?use_cache=true' \
258
+ -H 'Authorization: Bearer YOUR_JWT_TOKEN' \
259
+ -H 'Content-Type: application/json' \
260
+ -d '{
261
+ "widget_id": "wid_revenue_trend_12m_001",
262
+ "chart_type": "line",
263
+ "months": 12,
264
+ "period_window": "last_12_months"
265
+ }'
266
+ ```
267
+
268
+ ### Table Widget
269
+ ```bash
270
+ curl -X 'POST' \
271
+ 'http://localhost:9100/api/v1/tables/widget?use_cache=true' \
272
+ -H 'Authorization: Bearer YOUR_JWT_TOKEN' \
273
+ -H 'Content-Type: application/json' \
274
+ -d '{
275
+ "widget_id": "wid_recent_orders_001",
276
+ "limit": 10,
277
+ "offset": 0
278
+ }'
279
+ ```
280
+
281
+ ### KPI Widget
282
+ ```bash
283
+ curl -X 'POST' \
284
+ 'http://localhost:9100/api/v1/kpi/stats' \
285
+ -H 'Authorization: Bearer YOUR_JWT_TOKEN' \
286
+ -H 'Content-Type: application/json' \
287
+ -d '{
288
+ "period_window": "mtd"
289
+ }'
290
+ ```
291
+
292
+ ## Notes
293
+
294
+ 1. All endpoints require a valid JWT token in the Authorization header
295
+ 2. The `widget_id` must match the endpoint being called
296
+ 3. Widget access is controlled by the `widget_access` array in the user's role document
297
+ 4. Use `use_cache=true` query parameter to enable caching (recommended)
298
+ 5. Branch filtering is optional - omit `branch_id` to get data for all branches
app/routers/chart_widget_router.py CHANGED
@@ -2,7 +2,7 @@
2
  Chart Widget Router for dashboard chart widget endpoints.
3
  Implements all 9 chart widgets with role-based access control.
4
  """
5
- from fastapi import APIRouter, Depends, Query, HTTPException
6
  from typing import Optional
7
 
8
  from insightfy_utils.logging import get_logger
@@ -100,7 +100,15 @@ async def require_view_personal_charts_permission(current_user: dict = Depends(g
100
  description="Get monthly revenue trend chart. Requires widget access."
101
  )
102
  async def get_revenue_trend_chart(
103
- request: ChartWidgetRequest,
 
 
 
 
 
 
 
 
104
  use_cache: bool = Query(True, description="Use cached data if available"),
105
  current_user: dict = Depends(get_current_user)
106
  ):
@@ -146,7 +154,15 @@ async def get_revenue_trend_chart(
146
  description="Get monthly gross margin trend chart. Requires widget access."
147
  )
148
  async def get_gross_margin_trend_chart(
149
- request: ChartWidgetRequest,
 
 
 
 
 
 
 
 
150
  use_cache: bool = Query(True, description="Use cached data if available"),
151
  current_user: dict = Depends(get_current_user)
152
  ):
 
2
  Chart Widget Router for dashboard chart widget endpoints.
3
  Implements all 9 chart widgets with role-based access control.
4
  """
5
+ from fastapi import APIRouter, Depends, Query, Body, HTTPException
6
  from typing import Optional
7
 
8
  from insightfy_utils.logging import get_logger
 
100
  description="Get monthly revenue trend chart. Requires widget access."
101
  )
102
  async def get_revenue_trend_chart(
103
+ request: ChartWidgetRequest = Body(
104
+ ...,
105
+ example={
106
+ "widget_id": "wid_revenue_trend_12m_001",
107
+ "chart_type": "line",
108
+ "months": 12,
109
+ "period_window": "last_12_months"
110
+ }
111
+ ),
112
  use_cache: bool = Query(True, description="Use cached data if available"),
113
  current_user: dict = Depends(get_current_user)
114
  ):
 
154
  description="Get monthly gross margin trend chart. Requires widget access."
155
  )
156
  async def get_gross_margin_trend_chart(
157
+ request: ChartWidgetRequest = Body(
158
+ ...,
159
+ example={
160
+ "widget_id": "wid_gross_margin_trend_12m_001",
161
+ "chart_type": "line",
162
+ "months": 12,
163
+ "period_window": "last_12_months"
164
+ }
165
+ ),
166
  use_cache: bool = Query(True, description="Use cached data if available"),
167
  current_user: dict = Depends(get_current_user)
168
  ):
scripts/add_request_examples.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Script to add request body examples to chart widget endpoints.
3
+ """
4
+
5
+ # Mapping of endpoint paths to their widget IDs and example bodies
6
+ ENDPOINT_EXAMPLES = {
7
+ "/gross-margin-trend": {
8
+ "widget_id": "wid_gross_margin_trend_12m_001",
9
+ "chart_type": "line",
10
+ "months": 12,
11
+ "period_window": "last_12_months"
12
+ },
13
+ "/channel-mix": {
14
+ "widget_id": "wid_channel_mix_001",
15
+ "chart_type": "donut",
16
+ "period_window": "last_12_months"
17
+ },
18
+ "/top-skus": {
19
+ "widget_id": "wid_top_5_skus_001",
20
+ "chart_type": "bar",
21
+ "limit": 5,
22
+ "period_window": "last_12_months"
23
+ },
24
+ "/inventory-status": {
25
+ "widget_id": "wid_inventory_status_001",
26
+ "chart_type": "donut"
27
+ },
28
+ "/top-selling-products": {
29
+ "widget_id": "wid_top_selling_products_30d_001",
30
+ "chart_type": "bar",
31
+ "window_days": 30,
32
+ "limit": 10
33
+ },
34
+ "/staff-performance": {
35
+ "widget_id": "wid_staff_performance_001",
36
+ "chart_type": "bar",
37
+ "window_days": 30,
38
+ "limit": 10
39
+ },
40
+ "/my-sales-trend": {
41
+ "widget_id": "wid_personal_sales_trend_30d_001",
42
+ "chart_type": "line",
43
+ "window_days": 30
44
+ },
45
+ "/my-top-products": {
46
+ "widget_id": "wid_top_products_sold_by_me_001",
47
+ "chart_type": "bar",
48
+ "window_days": 30,
49
+ "limit": 5
50
+ }
51
+ }
52
+
53
+ print("Chart Widget Request Body Examples:")
54
+ print("=" * 80)
55
+ for endpoint, example in ENDPOINT_EXAMPLES.items():
56
+ print(f"\n{endpoint}:")
57
+ print(f" {example}")