Skip to content

Commit

Permalink
module 3 task 1 версия 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lrvchgost committed Dec 19, 2024
1 parent 5443036 commit 77dca19
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions 03-di/01-notification-service/1734634566473/logs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[21:56:11 GMT+0300 (Moscow Standard Time)]: Email sent to [email protected]: [Новая задача] Вы назначены ответственным за задачу: "Сделать домашнюю работу"
[21:56:12 GMT+0300 (Moscow Standard Time)]: Email sent to [email protected]: [Новая задача] Вы назначены ответственным за задачу: "Сделать домашнюю работу"
[21:56:13 GMT+0300 (Moscow Standard Time)]: Email sent to [email protected]: [Новая задача] Вы назначены ответственным за задачу: "Сделать домашнюю работу"
2 changes: 2 additions & 0 deletions 03-di/01-notification-service/providers/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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,7 +1,10 @@
import { Injectable, Inject } from "@nestjs/common";
import { LoggerService } from './Logger';
import { ILogger } from './types';

@Injectable()
export class NotificationService {
constructor(private logger?: ILogger) {
constructor(@Inject(LoggerService) private logger: ILogger) {
console.log('NotificationService', logger);
};

Expand All @@ -10,14 +13,14 @@ export class NotificationService {

console.log(mess);

this.logger?.log(mess);
this.logger.log(mess);
}

sendSMS(to: string, message: string): void {
const mess = `SMS sent to ${to}: ${message}`;

console.log(mess);

this.logger?.log(mess);
this.logger.log(mess);
}
}
6 changes: 3 additions & 3 deletions 03-di/01-notification-service/tasks/tasks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { TasksController } from "./tasks.controller";
import { TasksService } from "./tasks.service";
import { UsersModule } from "../users/users.module";
import { NotificationService } from "../providers/NotificationService";
import { FileLoggerService, ConsoleLoggerService } from '../providers/Logger';
import { FileLoggerService, ConsoleLoggerService, LoggerService } from '../providers/Logger';

const logger = process.env.LOGGER === 'file' ? new FileLoggerService(Date.now().toString(), 'logs.txt') : new ConsoleLoggerService();
const getLogger = () => process.env.LOGGER === 'file' ? new FileLoggerService(Date.now().toString(), 'logs.txt') : new ConsoleLoggerService();

@Module({
imports: [UsersModule],
controllers: [TasksController],
providers: [TasksService, { provide: NotificationService, useFactory: () => new NotificationService(logger) }],
providers: [TasksService, NotificationService, { provide: LoggerService, useFactory: () => getLogger()}],
})
export class TasksModule {}
2 changes: 1 addition & 1 deletion 03-di/01-notification-service/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class TasksService {

constructor(
private usersService: UsersService,
@Inject(NotificationService) private notificationService: NotificationService
private notificationService: NotificationService
) {};

async createTask(createTaskDto: CreateTaskDto) {
Expand Down

0 comments on commit 77dca19

Please sign in to comment.