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, ) {} async findByPhone(tenantId: string, phone: string): Promise { return this.userRepository.findOne({ where: { tenant_id: tenantId, phone } }); } async findById(tenantId: string, id: string): Promise { return this.userRepository.findOne({ where: { tenant_id: tenantId, id } }); } async create(tenantId: string, phone: string, role: string = 'rider'): Promise { const user = this.userRepository.create({ tenant_id: tenantId, phone, role: role as UserRole }); return this.userRepository.save(user); } async findOrCreate(tenantId: string, phone: string, role: string = 'rider'): Promise { const existing = await this.findByPhone(tenantId, phone); return existing ?? this.create(tenantId, phone, role); } async listByTenant(tenantId: string): Promise { return this.userRepository.find({ where: { tenant_id: tenantId } }); } async updateProfile( tenantId: string, id: string, data: Partial>, ): Promise { const patch: Partial = {}; if (data.name !== undefined) patch.name = data.name; if (data.language !== undefined) patch.language = data.language; if (Object.keys(patch).length > 0) { await this.userRepository.update({ tenant_id: tenantId, id }, patch); } return this.findById(tenantId, id); } /** لغة الإشعارات للمستخدم — استعلام خفيف (عمود واحد) يُستدعى قبل كل push. */ async getLanguage(tenantId: string, id: string): Promise { const row = await this.userRepository.findOne({ where: { tenant_id: tenantId, id }, select: { language: true }, }); return row?.language ?? null; } async setRole(tenantId: string, id: string, role: string): Promise { await this.userRepository.update( { tenant_id: tenantId, id }, { role: role as UserRole }, ); } }