Refactor admin module structure: moved services and controllers to providers and controllers directories, added PermissionService and PermissionController, updated Admin entity relationships, and created AdminRole entity.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { Admin } from '../entities/admin.entity';
|
||||
import { Role } from '../entities/role.entity';
|
||||
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
|
||||
@Injectable()
|
||||
export class AdminService {
|
||||
constructor(
|
||||
@InjectRepository(Admin)
|
||||
private readonly adminRepository: EntityRepository<Admin>,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async findByPhone(phone: string): Promise<Admin | null> {
|
||||
return this.adminRepository.findOne({ phone });
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Admin | null> {
|
||||
return this.adminRepository.findOne({ id });
|
||||
}
|
||||
async findAll(): Promise<Admin[]> {
|
||||
return this.adminRepository.findAll();
|
||||
}
|
||||
|
||||
async create(data: { phone: string; firstName?: string; lastName?: string; roleId: string; restId: string }) {
|
||||
const { phone, firstName, lastName, roleId, restId } = data;
|
||||
|
||||
// check existing
|
||||
const existing = await this.adminRepository.findOne({ phone, restaurant: { id: restId } });
|
||||
if (existing) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
// load Role and Restaurant using the EntityManager
|
||||
const role = await this.em.findOne(Role, { id: roleId });
|
||||
if (!role) throw new Error('Role not found');
|
||||
|
||||
const restaurant = await this.em.findOne(Restaurant, { id: restId });
|
||||
if (!restaurant) throw new Error('Restaurant not found');
|
||||
|
||||
const admin = this.adminRepository.create({ phone, firstName, lastName, role, restaurant });
|
||||
await this.em.persistAndFlush(admin);
|
||||
return admin;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user