| Total Complexity | 2 |
| Complexity/F | 2 |
| Lines of Code | 31 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { Inject } from '@nestjs/common'; |
||
| 2 | import { EventsHandler, ICommandBus } from '@nestjs/cqrs'; |
||
| 3 | import { IProductRepository } from 'src/Domain/Product/Repository/IProductRepository'; |
||
| 4 | import { CreateSchoolProductCommand } from '../Command/Product/CreateSchoolProductCommand'; |
||
| 5 | import { SchoolCreatedEvent } from '../Event/SchoolCreatedEvent'; |
||
| 6 | |||
| 7 | @EventsHandler(SchoolCreatedEvent) |
||
| 8 | export class SchoolCreatedEventListener { |
||
| 9 | constructor( |
||
| 10 | @Inject('ICommandBus') |
||
| 11 | private readonly commandBus: ICommandBus, |
||
| 12 | @Inject('IProductRepository') |
||
| 13 | private readonly productRepository: IProductRepository |
||
| 14 | ) {} |
||
| 15 | |||
| 16 | public async handle({ schoolId }: SchoolCreatedEvent): Promise<void> { |
||
| 17 | const products = await this.productRepository.findProductsToImport(); |
||
| 18 | |||
| 19 | for (const product of products) { |
||
| 20 | await this.commandBus.execute( |
||
| 21 | new CreateSchoolProductCommand( |
||
| 22 | product.getUnitPrice(), |
||
| 23 | product.getUnitPrice(), |
||
| 24 | schoolId, |
||
| 25 | product.getId() |
||
| 26 | ) |
||
| 27 | ); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | } |
||
| 31 |