Passed
Push — development ( 811359...8d1f71 )
by Vad
12:40 queued 13s
created

backend/src/users/users.service.ts   A

Complexity

Total Complexity 11
Complexity/F 1.83

Size

Lines of Code 80
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 80
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 11
mnd 5
bc 5
fnc 6
bpm 0.8333
cpm 1.8333
noi 1

6 Functions

Rating   Name   Duplication   Size   Complexity  
A UsersService.updateTerms 0 15 1
A UsersService.softDeleteUser 0 11 2
A UsersService.findAll 0 3 1
A UsersService.findById 0 7 2
A UsersService.adjustFunds 0 17 4
A UsersService.update 0 5 1
1 9
import { Injectable } from '@nestjs/common';
2 9
import { InjectRepository } from '@nestjs/typeorm';
3 9
import { Repository } from 'typeorm';
4 9
import { User } from './entities/user.entity';
5 9
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 9
export class UsersService {
11
  constructor(
12
    @InjectRepository(User)
13 7
    private userRepository: Repository<User>,
14
  ) {}
15
16
  async updateTerms(githubId: string, hasAcceptedTerms: boolean): Promise<User> {
17 2
    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 2
    user.hasAcceptedTerms = hasAcceptedTerms;
29 2
    return this.userRepository.save(user);
30
  }
31
  // Find all customers
32
  async findAll(): Promise<User[]> {
33 1
    return this.userRepository.find();
34
  }
35
  // Find a customer by ID
36
  async findById(githubId: string): Promise<User> {
37 9
    const user = await this.userRepository.findOne({ where: { githubId } });
38 9
    if (!user) {
39 1
      throw new NotFoundException('User not found');
40
    }
41 8
    return user;
42
  }
43
  // Update customer fields
44
  async update(githubId: string, updateUserDto: UpdateUserDto): Promise<User> {
45 1
    const user = await this.findById(githubId);
46
47 1
    return this.userRepository.save({ ...user, ...updateUserDto });
48
  }
49
50
  async adjustFunds(githubId: string, adjustFundsDto: AdjustFundsDto): Promise<User> {
51 4
    const user = await this.userRepository.findOneBy({ githubId });
52
53 4
    if (!user) {
54 1
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
55
    }
56
57
    // Update balance and/or isMonthlyPayment only if provided
58 3
    if (adjustFundsDto.balance !== undefined) {
59 2
      user.balance = adjustFundsDto.balance;
60
    }
61 3
    if (adjustFundsDto.isMonthlyPayment !== undefined) {
62 1
      user.isMonthlyPayment = adjustFundsDto.isMonthlyPayment;
63
    }
64
65 3
    return this.userRepository.save(user);
66
  }
67
68
  async softDeleteUser(githubId: string): Promise<User> {
69 3
    const user = await this.userRepository.findOne({ where: { githubId } });
70
71 3
    if (!user) {
72 1
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
73
    }
74
75
    // Update the roles to include "inactive"
76 2
    user.roles = ['inactive'];
77 2
    return this.userRepository.save(user);
78
  }
79
}
80