diff --git a/package-lock.json b/package-lock.json index e4340ce..ad55ffb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "dotenv": "^16.3.1", + "env-var": "^7.4.1", "mongoose": "^8.0.2", "mysql2": "^3.6.5", "nodemailer": "^6.9.7", @@ -4947,6 +4948,14 @@ "url": "https://github.com/fb55/entities?sponsor=1" } }, + "node_modules/env-var": { + "version": "7.4.1", + "resolved": "https://registry.npmjs.org/env-var/-/env-var-7.4.1.tgz", + "integrity": "sha512-H8Ga2SbXTQwt6MKEawWSvmxoH1+J6bnAXkuyE7eDvbGmrhIL2i+XGjzGM3DFHcJu8GY1zY9/AnBJY8uGQYPHiw==", + "engines": { + "node": ">=10" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", diff --git a/package.json b/package.json index 2b35290..60e51f6 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "dotenv": "^16.3.1", + "env-var": "^7.4.1", "mongoose": "^8.0.2", "mysql2": "^3.6.5", "nodemailer": "^6.9.7", diff --git a/src/api/notifications/notifications.service.ts b/src/api/notifications/notifications.service.ts index eae04b4..69c4258 100644 --- a/src/api/notifications/notifications.service.ts +++ b/src/api/notifications/notifications.service.ts @@ -18,7 +18,7 @@ export class NotificationsService { async findById(id: string): Promise { const notification = await this.notificationRepository.findById(id); if (!notification) { - throw new NotFoundException('User not found'); + throw new NotFoundException('Notification not found'); } return notification; } diff --git a/src/app.module.ts b/src/app.module.ts index 7e75dab..fe7366a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -9,11 +9,23 @@ import { EmailsModule } from './api/emails/emails.module'; import { ReactAdapter } from '@webtre/nestjs-mailer-react-adapter'; import { MailerModule } from '@nestjs-modules/mailer'; import { AuthModule } from './auth/auth.module'; +import { ConfigModule, ConfigService } from '@nestjs/config'; +import configuration from './config/configuration'; @Module({ imports: [ - MongooseModule.forRoot(process.env.MONGO_URI, { - dbName: process.env.DB_NAME, + ConfigModule.forRoot({ + envFilePath: `${process.cwd()}/.env.${process.env.NODE_ENV}`, + isGlobal: true, + load: [configuration], + }), + MongooseModule.forRootAsync({ + imports: [ConfigModule], + useFactory: async (configService: ConfigService) => ({ + uri: configService.get('MONGO_URI'), + dbName: configService.get('DB_NAME'), + }), + inject: [ConfigService], }), MailerModule.forRoot({ transport: { diff --git a/src/config/configuration.ts b/src/config/configuration.ts new file mode 100644 index 0000000..cdcdc82 --- /dev/null +++ b/src/config/configuration.ts @@ -0,0 +1,24 @@ +import * as env from 'env-var'; +import { Deployment, Env } from '../constants/env.constants'; + +export default () => ({ + environment: env + .get('NODE_ENV') + .required(true) + .default(Env.PRODUCTION) + .asEnum(Object.values(Env)), + deployment: env + .get('DEPLOYMENT') + .default(Deployment.DOCKER) + .asEnum(Object.values(Deployment)), + app: { + port: env.get('PORT').default(8000).asPortNumber(), + swaggerPath: env.get('SWAGGER_PATH').default('').asString(), + }, + database: { + uri: env.get('MONGO_URI').required(true).asString(), + }, + common: { + urlPrefix: env.get('URL_PREFIX').required(true).default('api').asString(), + }, +}); diff --git a/src/constants/env.constants.ts b/src/constants/env.constants.ts new file mode 100644 index 0000000..28a955c --- /dev/null +++ b/src/constants/env.constants.ts @@ -0,0 +1,7 @@ +export enum Env { + PRODUCTION = 'production', +} + +export enum Deployment { + DOCKER = 'docker', +}