219 lines
6.5 KiB
TypeScript
219 lines
6.5 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Patch,
|
|
Post,
|
|
Delete,
|
|
Body,
|
|
Param,
|
|
UseGuards,
|
|
Req,
|
|
HttpStatus,
|
|
BadRequestException,
|
|
} from '@nestjs/common';
|
|
import { UsersService } from './users.service';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import {
|
|
ApiTags,
|
|
ApiBearerAuth,
|
|
ApiOperation,
|
|
ApiResponse,
|
|
ApiOkResponse,
|
|
ApiCreatedResponse,
|
|
ApiBadRequestResponse,
|
|
} from '@nestjs/swagger';
|
|
import { UpdateProfileDto } from './dto/update-profile.dto';
|
|
import { AddressDto } from './dto/address.dto';
|
|
|
|
@ApiTags('Users - مدیریت کاربران و آدرسها')
|
|
@ApiBearerAuth()
|
|
@Controller('users')
|
|
@ApiResponse({
|
|
status: HttpStatus.UNAUTHORIZED,
|
|
description: 'عدم احراز هویت - توکن معتبر نیست یا ارسال نشده است',
|
|
schema: {
|
|
example: {
|
|
success: false,
|
|
message: 'Unauthorized',
|
|
code: 'UNAUTHORIZED',
|
|
details: {},
|
|
},
|
|
},
|
|
})
|
|
export class UsersController {
|
|
constructor(private readonly usersService: UsersService) {}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('profile')
|
|
@ApiOperation({ summary: 'دریافت پروفایل کاربر فعلی' })
|
|
@ApiOkResponse({
|
|
description: 'مشخصات کامل کاربر به همراه آدرسها و اطلاعات حیوانات خانگی',
|
|
schema: {
|
|
example: {
|
|
id: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
|
firstName: 'کاربر',
|
|
lastName: 'جدید',
|
|
email: '09123456789@temp.local',
|
|
mobile: '09123456789',
|
|
role: 'User_PetOwner',
|
|
walletBalance: '1500000.00',
|
|
charityDonationTotal: '25000.00',
|
|
addresses: [],
|
|
pets: [],
|
|
createdAt: '2026-05-26T15:20:00.000Z',
|
|
updatedAt: '2026-05-26T15:20:00.000Z',
|
|
},
|
|
},
|
|
})
|
|
getProfile(@Req() req: any) {
|
|
return this.usersService.findById(req.user.id);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Patch('profile')
|
|
@ApiOperation({ summary: 'ویرایش پروفایل کاربر فعلی' })
|
|
@ApiOkResponse({
|
|
description: 'پروفایل با موفقیت ویرایش شد',
|
|
schema: {
|
|
example: {
|
|
id: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
|
firstName: 'علی',
|
|
lastName: 'احمدی',
|
|
email: 'ali@example.com',
|
|
mobile: '09123456789',
|
|
role: 'User_PetOwner',
|
|
walletBalance: '1500000.00',
|
|
charityDonationTotal: '25000.00',
|
|
},
|
|
},
|
|
})
|
|
@ApiBadRequestResponse({
|
|
description: 'خطا در صحتسنجی فیلدهای ورودی',
|
|
schema: {
|
|
example: {
|
|
success: false,
|
|
message: 'ایمیل نامعتبر است',
|
|
code: 'BAD_REQUEST',
|
|
details: {
|
|
message: ['ایمیل نامعتبر است'],
|
|
},
|
|
},
|
|
},
|
|
})
|
|
updateProfile(@Req() req: any, @Body() updateProfileDto: UpdateProfileDto) {
|
|
return this.usersService.update(req.user.id, updateProfileDto);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post('addresses')
|
|
@ApiOperation({ summary: 'ایجاد آدرس جدید برای کاربر' })
|
|
@ApiCreatedResponse({
|
|
description: 'آدرس جدید با موفقیت ایجاد شد',
|
|
schema: {
|
|
example: {
|
|
id: 'b5a6c7d8-1234-5678-abcd-ef1234567890',
|
|
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
|
title: 'خانه',
|
|
receptorName: 'علی احمدی',
|
|
phone: '09123456789',
|
|
province: 'تهران',
|
|
city: 'تهران',
|
|
detail: 'خیابان آزادی، کوچه مریم، پلاک ۱۰',
|
|
zipCode: '1456789012',
|
|
isDefault: false,
|
|
createdAt: '2026-05-26T18:00:00.000Z',
|
|
},
|
|
},
|
|
})
|
|
addAddress(@Req() req: any, @Body() addressDto: AddressDto) {
|
|
return this.usersService.addAddress(req.user.id, addressDto);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Patch('addresses/:addressId')
|
|
@ApiOperation({ summary: 'ویرایش آدرس کاربر' })
|
|
@ApiOkResponse({
|
|
description: 'آدرس با موفقیت ویرایش شد',
|
|
schema: {
|
|
example: {
|
|
id: 'b5a6c7d8-1234-5678-abcd-ef1234567890',
|
|
userId: 'd8c4e428-cd2f-4c55-bfa3-dfa312bb6480',
|
|
title: 'دفتر کار',
|
|
receptorName: 'علی احمدی',
|
|
phone: '09123456789',
|
|
province: 'تهران',
|
|
city: 'تهران',
|
|
detail: 'خیابان ولیعصر، برج سپهر، طبقه ۴',
|
|
zipCode: '1456789012',
|
|
isDefault: false,
|
|
createdAt: '2026-05-26T18:00:00.000Z',
|
|
},
|
|
},
|
|
})
|
|
updateAddress(
|
|
@Req() req: any,
|
|
@Param('addressId') addressId: string,
|
|
@Body() addressDto: AddressDto,
|
|
) {
|
|
return this.usersService.updateAddress(req.user.id, addressId, addressDto);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Delete('addresses/:addressId')
|
|
@ApiOperation({ summary: 'حذف آدرس کاربر' })
|
|
@ApiOkResponse({
|
|
description: 'آدرس با موفقیت حذف شد',
|
|
schema: {
|
|
example: {
|
|
success: true,
|
|
message: 'آدرس با موفقیت حذف شد',
|
|
},
|
|
},
|
|
})
|
|
deleteAddress(@Req() req: any, @Param('addressId') addressId: string) {
|
|
return this.usersService.deleteAddress(req.user.id, addressId);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Patch('addresses/:addressId/default')
|
|
@ApiOperation({ summary: 'انتخاب آدرس به عنوان پیشفرض' })
|
|
@ApiOkResponse({
|
|
description: 'آدرس به عنوان پیشفرض ثبت شد',
|
|
schema: {
|
|
example: {
|
|
success: true,
|
|
message: 'آدرس پیشفرض با موفقیت تغییر کرد',
|
|
},
|
|
},
|
|
})
|
|
setDefaultAddress(@Req() req: any, @Param('addressId') addressId: string) {
|
|
return this.usersService.setDefaultAddress(req.user.id, addressId);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post('wallet/top-up')
|
|
@ApiOperation({ summary: 'شارژ کیف پول کاربر' })
|
|
@ApiCreatedResponse({
|
|
description: 'کیف پول با موفقیت شارژ شد',
|
|
schema: {
|
|
example: {
|
|
id: 'txn-uuid',
|
|
userId: 'user-uuid',
|
|
amount: '500000.00',
|
|
type: 'deposit',
|
|
status: 'completed',
|
|
description: 'شارژ کیف پول',
|
|
createdAt: '2026-07-11T08:00:00.000Z',
|
|
},
|
|
},
|
|
})
|
|
@ApiBadRequestResponse({ description: 'مبلغ نامعتبر است' })
|
|
async topUpWallet(@Req() req: any, @Body() body: { amount: number }) {
|
|
const amount = Number(body.amount);
|
|
if (!amount || amount <= 0) {
|
|
throw new BadRequestException('مبلغ شارژ باید بزرگتر از صفر باشد');
|
|
}
|
|
return this.usersService.topUpWallet(req.user.id, amount);
|
|
}
|
|
}
|