From 8d6b995d9b445ed690de888081f0881b120de186 Mon Sep 17 00:00:00 2001 From: Roman Lonskii Date: Thu, 19 Dec 2024 22:57:32 +0300 Subject: [PATCH] =?UTF-8?q?module=203=20task=201=20=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=81=D0=B8=D1=8F=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 03-di/01-notification-service/app.module.ts | 14 +++++- 03-di/01-notification-service/before-test.sh | 5 ++ 03-di/01-notification-service/config.json | 5 ++ .../configuration/configuration.module.ts | 16 +++++++ .../configuration/configuration.service.ts | 47 +++++++++++++++++++ 03-di/01-notification-service/package.json | 2 +- .../providers/Logger.ts | 2 - .../providers/NotificationService.ts | 7 +-- .../tasks/task.model.ts | 10 ++-- .../tasks/tasks.module.ts | 3 +- .../tasks/tasks.service.ts | 4 +- 03-di/01-notification-service/tokens.ts | 1 + 03-di/01-notification-service/tsconfig.json | 5 +- .../users/user.model.ts | 8 ++-- package.json | 3 +- 15 files changed, 111 insertions(+), 21 deletions(-) create mode 100755 03-di/01-notification-service/before-test.sh create mode 100644 03-di/01-notification-service/config.json create mode 100644 03-di/01-notification-service/configuration/configuration.module.ts create mode 100644 03-di/01-notification-service/configuration/configuration.service.ts create mode 100644 03-di/01-notification-service/tokens.ts diff --git a/03-di/01-notification-service/app.module.ts b/03-di/01-notification-service/app.module.ts index 3d6655f..819aab3 100644 --- a/03-di/01-notification-service/app.module.ts +++ b/03-di/01-notification-service/app.module.ts @@ -1,7 +1,19 @@ import { Module } from "@nestjs/common"; import { TasksModule } from "./tasks/tasks.module"; +import {ConfigurationModule} from "./configuration/configuration.module"; +import * as path from 'node:path'; + +const configFileName = path.join('./', 'config.json'); + +console.log(configFileName); +console.log(__dirname); + +// const configFileName = path.resolve( +// `${__dirname}`, +// "./config.json" +// ) @Module({ - imports: [TasksModule], + imports: [TasksModule, ConfigurationModule.forRoot(configFileName) ], }) export class AppModule {} diff --git a/03-di/01-notification-service/before-test.sh b/03-di/01-notification-service/before-test.sh new file mode 100755 index 0000000..bd9ef27 --- /dev/null +++ b/03-di/01-notification-service/before-test.sh @@ -0,0 +1,5 @@ +echo ${pwd} +ls +echo "============" +ls ./dist +cp config.json ./dist/config.json diff --git a/03-di/01-notification-service/config.json b/03-di/01-notification-service/config.json new file mode 100644 index 0000000..b5a0461 --- /dev/null +++ b/03-di/01-notification-service/config.json @@ -0,0 +1,5 @@ +{ + "emailGetway": "some email getway", + "smsGetway": "some sms getway", + "someValue": "someValue" +} diff --git a/03-di/01-notification-service/configuration/configuration.module.ts b/03-di/01-notification-service/configuration/configuration.module.ts new file mode 100644 index 0000000..261a96b --- /dev/null +++ b/03-di/01-notification-service/configuration/configuration.module.ts @@ -0,0 +1,16 @@ +import { Module, DynamicModule, Global } from "@nestjs/common"; +import { ConfigurationService } from './configuration.service'; + +@Global() +@Module({ + providers: [ConfigurationService], + exports: [ConfigurationService] +}) +export class ConfigurationModule { + static forRoot(fileName: string): DynamicModule { + return { + module: ConfigurationModule, + providers: [{ provide: 'CONFIG_FILE_NAME', useFactory: () => fileName }], + }; + } +} diff --git a/03-di/01-notification-service/configuration/configuration.service.ts b/03-di/01-notification-service/configuration/configuration.service.ts new file mode 100644 index 0000000..3140b31 --- /dev/null +++ b/03-di/01-notification-service/configuration/configuration.service.ts @@ -0,0 +1,47 @@ +import * as fs from 'node:fs'; +import { Injectable, Inject } from "@nestjs/common"; +import { z } from "zod"; + +const ConfigSchema = z.object({ + emailGetway: z.string({ + required_error: "emailGetway not provided" + }).nonempty(), + smsGetway: z.string({ + required_error: "smsGetway not provided" + }), + someValue: z.string({ + required_error: "someValue not provided" + }) +}); + +type Config = z.infer; + +const defaultOptions: Config = { + emailGetway: 'default', + smsGetway: 'default', + someValue: 'default', +}; + +@Injectable() +export class ConfigurationService { + private config: Config = defaultOptions; + + constructor(@Inject('CONFIG_FILE_NAME') private fileName: string) { + try { + const config = JSON.parse(fs.readFileSync(this.fileName, 'utf8')); + + ConfigSchema.parse(config); + + this.config = config; + + console.log(this.config); + } catch (error) { + // console.error(error); + throw error; + } + } + + get options() { + return this.config; + } +} diff --git a/03-di/01-notification-service/package.json b/03-di/01-notification-service/package.json index 1bb8bbe..c201e3e 100644 --- a/03-di/01-notification-service/package.json +++ b/03-di/01-notification-service/package.json @@ -1,6 +1,6 @@ { "scripts": { "start": "nest start --watch", - "test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --no-warnings --experimental-vm-modules\" NODE_ENV=test jest --config jest.config.js --runInBand" + "test": "./before-test.sh && cross-env NODE_OPTIONS=\"$NODE_OPTIONS --no-warnings --experimental-vm-modules\" NODE_ENV=test jest --config jest.config.js --runInBand" } } diff --git a/03-di/01-notification-service/providers/Logger.ts b/03-di/01-notification-service/providers/Logger.ts index 64a5ec6..a04806c 100644 --- a/03-di/01-notification-service/providers/Logger.ts +++ b/03-di/01-notification-service/providers/Logger.ts @@ -2,8 +2,6 @@ import {ILogger} from './types'; import * as fs from 'node:fs'; import * as path from 'node:path'; -export const LoggerService = 'LoggerService'; - export class FileLoggerService implements ILogger { constructor(private path: string, private fileName: string) { try { diff --git a/03-di/01-notification-service/providers/NotificationService.ts b/03-di/01-notification-service/providers/NotificationService.ts index 6c8684f..a77d122 100644 --- a/03-di/01-notification-service/providers/NotificationService.ts +++ b/03-di/01-notification-service/providers/NotificationService.ts @@ -1,11 +1,12 @@ import { Injectable, Inject } from "@nestjs/common"; -import { LoggerService } from './Logger'; +import { LoggerService } from '../tokens'; import { ILogger } from './types'; +import {ConfigurationService} from "../configuration/configuration.service"; @Injectable() export class NotificationService { - constructor(@Inject(LoggerService) private logger: ILogger) { - console.log('NotificationService', logger); + constructor(@Inject(LoggerService) private logger: ILogger, private configurationService: ConfigurationService) { + console.log('NotificationService', this.configurationService.options); }; sendEmail(to: string, subject: string, message: string): void { diff --git a/03-di/01-notification-service/tasks/task.model.ts b/03-di/01-notification-service/tasks/task.model.ts index 9252184..1ee0a9b 100644 --- a/03-di/01-notification-service/tasks/task.model.ts +++ b/03-di/01-notification-service/tasks/task.model.ts @@ -10,22 +10,22 @@ export enum TaskStatus { export class Task { @IsString() @IsNotEmpty() - id: string; + id: string = ''; @IsString() @IsNotEmpty() - title: string; + title: string = ''; @IsString() @IsNotEmpty() - description: string; + description: string = ''; @IsString() @IsIn(Object.values(TaskStatus)) - status: TaskStatus; + status: TaskStatus = TaskStatus.Pending; @IsNumber() - assignedTo?: number; + assignedTo: number = 0; } export class CreateTaskDto extends PickType(Task, [ diff --git a/03-di/01-notification-service/tasks/tasks.module.ts b/03-di/01-notification-service/tasks/tasks.module.ts index c6fc8c7..36fdfd9 100644 --- a/03-di/01-notification-service/tasks/tasks.module.ts +++ b/03-di/01-notification-service/tasks/tasks.module.ts @@ -3,7 +3,8 @@ import { TasksController } from "./tasks.controller"; import { TasksService } from "./tasks.service"; import { UsersModule } from "../users/users.module"; import { NotificationService } from "../providers/NotificationService"; -import { FileLoggerService, ConsoleLoggerService, LoggerService } from '../providers/Logger'; +import { FileLoggerService, ConsoleLoggerService } from '../providers/Logger'; +import {LoggerService} from "../tokens"; const getLogger = () => process.env.LOGGER === 'file' ? new FileLoggerService(Date.now().toString(), 'logs.txt') : new ConsoleLoggerService(); diff --git a/03-di/01-notification-service/tasks/tasks.service.ts b/03-di/01-notification-service/tasks/tasks.service.ts index 0a19f33..741f7cd 100644 --- a/03-di/01-notification-service/tasks/tasks.service.ts +++ b/03-di/01-notification-service/tasks/tasks.service.ts @@ -2,6 +2,7 @@ import { BadRequestException, Injectable, NotFoundException, Inject } from "@nes import { CreateTaskDto, Task, TaskStatus, UpdateTaskDto } from "./task.model"; import { UsersService } from '../users/users.service'; import { NotificationService } from '../providers/NotificationService'; +import {ConfigurationService} from "../configuration/configuration.service"; @Injectable() export class TasksService { @@ -9,7 +10,8 @@ export class TasksService { constructor( private usersService: UsersService, - private notificationService: NotificationService + private notificationService: NotificationService, + private configurationService: ConfigurationService ) {}; async createTask(createTaskDto: CreateTaskDto) { diff --git a/03-di/01-notification-service/tokens.ts b/03-di/01-notification-service/tokens.ts new file mode 100644 index 0000000..2e79ba5 --- /dev/null +++ b/03-di/01-notification-service/tokens.ts @@ -0,0 +1 @@ +export const LoggerService = 'LoggerService'; diff --git a/03-di/01-notification-service/tsconfig.json b/03-di/01-notification-service/tsconfig.json index f572c2a..97feb5e 100644 --- a/03-di/01-notification-service/tsconfig.json +++ b/03-di/01-notification-service/tsconfig.json @@ -12,11 +12,12 @@ "baseUrl": "./", "incremental": true, "skipLibCheck": true, - "strictNullChecks": false, + "strictNullChecks": true, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false, - "allowJs": true + "allowJs": true, + "strict": true } } diff --git a/03-di/01-notification-service/users/user.model.ts b/03-di/01-notification-service/users/user.model.ts index 83f108b..a4c4576 100644 --- a/03-di/01-notification-service/users/user.model.ts +++ b/03-di/01-notification-service/users/user.model.ts @@ -1,6 +1,6 @@ export class User { - id: number; - name: string; - email: string; - phone: string; + id: number = 0; + name: string = ''; + email: string = ''; + phone: string = ''; } diff --git a/package.json b/package.json index 0ec3094..ab2ef8e 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1", "sqlite3": "^5.1.7", - "typeorm": "^0.3.20" + "typeorm": "^0.3.20", + "zod": "^3.24.1" }, "devDependencies": { "@nestjs/cli": "^10.0.0",