40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
|
|
import { Result, UniqueID } from "@common/domain";
|
||
|
|
import { IUserRepository, User } from "..";
|
||
|
|
import { IUserService } from "./user-service.interface";
|
||
|
|
|
||
|
|
export class UserService implements IUserService {
|
||
|
|
constructor(private readonly userRepository: IUserRepository) {}
|
||
|
|
|
||
|
|
async findUsers(transaction?: any): Promise<Result<User[], Error>> {
|
||
|
|
const usersOrError = await this.userRepository.findAll(transaction);
|
||
|
|
if (usersOrError.isFailure) {
|
||
|
|
return Result.fail(usersOrError.error);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Solo devolver usuarios activos
|
||
|
|
const activeUsers = usersOrError.data.filter((user) => user /*.isActive*/);
|
||
|
|
return Result.ok(activeUsers);
|
||
|
|
}
|
||
|
|
|
||
|
|
async findUserById(userId: UniqueID, transaction?: any): Promise<Result<User>> {
|
||
|
|
return await this.userRepository.findById(userId, transaction);
|
||
|
|
}
|
||
|
|
|
||
|
|
/*public async createUser(
|
||
|
|
data: { name: string; email: EmailAddress },
|
||
|
|
transaction?: Transaction
|
||
|
|
): Promise<Result<User>> {
|
||
|
|
// Evitar duplicados por email
|
||
|
|
const existingUser = await this.userRepository.findByEmail(data.email);
|
||
|
|
if (existingUser.isSuccess) {
|
||
|
|
return Result.fail(new Error("El correo ya está registrado."));
|
||
|
|
}
|
||
|
|
|
||
|
|
const newUser = User.create({
|
||
|
|
email,
|
||
|
|
username
|
||
|
|
})
|
||
|
|
return await this.userRepository.save(newUser, transaction);
|
||
|
|
}*/
|
||
|
|
}
|