bsuiness
This commit is contained in:
@@ -1,17 +1,7 @@
|
|||||||
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
|
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
|
||||||
import type { Request } from 'express';
|
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 => {
|
export const AdminId = createParamDecorator((data: unknown, ctx: ExecutionContext): string => {
|
||||||
const request = ctx.switchToHttp().getRequest<Request & { adminId?: string }>();
|
const request = ctx.switchToHttp().getRequest<Request & { adminId?: string }>();
|
||||||
return request.adminId || '';
|
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 || '';
|
||||||
|
});
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
export { UserId } from './user-id.decorator';
|
export { BusinessSlug } from './rest-slug.decorator';
|
||||||
export { RestId } from './rest-id.decorator';
|
export { AdminId } from './admin-id.decorator';
|
||||||
|
export { BusinessId } from './business-id.decorator';
|
||||||
export { RateLimit } from './rate-limit.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,7 +1,7 @@
|
|||||||
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
|
import { createParamDecorator, type ExecutionContext } from '@nestjs/common';
|
||||||
import type { Request } from 'express';
|
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 }>();
|
const request = ctx.switchToHttp().getRequest<Request & { slug?: string }>();
|
||||||
return request.slug || '';
|
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 { ApiTags, ApiOperation, ApiBody, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
|
||||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.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 { AdminId } from 'src/common/decorators/admin-id.decorator';
|
||||||
import { Permission } from 'src/common/enums/permission.enum';
|
import { Permission } from 'src/common/enums/permission.enum';
|
||||||
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
||||||
@@ -15,44 +15,44 @@ import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|||||||
export class AdminController {
|
export class AdminController {
|
||||||
constructor(private readonly adminService: AdminService) { }
|
constructor(private readonly adminService: AdminService) { }
|
||||||
|
|
||||||
@Get('admin/admins')
|
// @Get('admin/admins')
|
||||||
@UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
@Permissions(Permission.MANAGE_ADMINS)
|
// @Permissions(Permission.MANAGE_ADMINS)
|
||||||
getAll(@RestId() restId: string) {
|
// getAll(@BusinessId() restId: string) {
|
||||||
return this.adminService.findAllByRestaurantId(restId);
|
// return this.adminService.findAllByRestaurantId(restId);
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Get('admin/admins/profile')
|
// @Get('admin/admins/profile')
|
||||||
@UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
@Permissions(Permission.MANAGE_ADMINS)
|
// @Permissions(Permission.MANAGE_ADMINS)
|
||||||
getMe(@AdminId() adminId: string, @RestId() restId: string) {
|
// getMe(@AdminId() adminId: string, @BusinessId() restId: string) {
|
||||||
return this.adminService.finAdminById(adminId, restId);
|
// return this.adminService.finAdminById(adminId, restId);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
@Patch('admin/admins/:adminId')
|
// @Patch('admin/admins/:adminId')
|
||||||
@UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
@Permissions(Permission.MANAGE_ADMINS)
|
// @Permissions(Permission.MANAGE_ADMINS)
|
||||||
@ApiOperation({ summary: 'Update an admin' })
|
// @ApiOperation({ summary: 'Update an admin' })
|
||||||
@ApiParam({ name: 'adminId', description: 'Admin ID' })
|
// @ApiParam({ name: 'adminId', description: 'Admin ID' })
|
||||||
update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto,
|
// update(@Param('adminId') adminId: string, @Body() dto: UpdateAdminDto,
|
||||||
@RestId() restId: string) {
|
// @BusinessId() restId: string) {
|
||||||
return this.adminService.update(adminId, restId, dto);
|
// return this.adminService.update(adminId, restId, dto);
|
||||||
}
|
// }
|
||||||
|
|
||||||
@Get('admin/admins/:adminId')
|
// @Get('admin/admins/:adminId')
|
||||||
@UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
@Permissions(Permission.MANAGE_ADMINS)
|
// @Permissions(Permission.MANAGE_ADMINS)
|
||||||
getById(@Param('adminId') adminId: string, @RestId() restId: string) {
|
// getById(@Param('adminId') adminId: string, @BusinessId() restId: string) {
|
||||||
return this.adminService.finAdminById(adminId, restId);
|
// return this.adminService.finAdminById(adminId, restId);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// @Delete('admin/admins/:adminId')
|
// @Delete('admin/admins/:adminId')
|
||||||
// @UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
// @Permissions(Permission.MANAGE_ADMINS)
|
// @Permissions(Permission.MANAGE_ADMINS)
|
||||||
// @ApiOperation({ summary: 'Delete an admin by ID' })
|
// @ApiOperation({ summary: 'Delete an admin by ID' })
|
||||||
// @ApiParam({ name: 'id', description: 'Admin 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);
|
// return this.adminService.remove(adminId, restId);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|||||||
@@ -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 { SetupBusinessDto } from './dto/create-business.dto';
|
||||||
import { UpdateBusinessDto } from './dto/update-business.dto';
|
import { UpdateBusinessDto } from './dto/update-business.dto';
|
||||||
import { EntityManager } from '@mikro-orm/postgresql';
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
@@ -50,6 +50,14 @@ export class BusinessService {
|
|||||||
return business
|
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) {
|
update(id: number, updateBusinessDto: UpdateBusinessDto) {
|
||||||
return `This action updates a #${id} business`;
|
return `This action updates a #${id} business`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 { CatalogueService } from './catalogue.service';
|
||||||
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
|
import { CreateCatalogueDto } from './dto/create-catalogue.dto';
|
||||||
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
||||||
import { FindCataloguesDto } from './dto/find-catalogues.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 {
|
export class CatalogueController {
|
||||||
constructor(private readonly catalogueService: CatalogueService) { }
|
constructor(private readonly catalogueService: CatalogueService) { }
|
||||||
|
|
||||||
@Post()
|
// ============= Public Routes ==============
|
||||||
create(@Body() dto: CreateCatalogueDto) {
|
|
||||||
return this.catalogueService.create(dto);
|
@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) {
|
findAll(@Query() queryDto: FindCataloguesDto) {
|
||||||
return this.catalogueService.findAll(queryDto);
|
return this.catalogueService.findAll(queryDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get('admin/catalogue/:id')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
findOne(@Param('id') id: string) {
|
findOne(@Param('id') id: string) {
|
||||||
return this.catalogueService.findOne(id);
|
return this.catalogueService.findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Patch('admin/catalogue/:id')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
update(@Param('id') id: string, @Body() dto: UpdateCatalogueDto) {
|
update(@Param('id') id: string, @Body() dto: UpdateCatalogueDto) {
|
||||||
return this.catalogueService.update(id, dto);
|
return this.catalogueService.update(id, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Delete('admin/catalogue/:id')
|
||||||
|
@UseGuards(AdminAuthGuard)
|
||||||
remove(@Param('id') id: string) {
|
remove(@Param('id') id: string) {
|
||||||
return this.catalogueService.remove(id);
|
return this.catalogueService.remove(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 { CreateCatalogueDto } from './dto/create-catalogue.dto';
|
||||||
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
import { UpdateCatalogueDto } from './dto/update-catalogue.dto';
|
||||||
import { EntityManager } from '@mikro-orm/postgresql';
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
import { Catalogue } from './entities/catalogue.entity';
|
import { Catalogue } from './entities/catalogue.entity';
|
||||||
import { Business } from '../business/entities/business.entity';
|
|
||||||
import { CatalogueStatus } from './enums/company-status.enum';
|
import { CatalogueStatus } from './enums/company-status.enum';
|
||||||
import { CatalogueRepository } from './repositories/catalogue.repository';
|
import { CatalogueRepository } from './repositories/catalogue.repository';
|
||||||
import { FindCataloguesDto } from './dto/find-catalogues.dto';
|
import { FindCataloguesDto } from './dto/find-catalogues.dto';
|
||||||
|
import { BusinessService } from '../business/business.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CatalogueService {
|
export class CatalogueService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
private readonly catalogueRepository: CatalogueRepository
|
private readonly catalogueRepository: CatalogueRepository,
|
||||||
|
private readonly businessService: BusinessService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async create(dto: CreateCatalogueDto) {
|
async create(dto: CreateCatalogueDto, businessId: string) {
|
||||||
const business = await this.em.findOne(Business, {})
|
const business = await this.businessService.findByIdOrFail(businessId)
|
||||||
if (!business) {
|
|
||||||
throw new BadRequestException()
|
|
||||||
}
|
|
||||||
const catalogue = this.em.create(Catalogue,
|
const catalogue = this.em.create(Catalogue,
|
||||||
{ ...dto, business, status: CatalogueStatus.PENDING })
|
{ ...dto, business, status: CatalogueStatus.PENDING })
|
||||||
|
|
||||||
await this.em.persistAndFlush(catalogue)
|
await this.em.persistAndFlush(catalogue)
|
||||||
|
|
||||||
return catalogue
|
return catalogue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user