39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { User, UserRole } from './entities/user.entity';
|
|
|
|
@Injectable()
|
|
export class UsersService {
|
|
constructor(
|
|
@InjectRepository(User)
|
|
private readonly userRepository: Repository<User>,
|
|
) {}
|
|
|
|
async findByPhone(tenantId: string, phone: string): Promise<User | null> {
|
|
return this.userRepository.findOne({ where: { tenant_id: tenantId, phone } });
|
|
}
|
|
|
|
async findById(tenantId: string, id: string): Promise<User | null> {
|
|
return this.userRepository.findOne({ where: { tenant_id: tenantId, id } });
|
|
}
|
|
|
|
async create(tenantId: string, phone: string, role: string = 'rider'): Promise<User> {
|
|
const user = this.userRepository.create({ tenant_id: tenantId, phone, role: role as UserRole });
|
|
return this.userRepository.save(user);
|
|
}
|
|
|
|
async listByTenant(tenantId: string): Promise<User[]> {
|
|
return this.userRepository.find({ where: { tenant_id: tenantId } });
|
|
}
|
|
|
|
async updateProfile(
|
|
tenantId: string,
|
|
id: string,
|
|
data: Partial<Pick<User, 'name'>>,
|
|
): Promise<User | null> {
|
|
await this.userRepository.update({ tenant_id: tenantId, id }, { name: data.name });
|
|
return this.findById(tenantId, id);
|
|
}
|
|
}
|