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,28 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsArray, ValidateNested, IsNumber, IsOptional, IsString } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
class OrderItemDto {
|
||||
@ApiProperty({ description: 'ID محصول' })
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
productId: string;
|
||||
|
||||
@ApiProperty({ description: 'تعداد محصول' })
|
||||
@IsNotEmpty()
|
||||
@IsNumber()
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
export class CreateOrderDto {
|
||||
@ApiPropertyOptional({ description: 'ID حیوان خانگی مرتبط' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
petId?: string;
|
||||
|
||||
@ApiProperty({ description: 'لیست اقلام سفارش', type: [OrderItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => OrderItemDto)
|
||||
items: OrderItemDto[];
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Controller, Get, Post, Body, Param, UseGuards, Req } from '@nestjs/common';
|
||||
import { OrdersService } from './orders.service';
|
||||
import { CreateOrderDto } from './dto/create-order.dto';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
|
||||
@ApiTags('Orders')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Controller('orders')
|
||||
export class OrdersController {
|
||||
constructor(private readonly ordersService: OrdersService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'ثبت سفارش جدید' })
|
||||
create(@Req() req: any, @Body() createOrderDto: CreateOrderDto) {
|
||||
return this.ordersService.create(req.user.id, createOrderDto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'لیست سفارشهای کاربر' })
|
||||
findAll(@Req() req: any) {
|
||||
return this.ordersService.findAllByUser(req.user.id);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'جزئیات یک سفارش' })
|
||||
findOne(@Req() req: any, @Param('id') id: string) {
|
||||
return this.ordersService.findOne(id, req.user.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { OrdersService } from './orders.service';
|
||||
import { OrdersController } from './orders.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [OrdersController],
|
||||
providers: [OrdersService],
|
||||
})
|
||||
export class OrdersModule {}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateOrderDto } from './dto/create-order.dto';
|
||||
|
||||
@Injectable()
|
||||
export class OrdersService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async create(userId: string, createOrderDto: CreateOrderDto) {
|
||||
let totalAmount = 0;
|
||||
const orderItems = [];
|
||||
|
||||
for (const item of createOrderDto.items) {
|
||||
const product = await this.prisma.product.findUnique({ where: { id: item.productId } });
|
||||
if (!product) {
|
||||
throw new NotFoundException(`محصول با شناسه ${item.productId} یافت نشد`);
|
||||
}
|
||||
totalAmount += Number(product.priceValue) * item.quantity;
|
||||
orderItems.push({
|
||||
productId: product.id,
|
||||
quantity: item.quantity,
|
||||
});
|
||||
}
|
||||
|
||||
if (orderItems.length === 0) {
|
||||
throw new BadRequestException('سبد خرید خالی است');
|
||||
}
|
||||
|
||||
return this.prisma.order.create({
|
||||
data: {
|
||||
userId,
|
||||
totalAmount,
|
||||
status: 'processing',
|
||||
orderItems: {
|
||||
create: orderItems,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
orderItems: {
|
||||
include: { product: true }
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByUser(userId: string) {
|
||||
return this.prisma.order.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
orderItems: { include: { product: true } },
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string, userId: string) {
|
||||
const order = await this.prisma.order.findFirst({
|
||||
where: { id, userId },
|
||||
include: {
|
||||
orderItems: { include: { product: true } },
|
||||
},
|
||||
});
|
||||
|
||||
if (!order) {
|
||||
throw new NotFoundException('سفارش یافت نشد');
|
||||
}
|
||||
return order;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user