From e11ba1c5c502ed6ce1c9de166d0f5a5e9a342443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Thu, 26 Oct 2023 23:54:56 -0300 Subject: [PATCH 1/5] Migrated create rate plans functions --- src/rate-plans/rate-plans.module.ts | 23 ++++++--- src/rate-plans/rate-plans.resolver.ts | 32 ++---------- src/rate-plans/rate-plans.service.ts | 28 ----------- src/rate-plans/resolvers/create.resolver.ts | 46 ++++++++++++++++++ src/rate-plans/services/create.service.ts | 54 +++++++++++++++++++++ 5 files changed, 120 insertions(+), 63 deletions(-) create mode 100644 src/rate-plans/resolvers/create.resolver.ts create mode 100644 src/rate-plans/services/create.service.ts diff --git a/src/rate-plans/rate-plans.module.ts b/src/rate-plans/rate-plans.module.ts index bd8a909..855151d 100644 --- a/src/rate-plans/rate-plans.module.ts +++ b/src/rate-plans/rate-plans.module.ts @@ -1,16 +1,27 @@ //External -import { Module } from '@nestjs/common'; +import { Module } from "@nestjs/common"; //Service -import { RatePlansService } from './rate-plans.service'; +import { RatePlansService } from "./rate-plans.service"; //Resolvers -import { RatePlansResolver } from './rate-plans.resolver'; +import { RatePlansResolver } from "./rate-plans.resolver"; //Modules -import { DatabaseModule } from 'src/database/database.module'; +import { DatabaseModule } from "src/database/database.module"; //Providers -import { RatePlansProviders } from './rate-plans.providers'; +import { RatePlansProviders } from "./rate-plans.providers"; +import { CreateRatePlansService } from "./services/create.service"; +import { CreateRatePlansResolver } from "./resolvers/create.resolver"; @Module({ imports: [DatabaseModule], - providers: [...RatePlansProviders, RatePlansService, RatePlansResolver], + providers: [ + //Providers + ...RatePlansProviders, + //createRatePlan + CreateRatePlansService, + CreateRatePlansResolver, + //Next for refactor + RatePlansService, + RatePlansResolver, + ], }) export class RatePlansModule {} diff --git a/src/rate-plans/rate-plans.resolver.ts b/src/rate-plans/rate-plans.resolver.ts index b23356f..29a0bc7 100644 --- a/src/rate-plans/rate-plans.resolver.ts +++ b/src/rate-plans/rate-plans.resolver.ts @@ -2,12 +2,12 @@ //External import { Query, Resolver, Args, Int, Mutation } from "@nestjs/graphql"; import { ApiOperation, ApiTags } from "@nestjs/swagger"; -import { Get, Patch, Post } from "@nestjs/common"; -//Service -import { RatePlansService } from "./rate-plans.service"; +import { Get, Patch } from "@nestjs/common"; //Models import { RatePlans } from "./models/rate-plans.entity"; import { RatePlansDTO } from "./models/rate-plans.dto"; +//Service +import { RatePlansService } from "./rate-plans.service"; //const-vars let msgResponse: string; let msgLog: string; @@ -19,32 +19,6 @@ let msgLog: string; export class RatePlansResolver { constructor(private ratePlansService: RatePlansService) {} - /** - * @description Controller to add a rate plan - * @param {RatePlansDTO} ratePlanObj RatePlansDTO type - * @returns a response with the rate plan object and status code - */ - @Mutation(() => RatePlans, { name: "createRatePlan" }) - @Post("/") - @ApiOperation({ summary: "Add a rate plans" }) - async createRatePlan( - @Args({ name: "ratePlanObj" }) ratePlanObj: RatePlansDTO - ): Promise { - try { - msgResponse = null; - msgLog = null; - - // --- start with database operations --- - return await this.ratePlansService.createRatePlan(ratePlanObj); - // --- end with database operations --- - } catch (error) { - msgResponse = "ERROR in createRatePlan function resolver"; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } - /** * @description Controller to update a rate plan * @param {number} id number type diff --git a/src/rate-plans/rate-plans.service.ts b/src/rate-plans/rate-plans.service.ts index ad5037f..f1a6bb5 100644 --- a/src/rate-plans/rate-plans.service.ts +++ b/src/rate-plans/rate-plans.service.ts @@ -12,7 +12,6 @@ import { validateObject } from "./helpers/models/validateObject"; let checkObj: any; let ratePlansList: RatePlans[]; let ratePlanObj: RatePlans; -let newRatePlan: any; let updateRatePlan: any; let msgResponse: string; let msgLog: string; @@ -28,33 +27,6 @@ export class RatePlansService { private ratePlansRepository: Repository ) {} - /** - * @description Service to create and save a rate plan - * @param {RatePlansDTO} ratePlan RatePlansDTO type - * @returns an object with the ratePlan - */ - async createRatePlan(ratePlan: RatePlansDTO): Promise { - try { - msgResponse = null; - msgLog = null; - - //-- start with validation object --- - checkObj = await validateObject(ratePlan); - if (checkObj.length) { - return checkObj; - } - //-- end with validation object --- - newRatePlan = this.ratePlansRepository.create(ratePlan); - - return await this.ratePlansRepository.save(newRatePlan); - } catch (error) { - msgResponse = "ERROR in createRatePlan function service"; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } - /** * @description Service to update and save a rate plan * @param {number} inputId number type diff --git a/src/rate-plans/resolvers/create.resolver.ts b/src/rate-plans/resolvers/create.resolver.ts new file mode 100644 index 0000000..c71c518 --- /dev/null +++ b/src/rate-plans/resolvers/create.resolver.ts @@ -0,0 +1,46 @@ +/* eslint-disable prettier/prettier */ +//External +import { Resolver, Args, Mutation } from '@nestjs/graphql'; +import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { Post } from '@nestjs/common'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +import { RatePlansDTO } from '.././models/rate-plans.dto'; +//Service +import { CreateRatePlansService } from '.././services/create.service'; +//const-vars +let msgResponse: string; +let msgLog: string; + +//Check +@Resolver() +@ApiTags('CreateRatePlansResolver') +export class CreateRatePlansResolver { + constructor(private createPlansService: CreateRatePlansService) {} + + /** + * @description function for add a rate plan to database + * @param {RatePlansDTO} ratePlanObj RatePlansDTO type + * @returns a response with the rate plan object and status code + */ + @Mutation(() => RatePlans, { name: 'createRatePlan' }) + @Post('/') + @ApiOperation({ summary: 'Add a rate plans' }) + async createRatePlan( + @Args({ name: 'ratePlanObj' }) ratePlanObj: RatePlansDTO, + ): Promise { + try { + msgResponse = null; + msgLog = null; + + // --- start with database operations --- + return await this.createPlansService.create(ratePlanObj); + // --- end with database operations --- + } catch (error) { + msgResponse = 'ERROR in createRatePlan function for CreateRatePlansResolver class'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } +} diff --git a/src/rate-plans/services/create.service.ts b/src/rate-plans/services/create.service.ts new file mode 100644 index 0000000..e4ca59c --- /dev/null +++ b/src/rate-plans/services/create.service.ts @@ -0,0 +1,54 @@ +/* eslint-disable prettier/prettier */ +//External +import { Injectable, Inject } from "@nestjs/common"; +import { Repository } from "typeorm"; +//Models +import { RatePlans } from "../models/rate-plans.entity"; +import { RatePlansDTO } from "../models/rate-plans.dto"; +//Enums +//Helpers +import { validateObject } from "../helpers/models/validateObject"; +//Const-vars +let checkObj: any; +let newRatePlan: any; +let msgResponse: string; +let msgLog: string; + +/** + * @description Class service for create a rate plan + * @param {Object} event Object type + */ +@Injectable() +export class CreateRatePlansService { + constructor( + @Inject("RATE_PLANS_REPOSITORY") + private ratePlansRepository: Repository + ) {} + + /** + * @description function to create and save a rate plan + * @param {RatePlansDTO} ratePlan RatePlansDTO type + * @returns an object with the ratePlan + */ + async create(ratePlan: RatePlansDTO): Promise { + try { + msgResponse = null; + msgLog = null; + + //-- start with validation object --- + checkObj = await validateObject(ratePlan); + if (checkObj.length) { + return checkObj; + } + //-- end with validation object --- + newRatePlan = this.ratePlansRepository.create(ratePlan); + + return await this.ratePlansRepository.save(newRatePlan); + } catch (error) { + msgResponse = "ERROR in create function for CreateRatePlansService class"; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } +} \ No newline at end of file From 179f337ac095ff7f1a254229db0d6f4d256adce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Sun, 5 Nov 2023 22:15:02 -0300 Subject: [PATCH 2/5] For review update rate plan logic --- src/rate-plans/rate-plans.module.ts | 7 +- src/rate-plans/rate-plans.resolver.ts | 78 ++++++----------- src/rate-plans/rate-plans.service.ts | 97 ++++++++------------- src/rate-plans/resolvers/update.resolver.ts | 47 ++++++++++ src/rate-plans/services/update.service.ts | 62 +++++++++++++ 5 files changed, 176 insertions(+), 115 deletions(-) create mode 100644 src/rate-plans/resolvers/update.resolver.ts create mode 100644 src/rate-plans/services/update.service.ts diff --git a/src/rate-plans/rate-plans.module.ts b/src/rate-plans/rate-plans.module.ts index 855151d..a2a906e 100644 --- a/src/rate-plans/rate-plans.module.ts +++ b/src/rate-plans/rate-plans.module.ts @@ -10,15 +10,20 @@ import { DatabaseModule } from "src/database/database.module"; import { RatePlansProviders } from "./rate-plans.providers"; import { CreateRatePlansService } from "./services/create.service"; import { CreateRatePlansResolver } from "./resolvers/create.resolver"; +import { UpdateRatePlansService } from "./services/update.service"; +import { UpdateRatePlansResolver } from "./resolvers/update.resolver"; @Module({ imports: [DatabaseModule], providers: [ //Providers ...RatePlansProviders, - //createRatePlan + //CreateRatePlan CreateRatePlansService, CreateRatePlansResolver, + //UpdateRatePlan + UpdateRatePlansService, + UpdateRatePlansResolver, //Next for refactor RatePlansService, RatePlansResolver, diff --git a/src/rate-plans/rate-plans.resolver.ts b/src/rate-plans/rate-plans.resolver.ts index 93e5493..25bb28f 100644 --- a/src/rate-plans/rate-plans.resolver.ts +++ b/src/rate-plans/rate-plans.resolver.ts @@ -19,59 +19,33 @@ let msgLog: string; export class RatePlansResolver { constructor(private ratePlansService: RatePlansService) {} - /** - * @description Controller to add a rate plan - * @param {RatePlansDTO} ratePlanObj RatePlansDTO type - * @returns a response with the rate plan object and status code - */ - @Mutation(() => RatePlans, { name: 'createRatePlan' }) - @Post('/') - @ApiOperation({ summary: 'Add a rate plans' }) - async createRatePlan( - @Args({ name: 'ratePlanObj' }) ratePlanObj: RatePlansDTO, - ): Promise { - try { - msgResponse = null; - msgLog = null; - - // --- start with database operations --- - return await this.ratePlansService.createRatePlan(ratePlanObj); - // --- end with database operations --- - } catch (error) { - msgResponse = 'ERROR in createRatePlan function resolver'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } - - /** - * @description Controller to update a rate plan - * @param {number} id number type - * @param {RatePlansDTO} ratePlanObj RatePlansDTO type - * @returns a response with the rate plan object and status code - */ - @Mutation(() => RatePlans, { name: 'updateRatePlan' }) - @Patch('/:id') - @ApiOperation({ summary: 'Update a rate plans' }) - async updateRatePlan( - @Args({ name: 'id' }) id: number, - @Args({ name: 'ratePlanObj' }) ratePlanObj: RatePlansDTO, - ): Promise { - try { - msgResponse = null; - msgLog = null; + // /** + // * @description Controller to update a rate plan + // * @param {number} id number type + // * @param {RatePlansDTO} ratePlanObj RatePlansDTO type + // * @returns a response with the rate plan object and status code + // */ + // @Mutation(() => RatePlans, { name: 'updateRatePlan' }) + // @Patch('/:id') + // @ApiOperation({ summary: 'Update a rate plans' }) + // async updateRatePlan( + // @Args({ name: 'id' }) id: number, + // @Args({ name: 'ratePlanObj' }) ratePlanObj: RatePlansDTO, + // ): Promise { + // try { + // msgResponse = null; + // msgLog = null; - //--- start with database operations ---- - return await this.ratePlansService.updateRatePlan(id, ratePlanObj); - //--- end with database operations ---- - } catch (error) { - msgResponse = 'ERROR in updateRatePlan function resolver'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } + // //--- start with database operations ---- + // return await this.ratePlansService.updateRatePlan(id, ratePlanObj); + // //--- end with database operations ---- + // } catch (error) { + // msgResponse = 'ERROR in updateRatePlan function resolver'; + // msgLog = msgResponse + `Caused by ${error}`; + // console.log(msgLog); + // return msgResponse; + // } + // } /** * @description Controller to get a paginated listing of all rate plans diff --git a/src/rate-plans/rate-plans.service.ts b/src/rate-plans/rate-plans.service.ts index ed38aa4..a6b6d51 100644 --- a/src/rate-plans/rate-plans.service.ts +++ b/src/rate-plans/rate-plans.service.ts @@ -39,68 +39,41 @@ export class RatePlansService { private ratePlansRepository: Repository, ) {} - /** - * @description Service to create and save a rate plan - * @param {RatePlansDTO} ratePlan RatePlansDTO type - * @returns an object with the ratePlan - */ - async createRatePlan(ratePlan: RatePlansDTO): Promise { - try { - msgResponse = null; - msgLog = null; - - //-- start with validation object --- - checkObj = await validateObject(ratePlan); - if (checkObj.length) { - return checkObj; - } - //-- end with validation object --- - newRatePlan = this.ratePlansRepository.create(ratePlan); - - return await this.ratePlansRepository.save(newRatePlan); - } catch (error) { - msgResponse = 'ERROR in createRatePlan function service'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } - - /** - * @description Service to update and save a rate plan - * @param {number} inputId number type - * @param {RatePlansDTO} ratePlan RatePlansDTO type - * @returns an object with the ratePlan - */ - async updateRatePlan( - inputId: number, - ratePlan: RatePlansDTO, - ): Promise { - try { - msgResponse = null; - msgLog = null; - - //-- start with validation object --- - checkObj = await validateObject(ratePlan); - if (checkObj.length) { - return checkObj; - } - //-- end with validation object --- - - //-- start with database operation --- - updateRatePlan = await this.ratePlansRepository.update(inputId, ratePlan); - - if (updateRatePlan != (null || undefined)) { - return await this.getByIdRatePlans(inputId); - } - //-- end with database operation --- - } catch (error) { - msgResponse = 'ERROR in updateRatePlan function service'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } + // /** + // * @description Service to update and save a rate plan + // * @param {number} inputId number type + // * @param {RatePlansDTO} ratePlan RatePlansDTO type + // * @returns an object with the ratePlan + // */ + // async updateRatePlan( + // inputId: number, + // ratePlan: RatePlansDTO, + // ): Promise { + // try { + // msgResponse = null; + // msgLog = null; + + // //-- start with validation object --- + // checkObj = await validateObject(ratePlan); + // if (checkObj.length) { + // return checkObj; + // } + // //-- end with validation object --- + + // //-- start with database operation --- + // updateRatePlan = await this.ratePlansRepository.update(inputId, ratePlan); + + // if (updateRatePlan != (null || undefined)) { + // return await this.getByIdRatePlans(inputId); + // } + // //-- end with database operation --- + // } catch (error) { + // msgResponse = 'ERROR in updateRatePlan function service'; + // msgLog = msgResponse + `Caused by ${error}`; + // console.log(msgLog); + // return msgResponse; + // } + // } /** * @description Service to get a paginated listing of all rate plans diff --git a/src/rate-plans/resolvers/update.resolver.ts b/src/rate-plans/resolvers/update.resolver.ts new file mode 100644 index 0000000..ea49d23 --- /dev/null +++ b/src/rate-plans/resolvers/update.resolver.ts @@ -0,0 +1,47 @@ +/* eslint-disable prettier/prettier */ +//External +import { Resolver, Args, Mutation } from '@nestjs/graphql'; +import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { Patch } from '@nestjs/common'; +//Service +import { UpdateRatePlansService } from '../services/update.service'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +import { RatePlansDTO } from '.././models/rate-plans.dto'; +//Vars +let msgResponse: string; +let msgLog: string; + +@Resolver() +@ApiTags('UpdateRatePlansResolver') +export class UpdateRatePlansResolver { + constructor(private updateRatePlansService: UpdateRatePlansService) {} + + /** + * @description Controller to update a rate plan + * @param {number} id number type + * @param {RatePlansDTO} ratePlanObj RatePlansDTO type + * @returns a response with the rate plan object and status code + */ + @Mutation(() => RatePlans, { name: 'updateRatePlan' }) + @Patch('/:id') + @ApiOperation({ summary: 'Update a rate plans' }) + async updateRatePlan( + @Args({ name: 'id' }) id: number, + @Args({ name: 'ratePlanObj' }) ratePlanObj: RatePlansDTO, + ): Promise { + try { + msgResponse = null; + msgLog = null; + + //--- start with database operations ---- + return await this.updateRatePlansService.update(id, ratePlanObj); + //--- end with database operations ---- + } catch (error) { + msgResponse = 'ERROR in updateRatePlan function resolver'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } + } \ No newline at end of file diff --git a/src/rate-plans/services/update.service.ts b/src/rate-plans/services/update.service.ts new file mode 100644 index 0000000..0e7eed6 --- /dev/null +++ b/src/rate-plans/services/update.service.ts @@ -0,0 +1,62 @@ +/* eslint-disable prettier/prettier */ +//External +import { Injectable, Inject } from '@nestjs/common'; +import { Repository } from 'typeorm'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +import { RatePlansDTO } from '.././models/rate-plans.dto'; +//Helpers +import { validateObject } from '.././helpers/models/validateObject'; +//vars +let checkObj: any; +let updatedRatePlan: any; +let msgResponse: string; +let msgLog: string; +/** + * @description Rate plants service for all crud operations + * @param {Object} event Object type + */ +@Injectable() +export class UpdateRatePlansService { + constructor( + @Inject('RATE_PLANS_REPOSITORY') + private ratePlansRepository: Repository, + ) {} + + /** + * @description Service to update and save a rate plan + * @param {number} inputId number type + * @param {RatePlansDTO} ratePlan RatePlansDTO type + * @returns an object with the ratePlan + */ + async update( + inputId: number, + ratePlan: RatePlansDTO, + ): Promise { + try { + msgResponse = null; + msgLog = null; + + //-- start with validation object --- + checkObj = await validateObject(ratePlan); + if (checkObj.length) { + return checkObj; + } + //-- end with validation object --- + + //-- start with database operation --- + updatedRatePlan = await this.ratePlansRepository.update(inputId, ratePlan); + + if (updatedRatePlan != (null || undefined)) { + return ratePlan; + } + + //-- end with database operation --- + } catch (error) { + msgResponse = 'ERROR in update function for UpdateRatePlansService class'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } +} \ No newline at end of file From eed2dfcc79ed3c15a2655e15be49041285875b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Tue, 7 Nov 2023 23:16:42 -0300 Subject: [PATCH 3/5] Migrated gell all logic function --- src/rate-plans/rate-plans.module.ts | 5 + src/rate-plans/rate-plans.resolver.ts | 77 ++++------- src/rate-plans/rate-plans.service.ts | 134 +++++++------------ src/rate-plans/resolvers/get-all.resolver.ts | 58 ++++++++ src/rate-plans/services/get-all.service.ts | 100 ++++++++++++++ 5 files changed, 234 insertions(+), 140 deletions(-) create mode 100644 src/rate-plans/resolvers/get-all.resolver.ts create mode 100644 src/rate-plans/services/get-all.service.ts diff --git a/src/rate-plans/rate-plans.module.ts b/src/rate-plans/rate-plans.module.ts index a2a906e..9872846 100644 --- a/src/rate-plans/rate-plans.module.ts +++ b/src/rate-plans/rate-plans.module.ts @@ -12,6 +12,8 @@ import { CreateRatePlansService } from "./services/create.service"; import { CreateRatePlansResolver } from "./resolvers/create.resolver"; import { UpdateRatePlansService } from "./services/update.service"; import { UpdateRatePlansResolver } from "./resolvers/update.resolver"; +import { GetAllRatePlansService } from "./services/get-all.service"; +import { GetAllRatePlansResolver } from "./resolvers/get-all.resolver"; @Module({ imports: [DatabaseModule], @@ -24,6 +26,9 @@ import { UpdateRatePlansResolver } from "./resolvers/update.resolver"; //UpdateRatePlan UpdateRatePlansService, UpdateRatePlansResolver, + //GetAllRatePlan + GetAllRatePlansService, + GetAllRatePlansResolver, //Next for refactor RatePlansService, RatePlansResolver, diff --git a/src/rate-plans/rate-plans.resolver.ts b/src/rate-plans/rate-plans.resolver.ts index 25bb28f..00b2ecb 100644 --- a/src/rate-plans/rate-plans.resolver.ts +++ b/src/rate-plans/rate-plans.resolver.ts @@ -1,13 +1,12 @@ /* eslint-disable prettier/prettier */ //External -import { Query, Resolver, Args, Int, Mutation } from '@nestjs/graphql'; +import { Query, Resolver, Args, Int } from '@nestjs/graphql'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; -import { Get, Patch, Post } from '@nestjs/common'; +import { Get } from '@nestjs/common'; //Service import { RatePlansService } from './rate-plans.service'; //Models import { RatePlans } from './models/rate-plans.entity'; -import { RatePlansDTO } from './models/rate-plans.dto'; //const-vars let msgResponse: string; let msgLog: string; @@ -20,70 +19,42 @@ export class RatePlansResolver { constructor(private ratePlansService: RatePlansService) {} // /** - // * @description Controller to update a rate plan - // * @param {number} id number type - // * @param {RatePlansDTO} ratePlanObj RatePlansDTO type - // * @returns a response with the rate plan object and status code + // * @description Controller to get a paginated listing of all rate plans + // * @param {number} pageNro number type + // * @param {number} pageSize number type + // * @param {string} orderBy string type + // * @param {string} orderAt string type + // * @returns a response with the rate plans paginated list and status code // */ - // @Mutation(() => RatePlans, { name: 'updateRatePlan' }) - // @Patch('/:id') - // @ApiOperation({ summary: 'Update a rate plans' }) - // async updateRatePlan( - // @Args({ name: 'id' }) id: number, - // @Args({ name: 'ratePlanObj' }) ratePlanObj: RatePlansDTO, - // ): Promise { + // @Get('/list') + // @ApiOperation({ summary: 'Get all paginated rate plans' }) + // @Query(() => [RatePlans], { name: 'getAllRatePlans' }) + // async getAllRatePlans( + // @Args({ name: 'pageNro', type: () => Int }) pageNro: number, + // @Args({ name: 'pageSize', type: () => Int }) pageSize: number, + // @Args({ name: 'orderBy' }) orderBy: string, + // @Args({ name: 'orderAt' }) orderAt: string, + // ): Promise { // try { // msgResponse = null; // msgLog = null; // //--- start with database operations ---- - // return await this.ratePlansService.updateRatePlan(id, ratePlanObj); + // return await this.ratePlansService.getAllRatePlans( + // pageNro, + // pageSize, + // orderBy, + // orderAt, + // ); // //--- end with database operations ---- // } catch (error) { - // msgResponse = 'ERROR in updateRatePlan function resolver'; + // msgResponse = 'ERROR in getAllRatePlans function resolver'; // msgLog = msgResponse + `Caused by ${error}`; // console.log(msgLog); // return msgResponse; // } // } - /** - * @description Controller to get a paginated listing of all rate plans - * @param {number} pageNro number type - * @param {number} pageSize number type - * @param {string} orderBy string type - * @param {string} orderAt string type - * @returns a response with the rate plans paginated list and status code - */ - @Get('/list') - @ApiOperation({ summary: 'Get all paginated rate plans' }) - @Query(() => [RatePlans], { name: 'getAllRatePlans' }) - async getAllRatePlans( - @Args({ name: 'pageNro', type: () => Int }) pageNro: number, - @Args({ name: 'pageSize', type: () => Int }) pageSize: number, - @Args({ name: 'orderBy' }) orderBy: string, - @Args({ name: 'orderAt' }) orderAt: string, - ): Promise { - try { - msgResponse = null; - msgLog = null; - - //--- start with database operations ---- - return await this.ratePlansService.getAllRatePlans( - pageNro, - pageSize, - orderBy, - orderAt, - ); - //--- end with database operations ---- - } catch (error) { - msgResponse = 'ERROR in getAllRatePlans function resolver'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } - /** * @description Controller to get a rate plan according to your id diff --git a/src/rate-plans/rate-plans.service.ts b/src/rate-plans/rate-plans.service.ts index a6b6d51..867ec70 100644 --- a/src/rate-plans/rate-plans.service.ts +++ b/src/rate-plans/rate-plans.service.ts @@ -4,11 +4,9 @@ import { Injectable, Inject } from '@nestjs/common'; import { Repository } from 'typeorm'; //Models import { RatePlans } from './models/rate-plans.entity'; -import { RatePlansDTO } from './models/rate-plans.dto'; //Enums import { paginationNameValueError } from './enums/pagination/errors'; //Helpers -import { validateObject } from './helpers/models/validateObject'; import { checkOrderAt, checkOrderBy } from './helpers/pagination/rate-plans'; //const const ORDER_BY_NAME_VALUE_ERROR = @@ -16,11 +14,8 @@ const ORDER_BY_NAME_VALUE_ERROR = const ORDER_AT_NAME_VALUE_ERROR = paginationNameValueError.ORDER_AT_NAME_VALUE_ERROR; //vars -let checkObj: any; let ratePlansList: RatePlans[]; let ratePlanObj: RatePlans; -let newRatePlan: any; -let updateRatePlan: any; let msgResponse: string; let msgLog: string; //Pagination @@ -39,105 +34,70 @@ export class RatePlansService { private ratePlansRepository: Repository, ) {} + // /** - // * @description Service to update and save a rate plan - // * @param {number} inputId number type - // * @param {RatePlansDTO} ratePlan RatePlansDTO type - // * @returns an object with the ratePlan + // * @description Service to get a paginated listing of all rate plans + // * @param {number} pageNroParam number type + // * @param {number} pageSizeParam number type + // * @param {string} orderByParam string type + // * @param {string} orderAtParam string type + // * @returns an object with the products paginated list // */ - // async updateRatePlan( - // inputId: number, - // ratePlan: RatePlansDTO, - // ): Promise { + // async getAllRatePlans( + // pageNroParam: number, + // pageSizeParam: number, + // orderByParam: string, + // orderAtParam: string, + // ): Promise { // try { + // //pagination + // pageSize = 20; + // pageNro = 0; + // orderBy = 'id'; + // orderAt = 'ASC'; // msgResponse = null; // msgLog = null; - // //-- start with validation object --- - // checkObj = await validateObject(ratePlan); - // if (checkObj.length) { - // return checkObj; + // //-- start with pagination --- + // pageNro = pageNroParam != (null && undefined) ? pageNroParam : pageNro; + // pageSize = + // pageSizeParam != (null && undefined) ? pageSizeParam : pageSize; + // orderBy = orderByParam != (null && undefined) ? orderByParam : orderBy; + // orderAt = orderAtParam != (null && undefined) ? orderAtParam : orderAt; + + // orderBy = await checkOrderBy(orderBy); + + // if (orderBy == (null || undefined)) { + // return ORDER_BY_NAME_VALUE_ERROR; // } - // //-- end with validation object --- - // //-- start with database operation --- - // updateRatePlan = await this.ratePlansRepository.update(inputId, ratePlan); + // orderAt = await checkOrderAt(orderAt); - // if (updateRatePlan != (null || undefined)) { - // return await this.getByIdRatePlans(inputId); + // if (orderAt == (undefined || null)) { + // return ORDER_AT_NAME_VALUE_ERROR; // } - // //-- end with database operation --- + + // //-- end with pagination --- + + // // --- start with database operations --- + // ratePlansList = await this.ratePlansRepository.find({ + // order: { + // [orderBy]: orderAt, + // }, + // skip: pageNro, + // take: pageSize, + // }); + // //--- end with database operations --- + + // return ratePlansList; // } catch (error) { - // msgResponse = 'ERROR in updateRatePlan function service'; + // msgResponse = 'ERROR in getAllRatePlans function service'; // msgLog = msgResponse + `Caused by ${error}`; // console.log(msgLog); // return msgResponse; // } // } - /** - * @description Service to get a paginated listing of all rate plans - * @param {number} pageNroParam number type - * @param {number} pageSizeParam number type - * @param {string} orderByParam string type - * @param {string} orderAtParam string type - * @returns an object with the products paginated list - */ - async getAllRatePlans( - pageNroParam: number, - pageSizeParam: number, - orderByParam: string, - orderAtParam: string, - ): Promise { - try { - //pagination - pageSize = 20; - pageNro = 0; - orderBy = 'id'; - orderAt = 'ASC'; - msgResponse = null; - msgLog = null; - - //-- start with pagination --- - pageNro = pageNroParam != (null && undefined) ? pageNroParam : pageNro; - pageSize = - pageSizeParam != (null && undefined) ? pageSizeParam : pageSize; - orderBy = orderByParam != (null && undefined) ? orderByParam : orderBy; - orderAt = orderAtParam != (null && undefined) ? orderAtParam : orderAt; - - orderBy = await checkOrderBy(orderBy); - - if (orderBy == (null || undefined)) { - return ORDER_BY_NAME_VALUE_ERROR; - } - - orderAt = await checkOrderAt(orderAt); - - if (orderAt == (undefined || null)) { - return ORDER_AT_NAME_VALUE_ERROR; - } - - //-- end with pagination --- - - // --- start with database operations --- - ratePlansList = await this.ratePlansRepository.find({ - order: { - [orderBy]: orderAt, - }, - skip: pageNro, - take: pageSize, - }); - //--- end with database operations --- - - return ratePlansList; - } catch (error) { - msgResponse = 'ERROR in getAllRatePlans function service'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } - /** * @description Service to get a rate plans * @param {number} inputId number type diff --git a/src/rate-plans/resolvers/get-all.resolver.ts b/src/rate-plans/resolvers/get-all.resolver.ts new file mode 100644 index 0000000..322ffe8 --- /dev/null +++ b/src/rate-plans/resolvers/get-all.resolver.ts @@ -0,0 +1,58 @@ +/* eslint-disable prettier/prettier */ +//External +import { Query, Resolver, Args, Int } from '@nestjs/graphql'; +import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { Get } from '@nestjs/common'; +//Service +import { GetAllRatePlansService } from '../services/get-all.service'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +//const-vars +let msgResponse: string; +let msgLog: string; + +//Check + +@Resolver() +@ApiTags('GetAllRatePlansResolver') +export class GetAllRatePlansResolver { + constructor(private getAllService: GetAllRatePlansService) {} + + /** + * @description Controller to get a paginated listing of all rate plans + * @param {number} pageNro number type + * @param {number} pageSize number type + * @param {string} orderBy string type + * @param {string} orderAt string type + * @returns a response with the rate plans paginated list and status code + */ + @Get('/list') + @ApiOperation({ summary: 'Get all paginated rate plans' }) + @Query(() => [RatePlans], { name: 'getAllRatePlans' }) + async getAllRatePlans( + @Args({ name: 'pageNro', type: () => Int }) pageNro: number, + @Args({ name: 'pageSize', type: () => Int }) pageSize: number, + @Args({ name: 'orderBy' }) orderBy: string, + @Args({ name: 'orderAt' }) orderAt: string, + ): Promise { + try { + msgResponse = null; + msgLog = null; + + //--- start with database operations ---- + return await this.getAllService.getAll( + pageNro, + pageSize, + orderBy, + orderAt, + ); + //--- end with database operations ---- + } catch (error) { + msgResponse = 'ERROR in getAllRatePlans function for GetAllRatePlansResolver class'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } + +} \ No newline at end of file diff --git a/src/rate-plans/services/get-all.service.ts b/src/rate-plans/services/get-all.service.ts new file mode 100644 index 0000000..166759c --- /dev/null +++ b/src/rate-plans/services/get-all.service.ts @@ -0,0 +1,100 @@ +/* eslint-disable prettier/prettier */ +//External +import { Injectable, Inject } from '@nestjs/common'; +import { Repository } from 'typeorm'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +//Enums +import { paginationNameValueError } from '.././enums/pagination/errors'; +//Helpers +import { checkOrderAt, checkOrderBy } from '.././helpers/pagination/rate-plans'; +//const +const ORDER_BY_NAME_VALUE_ERROR = + paginationNameValueError.ORDER_BY_NAME_VALUE_ERROR; +const ORDER_AT_NAME_VALUE_ERROR = + paginationNameValueError.ORDER_AT_NAME_VALUE_ERROR; +//vars +let ratePlansList: RatePlans[]; +let ratePlanObj: RatePlans; +let msgResponse: string; +let msgLog: string; +//Pagination +let pageSize: number; +let pageNro: number; +let orderBy: string; +let orderAt: string; +/** + * @description Rate plants service for all crud operations + * @param {Object} event Object type + */ +@Injectable() +export class GetAllRatePlansService { + constructor( + @Inject('RATE_PLANS_REPOSITORY') + private ratePlansRepository: Repository, + ) {} + + + /** + * @description Service to get a paginated listing of all rate plans + * @param {number} pageNroParam number type + * @param {number} pageSizeParam number type + * @param {string} orderByParam string type + * @param {string} orderAtParam string type + * @returns an object with the products paginated list + */ + async getAll( + pageNroParam: number, + pageSizeParam: number, + orderByParam: string, + orderAtParam: string, + ): Promise { + try { + //pagination + pageSize = 20; + pageNro = 0; + orderBy = 'id'; + orderAt = 'ASC'; + msgResponse = null; + msgLog = null; + + //-- start with pagination --- + pageNro = pageNroParam != (null && undefined) ? pageNroParam : pageNro; + pageSize = + pageSizeParam != (null && undefined) ? pageSizeParam : pageSize; + orderBy = orderByParam != (null && undefined) ? orderByParam : orderBy; + orderAt = orderAtParam != (null && undefined) ? orderAtParam : orderAt; + + orderBy = await checkOrderBy(orderBy); + + if (orderBy == (null || undefined)) { + return ORDER_BY_NAME_VALUE_ERROR; + } + + orderAt = await checkOrderAt(orderAt); + + if (orderAt == (undefined || null)) { + return ORDER_AT_NAME_VALUE_ERROR; + } + + //-- end with pagination --- + + // --- start with database operations --- + ratePlansList = await this.ratePlansRepository.find({ + order: { + [orderBy]: orderAt, + }, + skip: pageNro, + take: pageSize, + }); + //--- end with database operations --- + + return ratePlansList; + } catch (error) { + msgResponse = 'ERROR in get-all function for GetAllRatePlansService class'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } + } \ No newline at end of file From 8801dbe12c6a51ca2c7251e392c3f0e13e90ff43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Wed, 8 Nov 2023 23:36:44 -0300 Subject: [PATCH 4/5] Migrated get-by-id functionality --- src/rate-plans/rate-plans.module.ts | 36 +++-- src/rate-plans/rate-plans.resolver.ts | 84 ------------ src/rate-plans/rate-plans.service.ts | 129 ------------------ .../resolvers/get-by-id.resolver.ts | 47 +++++++ src/rate-plans/services/get-by-id.service.ts | 65 +++++++++ 5 files changed, 129 insertions(+), 232 deletions(-) delete mode 100644 src/rate-plans/rate-plans.resolver.ts delete mode 100644 src/rate-plans/rate-plans.service.ts create mode 100644 src/rate-plans/resolvers/get-by-id.resolver.ts create mode 100644 src/rate-plans/services/get-by-id.service.ts diff --git a/src/rate-plans/rate-plans.module.ts b/src/rate-plans/rate-plans.module.ts index 9872846..f0fb71b 100644 --- a/src/rate-plans/rate-plans.module.ts +++ b/src/rate-plans/rate-plans.module.ts @@ -1,19 +1,17 @@ //External -import { Module } from "@nestjs/common"; -//Service -import { RatePlansService } from "./rate-plans.service"; -//Resolvers -import { RatePlansResolver } from "./rate-plans.resolver"; +import { Module } from '@nestjs/common'; //Modules -import { DatabaseModule } from "src/database/database.module"; +import { DatabaseModule } from 'src/database/database.module'; //Providers -import { RatePlansProviders } from "./rate-plans.providers"; -import { CreateRatePlansService } from "./services/create.service"; -import { CreateRatePlansResolver } from "./resolvers/create.resolver"; -import { UpdateRatePlansService } from "./services/update.service"; -import { UpdateRatePlansResolver } from "./resolvers/update.resolver"; -import { GetAllRatePlansService } from "./services/get-all.service"; -import { GetAllRatePlansResolver } from "./resolvers/get-all.resolver"; +import { RatePlansProviders } from './rate-plans.providers'; +import { CreateRatePlansService } from './services/create.service'; +import { CreateRatePlansResolver } from './resolvers/create.resolver'; +import { UpdateRatePlansService } from './services/update.service'; +import { UpdateRatePlansResolver } from './resolvers/update.resolver'; +import { GetAllRatePlansService } from './services/get-all.service'; +import { GetAllRatePlansResolver } from './resolvers/get-all.resolver'; +import { GetByIdRatePlansService } from './services/get-by-id.service'; +import { GetByIdRatePlansResolver } from './resolvers/get-by-id.resolver'; @Module({ imports: [DatabaseModule], @@ -26,12 +24,12 @@ import { GetAllRatePlansResolver } from "./resolvers/get-all.resolver"; //UpdateRatePlan UpdateRatePlansService, UpdateRatePlansResolver, - //GetAllRatePlan - GetAllRatePlansService, - GetAllRatePlansResolver, - //Next for refactor - RatePlansService, - RatePlansResolver, + //GetAllRatePlan + GetAllRatePlansService, + GetAllRatePlansResolver, + //GetByIdRatePlan + GetByIdRatePlansService, + GetByIdRatePlansResolver, ], }) export class RatePlansModule {} diff --git a/src/rate-plans/rate-plans.resolver.ts b/src/rate-plans/rate-plans.resolver.ts deleted file mode 100644 index 00b2ecb..0000000 --- a/src/rate-plans/rate-plans.resolver.ts +++ /dev/null @@ -1,84 +0,0 @@ -/* eslint-disable prettier/prettier */ -//External -import { Query, Resolver, Args, Int } from '@nestjs/graphql'; -import { ApiOperation, ApiTags } from '@nestjs/swagger'; -import { Get } from '@nestjs/common'; -//Service -import { RatePlansService } from './rate-plans.service'; -//Models -import { RatePlans } from './models/rate-plans.entity'; -//const-vars -let msgResponse: string; -let msgLog: string; - -//Check - -@Resolver() -@ApiTags('RatePlansResolver') -export class RatePlansResolver { - constructor(private ratePlansService: RatePlansService) {} - - // /** - // * @description Controller to get a paginated listing of all rate plans - // * @param {number} pageNro number type - // * @param {number} pageSize number type - // * @param {string} orderBy string type - // * @param {string} orderAt string type - // * @returns a response with the rate plans paginated list and status code - // */ - // @Get('/list') - // @ApiOperation({ summary: 'Get all paginated rate plans' }) - // @Query(() => [RatePlans], { name: 'getAllRatePlans' }) - // async getAllRatePlans( - // @Args({ name: 'pageNro', type: () => Int }) pageNro: number, - // @Args({ name: 'pageSize', type: () => Int }) pageSize: number, - // @Args({ name: 'orderBy' }) orderBy: string, - // @Args({ name: 'orderAt' }) orderAt: string, - // ): Promise { - // try { - // msgResponse = null; - // msgLog = null; - - // //--- start with database operations ---- - // return await this.ratePlansService.getAllRatePlans( - // pageNro, - // pageSize, - // orderBy, - // orderAt, - // ); - // //--- end with database operations ---- - // } catch (error) { - // msgResponse = 'ERROR in getAllRatePlans function resolver'; - // msgLog = msgResponse + `Caused by ${error}`; - // console.log(msgLog); - // return msgResponse; - // } - // } - - /** - * @description Controller to get a rate plan according to your id - - * @param {number} inputId number type - * @returns a response with the rate plan and status code - */ - @Get('/id/:inputId') - @ApiOperation({ summary: 'Get a rate plan according to your id' }) - @Query(() => RatePlans, { name: 'getByIdRatePlans' }) - async getByIdRatePlans( - @Args({ name: 'inputId', type: () => Int }) inputId: number, - ): Promise { - try { - msgResponse = null; - msgLog = null; - - //--- start with database operations ---- - return await this.ratePlansService.getByIdRatePlans(inputId); - //--- end with database operations ---- - } catch (error) { - msgResponse = 'ERROR in getByIdRatePlans function resolver'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } -} diff --git a/src/rate-plans/rate-plans.service.ts b/src/rate-plans/rate-plans.service.ts deleted file mode 100644 index 867ec70..0000000 --- a/src/rate-plans/rate-plans.service.ts +++ /dev/null @@ -1,129 +0,0 @@ -/* eslint-disable prettier/prettier */ -//External -import { Injectable, Inject } from '@nestjs/common'; -import { Repository } from 'typeorm'; -//Models -import { RatePlans } from './models/rate-plans.entity'; -//Enums -import { paginationNameValueError } from './enums/pagination/errors'; -//Helpers -import { checkOrderAt, checkOrderBy } from './helpers/pagination/rate-plans'; -//const -const ORDER_BY_NAME_VALUE_ERROR = - paginationNameValueError.ORDER_BY_NAME_VALUE_ERROR; -const ORDER_AT_NAME_VALUE_ERROR = - paginationNameValueError.ORDER_AT_NAME_VALUE_ERROR; -//vars -let ratePlansList: RatePlans[]; -let ratePlanObj: RatePlans; -let msgResponse: string; -let msgLog: string; -//Pagination -let pageSize: number; -let pageNro: number; -let orderBy: string; -let orderAt: string; -/** - * @description Rate plants service for all crud operations - * @param {Object} event Object type - */ -@Injectable() -export class RatePlansService { - constructor( - @Inject('RATE_PLANS_REPOSITORY') - private ratePlansRepository: Repository, - ) {} - - - // /** - // * @description Service to get a paginated listing of all rate plans - // * @param {number} pageNroParam number type - // * @param {number} pageSizeParam number type - // * @param {string} orderByParam string type - // * @param {string} orderAtParam string type - // * @returns an object with the products paginated list - // */ - // async getAllRatePlans( - // pageNroParam: number, - // pageSizeParam: number, - // orderByParam: string, - // orderAtParam: string, - // ): Promise { - // try { - // //pagination - // pageSize = 20; - // pageNro = 0; - // orderBy = 'id'; - // orderAt = 'ASC'; - // msgResponse = null; - // msgLog = null; - - // //-- start with pagination --- - // pageNro = pageNroParam != (null && undefined) ? pageNroParam : pageNro; - // pageSize = - // pageSizeParam != (null && undefined) ? pageSizeParam : pageSize; - // orderBy = orderByParam != (null && undefined) ? orderByParam : orderBy; - // orderAt = orderAtParam != (null && undefined) ? orderAtParam : orderAt; - - // orderBy = await checkOrderBy(orderBy); - - // if (orderBy == (null || undefined)) { - // return ORDER_BY_NAME_VALUE_ERROR; - // } - - // orderAt = await checkOrderAt(orderAt); - - // if (orderAt == (undefined || null)) { - // return ORDER_AT_NAME_VALUE_ERROR; - // } - - // //-- end with pagination --- - - // // --- start with database operations --- - // ratePlansList = await this.ratePlansRepository.find({ - // order: { - // [orderBy]: orderAt, - // }, - // skip: pageNro, - // take: pageSize, - // }); - // //--- end with database operations --- - - // return ratePlansList; - // } catch (error) { - // msgResponse = 'ERROR in getAllRatePlans function service'; - // msgLog = msgResponse + `Caused by ${error}`; - // console.log(msgLog); - // return msgResponse; - // } - // } - - /** - * @description Service to get a rate plans - * @param {number} inputId number type - * @returns an object with the products paginated list - */ - async getByIdRatePlans(inputId: number): Promise { - try { - msgResponse = null; - msgLog = null; - - // --- start with database operations --- - inputId = (inputId == (null || undefined || 0) ? 1 : inputId) || 1; - - ratePlanObj = await this.ratePlansRepository.findOne({ - where: { - id: inputId, - }, - }); - //--- end with database operations --- - - return ratePlanObj; - } catch (error) { - msgResponse = 'ERROR in getByIdRatePlans function service'; - msgLog = msgResponse + `Caused by ${error}`; - console.log(msgLog); - return msgResponse; - } - } -} diff --git a/src/rate-plans/resolvers/get-by-id.resolver.ts b/src/rate-plans/resolvers/get-by-id.resolver.ts new file mode 100644 index 0000000..4a63c49 --- /dev/null +++ b/src/rate-plans/resolvers/get-by-id.resolver.ts @@ -0,0 +1,47 @@ +/* eslint-disable prettier/prettier */ +//External +import { Query, Resolver, Args, Int } from '@nestjs/graphql'; +import { ApiOperation, ApiTags } from '@nestjs/swagger'; +import { Get } from '@nestjs/common'; +//Service +import { GetByIdRatePlansService } from '../services/get-by-id.service'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +//const-vars +let msgResponse: string; +let msgLog: string; + +//Check + +@Resolver() +@ApiTags('GetByIdRatePlansResolver') +export class GetByIdRatePlansResolver { + constructor(private getByIdRatePlansService: GetByIdRatePlansService) {} + + /** + * @description Controller to get a rate plan according to your id + + * @param {number} inputId number type + * @returns a response with the rate plan and status code + */ + @Get('/id/:inputId') + @ApiOperation({ summary: 'Get a rate plan according to your id' }) + @Query(() => RatePlans, { name: 'getByIdRatePlans' }) + async getByIdRatePlans( + @Args({ name: 'inputId', type: () => Int }) inputId: number, + ): Promise { + try { + msgResponse = null; + msgLog = null; + + //--- start with database operations ---- + return await this.getByIdRatePlansService.getById(inputId); + //--- end with database operations ---- + } catch (error) { + msgResponse = 'ERROR in getByIdRatePlans function resolver'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } +} diff --git a/src/rate-plans/services/get-by-id.service.ts b/src/rate-plans/services/get-by-id.service.ts new file mode 100644 index 0000000..69eaca5 --- /dev/null +++ b/src/rate-plans/services/get-by-id.service.ts @@ -0,0 +1,65 @@ +/* eslint-disable prettier/prettier */ +//External +import { Injectable, Inject } from '@nestjs/common'; +import { Repository } from 'typeorm'; +//Models +import { RatePlans } from '.././models/rate-plans.entity'; +//Enums +import { paginationNameValueError } from '.././enums/pagination/errors'; +//Helpers +import { checkOrderAt, checkOrderBy } from '.././helpers/pagination/rate-plans'; +//const +const ORDER_BY_NAME_VALUE_ERROR = + paginationNameValueError.ORDER_BY_NAME_VALUE_ERROR; +const ORDER_AT_NAME_VALUE_ERROR = + paginationNameValueError.ORDER_AT_NAME_VALUE_ERROR; +//vars +let ratePlansList: RatePlans[]; +let ratePlanObj: RatePlans; +let msgResponse: string; +let msgLog: string; +//Pagination +let pageSize: number; +let pageNro: number; +let orderBy: string; +let orderAt: string; +/** + * @description Rate plants service for all crud operations + * @param {Object} event Object type + */ +@Injectable() +export class GetByIdRatePlansService { + constructor( + @Inject('RATE_PLANS_REPOSITORY') + private ratePlansRepository: Repository, + ) {} + + /** + * @description Service to get a rate plans + * @param {number} inputId number type + * @returns an object with the products paginated list + */ + async getById(inputId: number): Promise { + try { + msgResponse = null; + msgLog = null; + + // --- start with database operations --- + inputId = (inputId == (null || undefined || 0) ? 1 : inputId) || 1; + + ratePlanObj = await this.ratePlansRepository.findOne({ + where: { + id: inputId, + }, + }); + //--- end with database operations --- + + return ratePlanObj; + } catch (error) { + msgResponse = 'ERROR in getById function for GetByIdService class'; + msgLog = msgResponse + `Caused by ${error}`; + console.log(msgLog); + return msgResponse; + } + } +} From d126628425b84c8e29a8ae99031431ec0d46c170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Weitzel?= Date: Fri, 10 Nov 2023 23:53:55 -0300 Subject: [PATCH 5/5] Updated files --- .../graphql/examples/createRatePlan.txt | 27 +++++++++ .../graphql/examples/getAllRatePlans.txt | 16 ++++++ .../graphql/examples/getByIdRatePlans.txt | 16 ++++++ .../graphql/examples/updateRatePlan.txt | 25 +++++++++ src/rate-plans/graphql/mutations.txt | 56 ------------------- src/rate-plans/graphql/queries.txt | 44 --------------- src/rate-plans/resolvers/create.resolver.ts | 5 +- src/rate-plans/resolvers/get-all.resolver.ts | 6 +- src/rate-plans/resolvers/update.resolver.ts | 2 +- src/rate-plans/services/create.service.ts | 18 +++--- src/rate-plans/services/get-all.service.ts | 6 +- src/rate-plans/services/update.service.ts | 9 ++- 12 files changed, 109 insertions(+), 121 deletions(-) create mode 100644 src/rate-plans/graphql/examples/createRatePlan.txt create mode 100644 src/rate-plans/graphql/examples/getAllRatePlans.txt create mode 100644 src/rate-plans/graphql/examples/getByIdRatePlans.txt create mode 100644 src/rate-plans/graphql/examples/updateRatePlan.txt delete mode 100644 src/rate-plans/graphql/mutations.txt delete mode 100644 src/rate-plans/graphql/queries.txt diff --git a/src/rate-plans/graphql/examples/createRatePlan.txt b/src/rate-plans/graphql/examples/createRatePlan.txt new file mode 100644 index 0000000..9585796 --- /dev/null +++ b/src/rate-plans/graphql/examples/createRatePlan.txt @@ -0,0 +1,27 @@ +# Create a rate plan +mutation { + createRatePlan( + ratePlanObj:{ + name: "IOPA-278", + description: "plan for groupal annual subscriptions, until ten peoples", + versionId: 7822111, + versionPlan: "2.4", + status: "Published", + typePlan: "Annually - Groupal Subscribers", + subscriptionCharge: 1.1, + numberOfTiers: 3, +} + ) { + id + name + description + versionId + versionPlan + status + typePlan + subscriptionCharge + numberOfTiers + creationDate + updateDate + } +} \ No newline at end of file diff --git a/src/rate-plans/graphql/examples/getAllRatePlans.txt b/src/rate-plans/graphql/examples/getAllRatePlans.txt new file mode 100644 index 0000000..f069b5a --- /dev/null +++ b/src/rate-plans/graphql/examples/getAllRatePlans.txt @@ -0,0 +1,16 @@ +# Get All Rate Plans +query { + getAllRatePlans(pageNro: 0, pageSize: 100, orderBy: "name", orderAt: "asc") { + id + name + description + versionId + versionPlan + status + typePlan + subscriptionCharge + numberOfTiers + creationDate + updateDate + } +} diff --git a/src/rate-plans/graphql/examples/getByIdRatePlans.txt b/src/rate-plans/graphql/examples/getByIdRatePlans.txt new file mode 100644 index 0000000..d606730 --- /dev/null +++ b/src/rate-plans/graphql/examples/getByIdRatePlans.txt @@ -0,0 +1,16 @@ +# get by id Rate Plan +query { + getByIdRatePlans(inputId: 2) { + id + name + description + versionId + versionPlan + status + typePlan + subscriptionCharge + numberOfTiers + creationDate + updateDate + } +} diff --git a/src/rate-plans/graphql/examples/updateRatePlan.txt b/src/rate-plans/graphql/examples/updateRatePlan.txt new file mode 100644 index 0000000..a5504ca --- /dev/null +++ b/src/rate-plans/graphql/examples/updateRatePlan.txt @@ -0,0 +1,25 @@ +# Update a Rate Plan +mutation { + updateRatePlan( + id : 2, + ratePlanObj:{ + name: "IOPA-27822", + description: "plan for groupal annual subscriptions, until ten peoples", + versionId: 7822111, + versionPlan: "2.4", + status: "Published", + typePlan: "Annually - Groupal Subscribers", + subscriptionCharge: 1.1, + numberOfTiers: 3, +} + ) { + name + description + versionId + versionPlan + status + typePlan + subscriptionCharge + numberOfTiers + } +} \ No newline at end of file diff --git a/src/rate-plans/graphql/mutations.txt b/src/rate-plans/graphql/mutations.txt deleted file mode 100644 index bdc9d1c..0000000 --- a/src/rate-plans/graphql/mutations.txt +++ /dev/null @@ -1,56 +0,0 @@ -# Add a rate plan -mutation { - createRatePlan( - ratePlanObj:{ - name: "IOPA-278", - description: "plan for groupal annual subscriptions, until ten peoples", - versionId: 7822111, - versionPlan: "2.4", - status: "Published", - typePlan: "Annually - Groupal Subscribers", - subscriptionCharge: 1.1, - numberOfTiers: 3, -} - ) { - id - name - description - versionId - versionPlan - status - typePlan - subscriptionCharge - numberOfTiers - creationDate - updateDate - } -} - -# Update a rate plan -mutation { - updateRatePlan( - id:8, - ratePlanObj:{ - name: "IOPA-279", - description: "plan for groupal annual subscriptions, until ten peoples", - versionId: 7822114, - versionPlan: "2.5", - status: "Published", - typePlan: "Annually - Groupal Subscribers", - subscriptionCharge: 1.2, - numberOfTiers: 3, -} - ) { - id - name - description - versionId - versionPlan - status - typePlan - subscriptionCharge - numberOfTiers - creationDate - updateDate - } -} \ No newline at end of file diff --git a/src/rate-plans/graphql/queries.txt b/src/rate-plans/graphql/queries.txt deleted file mode 100644 index 1a9e8d4..0000000 --- a/src/rate-plans/graphql/queries.txt +++ /dev/null @@ -1,44 +0,0 @@ - -# Get All paginated list -query { - getAllRatePlans( - pageNro: 0, - pageSize: 100, - orderBy: "id", - orderAt: "asc" - ) { - id - name - description - versionId - versionPlan - status - typePlan - subscriptionCharge - numberOfTiers - creationDate - updateDate - } -} - -# Get by id -query { - getByIdRatePlans( - inputId: 1, - ) { - id - name - description - versionId - versionPlan - status - typePlan - subscriptionCharge - numberOfTiers - creationDate - updateDate - } -} - - - diff --git a/src/rate-plans/resolvers/create.resolver.ts b/src/rate-plans/resolvers/create.resolver.ts index c71c518..9b85863 100644 --- a/src/rate-plans/resolvers/create.resolver.ts +++ b/src/rate-plans/resolvers/create.resolver.ts @@ -19,7 +19,7 @@ export class CreateRatePlansResolver { constructor(private createPlansService: CreateRatePlansService) {} /** - * @description function for add a rate plan to database + * @description function for add a rate plan to database * @param {RatePlansDTO} ratePlanObj RatePlansDTO type * @returns a response with the rate plan object and status code */ @@ -37,7 +37,8 @@ export class CreateRatePlansResolver { return await this.createPlansService.create(ratePlanObj); // --- end with database operations --- } catch (error) { - msgResponse = 'ERROR in createRatePlan function for CreateRatePlansResolver class'; + msgResponse = + 'ERROR in createRatePlan function for CreateRatePlansResolver class'; msgLog = msgResponse + `Caused by ${error}`; console.log(msgLog); return msgResponse; diff --git a/src/rate-plans/resolvers/get-all.resolver.ts b/src/rate-plans/resolvers/get-all.resolver.ts index 322ffe8..e3c8501 100644 --- a/src/rate-plans/resolvers/get-all.resolver.ts +++ b/src/rate-plans/resolvers/get-all.resolver.ts @@ -48,11 +48,11 @@ export class GetAllRatePlansResolver { ); //--- end with database operations ---- } catch (error) { - msgResponse = 'ERROR in getAllRatePlans function for GetAllRatePlansResolver class'; + msgResponse = + 'ERROR in getAllRatePlans function for GetAllRatePlansResolver class'; msgLog = msgResponse + `Caused by ${error}`; console.log(msgLog); return msgResponse; } } - -} \ No newline at end of file +} diff --git a/src/rate-plans/resolvers/update.resolver.ts b/src/rate-plans/resolvers/update.resolver.ts index ea49d23..52a6b47 100644 --- a/src/rate-plans/resolvers/update.resolver.ts +++ b/src/rate-plans/resolvers/update.resolver.ts @@ -44,4 +44,4 @@ export class UpdateRatePlansResolver { return msgResponse; } } - } \ No newline at end of file +} diff --git a/src/rate-plans/services/create.service.ts b/src/rate-plans/services/create.service.ts index e4ca59c..dc1097d 100644 --- a/src/rate-plans/services/create.service.ts +++ b/src/rate-plans/services/create.service.ts @@ -1,13 +1,13 @@ /* eslint-disable prettier/prettier */ //External -import { Injectable, Inject } from "@nestjs/common"; -import { Repository } from "typeorm"; +import { Injectable, Inject } from '@nestjs/common'; +import { Repository } from 'typeorm'; //Models -import { RatePlans } from "../models/rate-plans.entity"; -import { RatePlansDTO } from "../models/rate-plans.dto"; +import { RatePlans } from '../models/rate-plans.entity'; +import { RatePlansDTO } from '../models/rate-plans.dto'; //Enums //Helpers -import { validateObject } from "../helpers/models/validateObject"; +import { validateObject } from '../helpers/models/validateObject'; //Const-vars let checkObj: any; let newRatePlan: any; @@ -21,8 +21,8 @@ let msgLog: string; @Injectable() export class CreateRatePlansService { constructor( - @Inject("RATE_PLANS_REPOSITORY") - private ratePlansRepository: Repository + @Inject('RATE_PLANS_REPOSITORY') + private ratePlansRepository: Repository, ) {} /** @@ -45,10 +45,10 @@ export class CreateRatePlansService { return await this.ratePlansRepository.save(newRatePlan); } catch (error) { - msgResponse = "ERROR in create function for CreateRatePlansService class"; + msgResponse = 'ERROR in create function for CreateRatePlansService class'; msgLog = msgResponse + `Caused by ${error}`; console.log(msgLog); return msgResponse; } } -} \ No newline at end of file +} diff --git a/src/rate-plans/services/get-all.service.ts b/src/rate-plans/services/get-all.service.ts index 166759c..881408a 100644 --- a/src/rate-plans/services/get-all.service.ts +++ b/src/rate-plans/services/get-all.service.ts @@ -34,7 +34,6 @@ export class GetAllRatePlansService { private ratePlansRepository: Repository, ) {} - /** * @description Service to get a paginated listing of all rate plans * @param {number} pageNroParam number type @@ -91,10 +90,11 @@ export class GetAllRatePlansService { return ratePlansList; } catch (error) { - msgResponse = 'ERROR in get-all function for GetAllRatePlansService class'; + msgResponse = + 'ERROR in get-all function for GetAllRatePlansService class'; msgLog = msgResponse + `Caused by ${error}`; console.log(msgLog); return msgResponse; } } - } \ No newline at end of file +} diff --git a/src/rate-plans/services/update.service.ts b/src/rate-plans/services/update.service.ts index 0e7eed6..677e52d 100644 --- a/src/rate-plans/services/update.service.ts +++ b/src/rate-plans/services/update.service.ts @@ -45,9 +45,12 @@ export class UpdateRatePlansService { //-- end with validation object --- //-- start with database operation --- - updatedRatePlan = await this.ratePlansRepository.update(inputId, ratePlan); + updatedRatePlan = await this.ratePlansRepository.update( + inputId, + ratePlan, + ); - if (updatedRatePlan != (null || undefined)) { + if (updatedRatePlan != (null || undefined)) { return ratePlan; } @@ -59,4 +62,4 @@ export class UpdateRatePlansService { return msgResponse; } } -} \ No newline at end of file +}