diff --git a/app/templates/nestjsMongoose/E2E/__name__/delete.spec.ts.txt b/app/templates/nestjsMongoose/E2E/__name__/delete.spec.ts.txt index 13a0aae..7edfd02 100644 --- a/app/templates/nestjsMongoose/E2E/__name__/delete.spec.ts.txt +++ b/app/templates/nestjsMongoose/E2E/__name__/delete.spec.ts.txt @@ -3,15 +3,15 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> ❌', () => { it('should delete one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'DELETE' - const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}` + const path = `/<%= nameCamel %>s/${<%= nameCamel %>.id}` const { body } = await request(app.server.getHttpServer()) .delete(path) @@ -26,7 +26,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { it('should throw a not found error when can not find <%= nameCamel %>', async () => { const status = 404 const method = 'DELETE' - const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` + const path = `/<%= nameCamel %>s/507f1f77bcf86cd799439011` const { body } = await request(app.server.getHttpServer()) .delete(path) @@ -35,10 +35,11 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', - message: 'NOT_FOUND_<%= nameUp %>', + error: 'Not Found Error', + message: 'NOT_FOUND_PROJECT', statusCode: status, }, }) @@ -47,7 +48,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -55,7 +56,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsMongoose/E2E/__name__/index.spec.ts.txt b/app/templates/nestjsMongoose/E2E/__name__/index.spec.ts.txt index 42089fb..7060f5a 100644 --- a/app/templates/nestjsMongoose/E2E/__name__/index.spec.ts.txt +++ b/app/templates/nestjsMongoose/E2E/__name__/index.spec.ts.txt @@ -3,7 +3,7 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🗒', () => { it('should return all <%= namePluralCamel %> paginated and filtered', async () => { @@ -14,8 +14,8 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => { const method = 'GET' const path = `/<%= namePluralCamel %>?page=${page}&limit=${limit}&*deletedAt=null` - await <%= nameCamel %>Seeder.seedMany(2) - await <%= nameCamel %>Seeder.seedManyDeleted(2) + await <%= nameCamel %>Factory.count(2).create() + await <%= nameCamel %>Factory.count(2).deleted().create() const { body } = await request(app.server.getHttpServer()) .get(path) @@ -80,7 +80,7 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -88,7 +88,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsMongoose/E2E/__name__/show.spec.ts.txt b/app/templates/nestjsMongoose/E2E/__name__/show.spec.ts.txt index 1437666..1ec9a37 100644 --- a/app/templates/nestjsMongoose/E2E/__name__/show.spec.ts.txt +++ b/app/templates/nestjsMongoose/E2E/__name__/show.spec.ts.txt @@ -3,11 +3,11 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🔍', () => { it('should return one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'GET' @@ -34,9 +34,10 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => { expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', + error: 'Not Found Error', message: 'NOT_FOUND_<%= nameUp %>', statusCode: status, }, @@ -90,7 +91,7 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -98,7 +99,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt b/app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt index d2df8a6..e92a8e4 100644 --- a/app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt +++ b/app/templates/nestjsMongoose/E2E/__name__/store.spec.ts.txt @@ -3,7 +3,6 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' describe('\n[E2E] <%= namePascal %> 🧱', () => { it('should create a new <%= nameCamel %>', async () => { @@ -11,23 +10,25 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => { const method = 'POST' const path = `/<%= namePluralCamel %>` - const storeBlueprint = <%= nameCamel %>Seeder.storeBlueprint() + const payload = { + name: 'test' + } const { body } = await request(app.server.getHttpServer()) .post(path) - .send(storeBlueprint) + .send(payload) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) - expect(body.data.name).toBe(storeBlueprint.name) + expect(body.data.name).toBe(payload.name) }) // Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts // And create tests to validate if the schema validator are working well. // it('should throw validation errors when trying to create a <%= nameCamel %> with validation errors', async () => { - // const status = 400 + // const status = 422 // const path = `/<%= namePluralCamel %>` // // const { body } = await request(app.server.getHttpServer()) @@ -46,15 +47,12 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') app = await new App([AppModule]).initApp() database = new Database(app) - - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) }) afterEach(async () => { diff --git a/app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt b/app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt index d4d40c4..a6e8e4b 100644 --- a/app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt +++ b/app/templates/nestjsMongoose/E2E/__name__/update.spec.ts.txt @@ -3,46 +3,48 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🛠', () => { it('should update one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'PUT' - const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}` + const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}?apiKey=${apiKey}` - const updateBlueprint = <%= nameCamel %>Seeder.updateBlueprint() + const payload = { + name: 'test', + } const { body } = await request(app.server.getHttpServer()) .put(path) - .send(updateBlueprint) + .send(payload) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.data.id).toBe(<%= nameCamel %>.id) + expect(body.data.name).toBe(payload.name) }) it('should throw a not found error when can not find <%= nameCamel %>', async () => { const status = 404 const method = 'PUT' - const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` - - const updateBlueprint = <%= nameCamel %>Seeder.blueprint() + const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011?apiKey=${apiKey}` const { body } = await request(app.server.getHttpServer()) .put(path) - .send(updateBlueprint) + .send({ name: 'test' }) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', + error: 'Not Found Error', message: 'NOT_FOUND_<%= nameUp %>', statusCode: status, }, @@ -53,7 +55,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => { // And create tests to validate if the schema validator are working well. // it('should throw validation errors when trying to update a <%= nameCamel %> with validation errors', async () => { - // const status = 400 + // const status = 422 // const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` // // const { body } = await request(app.server.getHttpServer()) @@ -72,7 +74,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -80,7 +82,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { @@ -90,4 +92,3 @@ afterEach(async () => { await database.truncateCache() await app.closeApp() }) - diff --git a/app/templates/nestjsMongoose/__name__Controller.ts.txt b/app/templates/nestjsMongoose/__name__Controller.ts.txt index 5d554a4..12daf7e 100644 --- a/app/templates/nestjsMongoose/__name__Controller.ts.txt +++ b/app/templates/nestjsMongoose/__name__Controller.ts.txt @@ -30,8 +30,8 @@ export class <%= namePascal %>Controller { private <%= nameCamel %>Service: <%= namePascal %>Service @Get() - @ApiQuery({ name: 'limit', allowEmptyValue: true }) @ApiQuery({ name: 'page', allowEmptyValue: true }) + @ApiQuery({ name: 'limit', allowEmptyValue: true }) async index( @Query(QueryParamsPipe) queries: any, @Pagination() paginate: PaginationContract, diff --git a/app/templates/nestjsMongoose/__name__Factory.ts.txt b/app/templates/nestjsMongoose/__name__Factory.ts.txt new file mode 100644 index 0000000..29f5cba --- /dev/null +++ b/app/templates/nestjsMongoose/__name__Factory.ts.txt @@ -0,0 +1,25 @@ +import { Inject, Injectable } from '@nestjs/common' +import { Factory } from '@secjs/base/database/Factory' +import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' + +@Injectable() +export class <%= namePascal %>Factory extends Factory { + @Inject(<%= namePascal %>Seeder) protected seeder: <%= namePascal %>Seeder + + blueprint = () => { + return { + example: this.faker.name.findName(), + } + } + + fakeBlueprint = () => { + return { + id: this.faker.datatype.uuid(), + name: this.faker.name.findName(), + status: 'pendent', + createdAt: new Date(), + updatedAt: new Date(), + deletedAt: null, + } + } +} diff --git a/app/templates/nestjsMongoose/__name__Seeder.ts.txt b/app/templates/nestjsMongoose/__name__Seeder.ts.txt index 2618f97..c0438f4 100644 --- a/app/templates/nestjsMongoose/__name__Seeder.ts.txt +++ b/app/templates/nestjsMongoose/__name__Seeder.ts.txt @@ -1,75 +1,8 @@ -import { name } from 'faker' -import { <%= namePascal %> } from '@prisma/client' import { Inject, Injectable } from '@nestjs/common' -import { ExtraParams } from 'database/decorators/ExtraParams' +import { Seeder } from '@secjs/base/database/Seeder' import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository' @Injectable() -export class <%= namePascal %>Seeder { - @Inject(<%= namePascal %>Repository) private <%= nameCamel %>Repository: <%= namePascal %>Repository - - @ExtraParams(1) - blueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } - - async seedOne(extraParams?: any): Promise<<%= namePascal %>> { - return this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)) - } - - async seedMany(number: number, extraParams?: any): Promise<<%= namePascal %>[]> { - const promises = [] - - for (let i = 1; i <= number; i++) { - promises.push( - this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)), - ) - } - - return Promise.all(promises) - } - - @ExtraParams(1) - async seedOneDeleted(extraParams?: any): Promise<<%= namePascal %>> { - extraParams.deletedAt = new Date() - - return this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)) - } - - @ExtraParams(2) - async seedManyDeleted(number: number, extraParams?: any): Promise<<%= namePascal %>[]> { - const promises = [] - - extraParams.deletedAt = new Date() - - for (let i = 1; i <= number; i++) { - promises.push( - this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)), - ) - } - - return Promise.all(promises) - } - - @ExtraParams(1) - storeBlueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } - - @ExtraParams(1) - updateBlueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } +export class <%= namePascal %>Seeder extends Seeder { + @Inject(<%= namePascal %>Repository) protected repository: <%= namePascal %>Repository } diff --git a/app/templates/nestjsMongoose/__name__Service.ts.txt b/app/templates/nestjsMongoose/__name__Service.ts.txt index 0826f8b..d8d22c0 100644 --- a/app/templates/nestjsMongoose/__name__Service.ts.txt +++ b/app/templates/nestjsMongoose/__name__Service.ts.txt @@ -1,62 +1,13 @@ -import { - Create<%= namePascal %>Dto, - Update<%= namePascal %>Dto, -} from 'app/Contracts/Dtos/<%= namePascal %>Dto' - -import { Options } from 'app/Decorators/Services/Options' +import { Inject, Injectable } from '@nestjs/common' +import { Options } from '@secjs/base/decorators/Options' +import { BaseService } from '@secjs/base/services/BaseService' import { <%= namePascal %>Resource } from 'app/Resources/<%= namePascal %>Resource' import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository' -import { Inject, Injectable, NotFoundException } from '@nestjs/common' -import { ApiRequestContract, PaginationContract } from '@secjs/contracts' @Injectable() -export class <%= namePascal %>Service { - @Inject(<%= namePascal %>Repository) - private <%= nameCamel %>Repository: <%= namePascal %>Repository - - async findOneAndValidate(id: string, options?: ApiRequestContract) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.getOne(id, options) - - if (!<%= nameCamel %>) { - throw new NotFoundException('NOT_FOUND_<%= nameUp %>') - } - - return <%= nameCamel %> - } - - @Options() - async findAll(paginate: PaginationContract, options?: ApiRequestContract) { - const { data, meta, links }: any = await this.<%= nameCamel %>Repository.getAll(paginate, options) - - return { - meta, - links, - data: new <%= namePascal %>Resource().toArray(data), - } - } - - @Options() - async findOne(id: string, options?: ApiRequestContract) { - const <%= nameCamel %> = await this.findOneAndValidate(id, options) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async createOne(dto: Create<%= namePascal %>Dto) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.storeOne(dto) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async updateOne(id: string, dto: Update<%= namePascal %>Dto) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.updateOne(await this.findOneAndValidate(id), dto) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async deleteOne(id: string) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.deleteOne(await this.findOneAndValidate(id)) +export class <%= namePascal %>Service extends BaseService { + protected resourceName = '<%= nameCamel %>' + protected resource = new <%= namePascal %>Resource() - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } + @Inject(<%= namePascal %>Repository) protected repository: <%= namePascal %>Repository } diff --git a/app/templates/nestjsPrismaOrm/E2E/__name__/delete.spec.ts.txt b/app/templates/nestjsPrismaOrm/E2E/__name__/delete.spec.ts.txt index 13a0aae..7edfd02 100644 --- a/app/templates/nestjsPrismaOrm/E2E/__name__/delete.spec.ts.txt +++ b/app/templates/nestjsPrismaOrm/E2E/__name__/delete.spec.ts.txt @@ -3,15 +3,15 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> ❌', () => { it('should delete one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'DELETE' - const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}` + const path = `/<%= nameCamel %>s/${<%= nameCamel %>.id}` const { body } = await request(app.server.getHttpServer()) .delete(path) @@ -26,7 +26,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { it('should throw a not found error when can not find <%= nameCamel %>', async () => { const status = 404 const method = 'DELETE' - const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` + const path = `/<%= nameCamel %>s/507f1f77bcf86cd799439011` const { body } = await request(app.server.getHttpServer()) .delete(path) @@ -35,10 +35,11 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', - message: 'NOT_FOUND_<%= nameUp %>', + error: 'Not Found Error', + message: 'NOT_FOUND_PROJECT', statusCode: status, }, }) @@ -47,7 +48,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -55,7 +56,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsPrismaOrm/E2E/__name__/index.spec.ts.txt b/app/templates/nestjsPrismaOrm/E2E/__name__/index.spec.ts.txt index 42089fb..7060f5a 100644 --- a/app/templates/nestjsPrismaOrm/E2E/__name__/index.spec.ts.txt +++ b/app/templates/nestjsPrismaOrm/E2E/__name__/index.spec.ts.txt @@ -3,7 +3,7 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🗒', () => { it('should return all <%= namePluralCamel %> paginated and filtered', async () => { @@ -14,8 +14,8 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => { const method = 'GET' const path = `/<%= namePluralCamel %>?page=${page}&limit=${limit}&*deletedAt=null` - await <%= nameCamel %>Seeder.seedMany(2) - await <%= nameCamel %>Seeder.seedManyDeleted(2) + await <%= nameCamel %>Factory.count(2).create() + await <%= nameCamel %>Factory.count(2).deleted().create() const { body } = await request(app.server.getHttpServer()) .get(path) @@ -80,7 +80,7 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -88,7 +88,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsPrismaOrm/E2E/__name__/show.spec.ts.txt b/app/templates/nestjsPrismaOrm/E2E/__name__/show.spec.ts.txt index 1437666..1ec9a37 100644 --- a/app/templates/nestjsPrismaOrm/E2E/__name__/show.spec.ts.txt +++ b/app/templates/nestjsPrismaOrm/E2E/__name__/show.spec.ts.txt @@ -3,11 +3,11 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🔍', () => { it('should return one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'GET' @@ -34,9 +34,10 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => { expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', + error: 'Not Found Error', message: 'NOT_FOUND_<%= nameUp %>', statusCode: status, }, @@ -90,7 +91,7 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -98,7 +99,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsPrismaOrm/E2E/__name__/store.spec.ts.txt b/app/templates/nestjsPrismaOrm/E2E/__name__/store.spec.ts.txt index d2df8a6..e92a8e4 100644 --- a/app/templates/nestjsPrismaOrm/E2E/__name__/store.spec.ts.txt +++ b/app/templates/nestjsPrismaOrm/E2E/__name__/store.spec.ts.txt @@ -3,7 +3,6 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' describe('\n[E2E] <%= namePascal %> 🧱', () => { it('should create a new <%= nameCamel %>', async () => { @@ -11,23 +10,25 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => { const method = 'POST' const path = `/<%= namePluralCamel %>` - const storeBlueprint = <%= nameCamel %>Seeder.storeBlueprint() + const payload = { + name: 'test' + } const { body } = await request(app.server.getHttpServer()) .post(path) - .send(storeBlueprint) + .send(payload) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) - expect(body.data.name).toBe(storeBlueprint.name) + expect(body.data.name).toBe(payload.name) }) // Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts // And create tests to validate if the schema validator are working well. // it('should throw validation errors when trying to create a <%= nameCamel %> with validation errors', async () => { - // const status = 400 + // const status = 422 // const path = `/<%= namePluralCamel %>` // // const { body } = await request(app.server.getHttpServer()) @@ -46,15 +47,12 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') app = await new App([AppModule]).initApp() database = new Database(app) - - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) }) afterEach(async () => { diff --git a/app/templates/nestjsPrismaOrm/E2E/__name__/update.spec.ts.txt b/app/templates/nestjsPrismaOrm/E2E/__name__/update.spec.ts.txt index d4d40c4..a6e8e4b 100644 --- a/app/templates/nestjsPrismaOrm/E2E/__name__/update.spec.ts.txt +++ b/app/templates/nestjsPrismaOrm/E2E/__name__/update.spec.ts.txt @@ -3,46 +3,48 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🛠', () => { it('should update one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'PUT' - const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}` + const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}?apiKey=${apiKey}` - const updateBlueprint = <%= nameCamel %>Seeder.updateBlueprint() + const payload = { + name: 'test', + } const { body } = await request(app.server.getHttpServer()) .put(path) - .send(updateBlueprint) + .send(payload) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.data.id).toBe(<%= nameCamel %>.id) + expect(body.data.name).toBe(payload.name) }) it('should throw a not found error when can not find <%= nameCamel %>', async () => { const status = 404 const method = 'PUT' - const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` - - const updateBlueprint = <%= nameCamel %>Seeder.blueprint() + const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011?apiKey=${apiKey}` const { body } = await request(app.server.getHttpServer()) .put(path) - .send(updateBlueprint) + .send({ name: 'test' }) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', + error: 'Not Found Error', message: 'NOT_FOUND_<%= nameUp %>', statusCode: status, }, @@ -53,7 +55,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => { // And create tests to validate if the schema validator are working well. // it('should throw validation errors when trying to update a <%= nameCamel %> with validation errors', async () => { - // const status = 400 + // const status = 422 // const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` // // const { body } = await request(app.server.getHttpServer()) @@ -72,7 +74,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -80,7 +82,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { @@ -90,4 +92,3 @@ afterEach(async () => { await database.truncateCache() await app.closeApp() }) - diff --git a/app/templates/nestjsPrismaOrm/__name__Controller.ts.txt b/app/templates/nestjsPrismaOrm/__name__Controller.ts.txt index ac85ab3..21e842a 100644 --- a/app/templates/nestjsPrismaOrm/__name__Controller.ts.txt +++ b/app/templates/nestjsPrismaOrm/__name__Controller.ts.txt @@ -29,8 +29,8 @@ export class <%= namePascal %>Controller { private <%= nameCamel %>Service: <%= namePascal %>Service @Get() - @ApiQuery({ name: 'limit', allowEmptyValue: true }) @ApiQuery({ name: 'page', allowEmptyValue: true }) + @ApiQuery({ name: 'limit', allowEmptyValue: true }) async index( @Query(QueryParamsPipe) queries: any, @Pagination() paginate: PaginationContract, diff --git a/app/templates/nestjsPrismaOrm/__name__Factory.ts.txt b/app/templates/nestjsPrismaOrm/__name__Factory.ts.txt new file mode 100644 index 0000000..29f5cba --- /dev/null +++ b/app/templates/nestjsPrismaOrm/__name__Factory.ts.txt @@ -0,0 +1,25 @@ +import { Inject, Injectable } from '@nestjs/common' +import { Factory } from '@secjs/base/database/Factory' +import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' + +@Injectable() +export class <%= namePascal %>Factory extends Factory { + @Inject(<%= namePascal %>Seeder) protected seeder: <%= namePascal %>Seeder + + blueprint = () => { + return { + example: this.faker.name.findName(), + } + } + + fakeBlueprint = () => { + return { + id: this.faker.datatype.uuid(), + name: this.faker.name.findName(), + status: 'pendent', + createdAt: new Date(), + updatedAt: new Date(), + deletedAt: null, + } + } +} diff --git a/app/templates/nestjsPrismaOrm/__name__Seeder.ts.txt b/app/templates/nestjsPrismaOrm/__name__Seeder.ts.txt index 2618f97..c0438f4 100644 --- a/app/templates/nestjsPrismaOrm/__name__Seeder.ts.txt +++ b/app/templates/nestjsPrismaOrm/__name__Seeder.ts.txt @@ -1,75 +1,8 @@ -import { name } from 'faker' -import { <%= namePascal %> } from '@prisma/client' import { Inject, Injectable } from '@nestjs/common' -import { ExtraParams } from 'database/decorators/ExtraParams' +import { Seeder } from '@secjs/base/database/Seeder' import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository' @Injectable() -export class <%= namePascal %>Seeder { - @Inject(<%= namePascal %>Repository) private <%= nameCamel %>Repository: <%= namePascal %>Repository - - @ExtraParams(1) - blueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } - - async seedOne(extraParams?: any): Promise<<%= namePascal %>> { - return this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)) - } - - async seedMany(number: number, extraParams?: any): Promise<<%= namePascal %>[]> { - const promises = [] - - for (let i = 1; i <= number; i++) { - promises.push( - this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)), - ) - } - - return Promise.all(promises) - } - - @ExtraParams(1) - async seedOneDeleted(extraParams?: any): Promise<<%= namePascal %>> { - extraParams.deletedAt = new Date() - - return this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)) - } - - @ExtraParams(2) - async seedManyDeleted(number: number, extraParams?: any): Promise<<%= namePascal %>[]> { - const promises = [] - - extraParams.deletedAt = new Date() - - for (let i = 1; i <= number; i++) { - promises.push( - this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)), - ) - } - - return Promise.all(promises) - } - - @ExtraParams(1) - storeBlueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } - - @ExtraParams(1) - updateBlueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } +export class <%= namePascal %>Seeder extends Seeder { + @Inject(<%= namePascal %>Repository) protected repository: <%= namePascal %>Repository } diff --git a/app/templates/nestjsPrismaOrm/__name__Service.ts.txt b/app/templates/nestjsPrismaOrm/__name__Service.ts.txt index 02ea378..d8d22c0 100644 --- a/app/templates/nestjsPrismaOrm/__name__Service.ts.txt +++ b/app/templates/nestjsPrismaOrm/__name__Service.ts.txt @@ -1,56 +1,13 @@ -import { - Create<%= namePascal %>Dto, - Update<%= namePascal %>Dto, -} from 'app/Contracts/Dtos/<%= namePascal %>Dto' - -import { Options } from 'app/Decorators/Services/Options' +import { Inject, Injectable } from '@nestjs/common' +import { Options } from '@secjs/base/decorators/Options' +import { BaseService } from '@secjs/base/services/BaseService' import { <%= namePascal %>Resource } from 'app/Resources/<%= namePascal %>Resource' import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository' -import { Inject, Injectable, NotFoundException } from '@nestjs/common' -import { ApiRequestContract, PaginationContract } from '@secjs/contracts' @Injectable() -export class <%= namePascal %>Service { - @Inject(<%= namePascal %>Repository) - private <%= nameCamel %>Repository: <%= namePascal %>Repository - - @Options() - async findAll(paginate: PaginationContract, options?: ApiRequestContract) { - const { data, meta, links }: any = await this.<%= nameCamel %>Repository.getAll(paginate, options) - - return { - meta, - links, - data: new <%= namePascal %>Resource().toArray(data), - } - } - - @Options() - async findOne(id: string, options?: ApiRequestContract) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.getOne(id, options) - - if (!<%= nameCamel %>) { - throw new NotFoundException('NOT_FOUND_<%= nameUp %>') - } - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async createOne(dto: Create<%= namePascal %>Dto) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.storeOne(dto) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async updateOne(id: string, dto: Update<%= namePascal %>Dto) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.updateOne(await this.findOne(id), dto) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async deleteOne(id: string) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.deleteOne(await this.findOne(id)) +export class <%= namePascal %>Service extends BaseService { + protected resourceName = '<%= nameCamel %>' + protected resource = new <%= namePascal %>Resource() - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } + @Inject(<%= namePascal %>Repository) protected repository: <%= namePascal %>Repository } diff --git a/app/templates/nestjsTypeOrm/E2E/__name__/delete.spec.ts.txt b/app/templates/nestjsTypeOrm/E2E/__name__/delete.spec.ts.txt index 13a0aae..7edfd02 100644 --- a/app/templates/nestjsTypeOrm/E2E/__name__/delete.spec.ts.txt +++ b/app/templates/nestjsTypeOrm/E2E/__name__/delete.spec.ts.txt @@ -3,15 +3,15 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> ❌', () => { it('should delete one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'DELETE' - const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}` + const path = `/<%= nameCamel %>s/${<%= nameCamel %>.id}` const { body } = await request(app.server.getHttpServer()) .delete(path) @@ -26,7 +26,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { it('should throw a not found error when can not find <%= nameCamel %>', async () => { const status = 404 const method = 'DELETE' - const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` + const path = `/<%= nameCamel %>s/507f1f77bcf86cd799439011` const { body } = await request(app.server.getHttpServer()) .delete(path) @@ -35,10 +35,11 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', - message: 'NOT_FOUND_<%= nameUp %>', + error: 'Not Found Error', + message: 'NOT_FOUND_PROJECT', statusCode: status, }, }) @@ -47,7 +48,7 @@ describe('\n[E2E] <%= namePascal %> ❌', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -55,7 +56,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsTypeOrm/E2E/__name__/index.spec.ts.txt b/app/templates/nestjsTypeOrm/E2E/__name__/index.spec.ts.txt index 42089fb..7060f5a 100644 --- a/app/templates/nestjsTypeOrm/E2E/__name__/index.spec.ts.txt +++ b/app/templates/nestjsTypeOrm/E2E/__name__/index.spec.ts.txt @@ -3,7 +3,7 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🗒', () => { it('should return all <%= namePluralCamel %> paginated and filtered', async () => { @@ -14,8 +14,8 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => { const method = 'GET' const path = `/<%= namePluralCamel %>?page=${page}&limit=${limit}&*deletedAt=null` - await <%= nameCamel %>Seeder.seedMany(2) - await <%= nameCamel %>Seeder.seedManyDeleted(2) + await <%= nameCamel %>Factory.count(2).create() + await <%= nameCamel %>Factory.count(2).deleted().create() const { body } = await request(app.server.getHttpServer()) .get(path) @@ -80,7 +80,7 @@ describe('\n[E2E] <%= namePascal %> 🗒', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -88,7 +88,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsTypeOrm/E2E/__name__/show.spec.ts.txt b/app/templates/nestjsTypeOrm/E2E/__name__/show.spec.ts.txt index 1437666..1ec9a37 100644 --- a/app/templates/nestjsTypeOrm/E2E/__name__/show.spec.ts.txt +++ b/app/templates/nestjsTypeOrm/E2E/__name__/show.spec.ts.txt @@ -3,11 +3,11 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🔍', () => { it('should return one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'GET' @@ -34,9 +34,10 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => { expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', + error: 'Not Found Error', message: 'NOT_FOUND_<%= nameUp %>', statusCode: status, }, @@ -90,7 +91,7 @@ describe('\n[E2E] <%= namePascal %> 🔍', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -98,7 +99,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { diff --git a/app/templates/nestjsTypeOrm/E2E/__name__/store.spec.ts.txt b/app/templates/nestjsTypeOrm/E2E/__name__/store.spec.ts.txt index d2df8a6..e92a8e4 100644 --- a/app/templates/nestjsTypeOrm/E2E/__name__/store.spec.ts.txt +++ b/app/templates/nestjsTypeOrm/E2E/__name__/store.spec.ts.txt @@ -3,7 +3,6 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' describe('\n[E2E] <%= namePascal %> 🧱', () => { it('should create a new <%= nameCamel %>', async () => { @@ -11,23 +10,25 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => { const method = 'POST' const path = `/<%= namePluralCamel %>` - const storeBlueprint = <%= nameCamel %>Seeder.storeBlueprint() + const payload = { + name: 'test' + } const { body } = await request(app.server.getHttpServer()) .post(path) - .send(storeBlueprint) + .send(payload) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) - expect(body.data.name).toBe(storeBlueprint.name) + expect(body.data.name).toBe(payload.name) }) // Implement all properties in app/Contracts/Dtos/<%= namePascal %>Dto.ts // And create tests to validate if the schema validator are working well. // it('should throw validation errors when trying to create a <%= nameCamel %> with validation errors', async () => { - // const status = 400 + // const status = 422 // const path = `/<%= namePluralCamel %>` // // const { body } = await request(app.server.getHttpServer()) @@ -46,15 +47,12 @@ describe('\n[E2E] <%= namePascal %> 🧱', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') app = await new App([AppModule]).initApp() database = new Database(app) - - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) }) afterEach(async () => { diff --git a/app/templates/nestjsTypeOrm/E2E/__name__/update.spec.ts.txt b/app/templates/nestjsTypeOrm/E2E/__name__/update.spec.ts.txt index d4d40c4..a6e8e4b 100644 --- a/app/templates/nestjsTypeOrm/E2E/__name__/update.spec.ts.txt +++ b/app/templates/nestjsTypeOrm/E2E/__name__/update.spec.ts.txt @@ -3,46 +3,48 @@ import * as request from 'supertest' import { Debug } from '@secjs/logger' import { AppModule } from 'app/AppModule' import { App, Database } from 'test/Utils' -import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' +import { <%= namePascal %>Factory } from 'database/factories/<%= namePascal %>Factory' describe('\n[E2E] <%= namePascal %> 🛠', () => { it('should update one <%= nameCamel %>', async () => { - const <%= nameCamel %> = await <%= nameCamel %>Seeder.seedOne() + const <%= nameCamel %> = await <%= nameCamel %>Factory.create() const status = 200 const method = 'PUT' - const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}` + const path = `/<%= namePluralCamel %>/${<%= nameCamel %>.id}?apiKey=${apiKey}` - const updateBlueprint = <%= nameCamel %>Seeder.updateBlueprint() + const payload = { + name: 'test', + } const { body } = await request(app.server.getHttpServer()) .put(path) - .send(updateBlueprint) + .send(payload) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.data.id).toBe(<%= nameCamel %>.id) + expect(body.data.name).toBe(payload.name) }) it('should throw a not found error when can not find <%= nameCamel %>', async () => { const status = 404 const method = 'PUT' - const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` - - const updateBlueprint = <%= nameCamel %>Seeder.blueprint() + const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011?apiKey=${apiKey}` const { body } = await request(app.server.getHttpServer()) .put(path) - .send(updateBlueprint) + .send({ name: 'test' }) .expect(status) expect(body.method).toBe(method) expect(body.status).toBe(status) expect(body.error).toEqual({ + isSecJsException: true, name: 'NotFoundException', message: { - error: 'Not Found', + error: 'Not Found Error', message: 'NOT_FOUND_<%= nameUp %>', statusCode: status, }, @@ -53,7 +55,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => { // And create tests to validate if the schema validator are working well. // it('should throw validation errors when trying to update a <%= nameCamel %> with validation errors', async () => { - // const status = 400 + // const status = 422 // const path = `/<%= namePluralCamel %>/507f1f77bcf86cd799439011` // // const { body } = await request(app.server.getHttpServer()) @@ -72,7 +74,7 @@ describe('\n[E2E] <%= namePascal %> 🛠', () => { let app: App let database: Database -let <%= nameCamel %>Seeder: <%= namePascal %>Seeder +let <%= nameCamel %>Factory: <%= namePascal %>Factory beforeEach(async () => { Debug(`Executing ${beforeEach.name}`, 'api:test') @@ -80,7 +82,7 @@ beforeEach(async () => { app = await new App([AppModule]).initApp() database = new Database(app) - <%= nameCamel %>Seeder = app.getInstance(<%= namePascal %>Seeder) + <%= nameCamel %>Factory = app.getInstance(<%= namePascal %>Factory) }) afterEach(async () => { @@ -90,4 +92,3 @@ afterEach(async () => { await database.truncateCache() await app.closeApp() }) - diff --git a/app/templates/nestjsTypeOrm/__name__Controller.ts.txt b/app/templates/nestjsTypeOrm/__name__Controller.ts.txt index ac85ab3..21e842a 100644 --- a/app/templates/nestjsTypeOrm/__name__Controller.ts.txt +++ b/app/templates/nestjsTypeOrm/__name__Controller.ts.txt @@ -29,8 +29,8 @@ export class <%= namePascal %>Controller { private <%= nameCamel %>Service: <%= namePascal %>Service @Get() - @ApiQuery({ name: 'limit', allowEmptyValue: true }) @ApiQuery({ name: 'page', allowEmptyValue: true }) + @ApiQuery({ name: 'limit', allowEmptyValue: true }) async index( @Query(QueryParamsPipe) queries: any, @Pagination() paginate: PaginationContract, diff --git a/app/templates/nestjsTypeOrm/__name__Factory.ts.txt b/app/templates/nestjsTypeOrm/__name__Factory.ts.txt new file mode 100644 index 0000000..29f5cba --- /dev/null +++ b/app/templates/nestjsTypeOrm/__name__Factory.ts.txt @@ -0,0 +1,25 @@ +import { Inject, Injectable } from '@nestjs/common' +import { Factory } from '@secjs/base/database/Factory' +import { <%= namePascal %>Seeder } from 'database/seeds/<%= namePascal %>Seeder' + +@Injectable() +export class <%= namePascal %>Factory extends Factory { + @Inject(<%= namePascal %>Seeder) protected seeder: <%= namePascal %>Seeder + + blueprint = () => { + return { + example: this.faker.name.findName(), + } + } + + fakeBlueprint = () => { + return { + id: this.faker.datatype.uuid(), + name: this.faker.name.findName(), + status: 'pendent', + createdAt: new Date(), + updatedAt: new Date(), + deletedAt: null, + } + } +} diff --git a/app/templates/nestjsTypeOrm/__name__Seeder.ts.txt b/app/templates/nestjsTypeOrm/__name__Seeder.ts.txt index 9a4a6ea..c0438f4 100644 --- a/app/templates/nestjsTypeOrm/__name__Seeder.ts.txt +++ b/app/templates/nestjsTypeOrm/__name__Seeder.ts.txt @@ -1,75 +1,8 @@ -import { name } from 'faker' import { Inject, Injectable } from '@nestjs/common' -import { <%= namePascal %> } from 'app/Models/<%= namePascal %>' -import { ExtraParams } from 'database/decorators/ExtraParams' +import { Seeder } from '@secjs/base/database/Seeder' import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository' @Injectable() -export class <%= namePascal %>Seeder { - @Inject(<%= namePascal %>Repository) private <%= nameCamel %>Repository: <%= namePascal %>Repository - - @ExtraParams(1) - blueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } - - async seedOne(extraParams?: any): Promise<<%= namePascal %>> { - return this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)) - } - - async seedMany(number: number, extraParams?: any): Promise<<%= namePascal %>[]> { - const promises = [] - - for (let i = 1; i <= number; i++) { - promises.push( - this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)), - ) - } - - return Promise.all(promises) - } - - @ExtraParams(1) - async seedOneDeleted(extraParams?: any): Promise<<%= namePascal %>> { - extraParams.deletedAt = new Date() - - return this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)) - } - - @ExtraParams(2) - async seedManyDeleted(number: number, extraParams?: any): Promise<<%= namePascal %>[]> { - const promises = [] - - extraParams.deletedAt = new Date() - - for (let i = 1; i <= number; i++) { - promises.push( - this.<%= nameCamel %>Repository.storeOne(this.blueprint(extraParams)), - ) - } - - return Promise.all(promises) - } - - @ExtraParams(1) - storeBlueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } - - @ExtraParams(1) - updateBlueprint(extraParams?: any) { - return { - // Your model props here - name: name.findName(), - ...extraParams, - } - } +export class <%= namePascal %>Seeder extends Seeder { + @Inject(<%= namePascal %>Repository) protected repository: <%= namePascal %>Repository } diff --git a/app/templates/nestjsTypeOrm/__name__Service.ts.txt b/app/templates/nestjsTypeOrm/__name__Service.ts.txt index 02ea378..d8d22c0 100644 --- a/app/templates/nestjsTypeOrm/__name__Service.ts.txt +++ b/app/templates/nestjsTypeOrm/__name__Service.ts.txt @@ -1,56 +1,13 @@ -import { - Create<%= namePascal %>Dto, - Update<%= namePascal %>Dto, -} from 'app/Contracts/Dtos/<%= namePascal %>Dto' - -import { Options } from 'app/Decorators/Services/Options' +import { Inject, Injectable } from '@nestjs/common' +import { Options } from '@secjs/base/decorators/Options' +import { BaseService } from '@secjs/base/services/BaseService' import { <%= namePascal %>Resource } from 'app/Resources/<%= namePascal %>Resource' import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository' -import { Inject, Injectable, NotFoundException } from '@nestjs/common' -import { ApiRequestContract, PaginationContract } from '@secjs/contracts' @Injectable() -export class <%= namePascal %>Service { - @Inject(<%= namePascal %>Repository) - private <%= nameCamel %>Repository: <%= namePascal %>Repository - - @Options() - async findAll(paginate: PaginationContract, options?: ApiRequestContract) { - const { data, meta, links }: any = await this.<%= nameCamel %>Repository.getAll(paginate, options) - - return { - meta, - links, - data: new <%= namePascal %>Resource().toArray(data), - } - } - - @Options() - async findOne(id: string, options?: ApiRequestContract) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.getOne(id, options) - - if (!<%= nameCamel %>) { - throw new NotFoundException('NOT_FOUND_<%= nameUp %>') - } - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async createOne(dto: Create<%= namePascal %>Dto) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.storeOne(dto) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async updateOne(id: string, dto: Update<%= namePascal %>Dto) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.updateOne(await this.findOne(id), dto) - - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } - - async deleteOne(id: string) { - const <%= nameCamel %> = await this.<%= nameCamel %>Repository.deleteOne(await this.findOne(id)) +export class <%= namePascal %>Service extends BaseService { + protected resourceName = '<%= nameCamel %>' + protected resource = new <%= namePascal %>Resource() - return new <%= namePascal %>Resource().toJson(<%= nameCamel %>) - } + @Inject(<%= namePascal %>Repository) protected repository: <%= namePascal %>Repository } diff --git a/package.json b/package.json index de96fe6..7aa695a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "generator-secjs", - "version": "1.2.1", + "version": "1.2.2", "description": "🧬 Generator for any NodeJS Project or Framework", "main": "app/index.js", "scripts": { diff --git a/tests/nestjsMongoose.spec.js b/tests/nestjsMongoose.spec.js index 6d61e6a..8061fc1 100644 --- a/tests/nestjsMongoose.spec.js +++ b/tests/nestjsMongoose.spec.js @@ -18,6 +18,7 @@ describe('\n NestJS Mongoose 😸', () => { './Foo/Bar.ts', './Foo/BarSeeder.ts', './Foo/BarService.ts', + './Foo/BarFactory.ts', './Foo/Dtos/BarDto.ts', './Foo/BarResource.ts', './Foo/BarRepository.ts', diff --git a/tests/nestjsPrismaOrm.spec.js b/tests/nestjsPrismaOrm.spec.js index 75c9969..0dbb40c 100644 --- a/tests/nestjsPrismaOrm.spec.js +++ b/tests/nestjsPrismaOrm.spec.js @@ -17,6 +17,7 @@ describe('\n NestJS PrismaORM 😸', () => { assert.file([ './Foo/BarSeeder.ts', './Foo/BarService.ts', + './Foo/BarFactory.ts', './Foo/Dtos/BarDto.ts', './Foo/BarResource.ts', './Foo/BarController.ts', diff --git a/tests/nestjsTypeOrm.spec.js b/tests/nestjsTypeOrm.spec.js index bdcb72b..c5c52e3 100644 --- a/tests/nestjsTypeOrm.spec.js +++ b/tests/nestjsTypeOrm.spec.js @@ -18,6 +18,7 @@ describe('\n NestJS TypeORM 😸', () => { './Foo/Bar.ts', './Foo/BarSeeder.ts', './Foo/BarService.ts', + './Foo/BarFactory.ts', './Foo/Dtos/BarDto.ts', './Foo/BarResource.ts', './Foo/BarController.ts',