diff --git a/src/modules/users/controllers/users.controller.ts b/src/modules/users/controllers/users.controller.ts index 1338efe..d21d574 100644 --- a/src/modules/users/controllers/users.controller.ts +++ b/src/modules/users/controllers/users.controller.ts @@ -1,5 +1,5 @@ import { Controller, Get, UseGuards, Patch, Body, ValidationPipe, Post, Query, Delete, Param, UploadedFile, UseInterceptors } from '@nestjs/common'; -import { ApiTags, ApiBearerAuth, ApiOperation, ApiBody, ApiOkResponse, ApiHeader, ApiConsumes } from '@nestjs/swagger'; +import { ApiTags, ApiBearerAuth, ApiOperation, ApiBody, ApiOkResponse, ApiHeader, ApiConsumes, ApiParam } from '@nestjs/swagger'; import { FileInterceptor, type File } from '@nest-lab/fastify-multer'; import { AuthGuard } from 'src/modules/auth/guards/auth.guard'; import { UserService } from '../providers/user.service'; @@ -184,6 +184,22 @@ export class UsersController { return this.userService.findPaginated(restId, query); } + @UseGuards(AdminAuthGuard) + @ApiBearerAuth() + @Permissions(Permission.MANAGE_USERS) + @ApiHeader(API_HEADER_SLUG) + @ApiOperation({ summary: 'Create a new address for a user (admin)' }) + @ApiParam({ name: 'userId', description: 'User ID' }) + @ApiBody({ type: CreateUserAddressDto }) + @ApiOkResponse({ description: 'Created user address' }) + @Post('admin/users/:userId/addresses') + async adminCreateUserAddress( + @Param('userId') userId: string, + @Body(new ValidationPipe({ transform: true, whitelist: true })) dto: CreateUserAddressDto, + ) { + return this.userService.createUserAddress(userId, dto); + } + @UseGuards(AdminAuthGuard) @ApiBearerAuth() @Permissions(Permission.MANAGE_USERS) diff --git a/src/modules/users/providers/user.service.ts b/src/modules/users/providers/user.service.ts index ee878c7..5f6ff64 100644 --- a/src/modules/users/providers/user.service.ts +++ b/src/modules/users/providers/user.service.ts @@ -75,10 +75,14 @@ export class UserService { async findByPhone(phone: string): Promise { const normalizedPhone = normalizePhone(phone); - const user = await this.userRepository.findOne({ phone: normalizedPhone }); + const user = await this.userRepository.findOne( + { phone: normalizedPhone }, + { populate: ['addresses'] }, + ); if (!user) { throw new NotFoundException(`User with phone ${phone} not found.`); } + await user.addresses.loadItems(); return user; }