2024-09-01 19:22:00 +00:00
|
|
|
import type { SignupUsernameEmailDto } from '$lib/server/api/dtos/signup-username-email.dto'
|
|
|
|
|
import { CredentialsRepository } from '$lib/server/api/repositories/credentials.repository'
|
|
|
|
|
import { TokensService } from '$lib/server/api/services/tokens.service'
|
|
|
|
|
import { UserRolesService } from '$lib/server/api/services/user_roles.service'
|
|
|
|
|
import { inject, injectable } from 'tsyringe'
|
|
|
|
|
import { CredentialsType } from '../databases/tables'
|
|
|
|
|
import { type UpdateUser, UsersRepository } from '../repositories/users.repository'
|
|
|
|
|
import { CollectionsService } from './collections.service'
|
|
|
|
|
import { WishlistsService } from './wishlists.service'
|
2024-08-01 16:26:42 +00:00
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
|
export class UsersService {
|
|
|
|
|
constructor(
|
2024-08-13 22:19:57 +00:00
|
|
|
@inject(CollectionsService) private readonly collectionsService: CollectionsService,
|
2024-08-10 17:03:30 +00:00
|
|
|
@inject(CredentialsRepository) private readonly credentialsRepository: CredentialsRepository,
|
|
|
|
|
@inject(TokensService) private readonly tokenService: TokensService,
|
|
|
|
|
@inject(UsersRepository) private readonly usersRepository: UsersRepository,
|
|
|
|
|
@inject(UserRolesService) private readonly userRolesService: UserRolesService,
|
2024-09-01 19:22:00 +00:00
|
|
|
@inject(WishlistsService) private readonly wishlistsService: WishlistsService,
|
|
|
|
|
) {}
|
2024-08-01 16:26:42 +00:00
|
|
|
|
2024-08-10 17:03:30 +00:00
|
|
|
async create(data: SignupUsernameEmailDto) {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { firstName, lastName, email, username, password } = data
|
2024-08-10 17:03:30 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
const hashedPassword = await this.tokenService.createHashedToken(password)
|
2024-08-10 17:03:30 +00:00
|
|
|
const user = await this.usersRepository.create({
|
|
|
|
|
first_name: firstName,
|
|
|
|
|
last_name: lastName,
|
|
|
|
|
email,
|
|
|
|
|
username,
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2024-08-10 17:03:30 +00:00
|
|
|
|
|
|
|
|
if (!user) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return null
|
2024-08-10 17:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const credentials = await this.credentialsRepository.create({
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
type: CredentialsType.PASSWORD,
|
|
|
|
|
secret_data: hashedPassword,
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2024-08-10 17:03:30 +00:00
|
|
|
|
|
|
|
|
if (!credentials) {
|
2024-09-01 19:22:00 +00:00
|
|
|
await this.usersRepository.delete(user.id)
|
|
|
|
|
return null
|
2024-08-10 17:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
await this.userRolesService.addRoleToUser(user.id, 'user', true)
|
2024-08-10 17:10:57 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
await this.wishlistsService.createEmptyNoName(user.id)
|
|
|
|
|
await this.collectionsService.createEmptyNoName(user.id)
|
2024-08-10 17:03:30 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
return user
|
2024-08-10 17:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 03:28:15 +00:00
|
|
|
async updateUser(userId: string, data: UpdateUser) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.usersRepository.update(userId, data)
|
2024-08-19 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-01 16:26:42 +00:00
|
|
|
async findOneByUsername(username: string) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.usersRepository.findOneByUsername(username)
|
2024-08-01 16:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-23 02:26:22 +00:00
|
|
|
async findOneByEmail(email: string) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.usersRepository.findOneByEmail(email)
|
2024-08-23 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-01 16:26:42 +00:00
|
|
|
async findOneById(id: string) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return this.usersRepository.findOneById(id)
|
2024-08-01 16:26:42 +00:00
|
|
|
}
|
2024-08-29 23:12:40 +00:00
|
|
|
|
2024-09-10 02:25:16 +00:00
|
|
|
async updatePassword(userId: string, password: string) {
|
|
|
|
|
const hashedPassword = await this.tokenService.createHashedToken(password)
|
|
|
|
|
const currentCredentials = await this.credentialsRepository.findPasswordCredentialsByUserId(userId)
|
|
|
|
|
if (!currentCredentials) {
|
|
|
|
|
await this.credentialsRepository.create({
|
|
|
|
|
user_id: userId,
|
|
|
|
|
type: CredentialsType.PASSWORD,
|
|
|
|
|
secret_data: hashedPassword,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
await this.credentialsRepository.update(currentCredentials.id, {
|
|
|
|
|
secret_data: hashedPassword,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 23:12:40 +00:00
|
|
|
async verifyPassword(userId: string, data: { password: string }) {
|
2024-09-01 19:22:00 +00:00
|
|
|
const user = await this.usersRepository.findOneById(userId)
|
2024-08-29 23:12:40 +00:00
|
|
|
if (!user) {
|
2024-09-01 19:22:00 +00:00
|
|
|
throw new Error('User not found')
|
2024-08-29 23:12:40 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
const credential = await this.credentialsRepository.findOneByUserIdAndType(userId, CredentialsType.PASSWORD)
|
2024-08-29 23:12:40 +00:00
|
|
|
if (!credential) {
|
2024-09-01 19:22:00 +00:00
|
|
|
throw new Error('Password credentials not found')
|
2024-08-29 23:12:40 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
const { password } = data
|
|
|
|
|
return this.tokenService.verifyHashedToken(credential.secret_data, password)
|
2024-08-29 23:12:40 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
}
|