|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { adminHeaders, createAdminUser } from "../../helpers/create-admin-user" |
|
3
|
|
|
4
|
jest.setTimeout(50000) |
|
5
|
|
|
6
|
const env = { MEDUSA_FF_VIEW_CONFIGURATIONS: true } |
|
7
|
|
|
8
|
medusaIntegrationTestRunner({ |
|
9
|
env, |
|
10
|
testSuite: ({ dbConnection, api, getContainer, dbUtils }) => { |
|
11
|
describe("View Configurations API", () => { |
|
12
|
let adminHeader |
|
13
|
let secondAdminHeader |
|
14
|
let secondAdminUserId |
|
15
|
let adminUserId |
|
16
|
|
|
17
|
beforeAll(async () => { |
|
18
|
const container = getContainer() |
|
19
|
const { user: adminUser } = await createAdminUser( |
|
20
|
dbConnection, |
|
21
|
adminHeaders, |
|
22
|
container |
|
23
|
) |
|
24
|
adminHeader = adminHeaders.headers |
|
25
|
adminUserId = adminUser.id |
|
26
|
|
|
27
|
// Create a second admin user |
|
28
|
const secondAdminHeaders = { headers: {} } |
|
29
|
const { user: secondAdminUser } = await createAdminUser( |
|
30
|
dbConnection, |
|
31
|
secondAdminHeaders, |
|
32
|
container, |
|
33
|
{ email: "admin2@test.com" } |
|
34
|
) |
|
35
|
secondAdminUserId = secondAdminUser.id |
|
36
|
secondAdminHeader = secondAdminHeaders.headers |
|
37
|
|
|
38
|
await dbUtils.snapshot() |
|
39
|
}) |
|
40
|
|
|
41
|
describe("POST /admin/views/{entity}/configurations", () => { |
|
42
|
it("should create a personal view configuration", async () => { |
|
43
|
const payload = { |
|
44
|
name: "My Order View", |
|
45
|
configuration: { |
|
46
|
visible_columns: ["id", "display_id", "created_at"], |
|
47
|
column_order: ["display_id", "id", "created_at"], |
|
48
|
}, |
|
49
|
} |
|
50
|
|
|
51
|
const response = await api.post( |
|
52
|
"/admin/views/orders/configurations", |
|
53
|
payload, |
|
54
|
{ |
|
55
|
headers: secondAdminHeader, |
|
56
|
} |
|
57
|
) |
|
58
|
|
|
59
|
expect(response.status).toBe(200) |
|
60
|
expect(response.data.view_configuration).toMatchObject({ |
|
61
|
entity: "orders", |
|
62
|
name: "My Order View", |
|
63
|
user_id: secondAdminUserId, |
|
64
|
configuration: payload.configuration, |
|
65
|
}) |
|
66
|
expect(response.data.view_configuration.is_system_default).toBeFalsy() |
|
67
|
}) |
|
68
|
|
|
69
|
it("should create a system default view as admin", async () => { |
|
70
|
const payload = { |
|
71
|
name: "Default Order View", |
|
72
|
is_system_default: true, |
|
73
|
configuration: { |
|
74
|
visible_columns: ["id", "display_id", "created_at", "total"], |
|
75
|
column_order: ["display_id", "created_at", "total", "id"], |
|
76
|
}, |
|
77
|
} |
|
78
|
|
|
79
|
const response = await api.post( |
|
80
|
"/admin/views/orders/configurations", |
|
81
|
payload, |
|
82
|
{ |
|
83
|
headers: adminHeader, |
|
84
|
} |
|
85
|
) |
|
86
|
|
|
87
|
expect(response.status).toBe(200) |
|
88
|
expect(response.data.view_configuration).toMatchObject({ |
|
89
|
entity: "orders", |
|
90
|
name: "Default Order View", |
|
91
|
user_id: null, |
|
92
|
is_system_default: true, |
|
93
|
configuration: payload.configuration, |
|
94
|
}) |
|
95
|
}) |
|
96
|
}) |
|
97
|
|
|
98
|
describe("GET /admin/views/{entity}/configurations", () => { |
|
99
|
let systemView |
|
100
|
let personalView |
|
101
|
let otherUserView |
|
102
|
|
|
103
|
beforeEach(async () => { |
|
104
|
const container = getContainer() |
|
105
|
const settingsService = container.resolve("settings") |
|
106
|
|
|
107
|
// Create system default view |
|
108
|
systemView = await settingsService.createViewConfigurations({ |
|
109
|
entity: "orders", |
|
110
|
name: "System Default", |
|
111
|
is_system_default: true, |
|
112
|
user_id: null, |
|
113
|
configuration: { |
|
114
|
visible_columns: ["id", "display_id"], |
|
115
|
column_order: ["display_id", "id"], |
|
116
|
}, |
|
117
|
}) |
|
118
|
|
|
119
|
// Create personal view for non-admin user |
|
120
|
personalView = await settingsService.createViewConfigurations({ |
|
121
|
entity: "orders", |
|
122
|
name: "My Personal View", |
|
123
|
is_system_default: false, |
|
124
|
user_id: secondAdminUserId, |
|
125
|
configuration: { |
|
126
|
visible_columns: ["id", "total"], |
|
127
|
column_order: ["total", "id"], |
|
128
|
}, |
|
129
|
}) |
|
130
|
|
|
131
|
// Create view for another user |
|
132
|
otherUserView = await settingsService.createViewConfigurations({ |
|
133
|
entity: "orders", |
|
134
|
name: "Other User View", |
|
135
|
is_system_default: false, |
|
136
|
user_id: "other-user-id", |
|
137
|
configuration: { |
|
138
|
visible_columns: ["id"], |
|
139
|
column_order: ["id"], |
|
140
|
}, |
|
141
|
}) |
|
142
|
}) |
|
143
|
|
|
144
|
it("should list system defaults and personal views", async () => { |
|
145
|
const response = await api.get("/admin/views/orders/configurations", { |
|
146
|
headers: secondAdminHeader, |
|
147
|
}) |
|
148
|
|
|
149
|
expect(response.status).toBe(200) |
|
150
|
expect(response.data.view_configurations).toHaveLength(2) |
|
151
|
expect(response.data.view_configurations).toEqual( |
|
152
|
expect.arrayContaining([ |
|
153
|
expect.objectContaining({ id: systemView.id }), |
|
154
|
expect.objectContaining({ id: personalView.id }), |
|
155
|
]) |
|
156
|
) |
|
157
|
// Should not include other user's view |
|
158
|
expect(response.data.view_configurations).not.toEqual( |
|
159
|
expect.arrayContaining([ |
|
160
|
expect.objectContaining({ id: otherUserView.id }), |
|
161
|
]) |
|
162
|
) |
|
163
|
}) |
|
164
|
|
|
165
|
it("should filter by entity", async () => { |
|
166
|
const response = await api.get( |
|
167
|
"/admin/views/products/configurations", |
|
168
|
{ |
|
169
|
headers: secondAdminHeader, |
|
170
|
} |
|
171
|
) |
|
172
|
|
|
173
|
expect(response.status).toBe(200) |
|
174
|
expect(response.data.view_configurations).toHaveLength(0) |
|
175
|
}) |
|
176
|
}) |
|
177
|
|
|
178
|
describe("GET /admin/views/{entity}/configurations/:id", () => { |
|
179
|
let viewConfig |
|
180
|
|
|
181
|
beforeEach(async () => { |
|
182
|
const container = getContainer() |
|
183
|
const settingsService = container.resolve("settings") |
|
184
|
|
|
185
|
viewConfig = await settingsService.createViewConfigurations({ |
|
186
|
entity: "orders", |
|
187
|
name: "Test View", |
|
188
|
is_system_default: false, |
|
189
|
user_id: secondAdminUserId, |
|
190
|
configuration: { |
|
191
|
visible_columns: ["id"], |
|
192
|
column_order: ["id"], |
|
193
|
}, |
|
194
|
}) |
|
195
|
}) |
|
196
|
|
|
197
|
it("should retrieve own view configuration", async () => { |
|
198
|
const response = await api.get( |
|
199
|
`/admin/views/orders/configurations/${viewConfig.id}`, |
|
200
|
{ |
|
201
|
headers: secondAdminHeader, |
|
202
|
} |
|
203
|
) |
|
204
|
|
|
205
|
expect(response.status).toBe(200) |
|
206
|
expect(response.data.view_configuration).toMatchObject({ |
|
207
|
id: viewConfig.id, |
|
208
|
entity: "orders", |
|
209
|
name: "Test View", |
|
210
|
}) |
|
211
|
}) |
|
212
|
|
|
213
|
it("should prevent access to other user's view", async () => { |
|
214
|
const container = getContainer() |
|
215
|
const settingsService = container.resolve("settings") |
|
216
|
|
|
217
|
const otherView = await settingsService.createViewConfigurations({ |
|
218
|
entity: "orders", |
|
219
|
name: "Other View", |
|
220
|
is_system_default: false, |
|
221
|
user_id: "other-user-id", |
|
222
|
configuration: { |
|
223
|
visible_columns: ["id"], |
|
224
|
column_order: ["id"], |
|
225
|
}, |
|
226
|
}) |
|
227
|
|
|
228
|
const response = await api |
|
229
|
.get(`/admin/views/orders/configurations/${otherView.id}`, { |
|
230
|
headers: secondAdminHeader, |
|
231
|
}) |
|
232
|
.catch((e) => e.response) |
|
233
|
|
|
234
|
expect(response.status).toBe(400) |
|
235
|
}) |
|
236
|
}) |
|
237
|
|
|
238
|
describe("POST /admin/views/{entity}/configurations/:id", () => { |
|
239
|
let viewConfig |
|
240
|
|
|
241
|
beforeEach(async () => { |
|
242
|
const container = getContainer() |
|
243
|
const settingsService = container.resolve("settings") |
|
244
|
|
|
245
|
viewConfig = await settingsService.createViewConfigurations({ |
|
246
|
entity: "orders", |
|
247
|
name: "Test View", |
|
248
|
is_system_default: false, |
|
249
|
user_id: secondAdminUserId, |
|
250
|
configuration: { |
|
251
|
visible_columns: ["id"], |
|
252
|
column_order: ["id"], |
|
253
|
}, |
|
254
|
}) |
|
255
|
}) |
|
256
|
|
|
257
|
it("should update own view configuration", async () => { |
|
258
|
const payload = { |
|
259
|
name: "Updated View", |
|
260
|
configuration: { |
|
261
|
visible_columns: ["id", "total"], |
|
262
|
column_order: ["total", "id"], |
|
263
|
}, |
|
264
|
} |
|
265
|
|
|
266
|
const response = await api.post( |
|
267
|
`/admin/views/orders/configurations/${viewConfig.id}`, |
|
268
|
payload, |
|
269
|
{ |
|
270
|
headers: secondAdminHeader, |
|
271
|
} |
|
272
|
) |
|
273
|
|
|
274
|
expect(response.status).toBe(200) |
|
275
|
expect(response.data.view_configuration).toMatchObject({ |
|
276
|
id: viewConfig.id, |
|
277
|
name: "Updated View", |
|
278
|
configuration: payload.configuration, |
|
279
|
}) |
|
280
|
}) |
|
281
|
}) |
|
282
|
|
|
283
|
describe("DELETE /admin/views/{entity}/configurations/:id", () => { |
|
284
|
let viewConfig |
|
285
|
|
|
286
|
beforeEach(async () => { |
|
287
|
const container = getContainer() |
|
288
|
const settingsService = container.resolve("settings") |
|
289
|
|
|
290
|
viewConfig = await settingsService.createViewConfigurations({ |
|
291
|
entity: "orders", |
|
292
|
name: "Test View", |
|
293
|
is_system_default: false, |
|
294
|
user_id: secondAdminUserId, |
|
295
|
configuration: { |
|
296
|
visible_columns: ["id"], |
|
297
|
column_order: ["id"], |
|
298
|
}, |
|
299
|
}) |
|
300
|
}) |
|
301
|
|
|
302
|
it("should delete own view configuration", async () => { |
|
303
|
const response = await api.delete( |
|
304
|
`/admin/views/orders/configurations/${viewConfig.id}`, |
|
305
|
{ |
|
306
|
headers: secondAdminHeader, |
|
307
|
} |
|
308
|
) |
|
309
|
|
|
310
|
expect(response.status).toBe(200) |
|
311
|
expect(response.data).toMatchObject({ |
|
312
|
id: viewConfig.id, |
|
313
|
object: "view_configuration", |
|
314
|
deleted: true, |
|
315
|
}) |
|
316
|
|
|
317
|
// Verify it's deleted |
|
318
|
const getResponse = await api |
|
319
|
.get(`/admin/views/orders/configurations/${viewConfig.id}`, { |
|
320
|
headers: secondAdminHeader, |
|
321
|
}) |
|
322
|
.catch((e) => e.response) |
|
323
|
|
|
324
|
expect(getResponse.status).toBe(404) |
|
325
|
}) |
|
326
|
}) |
|
327
|
|
|
328
|
describe("GET /admin/views/{entity}/configurations/active", () => { |
|
329
|
beforeEach(async () => { |
|
330
|
const container = getContainer() |
|
331
|
const settingsService = container.resolve("settings") |
|
332
|
|
|
333
|
// Create and set active view |
|
334
|
const viewConfig = await settingsService.createViewConfigurations({ |
|
335
|
entity: "orders", |
|
336
|
name: "Active View", |
|
337
|
is_system_default: false, |
|
338
|
user_id: secondAdminUserId, |
|
339
|
configuration: { |
|
340
|
visible_columns: ["id", "total"], |
|
341
|
column_order: ["total", "id"], |
|
342
|
}, |
|
343
|
}) |
|
344
|
|
|
345
|
await settingsService.setActiveViewConfiguration( |
|
346
|
"orders", |
|
347
|
secondAdminUserId, |
|
348
|
viewConfig.id |
|
349
|
) |
|
350
|
}) |
|
351
|
|
|
352
|
it("should retrieve active view configuration", async () => { |
|
353
|
const response = await api.get( |
|
354
|
"/admin/views/orders/configurations/active", |
|
355
|
{ |
|
356
|
headers: secondAdminHeader, |
|
357
|
} |
|
358
|
) |
|
359
|
|
|
360
|
expect(response.status).toBe(200) |
|
361
|
expect(response.data.view_configuration).toMatchObject({ |
|
362
|
entity: "orders", |
|
363
|
name: "Active View", |
|
364
|
user_id: secondAdminUserId, |
|
365
|
}) |
|
366
|
}) |
|
367
|
|
|
368
|
it("should return null when no active view", async () => { |
|
369
|
const response = await api.get( |
|
370
|
"/admin/views/products/configurations/active", |
|
371
|
{ |
|
372
|
headers: secondAdminHeader, |
|
373
|
} |
|
374
|
) |
|
375
|
|
|
376
|
expect(response.status).toBe(200) |
|
377
|
expect(response.data.view_configuration).toBeNull() |
|
378
|
}) |
|
379
|
}) |
|
380
|
|
|
381
|
describe("POST /admin/views/{entity}/configurations/active", () => { |
|
382
|
let viewConfig |
|
383
|
|
|
384
|
beforeEach(async () => { |
|
385
|
const container = getContainer() |
|
386
|
const settingsService = container.resolve("settings") |
|
387
|
|
|
388
|
viewConfig = await settingsService.createViewConfigurations({ |
|
389
|
entity: "orders", |
|
390
|
name: "Test View", |
|
391
|
is_system_default: false, |
|
392
|
user_id: secondAdminUserId, |
|
393
|
configuration: { |
|
394
|
visible_columns: ["id"], |
|
395
|
column_order: ["id"], |
|
396
|
}, |
|
397
|
}) |
|
398
|
}) |
|
399
|
|
|
400
|
it("should set active view configuration", async () => { |
|
401
|
const response = await api.post( |
|
402
|
"/admin/views/orders/configurations/active", |
|
403
|
{ |
|
404
|
view_configuration_id: viewConfig.id, |
|
405
|
}, |
|
406
|
{ |
|
407
|
headers: secondAdminHeader, |
|
408
|
} |
|
409
|
) |
|
410
|
|
|
411
|
expect(response.status).toBe(200) |
|
412
|
expect(response.data.success).toBe(true) |
|
413
|
|
|
414
|
// Verify it's active |
|
415
|
const activeResponse = await api.get( |
|
416
|
"/admin/views/orders/configurations/active", |
|
417
|
{ |
|
418
|
headers: secondAdminHeader, |
|
419
|
} |
|
420
|
) |
|
421
|
|
|
422
|
expect(activeResponse.data.view_configuration.id).toBe(viewConfig.id) |
|
423
|
}) |
|
424
|
|
|
425
|
it("should clear active view and return to default when setting view_configuration_id to null", async () => { |
|
426
|
// First set an active view |
|
427
|
await api.post( |
|
428
|
"/admin/views/orders/configurations/active", |
|
429
|
{ |
|
430
|
view_configuration_id: viewConfig.id, |
|
431
|
}, |
|
432
|
{ |
|
433
|
headers: secondAdminHeader, |
|
434
|
} |
|
435
|
) |
|
436
|
|
|
437
|
// Verify it's active |
|
438
|
let activeResponse = await api.get( |
|
439
|
"/admin/views/orders/configurations/active", |
|
440
|
{ |
|
441
|
headers: secondAdminHeader, |
|
442
|
} |
|
443
|
) |
|
444
|
expect(activeResponse.data.view_configuration.id).toBe(viewConfig.id) |
|
445
|
|
|
446
|
// Now clear the active view |
|
447
|
const clearResponse = await api.post( |
|
448
|
"/admin/views/orders/configurations/active", |
|
449
|
{ |
|
450
|
view_configuration_id: null, |
|
451
|
}, |
|
452
|
{ |
|
453
|
headers: secondAdminHeader, |
|
454
|
} |
|
455
|
) |
|
456
|
|
|
457
|
expect(clearResponse.status).toBe(200) |
|
458
|
expect(clearResponse.data.success).toBe(true) |
|
459
|
|
|
460
|
// Verify the active view is cleared |
|
461
|
activeResponse = await api.get( |
|
462
|
"/admin/views/orders/configurations/active", |
|
463
|
{ |
|
464
|
headers: secondAdminHeader, |
|
465
|
} |
|
466
|
) |
|
467
|
|
|
468
|
// Debug output |
|
469
|
if (activeResponse.data.view_configuration) { |
|
470
|
console.log("Active view after clearing:", { |
|
471
|
id: activeResponse.data.view_configuration.id, |
|
472
|
name: activeResponse.data.view_configuration.name, |
|
473
|
is_system_default: |
|
474
|
activeResponse.data.view_configuration.is_system_default, |
|
475
|
}) |
|
476
|
} |
|
477
|
|
|
478
|
// Should either return null or a system default if one exists |
|
479
|
if (activeResponse.data.view_configuration) { |
|
480
|
expect( |
|
481
|
activeResponse.data.view_configuration.is_system_default |
|
482
|
).toBe(true) |
|
483
|
} else { |
|
484
|
expect(activeResponse.data.view_configuration).toBeNull() |
|
485
|
} |
|
486
|
expect(activeResponse.data.is_default_active).toBe(true) |
|
487
|
}) |
|
488
|
}) |
|
489
|
|
|
490
|
describe("System Default Views", () => { |
|
491
|
it("should make system default views available to all users", async () => { |
|
492
|
const container = getContainer() |
|
493
|
|
|
494
|
// Create a third admin user |
|
495
|
const thirdAdminHeaders = { headers: {} } |
|
496
|
const { user: thirdAdminUser } = await createAdminUser( |
|
497
|
dbConnection, |
|
498
|
thirdAdminHeaders, |
|
499
|
container, |
|
500
|
{ email: "admin3@test.com" } |
|
501
|
) |
|
502
|
|
|
503
|
// Admin 1 creates a system default view |
|
504
|
const systemDefaultView = await api.post( |
|
505
|
"/admin/views/orders/configurations", |
|
506
|
{ |
|
507
|
name: "System Default View", |
|
508
|
configuration: { |
|
509
|
visible_columns: ["id", "display_id", "created_at"], |
|
510
|
column_order: ["display_id", "id", "created_at"], |
|
511
|
}, |
|
512
|
is_system_default: true, |
|
513
|
}, |
|
514
|
adminHeaders |
|
515
|
) |
|
516
|
|
|
517
|
expect(systemDefaultView.status).toEqual(200) |
|
518
|
expect(systemDefaultView.data.view_configuration.user_id).toBeNull() |
|
519
|
|
|
520
|
// Admin 3 should be able to see this view |
|
521
|
const viewsForAdmin3 = await api.get( |
|
522
|
"/admin/views/orders/configurations", |
|
523
|
thirdAdminHeaders |
|
524
|
) |
|
525
|
|
|
526
|
expect(viewsForAdmin3.status).toEqual(200) |
|
527
|
const systemDefaults = viewsForAdmin3.data.view_configurations.filter( |
|
528
|
(v: any) => v.is_system_default |
|
529
|
) |
|
530
|
expect(systemDefaults).toHaveLength(1) |
|
531
|
expect(systemDefaults[0].name).toEqual("System Default View") |
|
532
|
|
|
533
|
// Admin 3 should also be able to retrieve it directly |
|
534
|
const directRetrieve = await api.get( |
|
535
|
`/admin/views/orders/configurations/${systemDefaultView.data.view_configuration.id}`, |
|
536
|
thirdAdminHeaders |
|
537
|
) |
|
538
|
|
|
539
|
expect(directRetrieve.status).toEqual(200) |
|
540
|
expect(directRetrieve.data.view_configuration.name).toEqual( |
|
541
|
"System Default View" |
|
542
|
) |
|
543
|
}) |
|
544
|
|
|
545
|
it("should allow creating system default without name", async () => { |
|
546
|
// Create a system default view without providing a name |
|
547
|
const systemDefaultView = await api.post( |
|
548
|
"/admin/views/customers/configurations", |
|
549
|
{ |
|
550
|
is_system_default: true, |
|
551
|
configuration: { |
|
552
|
visible_columns: ["id", "email", "first_name", "last_name"], |
|
553
|
column_order: ["email", "first_name", "last_name", "id"], |
|
554
|
}, |
|
555
|
// Note: no name field |
|
556
|
}, |
|
557
|
adminHeaders |
|
558
|
) |
|
559
|
|
|
560
|
expect(systemDefaultView.status).toEqual(200) |
|
561
|
expect(systemDefaultView.data.view_configuration.user_id).toBeNull() |
|
562
|
expect( |
|
563
|
systemDefaultView.data.view_configuration.is_system_default |
|
564
|
).toBe(true) |
|
565
|
// Name should be undefined/null when not provided |
|
566
|
expect(systemDefaultView.data.view_configuration.name).toBeFalsy() |
|
567
|
}) |
|
568
|
|
|
569
|
it("should set view as active when created with set_active flag", async () => { |
|
570
|
// Create a view with set_active = true |
|
571
|
const viewConfig = await api.post( |
|
572
|
"/admin/views/orders/configurations", |
|
573
|
{ |
|
574
|
name: "Auto-Active View", |
|
575
|
configuration: { |
|
576
|
visible_columns: ["id", "display_id", "status"], |
|
577
|
column_order: ["display_id", "status", "id"], |
|
578
|
}, |
|
579
|
set_active: true, |
|
580
|
}, |
|
581
|
{ headers: secondAdminHeader } |
|
582
|
) |
|
583
|
|
|
584
|
expect(viewConfig.status).toEqual(200) |
|
585
|
|
|
586
|
// Verify the view is now active |
|
587
|
const activeView = await api.get( |
|
588
|
"/admin/views/orders/configurations/active", |
|
589
|
{ headers: secondAdminHeader } |
|
590
|
) |
|
591
|
|
|
592
|
expect(activeView.status).toEqual(200) |
|
593
|
expect(activeView.data.view_configuration).toBeTruthy() |
|
594
|
expect(activeView.data.view_configuration.id).toEqual( |
|
595
|
viewConfig.data.view_configuration.id |
|
596
|
) |
|
597
|
expect(activeView.data.view_configuration.name).toEqual( |
|
598
|
"Auto-Active View" |
|
599
|
) |
|
600
|
}) |
|
601
|
|
|
602
|
it("should set view as active when updated with set_active flag", async () => { |
|
603
|
const container = getContainer() |
|
604
|
const settingsService = container.resolve("settings") |
|
605
|
|
|
606
|
// Create two views |
|
607
|
const view1 = await settingsService.createViewConfigurations({ |
|
608
|
entity: "orders", |
|
609
|
name: "View 1", |
|
610
|
is_system_default: false, |
|
611
|
user_id: secondAdminUserId, |
|
612
|
configuration: { |
|
613
|
visible_columns: ["id"], |
|
614
|
column_order: ["id"], |
|
615
|
}, |
|
616
|
}) |
|
617
|
|
|
618
|
const view2 = await settingsService.createViewConfigurations({ |
|
619
|
entity: "orders", |
|
620
|
name: "View 2", |
|
621
|
is_system_default: false, |
|
622
|
user_id: secondAdminUserId, |
|
623
|
configuration: { |
|
624
|
visible_columns: ["id", "total"], |
|
625
|
column_order: ["total", "id"], |
|
626
|
}, |
|
627
|
}) |
|
628
|
|
|
629
|
// Set view1 as active initially |
|
630
|
await settingsService.setActiveViewConfiguration( |
|
631
|
"orders", |
|
632
|
secondAdminUserId, |
|
633
|
view1.id |
|
634
|
) |
|
635
|
|
|
636
|
// Update view2 with set_active flag |
|
637
|
const updateResponse = await api.post( |
|
638
|
`/admin/views/orders/configurations/${view2.id}`, |
|
639
|
{ |
|
640
|
name: "Updated View 2", |
|
641
|
set_active: true, |
|
642
|
}, |
|
643
|
{ headers: secondAdminHeader } |
|
644
|
) |
|
645
|
|
|
646
|
expect(updateResponse.status).toEqual(200) |
|
647
|
|
|
648
|
// Verify view2 is now the active view |
|
649
|
const activeView = await api.get( |
|
650
|
"/admin/views/orders/configurations/active", |
|
651
|
{ headers: secondAdminHeader } |
|
652
|
) |
|
653
|
|
|
654
|
expect(activeView.status).toEqual(200) |
|
655
|
expect(activeView.data.view_configuration.id).toEqual(view2.id) |
|
656
|
expect(activeView.data.view_configuration.name).toEqual( |
|
657
|
"Updated View 2" |
|
658
|
) |
|
659
|
}) |
|
660
|
|
|
661
|
it("should allow resetting system default to code-level defaults", async () => { |
|
662
|
// Create a system default view |
|
663
|
const systemDefaultView = await api.post( |
|
664
|
"/admin/views/orders/configurations", |
|
665
|
{ |
|
666
|
name: "Custom System Default", |
|
667
|
is_system_default: true, |
|
668
|
configuration: { |
|
669
|
visible_columns: ["id", "status", "total"], |
|
670
|
column_order: ["status", "total", "id"], |
|
671
|
}, |
|
672
|
}, |
|
673
|
adminHeaders |
|
674
|
) |
|
675
|
|
|
676
|
expect(systemDefaultView.status).toEqual(200) |
|
677
|
const viewId = systemDefaultView.data.view_configuration.id |
|
678
|
|
|
679
|
// Verify it exists |
|
680
|
let viewsList = await api.get( |
|
681
|
"/admin/views/orders/configurations", |
|
682
|
adminHeaders |
|
683
|
) |
|
684
|
expect( |
|
685
|
viewsList.data.view_configurations.some((v: any) => v.id === viewId) |
|
686
|
).toBe(true) |
|
687
|
|
|
688
|
// Delete the system default view (reset to code defaults) |
|
689
|
const deleteResponse = await api.delete( |
|
690
|
`/admin/views/orders/configurations/${viewId}`, |
|
691
|
adminHeaders |
|
692
|
) |
|
693
|
|
|
694
|
expect(deleteResponse.status).toEqual(200) |
|
695
|
expect(deleteResponse.data.deleted).toBe(true) |
|
696
|
|
|
697
|
// Verify it's gone |
|
698
|
viewsList = await api.get( |
|
699
|
"/admin/views/orders/configurations", |
|
700
|
adminHeaders |
|
701
|
) |
|
702
|
expect( |
|
703
|
viewsList.data.view_configurations.some((v: any) => v.id === viewId) |
|
704
|
).toBe(false) |
|
705
|
|
|
706
|
// Getting active view should return null (falls back to code defaults) |
|
707
|
const activeView = await api.get( |
|
708
|
"/admin/views/orders/configurations/active", |
|
709
|
adminHeaders |
|
710
|
) |
|
711
|
expect(activeView.data.view_configuration).toBeNull() |
|
712
|
}) |
|
713
|
|
|
714
|
it("should return system default view when created and no user view is active", async () => { |
|
715
|
// Step 1: Create a system default view |
|
716
|
const systemDefaultView = await api.post( |
|
717
|
"/admin/views/orders/configurations", |
|
718
|
{ |
|
719
|
name: "System Default Orders", |
|
720
|
is_system_default: true, |
|
721
|
configuration: { |
|
722
|
visible_columns: [ |
|
723
|
"id", |
|
724
|
"display_id", |
|
725
|
"created_at", |
|
726
|
"customer", |
|
727
|
"total", |
|
728
|
], |
|
729
|
column_order: [ |
|
730
|
"display_id", |
|
731
|
"customer", |
|
732
|
"total", |
|
733
|
"created_at", |
|
734
|
"id", |
|
735
|
], |
|
736
|
filters: {}, |
|
737
|
sorting: { id: "created_at", desc: true }, |
|
738
|
search: "", |
|
739
|
}, |
|
740
|
}, |
|
741
|
adminHeaders |
|
742
|
) |
|
743
|
|
|
744
|
expect(systemDefaultView.status).toEqual(200) |
|
745
|
expect( |
|
746
|
systemDefaultView.data.view_configuration.is_system_default |
|
747
|
).toBe(true) |
|
748
|
|
|
749
|
// Step 2: Retrieve active view - should return the system default |
|
750
|
const activeView = await api.get( |
|
751
|
"/admin/views/orders/configurations/active", |
|
752
|
{ headers: secondAdminHeader } |
|
753
|
) |
|
754
|
|
|
755
|
expect(activeView.status).toEqual(200) |
|
756
|
expect(activeView.data.view_configuration).toBeTruthy() |
|
757
|
expect(activeView.data.view_configuration.id).toEqual( |
|
758
|
systemDefaultView.data.view_configuration.id |
|
759
|
) |
|
760
|
expect(activeView.data.view_configuration.name).toEqual( |
|
761
|
"System Default Orders" |
|
762
|
) |
|
763
|
expect(activeView.data.view_configuration.is_system_default).toBe( |
|
764
|
true |
|
765
|
) |
|
766
|
expect(activeView.data.is_default_active).toBe(true) |
|
767
|
expect(activeView.data.default_type).toEqual("system") |
|
768
|
}) |
|
769
|
}) |
|
770
|
|
|
771
|
describe("Filter, Sorting, and Search Persistence", () => { |
|
772
|
it("should save and restore filters, sorting, and search configuration", async () => { |
|
773
|
// Create a view with filters, sorting, and search |
|
774
|
const viewConfig = await api.post( |
|
775
|
"/admin/views/orders/configurations", |
|
776
|
{ |
|
777
|
name: "Filtered View", |
|
778
|
configuration: { |
|
779
|
visible_columns: ["id", "status", "total", "created_at"], |
|
780
|
column_order: ["status", "total", "created_at", "id"], |
|
781
|
filters: { |
|
782
|
status: ["pending", "completed"], |
|
783
|
total: { gte: 100 }, |
|
784
|
}, |
|
785
|
sorting: { id: "created_at", desc: true }, |
|
786
|
search: "test search", |
|
787
|
}, |
|
788
|
}, |
|
789
|
{ headers: secondAdminHeader } |
|
790
|
) |
|
791
|
|
|
792
|
expect(viewConfig.status).toEqual(200) |
|
793
|
expect( |
|
794
|
viewConfig.data.view_configuration.configuration.filters |
|
795
|
).toEqual({ |
|
796
|
status: ["pending", "completed"], |
|
797
|
total: { gte: 100 }, |
|
798
|
}) |
|
799
|
expect( |
|
800
|
viewConfig.data.view_configuration.configuration.sorting |
|
801
|
).toEqual({ |
|
802
|
id: "created_at", |
|
803
|
desc: true, |
|
804
|
}) |
|
805
|
expect( |
|
806
|
viewConfig.data.view_configuration.configuration.search |
|
807
|
).toEqual("test search") |
|
808
|
|
|
809
|
// Retrieve the view and verify filters are preserved |
|
810
|
const getResponse = await api.get( |
|
811
|
`/admin/views/orders/configurations/${viewConfig.data.view_configuration.id}`, |
|
812
|
{ headers: secondAdminHeader } |
|
813
|
) |
|
814
|
|
|
815
|
expect(getResponse.status).toEqual(200) |
|
816
|
expect( |
|
817
|
getResponse.data.view_configuration.configuration.filters |
|
818
|
).toEqual({ |
|
819
|
status: ["pending", "completed"], |
|
820
|
total: { gte: 100 }, |
|
821
|
}) |
|
822
|
}) |
|
823
|
|
|
824
|
it("should remove filters when updating a view without filters", async () => { |
|
825
|
// Create a view with filters |
|
826
|
const viewConfig = await api.post( |
|
827
|
"/admin/views/orders/configurations", |
|
828
|
{ |
|
829
|
name: "View with Filters", |
|
830
|
configuration: { |
|
831
|
visible_columns: ["id", "status", "total"], |
|
832
|
column_order: ["status", "total", "id"], |
|
833
|
filters: { |
|
834
|
status: ["pending", "completed"], |
|
835
|
total: { gte: 100 }, |
|
836
|
}, |
|
837
|
sorting: { id: "total", desc: true }, |
|
838
|
search: "initial search", |
|
839
|
}, |
|
840
|
}, |
|
841
|
{ headers: secondAdminHeader } |
|
842
|
) |
|
843
|
|
|
844
|
expect(viewConfig.status).toEqual(200) |
|
845
|
const viewId = viewConfig.data.view_configuration.id |
|
846
|
|
|
847
|
// Update the view to remove filters |
|
848
|
const updateResponse = await api.post( |
|
849
|
`/admin/views/orders/configurations/${viewId}`, |
|
850
|
{ |
|
851
|
configuration: { |
|
852
|
visible_columns: ["id", "status", "total"], |
|
853
|
column_order: ["status", "total", "id"], |
|
854
|
filters: {}, // Empty filters object |
|
855
|
sorting: null, // Remove sorting |
|
856
|
search: "", // Clear search |
|
857
|
}, |
|
858
|
}, |
|
859
|
{ headers: secondAdminHeader } |
|
860
|
) |
|
861
|
|
|
862
|
expect(updateResponse.status).toEqual(200) |
|
863
|
|
|
864
|
// Verify filters were removed |
|
865
|
expect( |
|
866
|
updateResponse.data.view_configuration.configuration.filters |
|
867
|
).toEqual({}) |
|
868
|
expect( |
|
869
|
updateResponse.data.view_configuration.configuration.sorting |
|
870
|
).toBeNull() |
|
871
|
expect( |
|
872
|
updateResponse.data.view_configuration.configuration.search |
|
873
|
).toEqual("") |
|
874
|
|
|
875
|
// Retrieve again to double-check persistence |
|
876
|
const getResponse = await api.get( |
|
877
|
`/admin/views/orders/configurations/${viewId}`, |
|
878
|
{ headers: secondAdminHeader } |
|
879
|
) |
|
880
|
|
|
881
|
expect(getResponse.status).toEqual(200) |
|
882
|
expect( |
|
883
|
getResponse.data.view_configuration.configuration.filters |
|
884
|
).toEqual({}) |
|
885
|
expect( |
|
886
|
getResponse.data.view_configuration.configuration.sorting |
|
887
|
).toBeNull() |
|
888
|
expect( |
|
889
|
getResponse.data.view_configuration.configuration.search |
|
890
|
).toEqual("") |
|
891
|
}) |
|
892
|
|
|
893
|
it("should update only specific filters while keeping others", async () => { |
|
894
|
// Create a view with multiple filters |
|
895
|
const viewConfig = await api.post( |
|
896
|
"/admin/views/orders/configurations", |
|
897
|
{ |
|
898
|
name: "Multi-Filter View", |
|
899
|
configuration: { |
|
900
|
visible_columns: ["id", "status", "total", "created_at"], |
|
901
|
column_order: ["status", "total", "created_at", "id"], |
|
902
|
filters: { |
|
903
|
status: ["pending", "completed"], |
|
904
|
total: { gte: 100, lte: 1000 }, |
|
905
|
created_at: { gte: "2024-01-01" }, |
|
906
|
}, |
|
907
|
sorting: { id: "created_at", desc: true }, |
|
908
|
search: "customer", |
|
909
|
}, |
|
910
|
}, |
|
911
|
{ headers: secondAdminHeader } |
|
912
|
) |
|
913
|
|
|
914
|
expect(viewConfig.status).toEqual(200) |
|
915
|
const viewId = viewConfig.data.view_configuration.id |
|
916
|
|
|
917
|
// Update to remove only the 'total' filter |
|
918
|
const updateResponse = await api.post( |
|
919
|
`/admin/views/orders/configurations/${viewId}`, |
|
920
|
{ |
|
921
|
configuration: { |
|
922
|
visible_columns: ["id", "status", "total", "created_at"], |
|
923
|
column_order: ["status", "total", "created_at", "id"], |
|
924
|
filters: { |
|
925
|
status: ["pending", "completed"], |
|
926
|
created_at: { gte: "2024-01-01" }, |
|
927
|
// 'total' filter removed |
|
928
|
}, |
|
929
|
sorting: { id: "created_at", desc: true }, |
|
930
|
search: "customer", |
|
931
|
}, |
|
932
|
}, |
|
933
|
{ headers: secondAdminHeader } |
|
934
|
) |
|
935
|
|
|
936
|
expect(updateResponse.status).toEqual(200) |
|
937
|
expect( |
|
938
|
updateResponse.data.view_configuration.configuration.filters |
|
939
|
).toEqual({ |
|
940
|
status: ["pending", "completed"], |
|
941
|
created_at: { gte: "2024-01-01" }, |
|
942
|
}) |
|
943
|
expect( |
|
944
|
updateResponse.data.view_configuration.configuration.filters.total |
|
945
|
).toBeUndefined() |
|
946
|
}) |
|
947
|
}) |
|
948
|
}) |
|
949
|
}, |
|
950
|
}) |