mongodb-dynamic-api 2.3.10 → 2.3.11

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/package.json +1 -1
  3. package/src/decorators/api-endpoint-visibility.decorator.js +2 -2
  4. package/src/decorators/api-endpoint-visibility.decorator.js.map +1 -1
  5. package/src/routes/delete-many/base-delete-many.service.js +17 -12
  6. package/src/routes/delete-many/base-delete-many.service.js.map +1 -1
  7. package/src/routes/delete-one/base-delete-one.service.js +17 -12
  8. package/src/routes/delete-one/base-delete-one.service.js.map +1 -1
  9. package/src/routes/duplicate-many/base-duplicate-many.service.js +1 -0
  10. package/src/routes/duplicate-many/base-duplicate-many.service.js.map +1 -1
  11. package/src/routes/duplicate-one/base-duplicate-one.service.js +1 -0
  12. package/src/routes/duplicate-one/base-duplicate-one.service.js.map +1 -1
  13. package/src/routes/get-one/base-get-one.service.js +17 -12
  14. package/src/routes/get-one/base-get-one.service.js.map +1 -1
  15. package/src/routes/replace-one/base-replace-one.service.js +1 -0
  16. package/src/routes/replace-one/base-replace-one.service.js.map +1 -1
  17. package/src/routes/update-many/base-update-many.service.js +1 -0
  18. package/src/routes/update-many/base-update-many.service.js.map +1 -1
  19. package/src/routes/update-one/base-update-one.service.js +1 -0
  20. package/src/routes/update-one/base-update-one.service.js.map +1 -1
  21. package/src/services/base/base.service.d.ts +2 -1
  22. package/src/services/base/base.service.js +14 -2
  23. package/src/services/base/base.service.js.map +1 -1
  24. package/src/version.json +1 -1
  25. package/test/dynamic-api-for-feature.e2e-spec.d.ts +1 -0
  26. package/test/dynamic-api-for-feature.e2e-spec.js +680 -0
  27. package/test/dynamic-api-for-feature.e2e-spec.js.map +1 -0
  28. package/test/dynamic-api-for-root.e2e-spec.js +2 -1
  29. package/test/dynamic-api-for-root.e2e-spec.js.map +1 -1
  30. package/test/e2e.setup.d.ts +38 -7
  31. package/test/e2e.setup.js +78 -36
  32. package/test/e2e.setup.js.map +1 -1
  33. package/test/utils.d.ts +28 -0
  34. package/test/utils.js +13 -0
  35. package/test/utils.js.map +1 -0
  36. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,680 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const mongoose_1 = require("@nestjs/mongoose");
