import { Injectable, Inject } from '@nestjs/common'; import * as bcrypt from 'bcryptjs'; import { IPaginator } from '@devstroupe/devkit-core'; import { IUserRepository } from '../domain/user.repository'; import { User } from '../domain/user'; @Injectable() export class UserService { constructor( @Inject('IUserRepository') private readonly userRepo: IUserRepository, ) {} async create(user: Omit): Promise { const existing = await this.userRepo.findByEmail(user.email); if (existing) { throw new Error('Email already registered'); } const hashedPassword = await bcrypt.hash(user.password, 10); return this.userRepo.create({ ...user, password: hashedPassword, }); } async findById(id: number): Promise { return this.userRepo.findById(id); } async findOne(id: number): Promise { return this.findById(id); } async findByEmail(email: string): Promise { return this.userRepo.findByEmail(email); } async update(id: number, user: Partial): Promise { if (user.password) { user.password = await bcrypt.hash(user.password, 10); } return this.userRepo.update(id, user); } async findAll(): Promise { return this.userRepo.findAll(); } async findMany(filter: any): Promise> { return this.userRepo.findMany(filter); } async delete(id: number): Promise { await this.userRepo.delete(id); } }