Skip to content

Commit

Permalink
module 3 task 1 версия 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lrvchgost committed Dec 19, 2024
1 parent 77dca19 commit 5e40d12
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 19 deletions.
6 changes: 5 additions & 1 deletion 03-di/01-notification-service/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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');

@Module({
imports: [TasksModule],
imports: [TasksModule, ConfigurationModule.forRoot(configFileName) ],
})
export class AppModule {}
5 changes: 5 additions & 0 deletions 03-di/01-notification-service/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"emailGetway": "some email getway",
"smsGetway": "some sms getway",
"someValue": "someValue"
}
Original file line number Diff line number Diff line change
@@ -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 }],
};
}
}
Original file line number Diff line number Diff line change
@@ -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<typeof ConfigSchema>;

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;
}
}
3 changes: 3 additions & 0 deletions 03-di/01-notification-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"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"
},
"dependencies": {
"zod": "^3.24.1"
}
}
2 changes: 0 additions & 2 deletions 03-di/01-notification-service/providers/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions 03-di/01-notification-service/tasks/task.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
3 changes: 2 additions & 1 deletion 03-di/01-notification-service/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 3 additions & 1 deletion 03-di/01-notification-service/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ 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 {
private tasks: Task[] = [];

constructor(
private usersService: UsersService,
private notificationService: NotificationService
private notificationService: NotificationService,
private configurationService: ConfigurationService
) {};

async createTask(createTaskDto: CreateTaskDto) {
Expand Down
1 change: 1 addition & 0 deletions 03-di/01-notification-service/tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const LoggerService = 'LoggerService';
5 changes: 3 additions & 2 deletions 03-di/01-notification-service/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
8 changes: 4 additions & 4 deletions 03-di/01-notification-service/users/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class User {
id: number;
name: string;
email: string;
phone: string;
id: number = 0;
name: string = '';
email: string = '';
phone: string = '';
}

0 comments on commit 5e40d12

Please sign in to comment.