Passed
Pull Request — master (#63)
by Nicolas
01:22
created

ActivityRepository.deleteById   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
1
import {Injectable} from '@nestjs/common';
2
import {InjectRepository} from '@nestjs/typeorm';
3
import {Repository} from 'typeorm';
4
import {IActivityRepository} from 'src/Domain/Activity/Repository/IActivityRepository';
5
import {Activity} from 'src/Domain/Activity/Activity.entity';
6
import {User} from 'src/Domain/User/User.entity';
7
8
@Injectable()
9
export class ActivityRepository implements IActivityRepository {
10
  constructor(
11
    @InjectRepository(Activity)
12
    private readonly repository: Repository<Activity>
13
  ) {}
14
15
  public save(activity: Activity): Promise<Activity> {
16
    return this.repository.save(activity);
17
  }
18
19
  public async getTimeSpentSumByUserAndDate(
20
    user: User,
21
    date: string
22
  ): Promise<number> {
23
    const result = await this.repository
24
      .createQueryBuilder('activity')
25
      .select('SUM(activity.time) as time')
26
      .where('activity.date = :date', {date})
27
      .andWhere('activity.user = :user', {user: user.getId()})
28
      .getRawOne();
29
30
    return Number(result.time) || 0;
31
  }
32
33
  public findOneById(id: string): Promise<Activity> {
34
    return this.repository
35
      .createQueryBuilder('activity')
36
      .select([
37
        'activity.id',
38
        'activity.time',
39
        'activity.summary',
40
        'activity.date',
41
        'user.id',
42
        'project.name',
43
        'customer.name',
44
        'task.name'
45
      ])
46
      .where('activity.id = :id', {id})
47
      .innerJoin('activity.task', 'task')
48
      .innerJoin('activity.project', 'project')
49
      .innerJoin('activity.user', 'user')
50
      .innerJoin('project.customer', 'customer')
51
      .getOne();
52
  }
53
54
  public deleteById(id: string): void {
55
    this.repository
56
      .createQueryBuilder('activity')
57
      .where('activity.id = :id', {id})
58
      .delete()
59
      .execute()
60
    ;
61
  }
62
63
  public findMonthlyActivities(
64
    date: string,
65
    userId: string,
66
    projectId: string
67
  ): Promise<Activity[]> {
68
    const month = new Date(date).getMonth() + 1;
69
    const year = new Date(date).getFullYear();
70
71
    const query = this.repository
72
      .createQueryBuilder('activity')
73
      .select([
74
        'activity.id',
75
        'activity.time',
76
        'activity.summary',
77
        'activity.date',
78
        'project.name',
79
        'customer.name',
80
        'task.name'
81
      ])
82
      .where('user.id = :userId', {userId})
83
      .andWhere('extract(month FROM activity.date) = :month', {month})
84
      .andWhere('extract(year FROM activity.date) = :year', {year})
85
      .innerJoin('activity.task', 'task')
86
      .innerJoin('activity.project', 'project')
87
      .innerJoin('project.customer', 'customer')
88
      .innerJoin('activity.user', 'user')
89
      .orderBy('activity.date', 'ASC')
90
      .addOrderBy('activity.time', 'ASC');
91
92
    if (projectId) {
93
      query
94
        .andWhere('project.id = :projectId', {projectId})
95
        .setParameter('projectId', projectId);
96
    }
97
98
    return query.getMany();
99
  }
100
}
101