@adminforth/dashboard 1.9.0 → 1.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,13 @@ export type DashboardWidgetDataResponse = {
25
25
  data: unknown
26
26
  }
27
27
 
28
+ export type DashboardMutationResponse = {
29
+ ok: boolean
30
+ error?: string
31
+ groupId?: string
32
+ widgetId?: string
33
+ }
34
+
28
35
  export type DashboardWidgetDataRequest = {
29
36
  pagination?: {
30
37
  page: number
@@ -119,6 +126,36 @@ async function callDashboardApi(path: string, body: Record<string, unknown>): Pr
119
126
  }
120
127
  }
121
128
 
129
+ async function callDashboardMutationApi(path: string, body: Record<string, unknown>): Promise<DashboardMutationResponse> {
130
+ const rawResponse = await fetch(path, {
131
+ method: 'POST',
132
+ headers: {
133
+ 'Content-Type': 'application/json',
134
+ 'accept-language': localStorage.getItem('af_lang') || 'en',
135
+ },
136
+ body: JSON.stringify(body),
137
+ })
138
+
139
+ const response = await parseDashboardResponse(rawResponse)
140
+
141
+ if (!rawResponse.ok) {
142
+ throw new DashboardApiError(
143
+ response?.error || rawResponse.statusText || `Dashboard request failed (${rawResponse.status})`,
144
+ normalizeValidationErrors(response),
145
+ )
146
+ }
147
+
148
+ if (!response || response.error || response.ok === false) {
149
+ throw new DashboardApiError(response?.error || 'Dashboard request failed', normalizeValidationErrors(response))
150
+ }
151
+
152
+ return {
153
+ ok: true,
154
+ groupId: response.groupId,
155
+ widgetId: response.widgetId,
156
+ }
157
+ }
158
+
122
159
  async function callDashboardWidgetDataApi(
123
160
  path: string,
124
161
  body: Record<string, unknown>,
@@ -156,39 +193,39 @@ export const dashboardApi = {
156
193
  return callDashboardApi('/adminapi/v1/dashboard/get-config', { slug })
157
194
  },
158
195
 
159
- async addDashboardGroup(slug: string): Promise<DashboardResponse> {
160
- return callDashboardApi('/adminapi/v1/dashboard/add_dashboard_group', { slug })
196
+ async addDashboardGroup(slug: string): Promise<DashboardMutationResponse> {
197
+ return callDashboardMutationApi('/adminapi/v1/dashboard/add_dashboard_group', { slug })
161
198
  },
162
199
 
163
200
  async moveDashboardGroup(
164
201
  slug: string,
165
202
  groupId: string,
166
203
  direction: DashboardGroupMoveDirection,
167
- ): Promise<DashboardResponse> {
168
- return callDashboardApi('/adminapi/v1/dashboard/move_dashboard_group', {
204
+ ): Promise<DashboardMutationResponse> {
205
+ return callDashboardMutationApi('/adminapi/v1/dashboard/move_dashboard_group', {
169
206
  slug,
170
207
  groupId,
171
208
  direction,
172
209
  })
173
210
  },
174
211
 
175
- async removeDashboardGroup(slug: string, groupId: string): Promise<DashboardResponse> {
176
- return callDashboardApi('/adminapi/v1/dashboard/remove_dashboard_group', {
212
+ async removeDashboardGroup(slug: string, groupId: string): Promise<DashboardMutationResponse> {
213
+ return callDashboardMutationApi('/adminapi/v1/dashboard/remove_dashboard_group', {
177
214
  slug,
178
215
  groupId,
179
216
  })
180
217
  },
181
218
 
182
- async setDashboardGroupConfig(slug: string, groupId: string, config: EditableDashboardGroupConfig): Promise<DashboardResponse> {
183
- return callDashboardApi('/adminapi/v1/dashboard/set_dashboard_group_config', {
219
+ async setDashboardGroupConfig(slug: string, groupId: string, config: EditableDashboardGroupConfig): Promise<DashboardMutationResponse> {
220
+ return callDashboardMutationApi('/adminapi/v1/dashboard/set_dashboard_group_config', {
184
221
  slug,
185
222
  groupId,
186
223
  config,
187
224
  })
188
225
  },
189
226
 
190
- async addDashboardWidget(slug: string, groupId: string): Promise<DashboardResponse> {
191
- return callDashboardApi('/adminapi/v1/dashboard/add_dashboard_widget', {
227
+ async addDashboardWidget(slug: string, groupId: string): Promise<DashboardMutationResponse> {
228
+ return callDashboardMutationApi('/adminapi/v1/dashboard/add_dashboard_widget', {
192
229
  slug,
193
230
  groupId,
194
231
  })
@@ -198,23 +235,23 @@ export const dashboardApi = {
198
235
  slug: string,
199
236
  widgetId: string,
200
237
  direction: DashboardWidgetMoveDirection,
201
- ): Promise<DashboardResponse> {
202
- return callDashboardApi('/adminapi/v1/dashboard/move_dashboard_widget', {
238
+ ): Promise<DashboardMutationResponse> {
239
+ return callDashboardMutationApi('/adminapi/v1/dashboard/move_dashboard_widget', {
203
240
  slug,
204
241
  widgetId,
205
242
  direction,
206
243
  })
207
244
  },
208
245
 
209
- async removeDashboardWidget(slug: string, widgetId: string): Promise<DashboardResponse> {
210
- return callDashboardApi('/adminapi/v1/dashboard/remove_dashboard_widget', {
246
+ async removeDashboardWidget(slug: string, widgetId: string): Promise<DashboardMutationResponse> {
247
+ return callDashboardMutationApi('/adminapi/v1/dashboard/remove_dashboard_widget', {
211
248
  slug,
212
249
  widgetId,
213
250
  })
214
251
  },
215
252
 
216
- async setWidgetConfig(slug: string, widgetId: string, config: unknown): Promise<DashboardResponse> {
217
- return callDashboardApi('/adminapi/v1/dashboard/set_widget_config', {
253
+ async setWidgetConfig(slug: string, widgetId: string, config: unknown): Promise<DashboardMutationResponse> {
254
+ return callDashboardMutationApi('/adminapi/v1/dashboard/set_widget_config', {
218
255
  slug,
219
256
  widgetId,
220
257
  config,
@@ -225,8 +262,8 @@ export const dashboardApi = {
225
262
  slug: string,
226
263
  widgetId: string,
227
264
  config: ConfigurableTableWidgetConfig,
228
- ): Promise<DashboardResponse> {
229
- return callDashboardApi('/adminapi/v1/dashboard/configure_table_widget', {
265
+ ): Promise<DashboardMutationResponse> {
266
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_table_widget', {
230
267
  slug,
231
268
  widgetId,
232
269
  config,
@@ -237,8 +274,8 @@ export const dashboardApi = {
237
274
  slug: string,
238
275
  widgetId: string,
239
276
  config: ConfigurableKpiCardWidgetConfig,
240
- ): Promise<DashboardResponse> {
241
- return callDashboardApi('/adminapi/v1/dashboard/configure_kpi_card_widget', {
277
+ ): Promise<DashboardMutationResponse> {
278
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_kpi_card_widget', {
242
279
  slug,
243
280
  widgetId,
244
281
  config,
@@ -249,8 +286,8 @@ export const dashboardApi = {
249
286
  slug: string,
250
287
  widgetId: string,
251
288
  config: ConfigurableGaugeCardWidgetConfig,
252
- ): Promise<DashboardResponse> {
253
- return callDashboardApi('/adminapi/v1/dashboard/configure_gauge_card_widget', {
289
+ ): Promise<DashboardMutationResponse> {
290
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_gauge_card_widget', {
254
291
  slug,
255
292
  widgetId,
256
293
  config,
@@ -261,8 +298,8 @@ export const dashboardApi = {
261
298
  slug: string,
262
299
  widgetId: string,
263
300
  config: ConfigurableLineChartWidgetConfig,
264
- ): Promise<DashboardResponse> {
265
- return callDashboardApi('/adminapi/v1/dashboard/configure_line_chart_widget', {
301
+ ): Promise<DashboardMutationResponse> {
302
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_line_chart_widget', {
266
303
  slug,
267
304
  widgetId,
268
305
  config,
@@ -273,8 +310,8 @@ export const dashboardApi = {
273
310
  slug: string,
274
311
  widgetId: string,
275
312
  config: ConfigurableBarChartWidgetConfig,
276
- ): Promise<DashboardResponse> {
277
- return callDashboardApi('/adminapi/v1/dashboard/configure_bar_chart_widget', {
313
+ ): Promise<DashboardMutationResponse> {
314
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_bar_chart_widget', {
278
315
  slug,
279
316
  widgetId,
280
317
  config,
@@ -285,8 +322,8 @@ export const dashboardApi = {
285
322
  slug: string,
286
323
  widgetId: string,
287
324
  config: ConfigurableStackedBarChartWidgetConfig,
288
- ): Promise<DashboardResponse> {
289
- return callDashboardApi('/adminapi/v1/dashboard/configure_stacked_bar_chart_widget', {
325
+ ): Promise<DashboardMutationResponse> {
326
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_stacked_bar_chart_widget', {
290
327
  slug,
291
328
  widgetId,
292
329
  config,
@@ -297,8 +334,8 @@ export const dashboardApi = {
297
334
  slug: string,
298
335
  widgetId: string,
299
336
  config: ConfigurablePieChartWidgetConfig,
300
- ): Promise<DashboardResponse> {
301
- return callDashboardApi('/adminapi/v1/dashboard/configure_pie_chart_widget', {
337
+ ): Promise<DashboardMutationResponse> {
338
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_pie_chart_widget', {
302
339
  slug,
303
340
  widgetId,
304
341
  config,
@@ -309,8 +346,8 @@ export const dashboardApi = {
309
346
  slug: string,
310
347
  widgetId: string,
311
348
  config: ConfigurableHistogramChartWidgetConfig,
312
- ): Promise<DashboardResponse> {
313
- return callDashboardApi('/adminapi/v1/dashboard/configure_histogram_chart_widget', {
349
+ ): Promise<DashboardMutationResponse> {
350
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_histogram_chart_widget', {
314
351
  slug,
315
352
  widgetId,
316
353
  config,
@@ -321,8 +358,8 @@ export const dashboardApi = {
321
358
  slug: string,
322
359
  widgetId: string,
323
360
  config: ConfigurableFunnelChartWidgetConfig,
324
- ): Promise<DashboardResponse> {
325
- return callDashboardApi('/adminapi/v1/dashboard/configure_funnel_chart_widget', {
361
+ ): Promise<DashboardMutationResponse> {
362
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_funnel_chart_widget', {
326
363
  slug,
327
364
  widgetId,
328
365
  config,
@@ -333,8 +370,8 @@ export const dashboardApi = {
333
370
  slug: string,
334
371
  widgetId: string,
335
372
  config: ConfigurablePivotTableWidgetConfig,
336
- ): Promise<DashboardResponse> {
337
- return callDashboardApi('/adminapi/v1/dashboard/configure_pivot_table_widget', {
373
+ ): Promise<DashboardMutationResponse> {
374
+ return callDashboardMutationApi('/adminapi/v1/dashboard/configure_pivot_table_widget', {
338
375
  slug,
339
376
  widgetId,
340
377
  config,
@@ -286,7 +286,8 @@ async function addGroup() {
286
286
  }
287
287
 
288
288
  try {
289
- applyDashboardResponse(await dashboardApi.addDashboardGroup(props.dashboardSlug))
289
+ await dashboardApi.addDashboardGroup(props.dashboardSlug)
290
+ await refreshDashboardConfig()
290
291
  } catch (error) {
291
292
  console.error('Failed to add dashboard group', error)
292
293
  }
@@ -298,7 +299,8 @@ async function addWidget(groupId: string) {
298
299
  }
299
300
 
300
301
  try {
301
- applyDashboardResponse(await dashboardApi.addDashboardWidget(props.dashboardSlug, groupId))
302
+ await dashboardApi.addDashboardWidget(props.dashboardSlug, groupId)
303
+ await refreshDashboardConfig()
302
304
  } catch (error) {
303
305
  console.error('Failed to add dashboard widget', error)
304
306
  }
@@ -310,9 +312,8 @@ async function moveGroup(groupId: string, direction: DashboardGroupMoveDirection
310
312
  }
311
313
 
312
314
  try {
313
- applyDashboardResponse(
314
- await dashboardApi.moveDashboardGroup(props.dashboardSlug, groupId, direction),
315
- )
315
+ await dashboardApi.moveDashboardGroup(props.dashboardSlug, groupId, direction)
316
+ await refreshDashboardConfig()
316
317
  } catch (error) {
317
318
  console.error('Failed to move dashboard group', error)
318
319
  }
@@ -324,7 +325,8 @@ async function removeGroup(groupId: string) {
324
325
  }
325
326
 
326
327
  try {
327
- applyDashboardResponse(await dashboardApi.removeDashboardGroup(props.dashboardSlug, groupId))
328
+ await dashboardApi.removeDashboardGroup(props.dashboardSlug, groupId)
329
+ await refreshDashboardConfig()
328
330
  } catch (error) {
329
331
  console.error('Failed to remove dashboard group', error)
330
332
  }
@@ -348,13 +350,12 @@ async function saveGroupConfig() {
348
350
  try {
349
351
  const groupConfig = parseYaml(groupConfigCode.value) as EditableDashboardGroupConfig
350
352
 
351
- applyDashboardResponse(
352
- await dashboardApi.setDashboardGroupConfig(
353
- props.dashboardSlug,
354
- editingGroupId.value,
355
- groupConfig,
356
- ),
353
+ await dashboardApi.setDashboardGroupConfig(
354
+ props.dashboardSlug,
355
+ editingGroupId.value,
356
+ groupConfig,
357
357
  )
358
+ await refreshDashboardConfig()
358
359
  closeGroupConfigEditor()
359
360
  } catch (error) {
360
361
  groupConfigError.value = error instanceof Error ? error.message : 'Invalid group config'
@@ -373,9 +374,8 @@ async function moveWidget(widgetId: string, direction: DashboardWidgetMoveDirect
373
374
  }
374
375
 
375
376
  try {
376
- applyDashboardResponse(
377
- await dashboardApi.moveDashboardWidget(props.dashboardSlug, widgetId, direction),
378
- )
377
+ await dashboardApi.moveDashboardWidget(props.dashboardSlug, widgetId, direction)
378
+ await refreshDashboardConfig()
379
379
  } catch (error) {
380
380
  console.error('Failed to move dashboard widget', error)
381
381
  }
@@ -387,7 +387,8 @@ async function removeWidget(widgetId: string) {
387
387
  }
388
388
 
389
389
  try {
390
- applyDashboardResponse(await dashboardApi.removeDashboardWidget(props.dashboardSlug, widgetId))
390
+ await dashboardApi.removeDashboardWidget(props.dashboardSlug, widgetId)
391
+ await refreshDashboardConfig()
391
392
  } catch (error) {
392
393
  console.error('Failed to remove dashboard widget', error)
393
394
  }
@@ -410,13 +411,12 @@ async function saveWidgetConfig() {
410
411
  widgetConfigFieldErrors.value = []
411
412
  const widgetConfig = parseYaml(widgetConfigCode.value) as DashboardWidgetConfig
412
413
 
413
- applyDashboardResponse(
414
- await dashboardApi.setWidgetConfig(
415
- props.dashboardSlug,
416
- editingWidgetId.value,
417
- serializeDashboardWidgetConfigForEditor(widgetConfig),
418
- ),
414
+ await dashboardApi.setWidgetConfig(
415
+ props.dashboardSlug,
416
+ editingWidgetId.value,
417
+ serializeDashboardWidgetConfigForEditor(widgetConfig),
419
418
  )
419
+ await refreshDashboardConfig()
420
420
  closeWidgetConfigEditor()
421
421
  } catch (error) {
422
422
  widgetConfigError.value = error instanceof Error ? error.message : 'Invalid widget config'
@@ -431,6 +431,10 @@ function closeWidgetConfigEditor() {
431
431
  widgetConfigFieldErrors.value = []
432
432
  }
433
433
 
434
+ async function refreshDashboardConfig() {
435
+ applyDashboardResponse(await dashboardApi.getDashboardConfig(props.dashboardSlug))
436
+ }
437
+
434
438
  function applyDashboardResponse(response: DashboardResponse) {
435
439
  draftConfig.value = cloneConfig(response.config)
436
440
  currentRevision.value = response.revision
@@ -8,19 +8,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { randomUUID } from 'crypto';
11
- import { DashboardApiResponseSchema, GroupIdRequestSchema, MoveGroupRequestSchema, SetGroupConfigRequestSchema, SlugRequestSchema, } from '../schema/api.js';
11
+ import { DashboardMutationResponseSchema, GroupIdRequestSchema, MoveGroupRequestSchema, SetGroupConfigRequestSchema, SlugRequestSchema, } from '../schema/api.js';
12
12
  export function registerGroupEndpoints(server, ctx) {
13
13
  server.endpoint({
14
14
  method: 'POST',
15
15
  path: '/dashboard/add_dashboard_group',
16
16
  description: 'Adds a new group to a dashboard configuration. Superadmin only.',
17
17
  request_schema: SlugRequestSchema,
18
- response_schema: DashboardApiResponseSchema,
18
+ response_schema: DashboardMutationResponseSchema,
19
19
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
20
20
  if (!ctx.canEditDashboard(adminUser)) {
21
21
  response.setStatus(403);
22
- return { error: 'Dashboard edit is not allowed' };
22
+ return { ok: false, error: 'Dashboard edit is not allowed' };
23
23
  }
24
+ let groupId = null;
24
25
  const updatedDashboard = yield ctx.updateDashboardConfig(body.slug, (config) => {
25
26
  const nextOrder = config.groups.length + 1;
26
27
  const group = {
@@ -28,13 +29,14 @@ export function registerGroupEndpoints(server, ctx) {
28
29
  label: 'New group',
29
30
  order: nextOrder,
30
31
  };
32
+ groupId = group.id;
31
33
  return Object.assign(Object.assign({}, config), { groups: [...config.groups, group] });
32
34
  });
33
35
  if (!updatedDashboard) {
34
36
  response.setStatus(404);
35
- return { error: 'Dashboard not found' };
37
+ return { ok: false, error: 'Dashboard not found' };
36
38
  }
37
- return updatedDashboard;
39
+ return { ok: true, groupId };
38
40
  }),
39
41
  });
40
42
  server.endpoint({
@@ -42,11 +44,11 @@ export function registerGroupEndpoints(server, ctx) {
42
44
  path: '/dashboard/set_dashboard_group_config',
43
45
  description: 'Replaces editable JSON configuration for a dashboard group while preserving group id and order. Superadmin only.',
44
46
  request_schema: SetGroupConfigRequestSchema,
45
- response_schema: DashboardApiResponseSchema,
47
+ response_schema: DashboardMutationResponseSchema,
46
48
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
47
49
  if (!ctx.canEditDashboard(adminUser)) {
48
50
  response.setStatus(403);
49
- return { error: 'Dashboard edit is not allowed' };
51
+ return { ok: false, error: 'Dashboard edit is not allowed' };
50
52
  }
51
53
  const groupId = body.groupId;
52
54
  let mutationError = null;
@@ -63,13 +65,13 @@ export function registerGroupEndpoints(server, ctx) {
63
65
  });
64
66
  if (!updatedDashboard) {
65
67
  response.setStatus(404);
66
- return { error: 'Dashboard not found' };
68
+ return { ok: false, error: 'Dashboard not found' };
67
69
  }
68
70
  if (mutationError) {
69
71
  response.setStatus(404);
70
- return { error: mutationError };
72
+ return { ok: false, error: mutationError };
71
73
  }
72
- return updatedDashboard;
74
+ return { ok: true };
73
75
  }),
74
76
  });
75
77
  server.endpoint({
@@ -77,11 +79,11 @@ export function registerGroupEndpoints(server, ctx) {
77
79
  path: '/dashboard/move_dashboard_group',
78
80
  description: 'Moves a dashboard group up or down in its dashboard. Superadmin only.',
79
81
  request_schema: MoveGroupRequestSchema,
80
- response_schema: DashboardApiResponseSchema,
82
+ response_schema: DashboardMutationResponseSchema,
81
83
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
82
84
  if (!ctx.canEditDashboard(adminUser)) {
83
85
  response.setStatus(403);
84
- return { error: 'Dashboard edit is not allowed' };
86
+ return { ok: false, error: 'Dashboard edit is not allowed' };
85
87
  }
86
88
  let mutationError = null;
87
89
  const updatedDashboard = yield ctx.updateDashboardConfig(body.slug, (config) => {
@@ -102,13 +104,13 @@ export function registerGroupEndpoints(server, ctx) {
102
104
  });
103
105
  if (!updatedDashboard) {
104
106
  response.setStatus(404);
105
- return { error: 'Dashboard not found' };
107
+ return { ok: false, error: 'Dashboard not found' };
106
108
  }
107
109
  if (mutationError) {
108
110
  response.setStatus(404);
109
- return { error: mutationError };
111
+ return { ok: false, error: mutationError };
110
112
  }
111
- return updatedDashboard;
113
+ return { ok: true };
112
114
  }),
113
115
  });
114
116
  server.endpoint({
@@ -116,11 +118,11 @@ export function registerGroupEndpoints(server, ctx) {
116
118
  path: '/dashboard/remove_dashboard_group',
117
119
  description: 'Removes a dashboard group and all widgets inside it. Superadmin only.',
118
120
  request_schema: GroupIdRequestSchema,
119
- response_schema: DashboardApiResponseSchema,
121
+ response_schema: DashboardMutationResponseSchema,
120
122
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
121
123
  if (!ctx.canEditDashboard(adminUser)) {
122
124
  response.setStatus(403);
123
- return { error: 'Dashboard edit is not allowed' };
125
+ return { ok: false, error: 'Dashboard edit is not allowed' };
124
126
  }
125
127
  const groupId = body.groupId;
126
128
  let mutationError = null;
@@ -134,13 +136,13 @@ export function registerGroupEndpoints(server, ctx) {
134
136
  });
135
137
  if (!updatedDashboard) {
136
138
  response.setStatus(404);
137
- return { error: 'Dashboard not found' };
139
+ return { ok: false, error: 'Dashboard not found' };
138
140
  }
139
141
  if (mutationError) {
140
142
  response.setStatus(404);
141
- return { error: mutationError };
143
+ return { ok: false, error: mutationError };
142
144
  }
143
- return updatedDashboard;
145
+ return { ok: true };
144
146
  }),
145
147
  });
146
148
  }
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { randomUUID } from 'crypto';
11
- import { ConfigureBarChartWidgetRequestSchema, ConfigureFunnelChartWidgetRequestSchema, ConfigureGaugeCardWidgetRequestSchema, ConfigureHistogramChartWidgetRequestSchema, ConfigureKpiCardWidgetRequestSchema, ConfigureLineChartWidgetRequestSchema, ConfigurePieChartWidgetRequestSchema, ConfigurePivotTableWidgetRequestSchema, ConfigureStackedBarChartWidgetRequestSchema, ConfigureTableWidgetRequestSchema, DashboardApiResponseSchema, DashboardWidgetDataResponseSchema, GroupIdRequestSchema, MoveWidgetRequestSchema, SetWidgetConfigRequestSchema, WidgetDataRequestSchema, WidgetIdRequestSchema, } from '../schema/api.js';
11
+ import { ConfigureBarChartWidgetRequestSchema, ConfigureFunnelChartWidgetRequestSchema, ConfigureGaugeCardWidgetRequestSchema, ConfigureHistogramChartWidgetRequestSchema, ConfigureKpiCardWidgetRequestSchema, ConfigureLineChartWidgetRequestSchema, ConfigurePieChartWidgetRequestSchema, ConfigurePivotTableWidgetRequestSchema, ConfigureStackedBarChartWidgetRequestSchema, ConfigureTableWidgetRequestSchema, DashboardMutationResponseSchema, DashboardWidgetDataResponseSchema, GroupIdRequestSchema, MoveWidgetRequestSchema, SetWidgetConfigRequestSchema, WidgetDataRequestSchema, WidgetIdRequestSchema, } from '../schema/api.js';
12
12
  function replaceWidgetConfig(ctx, slug, widgetId, widgetConfig) {
13
13
  return __awaiter(this, void 0, void 0, function* () {
14
14
  let mutationError = null;
@@ -35,23 +35,23 @@ function registerConfigureWidgetEndpoint(server, ctx, options) {
35
35
  path: options.path,
36
36
  description: options.description,
37
37
  request_schema: options.requestSchema,
38
- response_schema: DashboardApiResponseSchema,
38
+ response_schema: DashboardMutationResponseSchema,
39
39
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
40
40
  if (!ctx.canEditDashboard(adminUser)) {
41
41
  response.setStatus(403);
42
- return { error: 'Dashboard edit is not allowed' };
42
+ return { ok: false, error: 'Dashboard edit is not allowed' };
43
43
  }
44
44
  const request = body;
45
45
  const { updatedDashboard, mutationError } = yield replaceWidgetConfig(ctx, request.slug, request.widgetId, request.config);
46
46
  if (!updatedDashboard) {
47
47
  response.setStatus(404);
48
- return { error: 'Dashboard not found' };
48
+ return { ok: false, error: 'Dashboard not found' };
49
49
  }
50
50
  if (mutationError) {
51
51
  response.setStatus(404);
52
- return { error: mutationError };
52
+ return { ok: false, error: mutationError };
53
53
  }
54
- return updatedDashboard;
54
+ return { ok: true };
55
55
  }),
56
56
  });
57
57
  }
@@ -61,13 +61,14 @@ export function registerWidgetEndpoints(server, ctx) {
61
61
  path: '/dashboard/add_dashboard_widget',
62
62
  description: 'Adds a new empty widget to a dashboard group. Superadmin only.',
63
63
  request_schema: GroupIdRequestSchema,
64
- response_schema: DashboardApiResponseSchema,
64
+ response_schema: DashboardMutationResponseSchema,
65
65
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
66
66
  if (!ctx.canEditDashboard(adminUser)) {
67
67
  response.setStatus(403);
68
- return { error: 'Dashboard edit is not allowed' };
68
+ return { ok: false, error: 'Dashboard edit is not allowed' };
69
69
  }
70
70
  let mutationError = null;
71
+ let widgetId = null;
71
72
  const updatedDashboard = yield ctx.updateDashboardConfig(body.slug, (config) => {
72
73
  const group = config.groups.find((item) => item.id === body.groupId);
73
74
  if (!group) {
@@ -83,17 +84,18 @@ export function registerWidgetEndpoints(server, ctx) {
83
84
  order: nextOrder,
84
85
  target: 'empty',
85
86
  };
87
+ widgetId = widget.id;
86
88
  return Object.assign(Object.assign({}, config), { widgets: [...config.widgets, widget] });
87
89
  });
88
90
  if (!updatedDashboard) {
89
91
  response.setStatus(404);
90
- return { error: 'Dashboard not found' };
92
+ return { ok: false, error: 'Dashboard not found' };
91
93
  }
92
94
  if (mutationError) {
93
95
  response.setStatus(404);
94
- return { error: mutationError };
96
+ return { ok: false, error: mutationError };
95
97
  }
96
- return updatedDashboard;
98
+ return { ok: true, widgetId };
97
99
  }),
98
100
  });
99
101
  server.endpoint({
@@ -101,11 +103,11 @@ export function registerWidgetEndpoints(server, ctx) {
101
103
  path: '/dashboard/move_dashboard_widget',
102
104
  description: 'Moves a dashboard widget up or down inside its group. Superadmin only.',
103
105
  request_schema: MoveWidgetRequestSchema,
104
- response_schema: DashboardApiResponseSchema,
106
+ response_schema: DashboardMutationResponseSchema,
105
107
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
106
108
  if (!ctx.canEditDashboard(adminUser)) {
107
109
  response.setStatus(403);
108
- return { error: 'Dashboard edit is not allowed' };
110
+ return { ok: false, error: 'Dashboard edit is not allowed' };
109
111
  }
110
112
  let mutationError = null;
111
113
  const updatedDashboard = yield ctx.updateDashboardConfig(body.slug, (config) => {
@@ -133,13 +135,13 @@ export function registerWidgetEndpoints(server, ctx) {
133
135
  });
134
136
  if (!updatedDashboard) {
135
137
  response.setStatus(404);
136
- return { error: 'Dashboard not found' };
138
+ return { ok: false, error: 'Dashboard not found' };
137
139
  }
138
140
  if (mutationError) {
139
141
  response.setStatus(404);
140
- return { error: mutationError };
142
+ return { ok: false, error: mutationError };
141
143
  }
142
- return updatedDashboard;
144
+ return { ok: true };
143
145
  }),
144
146
  });
145
147
  server.endpoint({
@@ -147,11 +149,11 @@ export function registerWidgetEndpoints(server, ctx) {
147
149
  path: '/dashboard/remove_dashboard_widget',
148
150
  description: 'Removes one dashboard widget by id. Superadmin only.',
149
151
  request_schema: WidgetIdRequestSchema,
150
- response_schema: DashboardApiResponseSchema,
152
+ response_schema: DashboardMutationResponseSchema,
151
153
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
152
154
  if (!ctx.canEditDashboard(adminUser)) {
153
155
  response.setStatus(403);
154
- return { error: 'Dashboard edit is not allowed' };
156
+ return { ok: false, error: 'Dashboard edit is not allowed' };
155
157
  }
156
158
  let mutationError = null;
157
159
  const updatedDashboard = yield ctx.updateDashboardConfig(body.slug, (config) => {
@@ -164,13 +166,13 @@ export function registerWidgetEndpoints(server, ctx) {
164
166
  });
165
167
  if (!updatedDashboard) {
166
168
  response.setStatus(404);
167
- return { error: 'Dashboard not found' };
169
+ return { ok: false, error: 'Dashboard not found' };
168
170
  }
169
171
  if (mutationError) {
170
172
  response.setStatus(404);
171
- return { error: mutationError };
173
+ return { ok: false, error: mutationError };
172
174
  }
173
- return updatedDashboard;
175
+ return { ok: true };
174
176
  }),
175
177
  });
176
178
  server.endpoint({
@@ -178,23 +180,23 @@ export function registerWidgetEndpoints(server, ctx) {
178
180
  path: '/dashboard/set_widget_config',
179
181
  description: 'Replaces editable JSON configuration for a dashboard widget while preserving widget id, group id, and order. Superadmin only.',
180
182
  request_schema: SetWidgetConfigRequestSchema,
181
- response_schema: DashboardApiResponseSchema,
183
+ response_schema: DashboardMutationResponseSchema,
182
184
  handler: (_a) => __awaiter(this, [_a], void 0, function* ({ body, adminUser, response }) {
183
185
  if (!ctx.canEditDashboard(adminUser)) {
184
186
  response.setStatus(403);
185
- return { error: 'Dashboard edit is not allowed' };
187
+ return { ok: false, error: 'Dashboard edit is not allowed' };
186
188
  }
187
189
  const request = body;
188
190
  const { updatedDashboard, mutationError } = yield replaceWidgetConfig(ctx, request.slug, request.widgetId, request.config);
189
191
  if (!updatedDashboard) {
190
192
  response.setStatus(404);
191
- return { error: 'Dashboard not found' };
193
+ return { ok: false, error: 'Dashboard not found' };
192
194
  }
193
195
  if (mutationError) {
194
196
  response.setStatus(404);
195
- return { error: mutationError };
197
+ return { ok: false, error: mutationError };
196
198
  }
197
- return updatedDashboard;
199
+ return { ok: true };
198
200
  }),
199
201
  });
200
202
  registerConfigureWidgetEndpoint(server, ctx, {