13
+ const testing_1 = require("@nestjs/testing");
14
+ const mongoose_2 = require("mongoose");
15
+ const src_1 = require("../src");
16
+ const e2e_setup_1 = require("./e2e.setup");
17
+ require("dotenv/config");
18
+ const utils_1 = require("./utils");
19
+ describe('DynamicApiModule forFeature (e2e)', () => {
20
+ const uri = process.env.MONGO_DB_URL;
21
+ const initApp = async (forFeatureOptions, forRootOptions = {}, initFixtures, initMainCb) => {
22
+ const moduleRef = await testing_1.Test.createTestingModule({
23
+ imports: [
24
+ src_1.DynamicApiModule.forRoot(uri, forRootOptions),
25
+ src_1.DynamicApiModule.forFeature(forFeatureOptions),
26
+ ],
27
+ }).compile();
28
+ await (0, e2e_setup_1.createTestingApp)(moduleRef, initFixtures, initMainCb);
29
+ };
30
+ afterEach(async () => {
31
+ await (0, e2e_setup_1.closeTestingApp)(mongoose_2.default.connections);
32
+ });
33
+ describe('Entity extends BaseEntity', () => {
34
+ let TestEntity = class TestEntity extends src_1.BaseEntity {
35
+ };
36
+ __decorate([
37
+ (0, mongoose_1.Prop)({ type: String, required: true }),
38
+ __metadata("design:type", String)
39
+ ], TestEntity.prototype, "name", void 0);
40
+ __decorate([
41
+ (0, mongoose_1.Prop)({ type: String }),
42
+ __metadata("design:type", String)
43
+ ], TestEntity.prototype, "group", void 0);
44
+ TestEntity = __decorate([
45
+ (0, src_1.DynamicAPISchemaOptions)({
46
+ indexes: [{ fields: { name: 1 }, options: { unique: true } }],
47
+ }),
48
+ (0, mongoose_1.Schema)({ collection: 'test-entities' })
49
+ ], TestEntity);
50
+ beforeEach(async () => {
51
+ const fixtures = async (connection) => {
52
+ const model = await (0, utils_1.getModelFromEntity)(TestEntity);
53
+ await model.insertMany([
54
+ { name: 'test1' },
55
+ { name: 'test2' },
56
+ ]);
57
+ };
58
+ await initApp({
59
+ entity: TestEntity,
60
+ controllerOptions: {
61
+ path: 'test-entities',
62
+ },
63
+ }, {}, fixtures);
64
+ });
65
+ describe('GET /test-entities', () => {
66
+ it('should return all test entities', async () => {
67
+ const { body, status } = await e2e_setup_1.server.get('/test-entities');
68
+ expect(status).toBe(200);
69
+ expect(body).toEqual([
70
+ {
71
+ id: expect.any(String),
72
+ name: 'test1',
73
+ createdAt: expect.any(String),
74
+ updatedAt: expect.any(String),
75
+ },
76
+ {
77
+ id: expect.any(String),
78
+ name: 'test2',
79
+ createdAt: expect.any(String),
80
+ updatedAt: expect.any(String),
81
+ },
82
+ ]);
83
+ });
84
+ });
85
+ describe('GET /test-entities/:id', () => {
86
+ let entities;
87
+ beforeEach(async () => {
88
+ const { body } = await e2e_setup_1.server.get('/test-entities');
89
+ entities = body;
90
+ });
91
+ it('should return a test entity by id', async () => {
92
+ const { body, status } = await e2e_setup_1.server.get(`/test-entities/${entities[0].id}`);
93
+ expect(status).toBe(200);
94
+ expect(body).toEqual(entities[0]);
95
+ });
96
+ it('should return 404 if entity not found', async () => {
97
+ const { body, status } = await e2e_setup_1.server.get(`/test-entities/123`);
98
+ expect(status).toBe(404);
99
+ expect(body).toHaveProperty('error', 'Not Found');
100
+ expect(body).toHaveProperty('statusCode', 404);
101
+ expect(body).toHaveProperty('message', 'TestEntity not found');
102
+ });
103
+ });
104
+ describe('POST /test-entities/many', () => {
105
+ it('should create many test entities', async () => {
106
+ const { body, status } = await e2e_setup_1.server.post('/test-entities/many', {
107
+ list: [
108
+ { name: 'test3' },
109
+ { name: 'test4' },
110
+ ],
111
+ });
112
+ expect(status).toBe(201);
113
+ expect(body).toEqual([
114
+ {
115
+ id: expect.any(String),
116
+ name: 'test3',
117
+ createdAt: expect.any(String),
118
+ updatedAt: expect.any(String),
119
+ },
120
+ {
121
+ id: expect.any(String),
122
+ name: 'test4',
123
+ createdAt: expect.any(String),
124
+ updatedAt: expect.any(String),
125
+ },
126
+ ]);
127
+ });
128
+ it('should return 409 if entity has duplicated key', async () => {
129
+ const { body, status } = await e2e_setup_1.server.post('/test-entities/many', {
130
+ list: [
131
+ { name: 'test1' },
132
+ { name: 'test2' },
133
+ ],
134
+ });
135
+ expect(status).toBe(409);
136
+ expect(body).toHaveProperty('error', 'Conflict');
137
+ expect(body).toHaveProperty('statusCode', 409);
138
+ expect(body).toHaveProperty('message', 'name \'test1\' is already used');
139
+ });
140
+ });
141
+ describe('POST /test-entities', () => {
142
+ it('should create a test entity', async () => {
143
+ const { body, status } = await e2e_setup_1.server.post('/test-entities', { name: 'test3' });
144
+ expect(status).toBe(201);
145
+ expect(body).toEqual({
146
+ id: expect.any(String),
147
+ name: 'test3',
148
+ createdAt: expect.any(String),
149
+ updatedAt: expect.any(String),
150
+ });
151
+ });
152
+ it('should return 409 if entity has duplicated key', async () => {
153
+ const { body, status } = await e2e_setup_1.server.post('/test-entities', { name: 'test1' });
154
+ expect(status).toBe(409);
155
+ expect(body).toHaveProperty('error', 'Conflict');
156
+ expect(body).toHaveProperty('statusCode', 409);
157
+ expect(body).toHaveProperty('message', 'name \'test1\' is already used');
158
+ });
159
+ });
160
+ describe('PUT /test-entities/:id', () => {
161
+ let entityToReplace;
162
+ beforeEach(async () => {
163
+ const { body } = await e2e_setup_1.server.get('/test-entities');
164
+ entityToReplace = body.pop();
165
+ });
166
+ it('should replace a test entity by id', async () => {
167
+ const { body, status } = await e2e_setup_1.server.put(`/test-entities/${entityToReplace.id}`, { name: 'test-replaced' });
168
+ expect(status).toBe(200);
169
+ expect(body).toEqual({
170
+ id: expect.any(String),
171
+ name: 'test-replaced',
172
+ createdAt: expect.any(String),
173
+ updatedAt: expect.any(String),
174
+ });
175
+ });
176
+ it('should return 404 if entity not found', async () => {
177
+ const { body, status } = await e2e_setup_1.server.put(`/test-entities/123`, { name: 'test1-updated' });
178
+ expect(status).toBe(404);
179
+ expect(body).toHaveProperty('error', 'Not Found');
180
+ expect(body).toHaveProperty('statusCode', 404);
181
+ expect(body).toHaveProperty('message', 'TestEntity not found');
182
+ });
183
+ });
184
+ describe('PATCH /test-entities', () => {
185
+ let entities;
186
+ beforeEach(async () => {
187
+ const { body } = await e2e_setup_1.server.get('/test-entities');
188
+ entities = body;
189
+ });
190
+ it('should update many test entities', async () => {
191
+ const { body, status } = await e2e_setup_1.server.patch('/test-entities', {
192
+ group: 'many-patched',
193
+ }, { query: { ids: entities.map(({ id }) => id) } });
194
+ expect(status).toBe(200);
195
+ expect(body).toEqual([
196
+ {
197
+ ...entities[0],
198
+ group: 'many-patched',
199
+ updatedAt: expect.any(String),
200
+ },
201
+ {
202
+ ...entities[1],
203
+ group: 'many-patched',
204
+ updatedAt: expect.any(String),
205
+ },
206
+ ]);
207
+ });
208
+ it('should return 404 if entity not found', async () => {
209
+ const { body, status } = await e2e_setup_1.server.patch('/test-entities', {
210
+ group: 'many-patched',
211
+ }, { query: { ids: ['123'] } });
212
+ expect(status).toBe(404);
213
+ expect(body).toHaveProperty('error', 'Not Found');
214
+ expect(body).toHaveProperty('statusCode', 404);
215
+ expect(body).toHaveProperty('message', 'TestEntity not found');
216
+ });
217
+ });
218
+ describe('PATCH /test-entities/:id', () => {
219
+ let entityToUpdate;
220
+ beforeEach(async () => {
221
+ const { body } = await e2e_setup_1.server.get('/test-entities');
222
+ entityToUpdate = body.pop();
223
+ });
224
+ it('should update a test entity by id', async () => {
225
+ const { body, status } = await e2e_setup_1.server.patch(`/test-entities/${entityToUpdate.id}`, { name: 'test-updated' });
226
+ expect(status).toBe(200);
227
+ expect(body).toEqual({
228
+ id: expect.any(String),
229
+ name: 'test-updated',
230
+ createdAt: expect.any(String),
231
+ updatedAt: expect.any(String),
232
+ });
233
+ });
234
+ it('should return 404 if entity not found', async () => {
235
+ const { body, status } = await e2e_setup_1.server.patch(`/test-entities/123`, { name: 'test1-updated' });
236
+ expect(status).toBe(404);
237
+ expect(body).toHaveProperty('error', 'Not Found');
238
+ expect(body).toHaveProperty('statusCode', 404);
239
+ expect(body).toHaveProperty('message', 'TestEntity not found');
240
+ });
241
+ });
242
+ describe('DELETE /test-entities', () => {
243
+ let entities;
244
+ beforeEach(async () => {
245
+ const { body } = await e2e_setup_1.server.get('/test-entities');
246
+ entities = body;
247
+ });
248
+ it('should delete many test entities', async () => {
249
+ const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: entities.map(({ id }) => id) } });
250
+ expect(status).toBe(200);
251
+ expect(body).toEqual({ deletedCount: 2 });
252
+ const model = await (0, utils_1.getModelFromEntity)(TestEntity);
253
+ const updatedList = await model.find().lean().exec();
254
+ expect(updatedList).toEqual([]);
255
+ });
256
+ it('should return 200 with 0 for deletedCount if one of entities is not found', async () => {
257
+ const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: [entities[0].id, '123'] } });
258
+ expect(status).toBe(200);
259
+ expect(body).toEqual({ deletedCount: 0 });
260
+ });
261
+ });
262
+ describe('DELETE /test-entities/:id', () => {
263
+ let entityToDelete;
264
+ beforeEach(async () => {
265
+ const { body } = await e2e_setup_1.server.get('/test-entities');
266
+ entityToDelete = body.pop();
267
+ });
268
+ it('should delete a test entity by id', async () => {
269
+ const { body, status } = await e2e_setup_1.server.delete(`/test-entities/${entityToDelete.id}`);
270
+ expect(status).toBe(200);
271
+ expect(body).toEqual({ deletedCount: 1 });
272
+ });
273
+ it('should return 200 with deletedCount at 0 if entity not found', async () => {
274
+ const { body, status } = await e2e_setup_1.server.delete(`/test-entities/123`);
275
+ expect(status).toBe(200);
276
+ expect(body).toEqual({ deletedCount: 0 });
277
+ });
278
+ });
279
+ describe('POST /test-entities/duplicate', () => {
280
+ let entities;
281
+ beforeEach(async () => {
282
+ const { body } = await e2e_setup_1.server.get('/test-entities');
283
+ entities = body;
284
+ });
285
+ it('should duplicate test entities', async () => {
286
+ const { body, status } = await e2e_setup_1.server.post('/test-entities/duplicate', {
287
+ name: 'test1-duplicated',
288
+ group: 'duplicated',
289
+ }, { query: { ids: [entities[0].id] } });
290
+ expect(status).toBe(201);
291
+ expect(body).toEqual([
292
+ {
293
+ ...entities[0],
294
+ id: expect.any(String),
295
+ name: 'test1-duplicated',
296
+ group: 'duplicated',
297
+ createdAt: expect.any(String),
298
+ updatedAt: expect.any(String),
299
+ },
300
+ ]);
301
+ });
302
+ it('should return 404 if one of entities is not found', async () => {
303
+ const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate`, {
304
+ group: 'duplicated',
305
+ }, { query: { ids: [entities[0].id, '123'] } });
306
+ expect(status).toBe(404);
307
+ expect(body).toHaveProperty('error', 'Not Found');
308
+ expect(body).toHaveProperty('statusCode', 404);
309
+ expect(body).toHaveProperty('message', 'TestEntity not found');
310
+ });
311
+ });
312
+ describe('POST /test-entities/duplicate/:id', () => {
313
+ let entityToDuplicate;
314
+ beforeEach(async () => {
315
+ const { body } = await e2e_setup_1.server.get('/test-entities');
316
+ entityToDuplicate = body.pop();
317
+ });
318
+ it('should duplicate a test entity by id', async () => {
319
+ const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/${entityToDuplicate.id}`, {
320
+ name: 'test2-duplicated',
321
+ });
322
+ expect(status).toBe(201);
323
+ expect(body).toEqual({
324
+ ...entityToDuplicate,
325
+ id: expect.any(String),
326
+ name: 'test2-duplicated',
327
+ createdAt: expect.any(String),
328
+ updatedAt: expect.any(String),
329
+ });
330
+ });
331
+ it('should return 404 if entity not found', async () => {
332
+ const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/123`, {
333
+ name: 'test1-duplicated',
334
+ group: 'duplicated',
335
+ });
336
+ expect(status).toBe(404);
337
+ expect(body).toHaveProperty('error', 'Not Found');
338
+ expect(body).toHaveProperty('statusCode', 404);
339
+ expect(body).toHaveProperty('message', 'TestEntity not found');
340
+ });
341
+ });
342
+ });
343
+ describe('Entity extends SoftDeletableEntity', () => {
344
+ let TestDeletableEntity = class TestDeletableEntity extends src_1.SoftDeletableEntity {
345
+ };
346
+ __decorate([
347
+ (0, mongoose_1.Prop)({ type: String, required: true }),
348
+ __metadata("design:type", String)
349
+ ], TestDeletableEntity.prototype, "name", void 0);
350
+ __decorate([
351
+ (0, mongoose_1.Prop)({ type: String }),
352
+ __metadata("design:type", String)
353
+ ], TestDeletableEntity.prototype, "group", void 0);
354
+ TestDeletableEntity = __decorate([
355
+ (0, src_1.DynamicAPISchemaOptions)({
356
+ indexes: [{ fields: { name: 1, deletedAt: 1 }, options: { unique: true } }],
357
+ }),
358
+ (0, mongoose_1.Schema)({ collection: 'test-entities' })
359
+ ], TestDeletableEntity);
360
+ beforeEach(async () => {
361
+ const fixtures = async (connection) => {
362
+ const model = await (0, utils_1.getModelFromEntity)(TestDeletableEntity);
363
+ await model.insertMany([
364
+ { name: 'test1' },
365
+ { name: 'test2' },
366
+ ]);
367
+ };
368
+ await initApp({
369
+ entity: TestDeletableEntity,
370
+ controllerOptions: {
371
+ path: 'test-entities',
372
+ },
373
+ }, {}, fixtures);
374
+ });
375
+ describe('GET /test-entities', () => {
376
+ it('should return all test entities', async () => {
377
+ const { body, status } = await e2e_setup_1.server.get('/test-entities');
378
+ expect(status).toBe(200);
379
+ expect(body).toEqual([
380
+ {
381
+ id: expect.any(String),
382
+ name: 'test1',
383
+ createdAt: expect.any(String),
384
+ updatedAt: expect.any(String),
385
+ deletedAt: null,
386
+ },
387
+ {
388
+ id: expect.any(String),
389
+ name: 'test2',
390
+ createdAt: expect.any(String),
391
+ updatedAt: expect.any(String),
392
+ deletedAt: null,
393
+ },
394
+ ]);
395
+ });
396
+ });
397
+ describe('GET /test-entities/:id', () => {
398
+ let entities;
399
+ beforeEach(async () => {
400
+ const { body } = await e2e_setup_1.server.get('/test-entities');
401
+ entities = body;
402
+ });
403
+ it('should return a test entity by id', async () => {
404
+ const { body, status } = await e2e_setup_1.server.get(`/test-entities/${entities[0].id}`);
405
+ expect(status).toBe(200);
406
+ expect(body).toEqual(entities[0]);
407
+ });
408
+ it('should return 404 if entity not found', async () => {
409
+ const { body, status } = await e2e_setup_1.server.get(`/test-entities/123`);
410
+ expect(status).toBe(404);
411
+ expect(body).toHaveProperty('error', 'Not Found');
412
+ expect(body).toHaveProperty('statusCode', 404);
413
+ expect(body).toHaveProperty('message', 'TestDeletableEntity not found');
414
+ });
415
+ });
416
+ describe('POST /test-entities/many', () => {
417
+ it('should create many test entities', async () => {
418
+ const { body, status } = await e2e_setup_1.server.post('/test-entities/many', {
419
+ list: [
420
+ { name: 'test3' },
421
+ { name: 'test4' },
422
+ ],
423
+ });
424
+ expect(status).toBe(201);
425
+ expect(body).toEqual([
426
+ {
427
+ id: expect.any(String),
428
+ name: 'test3',
429
+ createdAt: expect.any(String),
430
+ updatedAt: expect.any(String),
431
+ deletedAt: null,
432
+ },
433
+ {
434
+ id: expect.any(String),
435
+ name: 'test4',
436
+ createdAt: expect.any(String),
437
+ updatedAt: expect.any(String),
438
+ deletedAt: null,
439
+ },
440
+ ]);
441
+ });
442
+ it('should return 409 if entity has duplicated key', async () => {
443
+ const { body, status } = await e2e_setup_1.server.post('/test-entities/many', {
444
+ list: [
445
+ { name: 'test1' },
446
+ { name: 'test2' },
447
+ ],
448
+ });
449
+ expect(status).toBe(409);
450
+ expect(body).toHaveProperty('error', 'Conflict');
451
+ expect(body).toHaveProperty('statusCode', 409);
452
+ expect(body).toHaveProperty('message', 'name \'test1\' is already used');
453
+ });
454
+ });
455
+ describe('POST /test-entities', () => {
456
+ it('should create a test entity', async () => {
457
+ const { body, status } = await e2e_setup_1.server.post('/test-entities', { name: 'test3' });
458
+ expect(status).toBe(201);
459
+ expect(body).toEqual({
460
+ id: expect.any(String),
461
+ name: 'test3',
462
+ createdAt: expect.any(String),
463
+ updatedAt: expect.any(String),
464
+ deletedAt: null,
465
+ });
466
+ });
467
+ it('should return 409 if entity has duplicated key', async () => {
468
+ const { body, status } = await e2e_setup_1.server.post('/test-entities', { name: 'test1' });
469
+ expect(status).toBe(409);
470
+ expect(body).toHaveProperty('error', 'Conflict');
471
+ expect(body).toHaveProperty('statusCode', 409);
472
+ expect(body).toHaveProperty('message', 'name \'test1\' is already used');
473
+ });
474
+ });
475
+ describe('PUT /test-entities/:id', () => {
476
+ let entityToReplace;
477
+ beforeEach(async () => {
478
+ const { body } = await e2e_setup_1.server.get('/test-entities');
479
+ entityToReplace = body.pop();
480
+ });
481
+ it('should replace a test entity by id', async () => {
482
+ const { body, status } = await e2e_setup_1.server.put(`/test-entities/${entityToReplace.id}`, { name: 'test-replaced' });
483
+ expect(status).toBe(200);
484
+ expect(body).toEqual({
485
+ id: expect.any(String),
486
+ name: 'test-replaced',
487
+ createdAt: expect.any(String),
488
+ updatedAt: expect.any(String),
489
+ deletedAt: null,
490
+ });
491
+ });
492
+ it('should return 404 if entity not found', async () => {
493
+ const { body, status } = await e2e_setup_1.server.put(`/test-entities/123`, { name: 'test1-updated' });
494
+ expect(status).toBe(404);
495
+ expect(body).toHaveProperty('error', 'Not Found');
496
+ expect(body).toHaveProperty('statusCode', 404);
497
+ expect(body).toHaveProperty('message', 'TestDeletableEntity not found');
498
+ });
499
+ });
500
+ describe('PATCH /test-entities', () => {
501
+ let entities;
502
+ beforeEach(async () => {
503
+ const { body } = await e2e_setup_1.server.get('/test-entities');
504
+ entities = body;
505
+ });
506
+ it('should update many test entities', async () => {
507
+ const { body, status } = await e2e_setup_1.server.patch('/test-entities', {
508
+ group: 'many-patched',
509
+ }, { query: { ids: entities.map(({ id }) => id) } });
510
+ expect(status).toBe(200);
511
+ expect(body).toEqual([
512
+ {
513
+ ...entities[0],
514
+ group: 'many-patched',
515
+ updatedAt: expect.any(String),
516
+ },
517
+ {
518
+ ...entities[1],
519
+ group: 'many-patched',
520
+ updatedAt: expect.any(String),
521
+ },
522
+ ]);
523
+ });
524
+ it('should return 404 if entity not found', async () => {
525
+ const { body, status } = await e2e_setup_1.server.patch('/test-entities', {
526
+ group: 'many-patched',
527
+ }, { query: { ids: ['123'] } });
528
+ expect(status).toBe(404);
529
+ expect(body).toHaveProperty('error', 'Not Found');
530
+ expect(body).toHaveProperty('statusCode', 404);
531
+ expect(body).toHaveProperty('message', 'TestDeletableEntity not found');
532
+ });
533
+ });
534
+ describe('PATCH /test-entities/:id', () => {
535
+ let entityToUpdate;
536
+ beforeEach(async () => {
537
+ const { body } = await e2e_setup_1.server.get('/test-entities');
538
+ entityToUpdate = body.pop();
539
+ });
540
+ it('should update a test entity by id', async () => {
541
+ const { body, status } = await e2e_setup_1.server.patch(`/test-entities/${entityToUpdate.id}`, { name: 'test-updated' });
542
+ expect(status).toBe(200);
543
+ expect(body).toEqual({
544
+ id: expect.any(String),
545
+ name: 'test-updated',
546
+ createdAt: expect.any(String),
547
+ updatedAt: expect.any(String),
548
+ deletedAt: null,
549
+ });
550
+ });
551
+ it('should return 404 if entity not found', async () => {
552
+ const { body, status } = await e2e_setup_1.server.patch(`/test-entities/123`, { name: 'test1-updated' });
553
+ expect(status).toBe(404);
554
+ expect(body).toHaveProperty('error', 'Not Found');
555
+ expect(body).toHaveProperty('statusCode', 404);
556
+ expect(body).toHaveProperty('message', 'TestDeletableEntity not found');
557
+ });
558
+ });
559
+ describe('DELETE /test-entities', () => {
560
+ let entities;
561
+ beforeEach(async () => {
562
+ const { body } = await e2e_setup_1.server.get('/test-entities');
563
+ entities = body;
564
+ });
565
+ it('should delete many test entities', async () => {
566
+ const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: entities.map(({ id }) => id) } });
567
+ expect(status).toBe(200);
568
+ expect(body).toEqual({ deletedCount: 2 });
569
+ const model = await (0, utils_1.getModelFromEntity)(TestDeletableEntity);
570
+ const updatedList = await model.find().lean().exec();
571
+ expect(updatedList).toEqual([
572
+ {
573
+ _id: expect.any(Object),
574
+ __v: expect.any(Number),
575
+ name: 'test1',
576
+ isDeleted: true,
577
+ createdAt: expect.any(Date),
578
+ updatedAt: expect.any(Date),
579
+ deletedAt: expect.any(Date),
580
+ },
581
+ {
582
+ _id: expect.any(Object),
583
+ __v: expect.any(Number),
584
+ name: 'test2',
585
+ isDeleted: true,
586
+ createdAt: expect.any(Date),
587
+ updatedAt: expect.any(Date),
588
+ deletedAt: expect.any(Date),
589
+ },
590
+ ]);
591
+ });
592
+ it('should return 200 with 0 for deletedCount if one of entities is not found', async () => {
593
+ const { body, status } = await e2e_setup_1.server.delete('/test-entities', { query: { ids: [entities[0].id, '123'] } });
594
+ expect(status).toBe(200);
595
+ expect(body).toEqual({ deletedCount: 0 });
596
+ });
597
+ });
598
+ describe('DELETE /test-entities/:id', () => {
599
+ let entityToDelete;
600
+ beforeEach(async () => {
601
+ const { body } = await e2e_setup_1.server.get('/test-entities');
602
+ entityToDelete = body.pop();
603
+ });
604
+ it('should delete a test entity by id', async () => {
605
+ const { body, status } = await e2e_setup_1.server.delete(`/test-entities/${entityToDelete.id}`);
606
+ expect(status).toBe(200);
607
+ expect(body).toEqual({ deletedCount: 1 });
608
+ });
609
+ it('should return 200 with deletedCount at 0 if entity not found', async () => {
610
+ const { body, status } = await e2e_setup_1.server.delete(`/test-entities/123`);
611
+ expect(status).toBe(200);
612
+ expect(body).toEqual({ deletedCount: 0 });
613
+ });
614
+ });
615
+ describe('POST /test-entities/duplicate', () => {
616
+ let entities;
617
+ beforeEach(async () => {
618
+ const { body } = await e2e_setup_1.server.get('/test-entities');
619
+ entities = body;
620
+ });
621
+ it('should duplicate test entities', async () => {
622
+ const { body, status } = await e2e_setup_1.server.post('/test-entities/duplicate', {
623
+ name: 'test1-duplicated',
624
+ group: 'duplicated',
625
+ }, { query: { ids: [entities[0].id] } });
626
+ expect(status).toBe(201);
627
+ expect(body).toEqual([
628
+ {
629
+ ...entities[0],
630
+ id: expect.any(String),
631
+ name: 'test1-duplicated',
632
+ group: 'duplicated',
633
+ createdAt: expect.any(String),
634
+ updatedAt: expect.any(String),
635
+ },
636
+ ]);
637
+ });
638
+ it('should return 404 if one of entities is not found', async () => {
639
+ const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate`, {
640
+ group: 'duplicated',
641
+ }, { query: { ids: [entities[0].id, '123'] } });
642
+ expect(status).toBe(404);
643
+ expect(body).toHaveProperty('error', 'Not Found');
644
+ expect(body).toHaveProperty('statusCode', 404);
645
+ expect(body).toHaveProperty('message', 'TestDeletableEntity not found');
646
+ });
647
+ });
648
+ describe('POST /test-entities/duplicate/:id', () => {
649
+ let entityToDuplicate;
650
+ beforeEach(async () => {
651
+ const { body } = await e2e_setup_1.server.get('/test-entities');
652
+ entityToDuplicate = body.pop();
653
+ });
654
+ it('should duplicate a test entity by id', async () => {
655
+ const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/${entityToDuplicate.id}`, {
656
+ name: 'test2-duplicated',
657
+ });
658
+ expect(status).toBe(201);
659
+ expect(body).toEqual({
660
+ ...entityToDuplicate,
661
+ id: expect.any(String),
662
+ name: 'test2-duplicated',
663
+ createdAt: expect.any(String),
664
+ updatedAt: expect.any(String),
665
+ });
666
+ });
667
+ it('should return 404 if entity not found', async () => {
668
+ const { body, status } = await e2e_setup_1.server.post(`/test-entities/duplicate/123`, {
669
+ name: 'test1-duplicated',
670
+ group: 'duplicated',
671
+ });
672
+ expect(status).toBe(404);
673
+ expect(body).toHaveProperty('error', 'Not Found');
674
+ expect(body).toHaveProperty('statusCode', 404);
675
+ expect(body).toHaveProperty('message', 'TestDeletableEntity not found');
676
+ });
677
+ });
678
+ });
679
+ });
680
+ //# sourceMappingURL=dynamic-api-for-feature.e2e-spec.js.map