Skip to content

Commit 100b01f

Browse files
committed
Resolve INotificationRepository injection and update NotificationController
1 parent a440c30 commit 100b01f

9 files changed

Lines changed: 94 additions & 20 deletions

File tree

package-lock.json

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
},
2222
"dependencies": {
2323
"@nestjs/common": "^11.0.1",
24+
"@nestjs/config": "^4.0.2",
2425
"@nestjs/core": "^11.0.1",
2526
"@nestjs/mongoose": "^11.0.3",
2627
"@nestjs/platform-express": "^11.0.1",
2728
"@types/joi": "^17.2.2",
29+
"dotenv": "^16.5.0",
2830
"joi": "^17.13.3",
2931
"mongoose": "^8.13.2",
3032
"reflect-metadata": "^0.2.2",

src/app.module.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Module } from '@nestjs/common';
2-
import { AppController } from './app.controller';
3-
import { AppService } from './app.service';
2+
import { MongooseModule } from '@nestjs/mongoose';
3+
import { ConfigModule } from '@nestjs/config';
4+
import { NotificationModule } from './notification/notification.module';
45

56
@Module({
6-
imports: [],
7-
controllers: [AppController],
8-
providers: [AppService],
7+
imports: [
8+
ConfigModule.forRoot({ isGlobal: true }),
9+
MongooseModule.forRoot('mongodb://localhost:27017/smartedu'),
10+
NotificationModule,
11+
],
912
})
1013
export class AppModule {}

src/application/factories/common/notification.factory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { NotificationChannel } from 'src/config/notification.config';
77

88
@Injectable()
99
export class NotificationFactory {
10+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
11+
static create(_arg0: any): INotificationStrategy {
12+
throw new Error('Method not implemented.');
13+
}
1014
constructor(
1115
private readonly emailStrategy: EmailNotificationStrategy,
1216
private readonly smsStrategy: SMSNotificationStrategy,

src/application/use-cases/send-notification.use-case.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export class SendNotificationUseCase {
4747
// Send notification
4848
const strategy: INotificationStrategy =
4949
NotificationFactory.create(mediaType);
50-
await strategy.send(
51-
notification.recipient,
52-
notification.subject,
53-
notification.body,
54-
);
50+
await strategy.send(notification);
5551

5652
// Save to MongoDB
5753
await this.notificationRepository.save(notification);
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Notification } from 'src/domain/entities/common/notification.entity';
2+
13
export interface INotificationStrategy {
2-
send(recipient: string, subject: string, body: string): Promise<void>;
4+
send(recipient: Notification): Promise<void>;
35
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { MediaValidation } from '../../entities/common/media-validation.entity';
1+
import { Notification } from '../../../domain/entities/common/notification.entity';
22

33
export interface IMediaValidationRepository {
4-
save(validation: MediaValidation): Promise<void>;
5-
findUserByMedia(media: string): Promise<MediaValidation | null>;
4+
save(validation: Notification): Promise<void>;
5+
findUserByMedia(media: string): Promise<Notification | null>;
66
}

src/notification/notification.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import { SMSNotificationStrategy } from '../application/strategies/sms-notificat
1515
],
1616
controllers: [NotificationController],
1717
providers: [
18-
NotificationRepository,
18+
{
19+
provide: 'INotificationRepository',
20+
useClass: NotificationRepository,
21+
},
1922
NotificationFactory,
2023
EmailNotificationStrategy,
2124
SMSNotificationStrategy,

src/presentation/controllers/notification.controller.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
2-
import { INotificationRepository } from '../../domain/interfaces/common/notification-repository.interface';
1+
import {
2+
Controller,
3+
Post,
4+
Body,
5+
HttpCode,
6+
HttpStatus,
7+
Inject,
8+
} from '@nestjs/common';
39
import { NotificationFactory } from '../../application/factories/common/notification.factory';
410
import { NotificationValidator } from '../../application/validators/notification.validator';
511
import { SendNotificationDto } from '../dtos/common/send-notification.dto';
612
import { Notification } from '../../domain/entities/common/notification.entity';
713
import { INotificationStrategy } from '../../domain/interfaces/common/notification-strategy.interface';
14+
import { INotificationRepository } from '../../domain/interfaces/common/notification-repository.interface';
815

916
@Controller('notifications')
1017
export class NotificationController {
1118
constructor(
1219
private readonly notificationFactory: NotificationFactory,
20+
@Inject('INotificationRepository')
1321
private readonly notificationRepository: INotificationRepository,
1422
) {}
1523

@@ -32,8 +40,9 @@ export class NotificationController {
3240
);
3341

3442
// Send notification
35-
const strategy: INotificationStrategy =
36-
this.notificationFactory.createStrategy(dto.mediaType);
43+
const strategy: INotificationStrategy = NotificationFactory.create(
44+
dto.mediaType as any,
45+
);
3746
await strategy.send(notification);
3847

3948
// Save notification after successful send

0 commit comments

Comments
 (0)