2024-07-31 01:50:46 +00:00
|
|
|
import { inject, injectable } from 'tsyringe';
|
|
|
|
|
import { LuciaProvider } from '../providers/lucia.provider';
|
2024-08-19 03:28:15 +00:00
|
|
|
import {UsersService} from "$lib/server/api/services/users.service";
|
|
|
|
|
import type {UpdateProfileDto} from "$lib/dtos/update-profile.dto";
|
|
|
|
|
import type {UpdateEmailDto} from "$lib/dtos/update-email.dto";
|
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(
|
|
|
|
|
@inject(LuciaProvider) private readonly lucia: LuciaProvider,
|
2024-08-19 03:28:15 +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-07-31 01:50:46 +00:00
|
|
|
return this.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-08-19 06:00:59 +00:00
|
|
|
const user = await this.usersService.findOneById(userId);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return {
|
|
|
|
|
error: 'User not found'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const existingUserForNewUsername = await this.usersService.findOneByUsername(data.username);
|
|
|
|
|
if (existingUserForNewUsername && existingUserForNewUsername.id !== userId) {
|
|
|
|
|
return {
|
|
|
|
|
error: 'Username already in use'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 03:28:15 +00:00
|
|
|
return this.usersService.updateUser(userId, {
|
|
|
|
|
first_name: data.firstName,
|
|
|
|
|
last_name: data.lastName,
|
2024-08-19 06:00:59 +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) {
|
|
|
|
|
return this.usersService.updateUser(userId, {
|
|
|
|
|
email: data.email
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-07-29 01:39:42 +00:00
|
|
|
}
|