forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.service.ts
More file actions
104 lines (92 loc) · 2.98 KB
/
Copy pathusers.service.ts
File metadata and controls
104 lines (92 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { GithubAccount, User } from '../common/entities';
import { UserRole } from '../common/enums';
export interface UpsertFromGithubInput {
githubId: string;
login: string;
email: string | null;
displayName: string | null;
avatarUrl: string | null;
profileUrl: string | null;
accessToken: string;
refreshToken?: string | null;
}
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User) private readonly userRepo: Repository<User>,
@InjectRepository(GithubAccount)
private readonly githubAccountRepo: Repository<GithubAccount>,
) {}
async findById(id: string): Promise<User> {
const user = await this.userRepo.findOne({
where: { id },
relations: { githubAccount: true },
});
if (!user) throw new NotFoundException(`User ${id} not found`);
return user;
}
async findByUsername(username: string): Promise<User | null> {
return this.userRepo.findOne({ where: { username } });
}
/** Finds or creates a User + GithubAccount from a completed OAuth handshake. */
async upsertFromGithub(input: UpsertFromGithubInput): Promise<User> {
let account = await this.githubAccountRepo.findOne({
where: { githubId: input.githubId },
relations: { user: true },
});
if (account) {
account.accessToken = input.accessToken;
account.refreshToken = input.refreshToken ?? null;
account.avatarUrl = input.avatarUrl;
account.profileUrl = input.profileUrl;
await this.githubAccountRepo.save(account);
return this.findById(account.userId);
}
let user = await this.userRepo.findOne({
where: { username: input.login },
});
if (!user) {
user = this.userRepo.create({
username: input.login,
email: input.email,
displayName: input.displayName,
avatarUrl: input.avatarUrl,
roles: [UserRole.CONTRIBUTOR],
});
user = await this.userRepo.save(user);
}
account = this.githubAccountRepo.create({
githubId: input.githubId,
login: input.login,
avatarUrl: input.avatarUrl,
profileUrl: input.profileUrl,
accessToken: input.accessToken,
refreshToken: input.refreshToken ?? null,
userId: user.id,
});
await this.githubAccountRepo.save(account);
return this.findById(user.id);
}
async addRole(userId: string, role: UserRole): Promise<User> {
const user = await this.findById(userId);
if (!user.roles.includes(role)) {
user.roles = [...user.roles, role];
await this.userRepo.save(user);
}
return user;
}
async setStellarAddress(
userId: string,
stellarAddress: string,
): Promise<User> {
const user = await this.findById(userId);
user.stellarAddress = stellarAddress;
return this.userRepo.save(user);
}
async list(): Promise<User[]> {
return this.userRepo.find();
}
}