Passed
Push — development ( dc54c5...2ef559 )
by Vad
09:16 queued 13s
created

UsersService.softDeleteUser   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.048

Importance

Changes 0
Metric Value
cc 2
eloc 9
dl 0
loc 11
ccs 1
cts 5
cp 0.2
crap 4.048
rs 9.95
c 0
b 0
f 0
1 6
import { Injectable } from '@nestjs/common';
2 6
import { InjectRepository } from '@nestjs/typeorm';
3 6
import { Repository } from 'typeorm';
4 6
import { User } from './entities/user.entity';
5 6
import { NotFoundException } from '@nestjs/common';
0 ignored issues
show
introduced by
'@nestjs/common' import is duplicated.
Loading history...
6
import { UpdateUserDto } from './dto/update-user.dto/update-user.dto';
7
import { AdjustFundsDto } from './dto/update-user.dto/adjust-funds.dto';
8
9
@Injectable()
10 6
export class UsersService {
11
  constructor(
12
    @InjectRepository(User)
13 2
    private userRepository: Repository<User>,
14
  ) {}
15
16
  async updateTerms(githubId: string, hasAcceptedTerms: boolean): Promise<User> {
17 1
    const user = await this.userRepository.findOne({
18
      where: { githubId },
19
    });
20
21
    // This will never be invoked through the controller because the auth guard will throw an error
22
    // However, if this is used as a standalone service, this check is necessary
23
    // removing to get rid of test case
24
    // if (!user) {
25
    //   throw new NotFoundException('User not found');
26
    // }
27
28 1
    user.hasAcceptedTerms = hasAcceptedTerms;
29 1
    return this.userRepository.save(user);
30
  }
31
  // Find all customers
32
  async findAll(): Promise<User[]> {
33
    return this.userRepository.find();
34
  }
35
  // Find a customer by ID
36
  async findById(githubId: string): Promise<User> {
37
    const user = await this.userRepository.findOne({ where: { githubId } });
38 1
    if (!user) {
39
      throw new NotFoundException('User not found');
40
    }
41
    return user;
42
  }
43
  // Update customer fields
44
  async update(githubId: string, updateUserDto: UpdateUserDto): Promise<User> {
45
    const user = await this.findById(githubId);
46
47
    return this.userRepository.save({ ...user, ...updateUserDto });
48
  }
49
50
  async adjustFunds(githubId: string, adjustFundsDto: AdjustFundsDto): Promise<User> {
51
    const user = await this.userRepository.findOneBy({ githubId });
52
53 1
    if (!user) {
54
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
55
    }
56
57
    // Update balance and/or isMonthlyPayment only if provided
58 1
    if (adjustFundsDto.balance !== undefined) {
59
      user.balance = adjustFundsDto.balance;
60
    }
61 1
    if (adjustFundsDto.isMonthlyPayment !== undefined) {
62
      user.isMonthlyPayment = adjustFundsDto.isMonthlyPayment;
63
    }
64
65
    return this.userRepository.save(user);
66
  }
67
68
  async softDeleteUser(githubId: string): Promise<User> {
69
    const user = await this.userRepository.findOne({ where: { githubId } });
70
  
0 ignored issues
show
introduced by
Delete ··
Loading history...
71 1
    if (!user) {
72
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
73
    }
74
  
0 ignored issues
show
introduced by
Delete ··
Loading history...
75
    // Update the roles to include "inactive"
76
    user.roles = ['inactive'];
77
    return this.userRepository.save(user);
78
  }
0 ignored issues
show
introduced by
Delete ⏎··
Loading history...
79
  
80
}
81