update
This commit is contained in:
@@ -10,12 +10,13 @@ import { RolePermission } from '../roles/entities/rolePermission.entity';
|
||||
import { AdminController } from './controllers/admin.controller';
|
||||
import { UtilsModule } from '../utils/utils.module';
|
||||
import { RestaurantsModule } from '../restaurants/restaurants.module';
|
||||
import { AdminRole } from './entities/adminRole.entity';
|
||||
|
||||
@Module({
|
||||
providers: [AdminService, AdminRepository],
|
||||
controllers: [AdminController],
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission]),
|
||||
MikroOrmModule.forFeature([Admin, Role, Permission, RolePermission, AdminRole]),
|
||||
JwtModule,
|
||||
UtilsModule,
|
||||
RestaurantsModule,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param } from '@nestjs/common';
|
||||
import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { AdminService } from '../providers/admin.service';
|
||||
import { CreateAdminDto } from '../dto/create-admin.dto';
|
||||
import { UpdateAdminDto } from '../dto/update-admin.dto';
|
||||
@@ -26,8 +26,8 @@ export class AdminController {
|
||||
@Get('profile')
|
||||
@ApiOperation({ summary: 'admin' })
|
||||
@ApiResponse({ status: 201, description: 'Admins' })
|
||||
async getMe(@AdminId() adminId: string) {
|
||||
const admin = await this.adminService.findById(adminId);
|
||||
async getMe(@AdminId() adminId: string, @RestId() restId: string) {
|
||||
const admin = await this.adminService.findById(adminId, restId);
|
||||
return admin;
|
||||
}
|
||||
|
||||
@@ -56,4 +56,20 @@ export class AdminController {
|
||||
update(@Param('id') id: string, @Body() dto: UpdateAdminDto, @RestId() restId: string): Promise<Admin> {
|
||||
return this.adminService.update(id, restId, dto);
|
||||
}
|
||||
@Get(':adminId')
|
||||
@ApiOperation({ summary: 'Get an admin by ID' })
|
||||
@ApiParam({ name: 'id', description: 'Admin ID' })
|
||||
@ApiResponse({ status: 200, description: 'Admin found successfully' })
|
||||
@ApiResponse({ status: 404, description: 'Admin not found' })
|
||||
getById(@Param('adminId') adminId: string, @RestId() restId: string): Promise<Admin | null> {
|
||||
return this.adminService.findById(adminId, restId);
|
||||
}
|
||||
@Delete(':adminId')
|
||||
@ApiOperation({ summary: 'Delete an admin by ID' })
|
||||
@ApiParam({ name: 'id', description: 'Admin ID' })
|
||||
@ApiResponse({ status: 200, description: 'Admin deleted successfully' })
|
||||
@ApiResponse({ status: 404, description: 'Admin not found' })
|
||||
deleteById(@Param('adminId') adminId: string, @RestId() restId: string): Promise<void> {
|
||||
return this.adminService.remove(adminId, restId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
|
||||
import { Admin } from './admin.entity';
|
||||
|
||||
@Entity({ tableName: 'admin_roles' })
|
||||
@Unique({ properties: ['admin', 'role', 'restaurant'] })
|
||||
@Unique({ properties: ['admin', 'restaurant'] })
|
||||
export class AdminRole extends BaseEntity {
|
||||
@ManyToOne(() => Admin)
|
||||
admin!: Admin;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
import { EntityRepository } from '@mikro-orm/core';
|
||||
import { Admin } from '../entities/admin.entity';
|
||||
@@ -15,6 +15,8 @@ export class AdminService {
|
||||
constructor(
|
||||
@InjectRepository(Admin)
|
||||
private readonly adminRepository: EntityRepository<Admin>,
|
||||
@InjectRepository(AdminRole)
|
||||
private readonly adminRoleRepository: EntityRepository<AdminRole>,
|
||||
private readonly em: EntityManager,
|
||||
private readonly cacheService: CacheService,
|
||||
) {}
|
||||
@@ -23,8 +25,11 @@ export class AdminService {
|
||||
return this.adminRepository.findOne({ phone });
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Admin | null> {
|
||||
const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role'] });
|
||||
async findById(adminId: string, restId: string): Promise<Admin | null> {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles', 'roles.role'] },
|
||||
);
|
||||
return admin;
|
||||
}
|
||||
|
||||
@@ -36,10 +41,10 @@ export class AdminService {
|
||||
const { phone, firstName, lastName, roleId } = dto;
|
||||
const currentAdmin = await this.adminRepository.findOne({
|
||||
phone,
|
||||
roles: { role: { id: roleId }, restaurant: { id: restId } },
|
||||
roles: { restaurant: { id: restId } },
|
||||
});
|
||||
if (currentAdmin) {
|
||||
return currentAdmin;
|
||||
throw new ConflictException('This Phone Number is already Admin for this restaurant');
|
||||
}
|
||||
const role = await this.em.findOne(Role, { id: roleId });
|
||||
if (!role) throw new Error('Role not found');
|
||||
@@ -77,8 +82,11 @@ export class AdminService {
|
||||
return admin;
|
||||
}
|
||||
|
||||
async update(id: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
|
||||
const admin = await this.adminRepository.findOne({ id }, { populate: ['roles', 'roles.role', 'roles.restaurant'] });
|
||||
async update(adminId: string, restId: string, dto: UpdateAdminDto): Promise<Admin> {
|
||||
const admin = await this.adminRepository.findOne(
|
||||
{ id: adminId, roles: { restaurant: { id: restId } } },
|
||||
{ populate: ['roles', 'roles.role', 'roles.restaurant'] },
|
||||
);
|
||||
|
||||
if (!admin) {
|
||||
throw new NotFoundException('Admin not found');
|
||||
@@ -111,7 +119,7 @@ export class AdminService {
|
||||
|
||||
// Find existing AdminRole for this admin and restaurant
|
||||
const existingAdminRole = await this.em.findOne(AdminRole, {
|
||||
admin: { id },
|
||||
admin: { id: adminId },
|
||||
restaurant: { id: restId },
|
||||
});
|
||||
|
||||
@@ -134,4 +142,14 @@ export class AdminService {
|
||||
await this.em.persistAndFlush(admin);
|
||||
return admin;
|
||||
}
|
||||
|
||||
async remove(adminId: string, restId: string): Promise<void> {
|
||||
const adminRole = await this.adminRoleRepository.findOne({ admin: { id: adminId }, restaurant: { id: restId } });
|
||||
if (!adminRole) {
|
||||
throw new NotFoundException('Admin role not found');
|
||||
}
|
||||
|
||||
await this.em.nativeDelete(AdminRole, { id: adminRole.id });
|
||||
await this.em.persistAndFlush(adminRole);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user