- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
239 lines
7.0 KiB
TypeScript
239 lines
7.0 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 { RolesGuard } from "../common/guards/roles.guard";
|
|
import { Roles } from "../common/decorators/roles.decorator";
|
|
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: { user: { id: string } }) {
|
|
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: { user: { id: string } },
|
|
@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: { user: { id: string } },
|
|
@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: { user: { id: string } },
|
|
@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: { user: { id: string } },
|
|
@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: { user: { id: string } },
|
|
@Param("addressId") addressId: string,
|
|
) {
|
|
return this.usersService.setDefaultAddress(req.user.id, addressId);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles("Admin", "ADMIN", "SuperAdmin")
|
|
@ApiBearerAuth()
|
|
@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: { user: { id: string } },
|
|
@Body() body: { amount: number; targetUserId?: string },
|
|
) {
|
|
const amount = Number(body.amount);
|
|
if (!amount || amount <= 0) {
|
|
throw new BadRequestException("مبلغ شارژ باید بزرگتر از صفر باشد");
|
|
}
|
|
const targetUserId = body.targetUserId || req.user.id;
|
|
return this.usersService.topUpWallet(targetUserId, amount);
|
|
}
|
|
}
|