| Total Complexity | 2 |
| Complexity/F | 2 |
| Lines of Code | 30 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { QueryHandler } from '@nestjs/cqrs'; |
||
| 2 | import { Inject } from '@nestjs/common'; |
||
| 3 | import { IVoucherRepository } from 'src/Domain/School/Repository/IVoucherRepository'; |
||
| 4 | import { VoucherNotFoundException } from 'src/Domain/School/Exception/VoucherNotFoundException'; |
||
| 5 | import { GetVoucherByCodeQuery } from './GetVoucherByCodeQuery'; |
||
| 6 | import { VoucherView } from '../../View/VoucherView'; |
||
| 7 | |||
| 8 | @QueryHandler(GetVoucherByCodeQuery) |
||
| 9 | export class GetVoucherByCodeQueryHandler { |
||
| 10 | constructor( |
||
| 11 | @Inject('IVoucherRepository') |
||
| 12 | private readonly voucherRepository: IVoucherRepository |
||
| 13 | ) {} |
||
| 14 | |||
| 15 | public async execute({ code }: GetVoucherByCodeQuery): Promise<VoucherView> { |
||
| 16 | const voucher = await this.voucherRepository.findOneByCode(code); |
||
| 17 | |||
| 18 | if (!voucher) { |
||
| 19 | throw new VoucherNotFoundException(); |
||
| 20 | } |
||
| 21 | |||
| 22 | return new VoucherView( |
||
| 23 | voucher.getId(), |
||
| 24 | voucher.getCode(), |
||
| 25 | voucher.getEmail(), |
||
| 26 | voucher.getSchool().getId(), |
||
| 27 | ); |
||
| 28 | } |
||
| 29 | } |
||
| 30 |