This commit is contained in:
2026-03-10 13:02:01 +03:30
parent 98bd3c735b
commit 7d24a8b6c5
10 changed files with 90 additions and 97 deletions
+1 -11
View File
@@ -1,17 +1,7 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
/**
* Decorator to extract userId from the authenticated request.
* Must be used after AdminAuthGuard or AuthGuard that sets request.userId.
*
* @example
* @Get('/profile')
* @UseGuards(AdminAuthGuard)
* getProfile(@UserId() userId: string) {
* return this.userService.findById(userId);
* }
*/
export const AdminId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { adminId?: string }>();
return request.adminId || '';
@@ -0,0 +1,8 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
export const BusinessId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { busId?: string }>();
return request.busId || '';
});
+4 -2
View File
@@ -1,3 +1,5 @@
export { UserId } from './user-id.decorator';
export { RestId } from './rest-id.decorator';
export { BusinessSlug } from './rest-slug.decorator';
export { AdminId } from './admin-id.decorator';
export { BusinessId } from './business-id.decorator';
export { RateLimit } from './rate-limit.decorator';
@@ -1,18 +0,0 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
/**
* Decorator to extract restId from the authenticated request.
* Must be used after AdminAuthGuard or AuthGuard that sets request.restId.
*
* @example
* @Get('/restaurants')
* @UseGuards(AdminAuthGuard)
* getRestaurants(@RestId() restId: string) {
* return this.restaurantService.findById(restId);
* }
*/
export const RestId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { restId?: string }>();
return request.restId || '';
});
+1 -1
View File
@@ -1,7 +1,7 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
export const RestSlug = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
export const BusinessSlug = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { slug?: string }>();
return request.slug || '';
});
@@ -1,18 +0,0 @@
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
import type { Request } from 'express';
/**
* Decorator to extract userId from the authenticated request.
* Must be used after AdminAuthGuard or AuthGuard that sets request.userId.
*
* @example
* @Get('/profile')
* @UseGuards(AdminAuthGuard)
* getProfile(@UserId() userId: string) {
* return this.userService.findById(userId);
* }
*/
export const UserId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
const request = ctx.switchToHttp().getRequest<Request & { userId?: string }>();
return request.userId || '';
});
@@ -4,7 +4,7 @@ import { UpdateAdminDto } from '../dto/update-admin.dto';
import { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { RestId } from 'src/common/decorators';
import { BusinessId } from 'src/common/decorators';
import { AdminId } from 'src/common/decorators/admin-id.decorator';
import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';
@@ -15,44 +15,44 @@ import { Permissions } from 'src/common/decorators/permissions.decorator';
export class AdminController {
constructor(private readonly adminService: AdminService) { }
@Get('admin/admins')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
getAll(@RestId() restId: string) {
return this.adminService.findAllByRestaurantId(restId);
}
// @Get('admin/admins')
// @UseGuards(AdminAuthGuard)
// @Permissions(Permission.MANAGE_ADMINS)
// getAll(@BusinessId() restId: string) {
// return this.adminService.findAllByRestaurantId(restId);
// }
@Get('admin/admins/profile')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
getMe(@AdminId() adminId: string, @RestId() restId: string) {
return this.adminService.finAdminById(adminId, restId);
}
// @Get('admin/admins/profile')
// @UseGuards(AdminAuthGuard)
// @Permissions(Permission.MANAGE_ADMINS)
// getMe(@AdminId() adminId: string, @BusinessId() restId: string) {
// return this.adminService.finAdminById(adminId, restId);
// }
@Patch('admin/admins/:adminId')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
@ApiOperation({ summary: 'Update an admin' })
@ApiParam({ name: 'adminId', description: 'Admin ID' })
update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto,
@RestId() restId: string) {
return this.adminService.update(adminId, restId, dto);
}
// @Patch('admin/admins/:adminId')
// @UseGuards(AdminAuthGuard)
// @Permissions(Permission.MANAGE_ADMINS)
// @ApiOperation({ summary: 'Update an admin' })
// @ApiParam({ name: 'adminId', description: 'Admin ID' })
// update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto,
// @BusinessId() restId: string) {
// return this.adminService.update(adminId, restId, dto);
// }
@Get('admin/admins/:adminId')
@UseGuards(AdminAuthGuard)
@Permissions(Permission.MANAGE_ADMINS)
getById(@Param('adminId') adminId: string, @RestId() restId: string) {
return this.adminService.finAdminById(adminId, restId);
}
// @Get('admin/admins/:adminId')
// @UseGuards(AdminAuthGuard)
// @Permissions(Permission.MANAGE_ADMINS)
// getById(@Param('adminId') adminId: string, @BusinessId() restId: string) {
// return this.adminService.finAdminById(adminId, restId);
// }
// @Delete('admin/admins/:adminId')
// @UseGuards(AdminAuthGuard)
// @Permissions(Permission.MANAGE_ADMINS)
// @ApiOperation({ summary: 'Delete an admin by ID' })
// @ApiParam({ name: 'id', description: 'Admin ID' })
// deleteById(@Param('adminId') adminId: string, @RestId() restId: string) {
// deleteById(@Param('adminId') adminId: string, @BusinessId() restId: string) {
// return this.adminService.remove(adminId, restId);
// }
+9 -1
View File
@@ -1,4 +1,4 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { SetupBusinessDto } from './dto/create-business.dto';
import { UpdateBusinessDto } from './dto/update-business.dto';
import { EntityManager } from '@mikro-orm/postgresql';
@@ -50,6 +50,14 @@ export class BusinessService {
return business
}
async findByIdOrFail(businessId: string) {
const business = await this.businessRepository.findOne({ id:businessId }, { populate: ['admin'] })
if (!business) {
throw new NotFoundException("Business not found")
}
return business
}
update(id: number, updateBusinessDto: UpdateBusinessDto) {
return `This action updates a #${id} business`;
}
+30 -9
View File
@@ -1,34 +1,55 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, Query } from '@nestjs/common';
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { CatalogueService } from './catalogue.service';
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
import { FindCataloguesDto } from './dto/find-catalogues.dto';
import { AdminAuthGuard } from '../auth/guards/adminAuth.guard';
import { BusinessId } from 'src/common/decorators';
@Controller('catalogue')
@Controller()
export class CatalogueController {
constructor(private readonly catalogueService: CatalogueService) { }
@Post()
create(@Body() dto: CreateCatalogueDto) {
return this.catalogueService.create(dto);
// ============= Public Routes ==============
@Get('public/catalogue')
findAllAsPublic(@Query() queryDto: FindCataloguesDto) {
return this.catalogueService.findAll(queryDto);
}
@Get()
@Get('admin/catalogue/:id')
findOneAsPublic(@Param('id') id: string) {
return this.catalogueService.findOne(id);
}
// ============= Admin Routes ==============
@Post('admin/catalogue')
@UseGuards(AdminAuthGuard)
create(@Body() dto: CreateCatalogueDto, @BusinessId() busId: string) {
return this.catalogueService.create(dto, busId);
}
@Get('admin/catalogue')
@UseGuards(AdminAuthGuard)
findAll(@Query() queryDto: FindCataloguesDto) {
return this.catalogueService.findAll(queryDto);
}
@Get(':id')
@Get('admin/catalogue/:id')
@UseGuards(AdminAuthGuard)
findOne(@Param('id') id: string) {
return this.catalogueService.findOne(id);
}
@Patch(':id')
@Patch('admin/catalogue/:id')
@UseGuards(AdminAuthGuard)
update(@Param('id') id: string, @Body() dto: UpdateCatalogueDto) {
return this.catalogueService.update(id, dto);
}
@Delete(':id')
@Delete('admin/catalogue/:id')
@UseGuards(AdminAuthGuard)
remove(@Param('id') id: string) {
return this.catalogueService.remove(id);
}
+8 -8
View File
@@ -1,29 +1,29 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
import { EntityManager } from '@mikro-orm/postgresql';
import { Catalogue } from './entities/catalogue.entity';
import { Business } from '../business/entities/business.entity';
import { CatalogueStatus } from './enums/company-status.enum';
import { CatalogueRepository } from './repositories/catalogue.repository';
import { FindCataloguesDto } from './dto/find-catalogues.dto';
import { BusinessService } from '../business/business.service';
@Injectable()
export class CatalogueService {
constructor(
private readonly em: EntityManager,
private readonly catalogueRepository: CatalogueRepository
private readonly catalogueRepository: CatalogueRepository,
private readonly businessService: BusinessService,
) { }
async create(dto: CreateCatalogueDto) {
const business = await this.em.findOne(Business, {})
if (!business) {
throw new BadRequestException()
}
async create(dto: CreateCatalogueDto, businessId: string) {
const business = await this.businessService.findByIdOrFail(businessId)
const catalogue = this.em.create(Catalogue,
{ ...dto, business, status: CatalogueStatus.PENDING })
await this.em.persistAndFlush(catalogue)
return catalogue
}