🚀 Initialize Musadaq SaaS: Full Backend + AI + React Dashboard + Docker Setup
This commit is contained in:
71
backend/src/modules/users/user.service.ts
Normal file
71
backend/src/modules/users/user.service.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* ════════════════════════════════════════════════════════════
|
||||
* مُصادَق (Musadaq) — Users Service
|
||||
* ════════════════════════════════════════════════════════════
|
||||
*/
|
||||
|
||||
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { User } from './entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* إضافة مستخدم لمكتب محاسبة
|
||||
*/
|
||||
async create(tenantId: string, dto: any): Promise<User> {
|
||||
const existing = await this.userRepository.findOne({
|
||||
where: { email: dto.email, tenant_id: tenantId },
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
throw new ConflictException('User with this email already exists in this office');
|
||||
}
|
||||
|
||||
const passwordHash = await bcrypt.hash(dto.password, 12);
|
||||
|
||||
const user = this.userRepository.create({
|
||||
...dto,
|
||||
password_hash: passwordHash,
|
||||
tenant_id: tenantId,
|
||||
});
|
||||
|
||||
return this.userRepository.save(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* قائمة مستخدمي المكتب
|
||||
*/
|
||||
async findAll(tenantId: string): Promise<User[]> {
|
||||
return this.userRepository.find({
|
||||
where: { tenant_id: tenantId, is_active: true },
|
||||
order: { created_at: 'ASC' },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* تفاصيل مستخدم
|
||||
*/
|
||||
async findOne(tenantId: string, id: string): Promise<User> {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { id, tenant_id: tenantId },
|
||||
});
|
||||
if (!user) throw new NotFoundException('User not found');
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* تعطيل مستخدم
|
||||
*/
|
||||
async remove(tenantId: string, id: string): Promise<void> {
|
||||
const user = await this.findOne(tenantId, id);
|
||||
await this.userRepository.update(id, { is_active: false });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user