@mikemajesty/microservice-crud 4.5.13 → 4.5.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikemajesty/microservice-crud",
3
- "version": "4.5.13",
3
+ "version": "4.5.14",
4
4
  "description": "Monorepo CLI",
5
5
  "main": "src/cli.js",
6
6
  "scripts": {
@@ -7,19 +7,13 @@ const getCoreUsecaseCreateTest = (name) => `import { Test } from '@nestjs/testin
7
7
 
8
8
  import { ILoggerAdapter } from '@/infra/logger';
9
9
  import { I${capitalizeFirstLetter(name)}CreateAdapter } from '@/modules/${name}/adapter';
10
- import { ApiInternalServerException } from '@/utils/exception';
11
10
  import { expectZodError, getMockUUID } from '@/utils/tests';
12
11
 
13
12
  import { ${capitalizeFirstLetter(name)}Entity } from '../../entity/${name}';
14
13
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
15
14
  import { ${capitalizeFirstLetter(name)}CreateInput, ${capitalizeFirstLetter(name)}CreateOutput, ${capitalizeFirstLetter(name)}CreateUsecase } from '../${name}-create';
16
15
 
17
- const successInput: ${capitalizeFirstLetter(name)}CreateInput = {
18
- name: 'name'
19
- };
20
- const failureInput: ${capitalizeFirstLetter(name)}CreateInput = {};
21
-
22
- describe('${capitalizeFirstLetter(name)}CreateUsecase', () => {
16
+ describe(${capitalizeFirstLetter(name)}CreateUsecase.name, () => {
23
17
  let usecase: I${capitalizeFirstLetter(name)}CreateAdapter;
24
18
  let repository: I${capitalizeFirstLetter(name)}Repository;
25
19
 
@@ -38,8 +32,8 @@ describe('${capitalizeFirstLetter(name)}CreateUsecase', () => {
38
32
  },
39
33
  {
40
34
  provide: I${capitalizeFirstLetter(name)}CreateAdapter,
41
- useFactory: (birdRepository: I${capitalizeFirstLetter(name)}Repository, logger: ILoggerAdapter) => {
42
- return new ${capitalizeFirstLetter(name)}CreateUsecase(birdRepository, logger);
35
+ useFactory: (${name}Repository: I${capitalizeFirstLetter(name)}Repository, logger: ILoggerAdapter) => {
36
+ return new ${capitalizeFirstLetter(name)}CreateUsecase(${name}Repository, logger);
43
37
  },
44
38
  inject: [I${capitalizeFirstLetter(name)}Repository, ILoggerAdapter]
45
39
  }
@@ -52,34 +46,24 @@ describe('${capitalizeFirstLetter(name)}CreateUsecase', () => {
52
46
 
53
47
  test('when no input is specified, should expect an error', async () => {
54
48
  await expectZodError(
55
- () => usecase.execute(failureInput),
49
+ () => usecase.execute({}),
56
50
  (issues) => {
57
51
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('name') }]);
58
52
  }
59
53
  );
60
54
  });
61
55
 
62
- test('when ${name} created successfully, should expect a ${name} that has been created', async () => {
56
+ const input: ${capitalizeFirstLetter(name)}CreateInput = {
57
+ name: 'name'
58
+ };
59
+
60
+ test('when ${name} created successfully, should expect a ${name}', async () => {
63
61
  const createOutput: ${capitalizeFirstLetter(name)}CreateOutput = { created: true, id: getMockUUID() };
64
62
 
65
63
  repository.findOne = jest.fn().mockResolvedValue(null);
66
64
  repository.create = jest.fn().mockResolvedValue(createOutput);
67
- repository.startSession = jest.fn().mockResolvedValue({
68
- commitTransaction: jest.fn()
69
- });
70
-
71
- await expect(usecase.execute(successInput)).resolves.toEqual(createOutput);
72
- });
73
-
74
- test('when transaction throw an error, should expect an error', async () => {
75
- repository.findOne = jest.fn().mockResolvedValue(null);
76
- repository.create = jest.fn().mockRejectedValue(new ApiInternalServerException());
77
- repository.startSession = jest.fn().mockResolvedValue({
78
- commitTransaction: jest.fn(),
79
- abortTransaction: jest.fn()
80
- });
81
65
 
82
- await expect(usecase.execute(successInput)).rejects.toThrow(ApiInternalServerException);
66
+ await expect(usecase.execute(input)).resolves.toEqual(createOutput);
83
67
  });
84
68
  });
85
69
  `
@@ -13,13 +13,7 @@ import { ${capitalizeFirstLetter(name)}Entity } from '../../entity/${name}';
13
13
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
14
14
  import { ${capitalizeFirstLetter(name)}DeleteInput, ${capitalizeFirstLetter(name)}DeleteOutput, ${capitalizeFirstLetter(name)}DeleteUsecase } from '../${name}-delete';
15
15
 
16
- const successInput: ${capitalizeFirstLetter(name)}DeleteInput = {
17
- id: getMockUUID()
18
- };
19
-
20
- const failureInput: ${capitalizeFirstLetter(name)}DeleteInput = {};
21
-
22
- describe('${capitalizeFirstLetter(name)}DeleteUsecase', () => {
16
+ describe(${capitalizeFirstLetter(name)}DeleteUsecase.name, () => {
23
17
  let usecase: I${capitalizeFirstLetter(name)}DeleteAdapter;
24
18
  let repository: I${capitalizeFirstLetter(name)}Repository;
25
19
 
@@ -47,20 +41,24 @@ describe('${capitalizeFirstLetter(name)}DeleteUsecase', () => {
47
41
 
48
42
  test('when no input is specified, should expect an error', async () => {
49
43
  await expectZodError(
50
- () => usecase.execute(failureInput),
44
+ () => usecase.execute({}),
51
45
  (issues) => {
52
46
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('id') }]);
53
47
  }
54
48
  );
55
49
  });
56
50
 
51
+ const input: ${capitalizeFirstLetter(name)}DeleteInput = {
52
+ id: getMockUUID()
53
+ };
54
+
57
55
  test('when ${name} not found, should expect an error', async () => {
58
56
  repository.findById = jest.fn().mockResolvedValue(null);
59
57
 
60
- await expect(usecase.execute(successInput)).rejects.toThrowError(ApiNotFoundException);
58
+ await expect(usecase.execute(input)).rejects.toThrow(ApiNotFoundException);
61
59
  });
62
60
 
63
- test('when ${name} deleted successfully, should expect a ${name} that has been deleted', async () => {
61
+ test('when ${name} deleted successfully, should expect a ${name}', async () => {
64
62
  const findByIdOutput: ${capitalizeFirstLetter(name)}DeleteOutput = new ${capitalizeFirstLetter(name)}Entity({
65
63
  id: getMockUUID(),
66
64
  name: 'dummy'
@@ -69,7 +67,7 @@ describe('${capitalizeFirstLetter(name)}DeleteUsecase', () => {
69
67
  repository.findById = jest.fn().mockResolvedValue(findByIdOutput);
70
68
  repository.updateOne = jest.fn();
71
69
 
72
- await expect(usecase.execute(successInput)).resolves.toEqual({
70
+ await expect(usecase.execute(input)).resolves.toEqual({
73
71
  ...findByIdOutput,
74
72
  deletedAt: expect.any(Date)
75
73
  });
@@ -13,13 +13,7 @@ import { ${capitalizeFirstLetter(name)}Entity } from '../../entity/${name}';
13
13
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
14
14
  import { ${capitalizeFirstLetter(name)}GetByIdInput, ${capitalizeFirstLetter(name)}GetByIdOutput, ${capitalizeFirstLetter(name)}GetByIdUsecase } from '../${name}-get-by-id';
15
15
 
16
- const successInput: ${capitalizeFirstLetter(name)}GetByIdInput = {
17
- id: getMockUUID()
18
- };
19
-
20
- const failureInput: ${capitalizeFirstLetter(name)}GetByIdInput = {};
21
-
22
- describe('${capitalizeFirstLetter(name)}GetByIdUsecase', () => {
16
+ describe(${capitalizeFirstLetter(name)}GetByIdUsecase.name, () => {
23
17
  let usecase: I${capitalizeFirstLetter(name)}GetByIdAdapter;
24
18
  let repository: I${capitalizeFirstLetter(name)}Repository;
25
19
 
@@ -47,20 +41,24 @@ describe('${capitalizeFirstLetter(name)}GetByIdUsecase', () => {
47
41
 
48
42
  test('when no input is specified, should expect an error', async () => {
49
43
  await expectZodError(
50
- () => usecase.execute(failureInput),
44
+ () => usecase.execute({}),
51
45
  (issues) => {
52
46
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('id') }]);
53
47
  }
54
48
  );
55
49
  });
56
50
 
51
+ const input: ${capitalizeFirstLetter(name)}GetByIdInput = {
52
+ id: getMockUUID()
53
+ };
54
+
57
55
  test('when ${name} not found, should expect an error', async () => {
58
56
  repository.findById = jest.fn().mockResolvedValue(null);
59
57
 
60
- await expect(usecase.execute(successInput)).rejects.toThrowError(ApiNotFoundException);
58
+ await expect(usecase.execute(input)).rejects.toThrow(ApiNotFoundException);
61
59
  });
62
60
 
63
- test('when ${name} found, should expect a ${name} that has been found', async () => {
61
+ test('when ${name} found, should expect a ${name}', async () => {
64
62
  const findByIdOutput: ${capitalizeFirstLetter(name)}GetByIdOutput = new ${capitalizeFirstLetter(name)}Entity({
65
63
  id: '61cc35f3-03d9-4b7f-9c63-59f32b013ef5',
66
64
  name: 'dummy'
@@ -68,7 +66,7 @@ describe('${capitalizeFirstLetter(name)}GetByIdUsecase', () => {
68
66
 
69
67
  repository.findById = jest.fn().mockResolvedValue(findByIdOutput);
70
68
 
71
- await expect(usecase.execute(successInput)).resolves.toEqual(findByIdOutput);
69
+ await expect(usecase.execute(input)).resolves.toEqual(findByIdOutput);
72
70
  });
73
71
  });
74
72
  `
@@ -12,11 +12,7 @@ import { ${capitalizeFirstLetter(name)}Entity } from '../../entity/${name}';
12
12
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
13
13
  import { ${capitalizeFirstLetter(name)}ListInput, ${capitalizeFirstLetter(name)}ListOutput, ${capitalizeFirstLetter(name)}ListUsecase } from '../${name}-list';
14
14
 
15
- const successInput: ${capitalizeFirstLetter(name)}ListInput = { limit: 1, page: 1, search: {}, sort: { createdAt: -1 } };
16
-
17
- const failureInput: ${capitalizeFirstLetter(name)}ListInput = { search: null, sort: null, limit: 10, page: 1 };
18
-
19
- describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
15
+ describe(${capitalizeFirstLetter(name)}ListUsecase.name, () => {
20
16
  let usecase: I${capitalizeFirstLetter(name)}ListAdapter;
21
17
  let repository: I${capitalizeFirstLetter(name)}Repository;
22
18
 
@@ -44,13 +40,15 @@ describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
44
40
 
45
41
  test('when sort input is specified, should expect an error', async () => {
46
42
  await expectZodError(
47
- () => usecase.execute(failureInput),
43
+ () => usecase.execute({ search: null, sort: null, limit: 10, page: 1 }),
48
44
  (issues) => {
49
45
  expect(issues).toEqual([{ message: 'Expected object, received null', path: 'sort' }]);
50
46
  }
51
47
  );
52
48
  });
53
49
 
50
+ const input: ${capitalizeFirstLetter(name)}ListInput = { limit: 1, page: 1, search: {}, sort: { createdAt: -1 } };
51
+
54
52
  test('when ${name} are found, should expect an ${name} list', async () => {
55
53
  const doc = new ${capitalizeFirstLetter(name)}Entity({
56
54
  id: getMockUUID(),
@@ -63,7 +61,7 @@ describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
63
61
 
64
62
  repository.paginate = jest.fn().mockResolvedValue(paginateOutput);
65
63
 
66
- await expect(usecase.execute(successInput)).resolves.toEqual({
64
+ await expect(usecase.execute(input)).resolves.toEqual({
67
65
  docs: [doc],
68
66
  page: 1,
69
67
  limit: 1,
@@ -76,7 +74,7 @@ describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
76
74
 
77
75
  repository.paginate = jest.fn().mockResolvedValue(paginateOutput);
78
76
 
79
- await expect(usecase.execute(successInput)).resolves.toEqual(paginateOutput);
77
+ await expect(usecase.execute(input)).resolves.toEqual(paginateOutput);
80
78
  });
81
79
  });
82
80
  `
@@ -12,16 +12,9 @@ import { expectZodError, getMockUUID } from '@/utils/tests';
12
12
 
13
13
  import { ${capitalizeFirstLetter(name)}Entity } from '../../entity/${name}';
14
14
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
15
- import { ${capitalizeFirstLetter(name)}UpdateInput, ${capitalizeFirstLetter(name)}UpdateOutput } from '../${name}-update';
16
- import { ${capitalizeFirstLetter(name)}UpdateUsecase } from '../${name}-update';
15
+ import { ${capitalizeFirstLetter(name)}UpdateInput, ${capitalizeFirstLetter(name)}UpdateOutput, ${capitalizeFirstLetter(name)}UpdateUsecase } from '../${name}-update';
17
16
 
18
- const successInput: ${capitalizeFirstLetter(name)}UpdateInput = {
19
- id: getMockUUID()
20
- };
21
-
22
- const failureInput: ${capitalizeFirstLetter(name)}UpdateInput = {};
23
-
24
- describe('${capitalizeFirstLetter(name)}UpdateUsecase', () => {
17
+ describe(${capitalizeFirstLetter(name)}UpdateUsecase.name, () => {
25
18
  let usecase: I${capitalizeFirstLetter(name)}UpdateAdapter;
26
19
  let repository: I${capitalizeFirstLetter(name)}Repository;
27
20
 
@@ -49,20 +42,24 @@ describe('${capitalizeFirstLetter(name)}UpdateUsecase', () => {
49
42
 
50
43
  test('when no input is specified, should expect an error', async () => {
51
44
  await expectZodError(
52
- () => usecase.execute(failureInput),
45
+ () => usecase.execute({}),
53
46
  (issues) => {
54
47
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('id') }]);
55
48
  }
56
49
  );
57
50
  });
58
51
 
52
+ const input: ${capitalizeFirstLetter(name)}UpdateInput = {
53
+ id: getMockUUID()
54
+ };
55
+
59
56
  test('when ${name} not found, should expect an error', async () => {
60
57
  repository.findById = jest.fn().mockResolvedValue(null);
61
58
 
62
- await expect(usecase.execute(successInput)).rejects.toThrowError(ApiNotFoundException);
59
+ await expect(usecase.execute(input)).rejects.toThrow(ApiNotFoundException);
63
60
  });
64
61
 
65
- test('when ${name} updated successfully, should expect an ${name} that has been updated', async () => {
62
+ test('when ${name} updated successfully, should expect an ${name}', async () => {
66
63
  const findByIdOutput: ${capitalizeFirstLetter(name)}UpdateOutput = new ${capitalizeFirstLetter(name)}Entity({
67
64
  id: getMockUUID(),
68
65
  name: 'dummy'
@@ -71,7 +68,7 @@ describe('${capitalizeFirstLetter(name)}UpdateUsecase', () => {
71
68
  repository.findById = jest.fn().mockResolvedValue(findByIdOutput);
72
69
  repository.updateOne = jest.fn().mockResolvedValue(null);
73
70
 
74
- await expect(usecase.execute(successInput)).resolves.toEqual(findByIdOutput);
71
+ await expect(usecase.execute(input)).resolves.toEqual(findByIdOutput);
75
72
  });
76
73
  });
77
74
  `
@@ -5,9 +5,9 @@ function capitalizeFirstLetter(string) {
5
5
 
6
6
  const getCoreUsecaseCreate = (name) => `import { z } from 'zod';
7
7
 
8
- import { ValidateSchema } from '@/utils/decorators';
9
8
  import { ILoggerAdapter } from '@/infra/logger';
10
9
  import { CreatedModel } from '@/infra/repository';
10
+ import { ValidateSchema } from '@/utils/decorators';
11
11
  import { IUsecase } from '@/utils/usecase';
12
12
 
13
13
  import { ${capitalizeFirstLetter(name)}Entity, ${capitalizeFirstLetter(name)}EntitySchema } from '../entity/${name}';
@@ -21,25 +21,19 @@ export type ${capitalizeFirstLetter(name)}CreateInput = z.infer<typeof ${capital
21
21
  export type ${capitalizeFirstLetter(name)}CreateOutput = CreatedModel;
22
22
 
23
23
  export class ${capitalizeFirstLetter(name)}CreateUsecase implements IUsecase {
24
- constructor(private readonly ${name}Repository: I${capitalizeFirstLetter(name)}Repository, private readonly loggerService: ILoggerAdapter) {}
24
+ constructor(
25
+ private readonly ${name}Repository: I${capitalizeFirstLetter(name)}Repository,
26
+ private readonly loggerService: ILoggerAdapter
27
+ ) {}
25
28
 
26
29
  @ValidateSchema(${capitalizeFirstLetter(name)}CreateSchema)
27
30
  async execute(input: ${capitalizeFirstLetter(name)}CreateInput): Promise<${capitalizeFirstLetter(name)}CreateOutput> {
28
31
  const entity = new ${capitalizeFirstLetter(name)}Entity(input);
29
32
 
30
- const session = await this.${name}Repository.startSession();
31
-
32
- try {
33
- const ${name} = await this.${name}Repository.create(entity, { session });
34
-
35
- await session.commitTransaction();
33
+ const ${name} = await this.${name}Repository.create(entity);
36
34
 
37
- this.loggerService.info({ message: '${name} created.', obj: { ${name} } });
38
- return ${name};
39
- } catch (error) {
40
- await session.abortTransaction();
41
- throw error;
42
- }
35
+ this.loggerService.info({ message: '${name} created.', obj: { ${name} } });
36
+ return ${name};
43
37
  }
44
38
  }
45
39
  `
@@ -27,7 +27,7 @@ export class ${capitalizeFirstLetter(name)}DeleteUsecase implements IUsecase {
27
27
  const model = await this.${name}Repository.findById(id);
28
28
 
29
29
  if (!model) {
30
- throw new ApiNotFoundException();
30
+ throw new ApiNotFoundException('${name}NotFound');
31
31
  }
32
32
 
33
33
  const ${name} = new ${capitalizeFirstLetter(name)}Entity(model);
@@ -26,7 +26,7 @@ export class ${capitalizeFirstLetter(name)}GetByIdUsecase implements IUsecase {
26
26
  const ${name} = await this.${name}Repository.findById(id);
27
27
 
28
28
  if (!${name}) {
29
- throw new ApiNotFoundException();
29
+ throw new ApiNotFoundException('${name}NotFound');
30
30
  }
31
31
 
32
32
  const entity = new ${capitalizeFirstLetter(name)}Entity(${name});
@@ -28,7 +28,7 @@ export class ${capitalizeFirstLetter(name)}UpdateUsecase implements IUsecase {
28
28
  const ${name} = await this.${name}Repository.findById(input.id);
29
29
 
30
30
  if (!${name}) {
31
- throw new ApiNotFoundException();
31
+ throw new ApiNotFoundException('${name}NotFound');
32
32
  }
33
33
 
34
34
  const ${name}Finded = new ${capitalizeFirstLetter(name)}Entity(${name});
@@ -14,28 +14,30 @@ import { ${capitalizeFirstLetter(name)}UpdateOutput } from '@/core/${name}/use-c
14
14
  import { Swagger } from '@/utils/docs/swagger';
15
15
 
16
16
  const input = new ${capitalizeFirstLetter(name)}Entity({
17
- name: '<name>'
17
+ name: 'name'
18
18
  });
19
19
 
20
20
  const output = new ${capitalizeFirstLetter(name)}Entity({ ...input, updatedAt: new Date(), createdAt: new Date(), deletedAt: null });
21
21
 
22
+ const BASE_URL = 'api/v1/${pluralize(name)}';
23
+
22
24
  export const SwaggerResponse = {
23
25
  create: {
24
26
  200: Swagger.defaultResponseJSON({
25
27
  status: 200,
26
- json: { created: true, id: '<uuid>' } as ${capitalizeFirstLetter(name)}CreateOutput,
27
- description: '${name} created.'
28
+ json: { created: true, id: 'uuid' } as ${capitalizeFirstLetter(name)}CreateOutput,
29
+ description: 'create ${name}.'
28
30
  })
29
31
  },
30
32
  update: {
31
33
  200: Swagger.defaultResponseJSON({
32
34
  status: 200,
33
35
  json: output as ${capitalizeFirstLetter(name)}UpdateOutput,
34
- description: '${name} updated.'
36
+ description: 'update ${name}.'
35
37
  }),
36
38
  404: Swagger.defaultResponseError({
37
39
  status: 404,
38
- route: 'api/v1/${pluralize(name)}',
40
+ route: BASE_URL.concat('/:id'),
39
41
  message: '${name}NotFound',
40
42
  description: '${name} not found.'
41
43
  })
@@ -44,11 +46,11 @@ export const SwaggerResponse = {
44
46
  200: Swagger.defaultResponseJSON({
45
47
  status: 200,
46
48
  json: output as ${capitalizeFirstLetter(name)}GetByIdOutput,
47
- description: '${name} found.'
49
+ description: 'get ${name}.'
48
50
  }),
49
51
  404: Swagger.defaultResponseError({
50
52
  status: 404,
51
- route: 'api/v1/${pluralize(name)}/:id',
53
+ route: BASE_URL.concat('/:id'),
52
54
  message: '${name}NotFound',
53
55
  description: '${name} not found.'
54
56
  })
@@ -57,11 +59,11 @@ export const SwaggerResponse = {
57
59
  200: Swagger.defaultResponseJSON({
58
60
  status: 200,
59
61
  json: output as ${capitalizeFirstLetter(name)}DeleteOutput,
60
- description: '${name} found.'
62
+ description: 'delete ${name}.'
61
63
  }),
62
64
  404: Swagger.defaultResponseError({
63
65
  status: 404,
64
- route: 'api/v1/${pluralize(name)}/:id',
66
+ route: BASE_URL.concat('/:id'),
65
67
  message: '${name}NotFound',
66
68
  description: '${name} not found.'
67
69
  })
@@ -70,30 +72,15 @@ export const SwaggerResponse = {
70
72
  200: Swagger.defaultResponseJSON({
71
73
  status: 200,
72
74
  json: { docs: [output], page: 1, limit: 1, total: 1 } as ${capitalizeFirstLetter(name)}ListOutput,
73
- description: '${name} created.'
75
+ description: 'list ${pluralize(name)}.'
74
76
  })
75
77
  }
76
78
  };
77
79
 
78
80
  export const SwaggerRequest = {
79
81
  createBody: Swagger.defaultRequestJSON({ ...input, id: undefined } as ${capitalizeFirstLetter(name)}Entity),
80
- updateBody: Swagger.defaultRequestJSON({ ...input, id: '<id>' } as ${capitalizeFirstLetter(name)}Entity),
81
- listQuery: {
82
- pagination: {
83
- limit: Swagger.defaultApiQueryOptions({ example: 10, name: 'limit', required: false }),
84
- page: Swagger.defaultApiQueryOptions({ example: 1, name: 'page', required: false })
85
- },
86
- sort: Swagger.defaultApiQueryOptions({
87
- name: 'sort',
88
- required: false,
89
- description: '<b>createdAt:desc,name:asc'
90
- }),
91
- search: Swagger.defaultApiQueryOptions({
92
- name: 'search',
93
- required: false,
94
- description: '<b>name:miau'
95
- })
96
- }
82
+ updateBody: Swagger.defaultRequestJSON(input as ${capitalizeFirstLetter(name)}Entity),
83
+ listQuery: Swagger.defaultRequestListJSON()
97
84
  };
98
85
  `
99
86
 
@@ -13,13 +13,7 @@ import { ${capitalizeFirstLetter(name)}Entity } from '../../entity/${name}';
13
13
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
14
14
  import { ${capitalizeFirstLetter(name)}CreateInput, ${capitalizeFirstLetter(name)}CreateOutput, ${capitalizeFirstLetter(name)}CreateUsecase } from '../${name}-create';
15
15
 
16
- const successInput: ${capitalizeFirstLetter(name)}CreateInput = {
17
- name: 'dummy'
18
- };
19
-
20
- const failureInput: ${capitalizeFirstLetter(name)}CreateInput = {};
21
-
22
- describe('${capitalizeFirstLetter(name)}CreateUsecase', () => {
16
+ describe(${capitalizeFirstLetter(name)}CreateUsecase.name, () => {
23
17
  let usecase: I${capitalizeFirstLetter(name)}CreateAdapter;
24
18
  let repository: I${capitalizeFirstLetter(name)}Repository;
25
19
 
@@ -52,19 +46,23 @@ describe('${capitalizeFirstLetter(name)}CreateUsecase', () => {
52
46
 
53
47
  test('when no input is specified, should expect an error', async () => {
54
48
  await expectZodError(
55
- () => usecase.execute(failureInput),
49
+ () => usecase.execute({}),
56
50
  (issues) => {
57
51
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('name') }]);
58
52
  }
59
53
  );
60
54
  });
61
55
 
62
- test('when ${name} created successfully, should expect a ${name} that has been created', async () => {
56
+ const input: ${capitalizeFirstLetter(name)}CreateInput = {
57
+ name: 'dummy'
58
+ };
59
+
60
+ test('when ${name} created successfully, should expect a ${name}', async () => {
63
61
  const createOutput: ${capitalizeFirstLetter(name)}CreateOutput = { created: true, id: getMockUUID() };
64
62
 
65
63
  repository.create = jest.fn().mockResolvedValue(createOutput);
66
-
67
- await expect(usecase.execute(successInput)).resolves.toEqual(createOutput);
64
+
65
+ await expect(usecase.execute(input)).resolves.toEqual(createOutput);
68
66
  });
69
67
  });
70
68
  `
@@ -13,13 +13,7 @@ import { expectZodError, getMockUUID } from '@/utils/tests';
13
13
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
14
14
  import { ${capitalizeFirstLetter(name)}Entity } from './../../entity/${name}';
15
15
 
16
- const successInput: ${capitalizeFirstLetter(name)}DeleteInput = {
17
- id: getMockUUID()
18
- };
19
-
20
- const failureInput: ${capitalizeFirstLetter(name)}DeleteInput = {};
21
-
22
- describe('${capitalizeFirstLetter(name)}DeleteUsecase', () => {
16
+ describe(${capitalizeFirstLetter(name)}DeleteUsecase.name, () => {
23
17
  let usecase: I${capitalizeFirstLetter(name)}DeleteAdapter;
24
18
  let repository: I${capitalizeFirstLetter(name)}Repository;
25
19
 
@@ -46,20 +40,24 @@ describe('${capitalizeFirstLetter(name)}DeleteUsecase', () => {
46
40
 
47
41
  test('when no input is specified, should expect an error', async () => {
48
42
  await expectZodError(
49
- () => usecase.execute(failureInput),
43
+ () => usecase.execute({}),
50
44
  (issues) => {
51
45
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('id') }]);
52
46
  }
53
47
  );
54
48
  });
55
49
 
50
+ const input: ${capitalizeFirstLetter(name)}DeleteInput = {
51
+ id: getMockUUID()
52
+ };
53
+
56
54
  test('when ${name} not found, should expect an error', async () => {
57
55
  repository.findById = jest.fn().mockResolvedValue(null);
58
56
 
59
- await expect(usecase.execute(successInput)).rejects.toThrowError(ApiNotFoundException);
57
+ await expect(usecase.execute(input)).rejects.toThrow(ApiNotFoundException);
60
58
  });
61
59
 
62
- test('when ${name} deleted successfully, should expect a ${name} that has been deleted', async () => {
60
+ test('when ${name} deleted successfully, should expect a ${name}', async () => {
63
61
  const findByIdOutput: ${capitalizeFirstLetter(name)}DeleteOutput = new ${capitalizeFirstLetter(name)}Entity({
64
62
  id: getMockUUID(),
65
63
  name: 'dummy'
@@ -68,7 +66,7 @@ describe('${capitalizeFirstLetter(name)}DeleteUsecase', () => {
68
66
  repository.findById = jest.fn().mockResolvedValue(findByIdOutput);
69
67
  repository.updateOne = jest.fn();
70
68
 
71
- await expect(usecase.execute(successInput)).resolves.toEqual({
69
+ await expect(usecase.execute(input)).resolves.toEqual({
72
70
  ...findByIdOutput,
73
71
  deletedAt: expect.any(Date)
74
72
  });
@@ -13,13 +13,7 @@ import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${na
13
13
  import { ${capitalizeFirstLetter(name)}GetByIdInput, ${capitalizeFirstLetter(name)}GetByIdOutput, ${capitalizeFirstLetter(name)}GetByIdUsecase } from '../${name}-get-by-id';
14
14
  import { ${capitalizeFirstLetter(name)}Entity } from './../../entity/${name}';
15
15
 
16
- const successInput: ${capitalizeFirstLetter(name)}GetByIdInput = {
17
- id: getMockUUID()
18
- };
19
-
20
- const failureInput: ${capitalizeFirstLetter(name)}GetByIdInput = {};
21
-
22
- describe('${capitalizeFirstLetter(name)}GetByIdUsecase', () => {
16
+ describe(${capitalizeFirstLetter(name)}GetByIdUsecase.name, () => {
23
17
  let usecase: I${capitalizeFirstLetter(name)}GetByIdAdapter;
24
18
  let repository: I${capitalizeFirstLetter(name)}Repository;
25
19
 
@@ -46,27 +40,31 @@ describe('${capitalizeFirstLetter(name)}GetByIdUsecase', () => {
46
40
 
47
41
  test('when no input is specified, should expect an error', async () => {
48
42
  await expectZodError(
49
- () => usecase.execute(failureInput),
43
+ () => usecase.execute({}),
50
44
  (issues) => {
51
45
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('id') }]);
52
46
  }
53
47
  );
54
48
  });
55
49
 
50
+ const input: ${capitalizeFirstLetter(name)}GetByIdInput = {
51
+ id: getMockUUID()
52
+ };
53
+
56
54
  test('when ${name} not found, should expect an error', async () => {
57
55
  repository.findById = jest.fn().mockResolvedValue(null);
58
56
 
59
- await expect(usecase.execute(successInput)).rejects.toThrowError(ApiNotFoundException);
57
+ await expect(usecase.execute(input)).rejects.toThrow(ApiNotFoundException);
60
58
  });
61
59
 
62
- test('when ${name} found, should expect a ${name} that has been found', async () => {
60
+ test('when ${name} found, should expect a ${name}', async () => {
63
61
  const findByIdOutput: ${capitalizeFirstLetter(name)}GetByIdOutput = new ${capitalizeFirstLetter(name)}Entity({
64
62
  id: getMockUUID(),
65
63
  name: 'dummy'
66
64
  });
67
65
  repository.findById = jest.fn().mockResolvedValue(findByIdOutput);
68
66
 
69
- await expect(usecase.execute(successInput)).resolves.toEqual(findByIdOutput);
67
+ await expect(usecase.execute(input)).resolves.toEqual(findByIdOutput);
70
68
  });
71
69
  });
72
70
  `
@@ -12,11 +12,7 @@ import { expectZodError, getMockUUID } from '@/utils/tests';
12
12
  import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${name}';
13
13
  import { ${capitalizeFirstLetter(name)}Entity } from './../../entity/${name}';
14
14
 
15
- const successInput: ${capitalizeFirstLetter(name)}ListInput = { limit: 1, page: 1, search: {}, sort: { createdAt: -1 } };
16
-
17
- const failureInput: ${capitalizeFirstLetter(name)}ListInput = { search: null, sort: null, limit: 10, page: 1 };
18
-
19
- describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
15
+ describe(${capitalizeFirstLetter(name)}ListUsecase.name, () => {
20
16
  let usecase: I${capitalizeFirstLetter(name)}ListAdapter;
21
17
  let repository: I${capitalizeFirstLetter(name)}Repository;
22
18
 
@@ -43,13 +39,15 @@ describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
43
39
 
44
40
  test('when sort input is specified, should expect an error', async () => {
45
41
  await expectZodError(
46
- () => usecase.execute(failureInput),
42
+ () => usecase.execute({ search: null, sort: null, limit: 10, page: 1 }),
47
43
  (issues) => {
48
44
  expect(issues).toEqual([{ message: 'Expected object, received null', path: 'sort' }]);
49
45
  }
50
46
  );
51
47
  });
52
48
 
49
+ const input: ${capitalizeFirstLetter(name)}ListInput = { limit: 1, page: 1, search: {}, sort: { createdAt: -1 } };
50
+
53
51
  test('when ${name} are found, should expect an ${name} list', async () => {
54
52
  const doc = new ${capitalizeFirstLetter(name)}Entity({
55
53
  id: getMockUUID(),
@@ -62,7 +60,7 @@ describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
62
60
 
63
61
  repository.paginate = jest.fn().mockResolvedValue(paginateOutput);
64
62
 
65
- await expect(usecase.execute(successInput)).resolves.toEqual({
63
+ await expect(usecase.execute(input)).resolves.toEqual({
66
64
  docs: [doc],
67
65
  page: 1,
68
66
  limit: 1,
@@ -75,7 +73,7 @@ describe('${capitalizeFirstLetter(name)}ListUsecase', () => {
75
73
 
76
74
  repository.paginate = jest.fn().mockResolvedValue(paginateOutput);
77
75
 
78
- await expect(usecase.execute(successInput)).resolves.toEqual(paginateOutput);
76
+ await expect(usecase.execute(input)).resolves.toEqual(paginateOutput);
79
77
  });
80
78
  });
81
79
  `
@@ -14,13 +14,7 @@ import { I${capitalizeFirstLetter(name)}Repository } from '../../repository/${na
14
14
  import { ${capitalizeFirstLetter(name)}UpdateInput, ${capitalizeFirstLetter(name)}UpdateOutput, ${capitalizeFirstLetter(name)}UpdateUsecase } from '../${name}-update';
15
15
  import { ${capitalizeFirstLetter(name)}Entity } from './../../entity/${name}';
16
16
 
17
- const successInput: ${capitalizeFirstLetter(name)}UpdateInput = {
18
- id: getMockUUID()
19
- };
20
-
21
- const failureInput: ${capitalizeFirstLetter(name)}UpdateInput = {};
22
-
23
- describe('${capitalizeFirstLetter(name)}UpdateUsecase', () => {
17
+ describe(${capitalizeFirstLetter(name)}UpdateUsecase.name, () => {
24
18
  let usecase: I${capitalizeFirstLetter(name)}UpdateAdapter;
25
19
  let repository: I${capitalizeFirstLetter(name)}Repository;
26
20
 
@@ -53,20 +47,24 @@ describe('${capitalizeFirstLetter(name)}UpdateUsecase', () => {
53
47
 
54
48
  test('when no input is specified, should expect an error', async () => {
55
49
  await expectZodError(
56
- () => usecase.execute(failureInput),
50
+ () => usecase.execute({}),
57
51
  (issues) => {
58
52
  expect(issues).toEqual([{ message: 'Required', path: ${capitalizeFirstLetter(name)}Entity.nameOf('id') }]);
59
53
  }
60
54
  );
61
55
  });
62
56
 
57
+ const input: ${capitalizeFirstLetter(name)}UpdateInput = {
58
+ id: getMockUUID()
59
+ };
60
+
63
61
  test('when ${name} not found, should expect an error', async () => {
64
62
  repository.findById = jest.fn().mockResolvedValue(null);
65
63
 
66
- await expect(usecase.execute(successInput)).rejects.toThrowError(ApiNotFoundException);
64
+ await expect(usecase.execute(input)).rejects.toThrow(ApiNotFoundException);
67
65
  });
68
66
 
69
- test('when ${name} updated successfully, should expect an ${name} that has been updated', async () => {
67
+ test('when ${name} updated successfully, should expect an ${name}', async () => {
70
68
  const findByIdOutput: ${capitalizeFirstLetter(name)}UpdateOutput = new ${capitalizeFirstLetter(name)}Entity({
71
69
  id: getMockUUID(),
72
70
  name: 'dummy'
@@ -75,7 +73,7 @@ describe('${capitalizeFirstLetter(name)}UpdateUsecase', () => {
75
73
  repository.findById = jest.fn().mockResolvedValue(findByIdOutput);
76
74
  repository.updateOne = jest.fn().mockResolvedValue(null);
77
75
 
78
- await expect(usecase.execute(successInput)).resolves.toEqual(findByIdOutput);
76
+ await expect(usecase.execute(input)).resolves.toEqual(findByIdOutput);
79
77
  });
80
78
  });
81
79
  `
@@ -28,7 +28,7 @@ export class ${capitalizeFirstLetter(name)}DeleteUsecase implements IUsecase {
28
28
  const model = await this.${name}Repository.findById(id);
29
29
 
30
30
  if (!model) {
31
- throw new ApiNotFoundException();
31
+ throw new ApiNotFoundException('${name}NotFound');
32
32
  }
33
33
 
34
34
  const ${name} = new ${capitalizeFirstLetter(name)}Entity(model);
@@ -29,7 +29,7 @@ export class ${capitalizeFirstLetter(name)}GetByIdUsecase implements IUsecase {
29
29
  const ${name} = await this.${name}Repository.findById(id);
30
30
 
31
31
  if (!${name}) {
32
- throw new ApiNotFoundException();
32
+ throw new ApiNotFoundException('${name}NotFound');
33
33
  }
34
34
 
35
35
  return new ${capitalizeFirstLetter(name)}Entity(${name});
@@ -29,7 +29,7 @@ export class ${capitalizeFirstLetter(name)}UpdateUsecase implements IUsecase {
29
29
  const ${name} = await this.${name}Repository.findById(input.id);
30
30
 
31
31
  if (!${name}) {
32
- throw new ApiNotFoundException();
32
+ throw new ApiNotFoundException('${name}NotFound');
33
33
  }
34
34
 
35
35
  const ${name}Finded = new ${capitalizeFirstLetter(name)}Entity(${name});
@@ -14,17 +14,19 @@ import { ${capitalizeFirstLetter(name)}UpdateOutput } from '@/core/${name}/use-c
14
14
  import { Swagger } from '@/utils/docs/swagger';
15
15
 
16
16
  const input = new ${capitalizeFirstLetter(name)}Entity({
17
- name: '<name>'
17
+ name: 'name'
18
18
  });
19
19
 
20
20
  const output = new ${capitalizeFirstLetter(name)}Entity({ ...input, updatedAt: new Date(), createdAt: new Date(), deletedAt: null });
21
21
 
22
+ const BASE_URL = 'api/v1/${pluralize(name)}';
23
+
22
24
  export const SwaggerResponse = {
23
25
  create: {
24
26
  200: Swagger.defaultResponseJSON({
25
27
  status: 200,
26
- json: { created: true, id: '<uuid>' } as ${capitalizeFirstLetter(name)}CreateOutput,
27
- description: '${name} created.'
28
+ json: { created: true, id: 'uuid' } as ${capitalizeFirstLetter(name)}CreateOutput,
29
+ description: 'create ${name}.'
28
30
  })
29
31
  },
30
32
  update: {
@@ -35,9 +37,9 @@ export const SwaggerResponse = {
35
37
  }),
36
38
  404: Swagger.defaultResponseError({
37
39
  status: 404,
38
- route: 'api/v1/${pluralize(name)}',
40
+ route: BASE_URL.concat('/:id'),
39
41
  message: '${name}NotFound',
40
- description: '${name} not found.'
42
+ description: 'update ${name}.'
41
43
  })
42
44
  },
43
45
  getById: {
@@ -48,52 +50,37 @@ export const SwaggerResponse = {
48
50
  }),
49
51
  404: Swagger.defaultResponseError({
50
52
  status: 404,
51
- route: 'api/v1/${pluralize(name)}/:id',
53
+ route: BASE_URL.concat('/:id'),
52
54
  message: '${name}NotFound',
53
- description: '${name} not found.'
55
+ description: 'get ${name}.'
54
56
  })
55
57
  },
56
58
  delete: {
57
59
  200: Swagger.defaultResponseJSON({
58
60
  status: 200,
59
61
  json: output as ${capitalizeFirstLetter(name)}DeleteOutput,
60
- description: '${name} found.'
62
+ description: 'delete ${name}.'
61
63
  }),
62
64
  404: Swagger.defaultResponseError({
63
65
  status: 404,
64
- route: 'api/v1/${pluralize(name)}/:id',
66
+ route: BASE_URL.concat('/:id'),
65
67
  message: '${name}NotFound',
66
- description: '${name} not found.'
68
+ description: '${name}NotFound.'
67
69
  })
68
70
  },
69
71
  list: {
70
72
  200: Swagger.defaultResponseJSON({
71
73
  status: 200,
72
74
  json: { docs: [output], page: 1, limit: 1, total: 1 } as ${capitalizeFirstLetter(name)}ListOutput,
73
- description: '${name} created.'
75
+ description: 'list ${pluralize(name)}.'
74
76
  })
75
77
  }
76
78
  };
77
79
 
78
80
  export const SwaggerRequest = {
79
81
  createBody: Swagger.defaultRequestJSON({ ...input, id: undefined } as ${capitalizeFirstLetter(name)}Entity),
80
- updateBody: Swagger.defaultRequestJSON({ ...input, id: '<id>' } as ${capitalizeFirstLetter(name)}Entity),
81
- listQuery: {
82
- pagination: {
83
- limit: Swagger.defaultApiQueryOptions({ example: 10, name: 'limit', required: false }),
84
- page: Swagger.defaultApiQueryOptions({ example: 1, name: 'page', required: false })
85
- },
86
- sort: Swagger.defaultApiQueryOptions({
87
- name: 'sort',
88
- required: false,
89
- description: '<b>createdAt:desc,name:asc'
90
- }),
91
- search: Swagger.defaultApiQueryOptions({
92
- name: 'search',
93
- required: false,
94
- description: '<b>name:miau'
95
- })
96
- }
82
+ updateBody: Swagger.defaultRequestJSON(input as ${capitalizeFirstLetter(name)}Entity),
83
+ listQuery: Swagger.defaultRequestListJSON()
97
84
  };
98
85
  `
99
86