Completed
Push — master ( e9382c...759d87 )
by Mathieu
28s queued 11s
created

QuoteRepository.findQuotes   A

Complexity

Conditions 1

Size

Total Lines 22
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 22
c 0
b 0
f 0
rs 9.376
cc 1
1
import {InjectRepository} from '@nestjs/typeorm';
2
import {Repository} from 'typeorm';
3
import {IQuoteRepository} from 'src/Domain/Accounting/Repository/IQuoteRepository';
4
import {Quote} from 'src/Domain/Accounting/Quote.entity';
5
import {MAX_ITEMS_PER_PAGE} from 'src/Application/Common/Pagination';
6
7
export class QuoteRepository implements IQuoteRepository {
8
  constructor(
9
    @InjectRepository(Quote)
10
    private readonly repository: Repository<Quote>
11
  ) {}
12
13
  public save(quote: Quote): Promise<Quote> {
14
    return this.repository.save(quote);
15
  }
16
17
  public countByYear(year: number): Promise<number> {
18
    return this.repository
19
      .createQueryBuilder('quote')
20
      .select('quote.id')
21
      .where('extract(year FROM quote.createdAt) = :year', {year})
22
      .getCount();
23
  }
24
25
  public findQuotes(page: number): Promise<[Quote[], number]> {
26
    return this.repository
27
      .createQueryBuilder('quote')
28
      .select([
29
        'quote.id',
30
        'quote.quoteId',
31
        'quote.status',
32
        'quote.createdAt',
33
        'project.id',
34
        'project.name',
35
        'customer.id',
36
        'customer.name',
37
        'item.quantity',
38
        'item.dailyRate'
39
      ])
40
      .innerJoin('quote.customer', 'customer')
41
      .innerJoin('quote.items', 'item')
42
      .leftJoin('quote.project', 'project')
43
      .orderBy('quote.createdAt', 'DESC')
44
      .limit(MAX_ITEMS_PER_PAGE)
45
      .offset((page - 1) * MAX_ITEMS_PER_PAGE)
46
      .getManyAndCount();
47
  }
48
49
  public find(id: string): Promise<Quote> {
50
    return this.repository
51
      .createQueryBuilder('quote')
52
      .select('quote.id')
53
      .where('quote.id = :id', {id})
54
      .getOne();
55
  }
56
}
57