2020-02-01 16:11:48 +01:00
|
|
|
import { Injectable, NotFoundException } from "@nestjs/common";
|
2020-09-05 21:07:05 +02:00
|
|
|
import { SpotifyConnection } from "../sources/spotify/spotify-connection.entity";
|
2020-02-01 16:11:48 +01:00
|
|
|
import { CreateOrUpdateDto } from "./dto/create-or-update.dto";
|
|
|
|
|
import { User } from "./user.entity";
|
|
|
|
|
import { UserRepository } from "./user.repository";
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class UsersService {
|
|
|
|
|
constructor(private readonly userRepository: UserRepository) {}
|
|
|
|
|
|
|
|
|
|
async findById(id: string): Promise<User> {
|
|
|
|
|
const user = await this.userRepository.findOne(id);
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException("UserNotFound");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 19:21:58 +01:00
|
|
|
async findAll(): Promise<User[]> {
|
|
|
|
|
return this.userRepository.find();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-01 16:11:48 +01:00
|
|
|
async createOrUpdate(data: CreateOrUpdateDto): Promise<User> {
|
|
|
|
|
let user = await this.userRepository.findOne({
|
2020-05-02 17:17:20 +02:00
|
|
|
where: { spotify: { id: data.spotify.id } },
|
2020-02-01 16:11:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
user = this.userRepository.create({
|
|
|
|
|
spotify: {
|
2020-05-02 17:17:20 +02:00
|
|
|
id: data.spotify.id,
|
|
|
|
|
},
|
2020-02-01 16:11:48 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.spotify.accessToken = data.spotify.accessToken;
|
|
|
|
|
user.spotify.refreshToken = data.spotify.refreshToken;
|
|
|
|
|
user.displayName = data.displayName;
|
|
|
|
|
user.photo = data.photo;
|
|
|
|
|
|
|
|
|
|
await this.userRepository.save(user);
|
|
|
|
|
|
|
|
|
|
return user;
|
|
|
|
|
}
|
2020-02-02 19:21:58 +01:00
|
|
|
|
|
|
|
|
async updateSpotifyConnection(
|
|
|
|
|
user: User,
|
|
|
|
|
spotify: SpotifyConnection
|
|
|
|
|
): Promise<void> {
|
2021-05-25 18:12:42 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
2020-02-02 19:21:58 +01:00
|
|
|
user.spotify = spotify;
|
|
|
|
|
await this.userRepository.save(user);
|
|
|
|
|
}
|
2020-02-01 16:11:48 +01:00
|
|
|
}
|