feat: connect UI texts, address CRUD, OTP autofocus and profile to backend
This commit is contained in:
1 parent
4e2c8d5cfe
commit
467532713c
82 files changed
+14436
-274
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsString, IsNotEmpty, IsOptional, IsBoolean } from 'class-validator';
|
||||
|
||||
export class AddressDto {
|
||||
@ApiProperty({ description: 'عنوان آدرس (مثلا خانه، کار)' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
title: string;
|
||||
|
||||
@ApiProperty({ description: 'نام تحویلگیرنده' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
receptorName: string;
|
||||
|
||||
@ApiProperty({ description: 'شماره تلفن گیرنده' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
phone: string;
|
||||
|
||||
@ApiProperty({ description: 'استان' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
province: string;
|
||||
|
||||
@ApiProperty({ description: 'شهر' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
city: string;
|
||||
|
||||
@ApiProperty({ description: 'جزئیات آدرس پستی' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
detail: string;
|
||||
|
||||
@ApiProperty({ description: 'کد پستی' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
zipCode: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'انتخاب به عنوان آدرس پیشفرض' })
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isDefault?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsOptional, IsString, IsEmail } from 'class-validator';
|
||||
|
||||
export class UpdateProfileDto {
|
||||
@ApiPropertyOptional({ description: 'نام کاربر' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
firstName?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'نام خانوادگی کاربر' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
lastName?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'ایمیل کاربر' })
|
||||
@IsOptional()
|
||||
@IsEmail({}, { message: 'ایمیل نامعتبر است' })
|
||||
email?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'شماره موبایل کاربر' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobile?: string;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { Controller, Get, Patch, Post, Delete, Body, Param, UseGuards, Req } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiResponse } from '@nestjs/swagger';
|
||||
import { UpdateProfileDto } from './dto/update-profile.dto';
|
||||
import { AddressDto } from './dto/address.dto';
|
||||
|
||||
@ApiTags('Users')
|
||||
@ApiBearerAuth()
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('profile')
|
||||
@ApiOperation({ summary: 'دریافت پروفایل کاربر فعلی' })
|
||||
getProfile(@Req() req: any) {
|
||||
return this.usersService.findById(req.user.id);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch('profile')
|
||||
@ApiOperation({ summary: 'ویرایش پروفایل کاربر فعلی' })
|
||||
@ApiResponse({ status: 200, description: 'پروفایل با موفقیت ویرایش شد' })
|
||||
updateProfile(@Req() req: any, @Body() updateProfileDto: UpdateProfileDto) {
|
||||
return this.usersService.update(req.user.id, updateProfileDto);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('addresses')
|
||||
@ApiOperation({ summary: 'ایجاد آدرس جدید برای کاربر' })
|
||||
@ApiResponse({ status: 201, description: 'آدرس جدید با موفقیت ایجاد شد' })
|
||||
addAddress(@Req() req: any, @Body() addressDto: AddressDto) {
|
||||
return this.usersService.addAddress(req.user.id, addressDto);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch('addresses/:addressId')
|
||||
@ApiOperation({ summary: 'ویرایش آدرس کاربر' })
|
||||
@ApiResponse({ status: 200, description: 'آدرس با موفقیت ویرایش شد' })
|
||||
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: 'حذف آدرس کاربر' })
|
||||
@ApiResponse({ status: 200, description: 'آدرس با موفقیت حذف شد' })
|
||||
deleteAddress(@Req() req: any, @Param('addressId') addressId: string) {
|
||||
return this.usersService.deleteAddress(req.user.id, addressId);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Patch('addresses/:addressId/default')
|
||||
@ApiOperation({ summary: 'انتخاب آدرس به عنوان پیشفرض' })
|
||||
@ApiResponse({ status: 200, description: 'آدرس به عنوان پیشفرض ثبت شد' })
|
||||
setDefaultAddress(@Req() req: any, @Param('addressId') addressId: string) {
|
||||
return this.usersService.setDefaultAddress(req.user.id, addressId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersService } from './users.service';
|
||||
import { UsersController } from './users.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findById(id: string) {
|
||||
return this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
pets: true,
|
||||
orders: {
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true }
|
||||
}
|
||||
}
|
||||
},
|
||||
addresses: true,
|
||||
walletTransactions: true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: string, data: any) {
|
||||
return this.prisma.user.update({
|
||||
where: { id },
|
||||
data,
|
||||
include: {
|
||||
pets: true,
|
||||
orders: {
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true }
|
||||
}
|
||||
}
|
||||
},
|
||||
addresses: true,
|
||||
walletTransactions: true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async addAddress(userId: string, data: any) {
|
||||
if (data.isDefault) {
|
||||
await this.prisma.userAddress.updateMany({
|
||||
where: { userId },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
}
|
||||
return this.prisma.userAddress.create({
|
||||
data: {
|
||||
title: data.title,
|
||||
receptorName: data.receptorName,
|
||||
phone: data.phone,
|
||||
province: data.province,
|
||||
city: data.city,
|
||||
detail: data.detail,
|
||||
zipCode: data.zipCode,
|
||||
isDefault: data.isDefault || false,
|
||||
userId: userId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async updateAddress(userId: string, addressId: string, data: any) {
|
||||
if (data.isDefault) {
|
||||
await this.prisma.userAddress.updateMany({
|
||||
where: { userId, NOT: { id: addressId } },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
}
|
||||
return this.prisma.userAddress.update({
|
||||
where: { id: addressId, userId },
|
||||
data: {
|
||||
title: data.title,
|
||||
receptorName: data.receptorName,
|
||||
phone: data.phone,
|
||||
province: data.province,
|
||||
city: data.city,
|
||||
detail: data.detail,
|
||||
zipCode: data.zipCode,
|
||||
isDefault: data.isDefault || false,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async deleteAddress(userId: string, addressId: string) {
|
||||
return this.prisma.userAddress.delete({
|
||||
where: { id: addressId, userId },
|
||||
});
|
||||
}
|
||||
|
||||
async setDefaultAddress(userId: string, addressId: string) {
|
||||
await this.prisma.userAddress.updateMany({
|
||||
where: { userId },
|
||||
data: { isDefault: false },
|
||||
});
|
||||
return this.prisma.userAddress.update({
|
||||
where: { id: addressId, userId },
|
||||
data: { isDefault: true },
|
||||
});
|
||||
}
|
||||
|
||||
async findByPhone(phoneNumber: string) {
|
||||
return this.prisma.user.findUnique({
|
||||
where: { mobile: phoneNumber },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user