Skip to content

Commit

Permalink
refactor changes
Browse files Browse the repository at this point in the history
implmente cambio de dependencia al hacer una validacion propia con la libreria joi
  • Loading branch information
GermanAlexis committed Sep 20, 2024
1 parent f663c4d commit fdc868c
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 119 deletions.
Empty file added .vscode/settings.json
Empty file.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@aws-amplify/backend": "^0.5.5",
"@aws-amplify/backend": "^1.2.2",
"@aws-amplify/cli": "^12.8.2",
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.23.5",
Expand Down Expand Up @@ -69,7 +69,7 @@
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"babel-cli": "^6.26.0",
"dotenv": "^16.3.1",
"dotenv": "^16.4.5",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
Expand Down
5 changes: 2 additions & 3 deletions src/clients/assessments/assessments.client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { AssessmentEntity } from 'src/models/assessment/assessment.entity';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { envs } from 'src/types/environment-config';

@Injectable()
export class AssessmentsClient {
constructor(
private readonly httpService: HttpService,
private readonly environmentConfigService: EnvironmentConfigService,
) {}

async getAssessmentByBootcampId(
Expand All @@ -16,7 +15,7 @@ export class AssessmentsClient {
try {
const response = await this.httpService
.get(
`${this.environmentConfigService.ASSESSEMENT_SERVICE_URL}/bootcamp/${bootcampId}`,
`${envs.ASSESSMENT_SERVICE_URL}/bootcamp/${bootcampId}`,
)
.toPromise();
return response.data;
Expand Down
16 changes: 6 additions & 10 deletions src/clients/bottcamps/bootcamps.client.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { BootcampEntity } from 'src/models/bootcamp/bootcamp.entity';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { envs } from 'src/types/environment-config';

@Injectable()
export class BootcampsClient {
constructor(
private readonly httpService: HttpService,
private readonly environmentConfigService: EnvironmentConfigService,
) {}

async getScoreAverage(bootcampId: string): Promise<number> {
try {
const response = await this.httpService
.get(
`${this.environmentConfigService.BOOTCAMP_SERVICE_URL}/score/${bootcampId}`,
`${envs.BOOTCAMP_SERVICE_URL}/score/${bootcampId}`,
)
.toPromise();
return response.data.score;
Expand All @@ -29,12 +28,9 @@ export class BootcampsClient {
): Promise<BootcampEntity> {
try {
const updateScoreResponse = await this.httpService
.post(
`${this.environmentConfigService.BOOTCAMP_SERVICE_URL}/score/${bootcampId}`,
{
score,
},
)
.post(`${envs.BOOTCAMP_SERVICE_URL}/score/${bootcampId}`, {
score,
})
.toPromise();
return updateScoreResponse.data;
} catch (error) {
Expand All @@ -46,7 +42,7 @@ export class BootcampsClient {
try {
const response = await this.httpService
.post(
`${this.environmentConfigService.BOOTCAMP_SERVICE_URL}/score/recalculate/${bootcampId}`,
`${envs.BOOTCAMP_SERVICE_URL}/score/recalculate/${bootcampId}`,
)
.toPromise();
return response.data;
Expand Down
7 changes: 3 additions & 4 deletions src/clients/files/files.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import * as FormData from 'form-data';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { envs } from 'src/types/environment-config';

@Injectable()
export class FilesClient {
constructor(
private readonly httpService: HttpService,
private readonly environmentConfigService: EnvironmentConfigService,
) {}

async uploadOne(file: Express.Multer.File | any) {
Expand All @@ -22,7 +21,7 @@ export class FilesClient {

const options = {
method: 'POST',
url: `${this.environmentConfigService.FILE_SERVICE_URL}/upload`,
url: `${envs.FILE_SERVICE_URL}/upload`,
headers: formData.getHeaders(),
data: formData,
} as const;
Expand All @@ -37,7 +36,7 @@ export class FilesClient {
async findOne(id: string) {
try {
return this.httpService.get(
`${this.environmentConfigService.FILE_SERVICE_URL}/${id}`,
`${envs.FILE_SERVICE_URL}/${id}`,
{ responseType: 'arraybuffer' },
);
} catch (err) {
Expand Down
5 changes: 2 additions & 3 deletions src/clients/reviews/reviews.client.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { envs } from 'src/types/environment-config';

@Injectable()
export class ReviewsClient {
constructor(
private readonly httpService: HttpService,
private readonly environmentConfigService: EnvironmentConfigService,
) {}

async getScoreAverage(bootcampId: string): Promise<number> {
try {
const response = await this.httpService
.get(
`${this.environmentConfigService.REVIEW_SERVICE_URL}/bootcamp/${bootcampId}/average`,
`${envs.REVIEW_SERVICE_URL}/bootcamp/${bootcampId}/average`,
)
.toPromise();
return response.data;
Expand Down
17 changes: 17 additions & 0 deletions src/config/database/type-orm-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { envs } from 'src/types/environment-config';
import { DataSourceOptions } from 'typeorm';

const config: DataSourceOptions = {
type: 'postgres',
host: envs.DATABASE_HOST,
port: envs.DATABASE_PORT,
username: envs.DATABASE_USER,
password: envs.DATABASE_PASSWORD,
database: envs.DATABASE_NAME,
// Only include necessary properties
entities: ['dist/**/*.entity{.ts,.js}'],
migrations: ['dist/migrations/*{.ts,.js}'], // Array of strings for migration file paths
synchronize: envs.NODE_ENV === 'dev',
};

export default config;
5 changes: 0 additions & 5 deletions src/modules/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { RouterModule } from '@nestjs/core';
import { PassportModule } from '@nestjs/passport';
import { AppController } from '../controllers/app.controller';
import { AppService } from '../services/app.service';
import { environmentConfigSchema } from '../types/environment-config';
import { AssessmentsModule } from './assessments/assessments.module';
import { BootcampsModule } from './bootcamps/bootcamps.module';
import { FilesModule } from './files/files.module';
Expand All @@ -28,10 +27,6 @@ import { UsersModule } from './users/users.module';
ConfigModule.forRoot({
envFilePath: ['.env.develop', '.env'],
isGlobal: true,
validationSchema: environmentConfigSchema,
validationOptions: {
allowUnknown: true,
},
}),
RouterModule.register([
{
Expand Down
3 changes: 1 addition & 2 deletions src/modules/assessments/assessments.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { BootcampsClient } from 'src/clients/bottcamps/bootcamps.client';
import { AssessmentsController } from 'src/controllers/assessments/assessments.controller';
import { AssessmentEntity } from 'src/models/assessment/assessment.entity';
import { AssessmentsService } from 'src/services/assessments/assessments.service';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { jwtConstants } from 'src/utils/jwt/constants.jwt';

@Module({
Expand All @@ -22,6 +21,6 @@ import { jwtConstants } from 'src/utils/jwt/constants.jwt';
TypeOrmModule.forFeature([AssessmentEntity]),
],
controllers: [AssessmentsController],
providers: [AssessmentsService, BootcampsClient, EnvironmentConfigService],
providers: [AssessmentsService, BootcampsClient],
})
export class AssessmentsModule {}
2 changes: 0 additions & 2 deletions src/modules/bootcamps/bootcamps.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { ReviewEntity } from 'src/models/review/review.entity';
import { TestimonialEntity } from 'src/models/testimonial/testimonial.entity';
import { UserEntity } from 'src/models/user/user.entity';
import { BootcampsService } from 'src/services/bootcamps/bootcamps.service';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { jwtConstants } from 'src/utils/jwt/constants.jwt';

@Module({
Expand Down Expand Up @@ -52,7 +51,6 @@ import { jwtConstants } from 'src/utils/jwt/constants.jwt';
FilesClient,
ReviewsClient,
AssessmentsClient,
EnvironmentConfigService,
],
exports: [TypeOrmModule],
})
Expand Down
11 changes: 0 additions & 11 deletions src/modules/environment-config/environment-config.module.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/modules/programs/programs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { FilesClient } from 'src/clients/files/files.client';
import { ProgramsController } from 'src/controllers/programs/programs.controller';
import { ProgramEntity } from 'src/models/program/program.entity';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { ProgramsService } from 'src/services/programs/programs.service';
import { jwtConstants } from 'src/utils/jwt/constants.jwt';

Expand All @@ -22,7 +21,7 @@ import { jwtConstants } from 'src/utils/jwt/constants.jwt';
TypeOrmModule.forFeature([ProgramEntity]),
],
controllers: [ProgramsController],
providers: [ProgramsService, FilesClient, EnvironmentConfigService],
providers: [ProgramsService, FilesClient],
exports: [TypeOrmModule],
})
export class ProgramsModule {}
33 changes: 3 additions & 30 deletions src/modules/typeorm/db.module.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { EnvironmentConfigService } from 'src/services/environment-config/environment-config.service';
import { TypeOrmModule, } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import type { EnvironmentConfig } from '../../types/environment-config';
import { EnvironmentConfigModule } from '../environment-config/environment-config.module';
import config from 'src/config/database/type-orm-config';

@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule, EnvironmentConfigModule],
inject: [ConfigService, EnvironmentConfigService],
useFactory: (
config: ConfigService<EnvironmentConfig>,
environmentConfigService: EnvironmentConfigService,
) => {
return {
type: 'postgres',
host: environmentConfigService.DATABASE_HOST,
port: environmentConfigService.DATABASE_PORT,
username: environmentConfigService.DATABASE_USER,
password: environmentConfigService.DATABASE_PASSWORD,
database: environmentConfigService.DATABASE_NAME,
autoLoadEntities: true,
synchronize:
environmentConfigService.NODE_ENV ===
('dev' satisfies EnvironmentConfig['NODE_ENV']),
} as TypeOrmModuleOptions;
},
}),
],
providers: [ConfigModule, DbModule],
exports: [],
imports: [TypeOrmModule.forRoot(config)],
})
export class DbModule {
constructor(private dataSource: DataSource) {}
Expand Down
30 changes: 0 additions & 30 deletions src/services/environment-config/environment-config.service.ts

This file was deleted.

54 changes: 39 additions & 15 deletions src/types/environment-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import 'dotenv/config'
import * as Joi from 'joi';
interface EnvironmentConfig {
DATABASE_HOST: string;
DATABASE_PORT: number;
DATABASE_USER: string;
DATABASE_PASSWORD: string;
DATABASE_NAME: string;
NODE_ENV: 'dev' | 'production';
PORT: number;
FILE_SERVICE_URL: string;
REVIEW_SERVICE_URL: string;
BOOTCAMP_SERVICE_URL: string;
ASSESSMENT_SERVICE_URL: string;
}

const environmentConfigSchema = Joi.object({
DATABASE_HOST: Joi.string().required(),
Expand All @@ -7,24 +21,34 @@ const environmentConfigSchema = Joi.object({
DATABASE_PASSWORD: Joi.string().required(),
DATABASE_NAME: Joi.string().required(),
NODE_ENV: Joi.string().valid('dev', 'production').required(),
PORT: Joi.number().required(),
FILE_SERVICE_URL: Joi.string().uri().required(),
REVIEW_SERVICE_URL: Joi.string().uri().required(),
BOOTCAMP_SERVICE_URL: Joi.string().uri().required(),
ASSESSEMENT_SERVICE_URL: Joi.string().uri().required(),
});
ASSESSMENT_SERVICE_URL: Joi.string().uri().required(),
}).unknown()

interface EnvironmentConfig {
DATABASE_HOST: string;
DATABASE_PORT: number;
DATABASE_USER: string;
DATABASE_PASSWORD: string;
DATABASE_NAME: string;
NODE_ENV: 'dev' | 'production';
FILE_SERVICE_URL: string;
REVIEW_SERVICE_URL: string;
BOOTCAMP_SERVICE_URL: string;
ASSESSEMENT_SERVICE_URL: string;

const { error, value } = environmentConfigSchema.validate(process.env);

if (error) {
throw new Error(`Config Validation ${error.message}`);
}

export { environmentConfigSchema };
export type { EnvironmentConfig };
const envVars: EnvironmentConfig = value;

export const envs = {
PORTS: envVars.PORT,
DATABASE_HOST: envVars.DATABASE_HOST,
DATABASE_PORT: envVars.DATABASE_PORT,
DATABASE_USER: envVars.DATABASE_USER,
DATABASE_PASSWORD: envVars.DATABASE_PASSWORD,
DATABASE_NAME: envVars.DATABASE_NAME,
NODE_ENV: envVars.NODE_ENV,
PORT: envVars.PORT,
FILE_SERVICE_URL: envVars.FILE_SERVICE_URL,
REVIEW_SERVICE_URL: envVars.REVIEW_SERVICE_URL,
BOOTCAMP_SERVICE_URL: envVars.BOOTCAMP_SERVICE_URL,
ASSESSMENT_SERVICE_URL: envVars.ASSESSMENT_SERVICE_URL,
};

0 comments on commit fdc868c

Please sign in to comment.