2024-09-01 19:22:00 +00:00
|
|
|
import type { UpdateEmailDto } from '$lib/server/api/dtos/update-email.dto'
|
|
|
|
|
import type { UpdateProfileDto } from '$lib/server/api/dtos/update-profile.dto'
|
|
|
|
|
import type { VerifyPasswordDto } from '$lib/server/api/dtos/verify-password.dto'
|
2024-09-04 23:04:41 +00:00
|
|
|
import { LuciaService } from '$lib/server/api/services/lucia.service'
|
2024-09-01 19:22:00 +00:00
|
|
|
import { UsersService } from '$lib/server/api/services/users.service'
|
|
|
|
|
import { inject, injectable } from 'tsyringe'
|
2024-07-29 01:39:42 +00:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
/* Service */
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
/* ---------------------------------- About --------------------------------- */
|
|
|
|
|
/*
|
|
|
|
|
Services are responsible for handling business logic and data manipulation.
|
2024-08-19 03:28:15 +00:00
|
|
|
They generally call on repositories or other services to complete a use-case.
|
2024-07-29 01:39:42 +00:00
|
|
|
*/
|
|
|
|
|
/* ---------------------------------- Notes --------------------------------- */
|
|
|
|
|
/*
|
|
|
|
|
Services should be kept as clean and simple as possible.
|
|
|
|
|
|
|
|
|
|
Create private functions to handle complex logic and keep the public methods as
|
|
|
|
|
simple as possible. This makes the service easier to read, test and understand.
|
|
|
|
|
*/
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
2024-07-31 01:50:46 +00:00
|
|
|
@injectable()
|
2024-07-29 01:39:42 +00:00
|
|
|
export class IamService {
|
2024-07-31 01:50:46 +00:00
|
|
|
constructor(
|
2024-09-04 23:04:41 +00:00
|
|
|
@inject(LuciaService) private luciaService: LuciaService,
|
2024-09-01 19:22:00 +00:00
|
|
|
@inject(UsersService) private readonly usersService: UsersService,
|
|
|
|
|
) {}
|
2024-07-31 01:50:46 +00:00
|
|
|
|
2024-07-29 01:39:42 +00:00
|
|
|
async logout(sessionId: string) {
|
2024-09-04 23:04:41 +00:00
|
|
|
return this.luciaService.lucia.invalidateSession(sessionId)
|
2024-07-29 01:39:42 +00:00
|
|
|
}
|
2024-08-19 03:28:15 +00:00
|
|
|
|
|
|
|
|
async updateProfile(userId: string, data: UpdateProfileDto) {
|
2024-09-01 19:22:00 +00:00
|
|
|
const user = await this.usersService.findOneById(userId)
|
2024-08-19 06:00:59 +00:00
|
|
|
if (!user) {
|
|
|
|
|
return {
|
2024-09-01 19:22:00 +00:00
|
|
|
error: 'User not found',
|
|
|
|
|
}
|
2024-08-19 06:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
const existingUserForNewUsername = await this.usersService.findOneByUsername(data.username)
|
2024-08-19 06:00:59 +00:00
|
|
|
if (existingUserForNewUsername && existingUserForNewUsername.id !== userId) {
|
|
|
|
|
return {
|
2024-09-01 19:22:00 +00:00
|
|
|
error: 'Username already in use',
|
|
|
|
|
}
|
2024-08-19 06:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 03:28:15 +00:00
|
|
|
return this.usersService.updateUser(userId, {
|
|
|
|
|
first_name: data.firstName,
|
|
|
|
|
last_name: data.lastName,
|
2024-09-01 19:22:00 +00:00
|
|
|
username: data.username !== user.username ? data.username : user.username,
|
|
|
|
|
})
|
2024-08-19 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateEmail(userId: string, data: UpdateEmailDto) {
|
2024-09-01 19:22:00 +00:00
|
|
|
const { email } = data
|
2024-08-23 02:26:22 +00:00
|
|
|
|
2024-09-01 19:22:00 +00:00
|
|
|
const existingUserEmail = await this.usersService.findOneByEmail(email)
|
2024-08-23 02:26:22 +00:00
|
|
|
if (existingUserEmail && existingUserEmail.id !== userId) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return null
|
2024-08-23 02:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-19 03:28:15 +00:00
|
|
|
return this.usersService.updateUser(userId, {
|
2024-08-23 02:26:22 +00:00
|
|
|
email,
|
2024-09-01 19:22:00 +00:00
|
|
|
})
|
2024-08-19 03:28:15 +00:00
|
|
|
}
|
2024-08-29 23:12:40 +00:00
|
|
|
|
|
|
|
|
async verifyPassword(userId: string, data: VerifyPasswordDto) {
|
2024-09-01 19:22:00 +00:00
|
|
|
const user = await this.usersService.findOneById(userId)
|
2024-08-29 23:12:40 +00:00
|
|
|
if (!user) {
|
2024-09-01 19:22:00 +00:00
|
|
|
return null
|
2024-08-29 23:12:40 +00:00
|
|
|
}
|
2024-09-01 19:22:00 +00:00
|
|
|
const { password } = data
|
|
|
|
|
return this.usersService.verifyPassword(userId, { password })
|
2024-08-29 23:12:40 +00:00
|
|
|
}
|
2024-07-29 01:39:42 +00:00
|
|
|
}
|