Add authentication guards and API documentation to admin and restaurant controllers; update restaurant service for soft delete; enhance database seeder with new permissions and roles
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { Controller, Post, Body, HttpCode, HttpStatus, Get } from '@nestjs/common';
|
||||
import { Controller, Post, Body, HttpCode, HttpStatus, Get, UseGuards } from '@nestjs/common';
|
||||
import { AdminService } from '../providers/admin.service';
|
||||
import { CreateAdminDto } from '../dto/create-admin.dto';
|
||||
import { ApiTags, ApiOperation, ApiBody, ApiResponse } from '@nestjs/swagger';
|
||||
import { ApiTags, ApiOperation, ApiBody, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiTags('admin')
|
||||
@Controller('admin')
|
||||
export class AdminController {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { PermissionService } from '../providers/permission.service';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiTags('admin/permissions')
|
||||
@Controller('admin/permissions')
|
||||
export class PermissionController {
|
||||
@@ -14,4 +17,3 @@ export class PermissionController {
|
||||
return this.permissionService.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +1,59 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, UseGuards, Delete, NotFoundException } from '@nestjs/common';
|
||||
import { RestaurantsService } from '../providers/restaurants.service';
|
||||
import { CreateRestaurantDto } from '../dto/create-restaurant.dto';
|
||||
import { UpdateRestaurantDto } from '../dto/update-restaurant.dto';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
import { ApiBearerAuth, ApiBody, ApiNotFoundResponse, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiTags('admin/restaurants')
|
||||
@Controller('admin/restaurants')
|
||||
export class RestaurantsController {
|
||||
constructor(private readonly restaurantsService: RestaurantsService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create a new restaurant' })
|
||||
@ApiBody({ type: CreateRestaurantDto })
|
||||
@ApiResponse({ status: 201, description: 'Restaurant created' })
|
||||
create(@Body() createRestaurantDto: CreateRestaurantDto) {
|
||||
return this.restaurantsService.create(createRestaurantDto);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get all restaurants' })
|
||||
@ApiResponse({ status: 200, description: 'Restaurants found' })
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.restaurantsService.findAll();
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Get a restaurant by slug' })
|
||||
@ApiResponse({ status: 200, description: 'Restaurant found' })
|
||||
@ApiNotFoundResponse({ type: NotFoundException, description: 'Restaurant not found' })
|
||||
@Get(':slug')
|
||||
@ApiOperation({ summary: 'Get a restaurant by slug' })
|
||||
@ApiResponse({ status: 200, description: 'Restaurant found' })
|
||||
@ApiNotFoundResponse({ description: 'Restaurant not found' })
|
||||
findOne(@Param('slug') slug: string) {
|
||||
return this.restaurantsService.findBySlug(slug);
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Update a restaurant' })
|
||||
@ApiBody({ type: UpdateRestaurantDto })
|
||||
@ApiResponse({ status: 200, description: 'Restaurant updated' })
|
||||
@ApiNotFoundResponse({ type: NotFoundException, description: 'Restaurant not found' })
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() updateRestaurantDto: UpdateRestaurantDto) {
|
||||
return this.restaurantsService.update(id, updateRestaurantDto);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'Delete a restaurant' })
|
||||
@ApiResponse({ status: 200, description: 'Restaurant deleted' })
|
||||
@ApiNotFoundResponse({ description: 'Restaurant not found' })
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.restaurantsService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,17 @@ export class RestaurantsService {
|
||||
return restaurant;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} restaurant`;
|
||||
async remove(id: string) {
|
||||
// Soft delete the restaurant by setting isActive to false
|
||||
const restaurant = await this.restRepository.findOne({ id });
|
||||
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
restaurant.isActive = false;
|
||||
await this.em.persistAndFlush(restaurant);
|
||||
|
||||
return restaurant;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ export class DatabaseSeeder extends Seeder {
|
||||
async run(em: EntityManager) {
|
||||
// 1. Create Permissions (English)
|
||||
const permissions = [
|
||||
{ name: 'manage_restaurants' },
|
||||
{ name: 'manage_foods' },
|
||||
{ name: 'manage_categories' },
|
||||
{ name: 'manage_orders' },
|
||||
@@ -20,6 +19,11 @@ export class DatabaseSeeder extends Seeder {
|
||||
{ name: 'manage_users' },
|
||||
{ name: 'view_reports' },
|
||||
{ name: 'manage_finances' },
|
||||
{ name: 'get_own_restaurant' },
|
||||
{ name: 'get_all_restaurants' },
|
||||
{ name: 'create_restaurant' },
|
||||
{ name: 'delete_restaurant' },
|
||||
{ name: 'update_restaurant' },
|
||||
];
|
||||
|
||||
const createdPermissions: Permission[] = [];
|
||||
@@ -33,12 +37,13 @@ export class DatabaseSeeder extends Seeder {
|
||||
}
|
||||
|
||||
// 2. Create Roles (English)
|
||||
const ownerRole = await em.findOne(Role, { name: 'owner' });
|
||||
if (!ownerRole) {
|
||||
const role = em.create(Role, { name: 'owner' });
|
||||
// Add all permissions to owner
|
||||
let restaurantRole = await em.findOne(Role, { name: 'restaurant' });
|
||||
if (!restaurantRole) {
|
||||
const role = em.create(Role, { name: 'restaurant' });
|
||||
// Add all permissions to restaurant role (including update_restaurant)
|
||||
createdPermissions.forEach(p => role.permissions.add(p));
|
||||
em.persist(role);
|
||||
restaurantRole = role;
|
||||
}
|
||||
|
||||
const accountantRole = await em.findOne(Role, { name: 'accountant' });
|
||||
@@ -52,11 +57,25 @@ export class DatabaseSeeder extends Seeder {
|
||||
em.persist(role);
|
||||
}
|
||||
|
||||
const superAdminRole = await em.findOne(Role, { name: 'superAdmin' });
|
||||
if (!superAdminRole) {
|
||||
const role = em.create(Role, { name: 'superAdmin' });
|
||||
// Add restaurant management permissions to superAdmin
|
||||
const restaurantPerms = createdPermissions.filter(
|
||||
p => p.name === 'create_restaurant' || p.name === 'delete_restaurant' || p.name === 'update_restaurant',
|
||||
);
|
||||
restaurantPerms.forEach(p => role.permissions.add(p));
|
||||
em.persist(role);
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
|
||||
// Get roles after flush
|
||||
const owner = await em.findOne(Role, { name: 'owner' });
|
||||
// Get roles after flush (refresh to ensure they're loaded)
|
||||
if (!restaurantRole) {
|
||||
restaurantRole = await em.findOne(Role, { name: 'restaurant' });
|
||||
}
|
||||
const accountant = await em.findOne(Role, { name: 'accountant' });
|
||||
const superAdmin = await em.findOne(Role, { name: 'superAdmin' });
|
||||
|
||||
// 3. Create Restaurants (Farsi)
|
||||
const zhivanRestaurant = await em.findOne(Restaurant, { slug: 'zhivan' });
|
||||
@@ -400,12 +419,12 @@ export class DatabaseSeeder extends Seeder {
|
||||
|
||||
// 6. Create Admins (Farsi)
|
||||
const adminMorteza = await em.findOne(Admin, { phone: '09362532122' });
|
||||
if (!adminMorteza && owner && zhivan) {
|
||||
if (!adminMorteza && restaurantRole && zhivan) {
|
||||
const admin = em.create(Admin, {
|
||||
phone: '09362532122',
|
||||
firstName: 'مرتضی',
|
||||
lastName: 'مرتضایی',
|
||||
role: owner,
|
||||
role: restaurantRole,
|
||||
restaurant: zhivan,
|
||||
});
|
||||
em.persist(admin);
|
||||
@@ -423,6 +442,18 @@ export class DatabaseSeeder extends Seeder {
|
||||
em.persist(admin);
|
||||
}
|
||||
|
||||
const adminMehrdad = await em.findOne(Admin, { phone: '0912928395' });
|
||||
if (!adminMehrdad && superAdmin) {
|
||||
const admin = em.create(Admin, {
|
||||
phone: '0912928395',
|
||||
firstName: 'مهرداد',
|
||||
lastName: 'مظفری',
|
||||
role: superAdmin,
|
||||
restaurant: null, // SuperAdmin doesn't belong to a specific restaurant
|
||||
});
|
||||
em.persist(admin);
|
||||
}
|
||||
|
||||
await em.flush();
|
||||
|
||||
// 7. Create Users (Farsi)
|
||||
|
||||
Reference in New Issue
Block a user