Files
dmenu-api/src/modules/restaurants/controllers/restaurants.controller.ts
T
morteza 5b6fb16f5c
deploy to danak / build_and_deploy (push) Has been cancelled
transactions
2026-07-04 12:26:30 +03:30

213 lines
8.4 KiB
TypeScript

import { Controller, Get, Post, Body, Patch, Param, UseGuards, Delete, Query, ValidationPipe } from '@nestjs/common';
import { RestaurantsService } from '../providers/restaurants.service';
import { SetupRestaurantDto } from '../dto/setup-restaurant.dto';
import { UpdateRestaurantDto } from '../dto/update-restaurant.dto';
import { UpgradeSubscriptionDto } from '../dto/upgrade-subscription.dto';
import { FindRestaurantsDto } from '../dto/find-restaurants.dto';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import {
ApiBearerAuth,
ApiBody,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiTags,
ApiHeader,
} from '@nestjs/swagger';
import { ActiveRestaurantListItemDto } from '../dto/active-restaurant-list-item.dto';
import { RestId } from 'src/common/decorators';
import { API_HEADER_SLUG } from 'src/common/constants';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { Permission } from 'src/common/enums/permission.enum';
import { CacheResponse } from 'src/common/decorators/cache-response.decorator';
import { CacheKeyPrefixes } from 'src/common/constants/cache-keys.constant';
import { UpdateRestaurantBgDto } from '../dto/update-restaurant-bg.dto';
import { ChargeRequestDto } from '../dto/charge-request.dto';
import { FinishChargeRequestDto } from '../dto/finish-charge-request.dto';
import { FindRestaurantWalletTransactionsDto } from '../dto/find-restaurant-wallet-transactions.dto';
@ApiTags('restaurants')
@Controller()
export class RestaurantsController {
constructor(private readonly restaurantsService: RestaurantsService) { }
@Get('public/restaurants')
@CacheResponse({ keyPrefix: CacheKeyPrefixes.RESTAURANT_SPEC, params: [] })
@ApiOperation({ summary: 'Get all active restaurants (summary fields)' })
@ApiOkResponse({ description: 'List of active restaurants', type: [ActiveRestaurantListItemDto] })
findAllActiveRestaurants() {
return this.restaurantsService.findAllActiveRestaurants();
}
@Get('public/restaurants/:slug')
@CacheResponse({ keyPrefix: CacheKeyPrefixes.RESTAURANT_SPEC, params: ['slug'] })
@ApiOperation({ summary: 'Get restaurant specification by slug' })
@ApiHeader(API_HEADER_SLUG)
@ApiParam({ name: 'slug', required: true, description: 'Restaurant slug' })
@ApiNotFoundResponse({ description: 'Restaurant not found' })
findBySlug(@Param('slug') slug: string) {
return this.restaurantsService.getRestaurantSpecification(slug);
}
/** Admin Endpoints */
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Get restaurant by ID from request' })
@Get('admin/restaurants/my-restaurant')
async findOne(@RestId() restId: string) {
return await this.restaurantsService.findOneOrFail(restId);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Update a restaurant' })
@ApiBody({ type: UpdateRestaurantDto })
@Patch('admin/restaurants/my-restaurant')
updateMyRestaurant(@RestId() restId: string, @Body() updateRestaurantDto: UpdateRestaurantDto) {
return this.restaurantsService.update(restId, updateRestaurantDto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Delete a restaurant' })
@Delete('admin/restaurants/:id')
remove(@Param('id') id: string) {
return this.restaurantsService.remove(id);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Update restaurant background and menu color' })
@ApiBody({ type: UpdateRestaurantBgDto })
@Patch('admin/restaurants/my-restaurant/background')
updateMyRestaurantBg(@RestId() restId: string, @Body() dto: UpdateRestaurantBgDto) {
return this.restaurantsService.updateBackground(restId, dto);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Get backgrounds' })
@Get('admin/restaurants/background')
getBackgrounds() {
return this.restaurantsService.findAllBackgrounds();
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Get restaurant wallet balance' })
@Get('admin/restaurants/wallet/balance')
getWalletBalance(@RestId() restId: string) {
return this.restaurantsService.getCurrentBalance(restId);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Get restaurant wallet transactions with pagination' })
@Get('admin/restaurants/wallet/transactions')
getWalletTransactions(
@RestId() restId: string,
@Query(new ValidationPipe({ transform: true, whitelist: true }))
query: FindRestaurantWalletTransactionsDto,
) {
return this.restaurantsService.getWalletTransactions(restId, query);
}
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@Permissions(Permission.UPDATE_RESTAURANT)
@ApiOperation({ summary: 'Request wallet charge via external invoice' })
@ApiBody({ type: ChargeRequestDto })
@Post('admin/restaurants/charge-request')
chargeRequest(@RestId() restId: string, @Body() dto: ChargeRequestDto) {
return this.restaurantsService.chargeRequest(restId, dto);
}
/** Super Admin Endpoints */
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all restaurants with pagination and filters' })
@Get('super-admin/restaurants')
findAll(@Query() dto: FindRestaurantsDto) {
return this.restaurantsService.findAll(dto);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Setup a new restaurant' })
@ApiBody({ type: SetupRestaurantDto })
@Post('super-admin/restaurants')
setupRestaurant(@Body() setupRestaurantDto: SetupRestaurantDto) {
return this.restaurantsService.setupRestuarant(setupRestaurantDto);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get restaurant by subscription ID' })
@ApiParam({ name: 'subscriptionId', required: true, description: 'Subscription ID' })
@Get('super-admin/restaurants/subscription/:subscriptionId')
findOneBySubscriptionId(@Param('subscriptionId') subscriptionId: string) {
return this.restaurantsService.findOneBySubscriptionId(subscriptionId);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get restaurant by ID' })
@ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
@Get('super-admin/restaurants/:id')
findOneById(@Param('id') id: string) {
return this.restaurantsService.findOneOrFail(id);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Update a restaurant by ID' })
@ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
@ApiBody({ type: UpdateRestaurantDto })
@Patch('super-admin/restaurants/:id')
updateRestaurant(@Param('id') id: string, @Body() updateRestaurantDto: UpdateRestaurantDto) {
return this.restaurantsService.update(id, updateRestaurantDto);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Upgrade subscription for a restaurant' })
@ApiParam({ name: 'subscriptionId', required: true, description: 'Subscription ID' })
@ApiBody({ type: UpgradeSubscriptionDto })
@Patch('super-admin/restaurants/subscription/:subscriptionId/upgrade')
upgradeSubscription(
@Param('subscriptionId') subscriptionId: string,
@Body() upgradeSubscriptionDto: UpgradeSubscriptionDto
) {
return this.restaurantsService.upgradeSubscription(subscriptionId, upgradeSubscriptionDto);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Finalize wallet charge after invoice payment' })
@ApiBody({ type: FinishChargeRequestDto })
@Post('super-admin/restaurants/charge-request/finish')
finishChargeRequest(@Body() dto: FinishChargeRequestDto) {
return this.restaurantsService.finishChargeRequest(dto);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Hard delete a restaurant and all related data' })
@ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
@Delete('super-admin/restaurants/:id/hard-delete')
hardDeleteRestaurant(@Param('id') id: string) {
return this.restaurantsService.hardDeleteRestaurant(id);
}
}