From 37f7b11dbc62b715aa9b73bf61c6a3a9b9c7ea61 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 21 Dec 2025 09:43:16 +0330 Subject: [PATCH] foods pagination --- src/core/interceptors/response.interceptor.ts | 10 ++ .../foods/controllers/food.controller.ts | 5 +- .../foods/repositories/food.repository.ts | 2 + .../users/controllers/users.controller.ts | 114 ++++-------------- 4 files changed, 39 insertions(+), 92 deletions(-) diff --git a/src/core/interceptors/response.interceptor.ts b/src/core/interceptors/response.interceptor.ts index 1d4dc28..5dc0d31 100755 --- a/src/core/interceptors/response.interceptor.ts +++ b/src/core/interceptors/response.interceptor.ts @@ -25,6 +25,16 @@ export class ResponseInterceptor implements NestInterceptor { return data; } + // Check if this is a paginated result (has both data and meta properties) + if (data && typeof data === 'object' && 'data' in data && 'meta' in data) { + return { + statusCode, + success: true, + data: data.data, + meta: data.meta, + }; + } + if (data && data.data !== undefined) { return { statusCode, diff --git a/src/modules/foods/controllers/food.controller.ts b/src/modules/foods/controllers/food.controller.ts index 4d9c1dd..5305755 100644 --- a/src/modules/foods/controllers/food.controller.ts +++ b/src/modules/foods/controllers/food.controller.ts @@ -88,8 +88,9 @@ export class FoodController { @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] }) @ApiQuery({ name: 'categoryId', required: false, type: String }) @ApiQuery({ name: 'isActive', required: false, type: Boolean }) - findAll(@Query() dto: FindFoodsDto, @RestId() restId: string) { - return this.foodsService.findAll(restId, dto); + async findAll(@Query() dto: FindFoodsDto, @RestId() restId: string) { + const result =await this.foodsService.findAll(restId, dto); + return result; } @UseGuards(AdminAuthGuard) diff --git a/src/modules/foods/repositories/food.repository.ts b/src/modules/foods/repositories/food.repository.ts index d9f0559..d80a2c0 100644 --- a/src/modules/foods/repositories/food.repository.ts +++ b/src/modules/foods/repositories/food.repository.ts @@ -62,5 +62,7 @@ export class FoodRepository extends EntityRepository { totalPages, }, }; + + } } diff --git a/src/modules/users/controllers/users.controller.ts b/src/modules/users/controllers/users.controller.ts index c8bea6d..2fd191e 100644 --- a/src/modules/users/controllers/users.controller.ts +++ b/src/modules/users/controllers/users.controller.ts @@ -9,6 +9,7 @@ import { UserId } from 'src/common/decorators/user-id.decorator'; import { RestId } from 'src/common/decorators'; import { FindUsersDto } from '../dto/find-user.dto'; import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard'; +import { API_HEADER_SLUG } from 'src/common/constants/index'; @ApiTags('User') @Controller() @@ -18,14 +19,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get the current authenticated user profile' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @Get('public/user/me') async getUser(@UserId() userId: string) { const user = await this.userService.findById(userId); @@ -34,14 +28,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Update the authenticated user profile' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiBody({ type: UpdateUserDto }) @Patch('public/user/update') async updateUser(@UserId() userId: string, @RestId() restId: string, @Body() dto: UpdateUserDto) { @@ -52,14 +39,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get all addresses for the authenticated user' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiOkResponse({ description: 'List of user addresses' }) @Get('public/user/addresses') async getUserAddresses(@UserId() userId: string) { @@ -70,14 +50,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get User Wallet' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @Get('public/user/wallet') async getUserWallet(@UserId() userId: string, @RestId() restId: string) { const wallet = await this.userService.getUserWallet(userId, restId); @@ -87,14 +60,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Create a new address for the authenticated user' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiBody({ type: CreateUserAddressDto }) @ApiOkResponse({ description: 'Created user address' }) @Post('public/user/addresses') @@ -109,14 +75,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get a single address by ID for the authenticated user' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiOkResponse({ description: 'User address details' }) @Get('public/user/addresses/:addressId') async getUserAddress(@UserId() userId: string, @Param('addressId') addressId: string) { @@ -127,14 +86,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Update an address for the authenticated user' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiBody({ type: UpdateUserAddressDto }) @ApiOkResponse({ description: 'Updated user address' }) @Patch('public/user/addresses/:addressId') @@ -150,14 +102,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Delete an address for the authenticated user' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiOkResponse({ description: 'Address deleted successfully' }) @Delete('public/user/addresses/:addressId') async deleteUserAddress(@UserId() userId: string, @Param('addressId') addressId: string) { @@ -168,14 +113,7 @@ export class UsersController { @UseGuards(AuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Set an address as default for the authenticated user' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) + @ApiHeader(API_HEADER_SLUG) @ApiOkResponse({ description: 'Address set as default' }) @Patch('public/user/addresses/:addressId/default') async setDefaultAddress(@UserId() userId: string, @Param('addressId') addressId: string) { @@ -183,6 +121,19 @@ export class UsersController { return address; } + @UseGuards(AuthGuard) + @ApiBearerAuth() + @ApiOperation({ summary: 'Convert user score (points) to wallet balance' }) + @ApiHeader(API_HEADER_SLUG) + @Post('public/user/convert-score-to-wallet') + async convertScoreToWallet(@UserId() userId: string, @RestId() restId: string) { + await this.userService.convertScoreToWallet(userId, restId); + return { + message: 'Score converted to wallet successfully', + }; + } + + @UseGuards(AdminAuthGuard) @ApiBearerAuth() @ApiOperation({ summary: 'Get paginated list of restuarant users with filters' }) @@ -196,22 +147,5 @@ export class UsersController { return this.userService.findAll(restId, query); } - @UseGuards(AuthGuard) - @ApiBearerAuth() - @ApiOperation({ summary: 'Convert user score (points) to wallet balance' }) - @ApiHeader({ - name: 'X-Slug', - required: true, - schema: { - type: 'string', - default: 'zhivan', - }, - }) - @Post('public/user/convert-score-to-wallet') - async convertScoreToWallet(@UserId() userId: string, @RestId() restId: string) { - await this.userService.convertScoreToWallet(userId, restId); - return { - message: 'Score converted to wallet successfully', - }; - } + }