| 1 |  |  | import { Injectable } from '@nestjs/common'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import { Repository } from 'typeorm'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import { InjectRepository } from '@nestjs/typeorm'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import { Discount } from 'src/Domain/School/Discount.entity'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import { IDiscountRepository } from 'src/Domain/School/Repository/IDiscountRepository'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import { School } from 'src/Domain/School/School.entity'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | @Injectable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | export class DiscountRepository implements IDiscountRepository { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |   constructor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     @InjectRepository(Discount) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private readonly repository: Repository<Discount> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |   ) {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   public save(discount: Discount): Promise<Discount> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     return this.repository.save(discount); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |   public remove(discount: Discount): void { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     this.repository.delete(discount.getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |   public findOneById(id: string): Promise<Discount | undefined> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     return this.repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |       .createQueryBuilder('discount') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |       .select(['discount.id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |       .where('discount.id = :id', { id }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |       .getOne(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |   public findOneByAmountAndSchool(amount: number, school: School): Promise<Discount | undefined> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     return this.repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |       .createQueryBuilder('discount') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |       .select(['discount.id']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |       .innerJoin('discount.school', 'school', 'school.id = :school', { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         school: school.getId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |       }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |       .where('discount.amount = :amount', { amount }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |       .getOne(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |   public findBySchool(schoolId: string): Promise<Discount[]> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     return this.repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |       .createQueryBuilder('discount') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |       .select([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         'discount.id', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         'discount.type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         'discount.amount', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         'discount.value', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |       ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |       .innerJoin('discount.school', 'school', 'school.id = :schoolId', { schoolId }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |       .orderBy('discount.amount', 'ASC') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |       .getMany(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |   public countBySchool(id: string): Promise<number> { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     return this.repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |       .createQueryBuilder('discount') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |       .select('discount.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |       .innerJoin('discount.school', 'school', 'school.id = :id', { id }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |       .getCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |   } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |