Add NestJS fabric trade API with Prisma and Docker
Provide backend for admin and user apps with Postgres, migrations, seed data, and docker-compose for LiteSpeed reverse-proxy deployment. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
import {
|
||||
IsBoolean,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
MinLength,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class CreateProductDto {
|
||||
@IsString()
|
||||
companyId!: string;
|
||||
|
||||
@IsString()
|
||||
catalogId!: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
name!: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
code!: string;
|
||||
|
||||
/** Alias accepted from admin UI */
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
productCode?: string;
|
||||
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
color!: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
minMeterage?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0.001)
|
||||
orderMultiplier?: number;
|
||||
|
||||
/** Alias from admin UI */
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0.001)
|
||||
orderStep?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
basePrice?: number | null;
|
||||
|
||||
/** Alias from admin UI */
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
price?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
stock?: number | null;
|
||||
|
||||
/** Alias from admin UI */
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
internalStock?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export class UpdateProductDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
catalogId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
name?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
code?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
productCode?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
color?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string | null;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
minMeterage?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0.001)
|
||||
orderMultiplier?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0.001)
|
||||
orderStep?: number;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
basePrice?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
price?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
stock?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsNumber()
|
||||
@Min(0)
|
||||
internalStock?: number | null;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export class SetProductActiveDto {
|
||||
@IsBoolean()
|
||||
isActive!: boolean;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { ProductsService } from './products.service';
|
||||
import { AuthType } from '../common/decorators/auth.decorators';
|
||||
import {
|
||||
CreateProductDto,
|
||||
SetProductActiveDto,
|
||||
UpdateProductDto,
|
||||
} from './dto/product.dto';
|
||||
|
||||
@Controller('products')
|
||||
export class ProductsController {
|
||||
constructor(private readonly productsService: ProductsService) {}
|
||||
|
||||
@AuthType('any')
|
||||
@Get()
|
||||
findAll(
|
||||
@Query('catalogId') catalogId?: string,
|
||||
@Query('companyId') companyId?: string,
|
||||
@Query('color') color?: string,
|
||||
@Query('activeOnly') activeOnly?: string,
|
||||
) {
|
||||
return this.productsService.findAll({
|
||||
catalogId,
|
||||
companyId,
|
||||
color,
|
||||
activeOnly: activeOnly === 'true' || activeOnly === '1',
|
||||
});
|
||||
}
|
||||
|
||||
@AuthType('any')
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.productsService.findOne(id);
|
||||
}
|
||||
|
||||
@AuthType('admin')
|
||||
@Post()
|
||||
create(@Body() dto: CreateProductDto) {
|
||||
return this.productsService.create(dto);
|
||||
}
|
||||
|
||||
@AuthType('admin')
|
||||
@Patch(':id')
|
||||
update(@Param('id') id: string, @Body() dto: UpdateProductDto) {
|
||||
return this.productsService.update(id, dto);
|
||||
}
|
||||
|
||||
@AuthType('admin')
|
||||
@Patch(':id/active')
|
||||
setActive(@Param('id') id: string, @Body() dto: SetProductActiveDto) {
|
||||
return this.productsService.setActive(id, dto.isActive);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ProductsController } from './products.controller';
|
||||
import { ProductsService } from './products.service';
|
||||
|
||||
@Module({
|
||||
controllers: [ProductsController],
|
||||
providers: [ProductsService],
|
||||
exports: [ProductsService],
|
||||
})
|
||||
export class ProductsModule {}
|
||||
@@ -0,0 +1,215 @@
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateProductDto, UpdateProductDto } from './dto/product.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ProductsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async findAll(filters: {
|
||||
catalogId?: string;
|
||||
companyId?: string;
|
||||
color?: string;
|
||||
activeOnly?: boolean;
|
||||
}) {
|
||||
const products = await this.prisma.product.findMany({
|
||||
where: {
|
||||
...(filters.catalogId ? { catalogId: filters.catalogId } : {}),
|
||||
...(filters.companyId ? { companyId: filters.companyId } : {}),
|
||||
...(filters.color
|
||||
? { color: { contains: filters.color, mode: 'insensitive' } }
|
||||
: {}),
|
||||
...(filters.activeOnly ? { isActive: true } : {}),
|
||||
},
|
||||
orderBy: [{ name: 'asc' }, { color: 'asc' }],
|
||||
include: {
|
||||
company: { select: { name: true } },
|
||||
catalog: { select: { code: true, name: true } },
|
||||
},
|
||||
});
|
||||
return products.map((p) => this.map(p));
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const product = await this.prisma.product.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
company: { select: { name: true } },
|
||||
catalog: { select: { code: true, name: true } },
|
||||
},
|
||||
});
|
||||
if (!product) throw new NotFoundException('محصول یافت نشد');
|
||||
return this.map(product);
|
||||
}
|
||||
|
||||
async create(dto: CreateProductDto) {
|
||||
const catalog = await this.prisma.catalog.findUnique({
|
||||
where: { id: dto.catalogId },
|
||||
});
|
||||
if (!catalog) throw new NotFoundException('کاتالوگ یافت نشد');
|
||||
if (catalog.companyId !== dto.companyId) {
|
||||
throw new ConflictException('کاتالوگ متعلق به این شرکت نیست');
|
||||
}
|
||||
|
||||
const code = (dto.code || dto.productCode || '').trim();
|
||||
if (!code) throw new ConflictException('کد محصول الزامی است');
|
||||
|
||||
try {
|
||||
const product = await this.prisma.product.create({
|
||||
data: {
|
||||
companyId: dto.companyId,
|
||||
catalogId: dto.catalogId,
|
||||
name: dto.name.trim(),
|
||||
code,
|
||||
color: dto.color.trim(),
|
||||
description: dto.description?.trim() || null,
|
||||
minMeterage: dto.minMeterage ?? 30,
|
||||
orderMultiplier: dto.orderMultiplier ?? dto.orderStep ?? 10,
|
||||
basePrice: this.pickNumber(dto.basePrice, dto.price),
|
||||
stock: this.pickNumber(dto.stock, dto.internalStock),
|
||||
isActive: dto.isActive ?? true,
|
||||
},
|
||||
include: {
|
||||
company: { select: { name: true } },
|
||||
catalog: { select: { code: true, name: true } },
|
||||
},
|
||||
});
|
||||
return this.map(product);
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2002') {
|
||||
throw new ConflictException('کد محصول در این شرکت تکراری است');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: string, dto: UpdateProductDto) {
|
||||
await this.ensure(id);
|
||||
const code =
|
||||
dto.code !== undefined
|
||||
? dto.code.trim()
|
||||
: dto.productCode !== undefined
|
||||
? dto.productCode.trim()
|
||||
: undefined;
|
||||
|
||||
try {
|
||||
const product = await this.prisma.product.update({
|
||||
where: { id },
|
||||
data: {
|
||||
catalogId: dto.catalogId,
|
||||
name: dto.name?.trim(),
|
||||
code,
|
||||
color: dto.color?.trim(),
|
||||
description:
|
||||
dto.description === undefined
|
||||
? undefined
|
||||
: dto.description?.trim() || null,
|
||||
minMeterage: dto.minMeterage,
|
||||
orderMultiplier:
|
||||
dto.orderMultiplier !== undefined
|
||||
? dto.orderMultiplier
|
||||
: dto.orderStep,
|
||||
basePrice:
|
||||
dto.basePrice !== undefined
|
||||
? dto.basePrice
|
||||
: dto.price !== undefined
|
||||
? dto.price
|
||||
: undefined,
|
||||
stock:
|
||||
dto.stock !== undefined
|
||||
? dto.stock
|
||||
: dto.internalStock !== undefined
|
||||
? dto.internalStock
|
||||
: undefined,
|
||||
isActive: dto.isActive,
|
||||
},
|
||||
include: {
|
||||
company: { select: { name: true } },
|
||||
catalog: { select: { code: true, name: true } },
|
||||
},
|
||||
});
|
||||
return this.map(product);
|
||||
} catch (e) {
|
||||
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2002') {
|
||||
throw new ConflictException('کد محصول در این شرکت تکراری است');
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async setActive(id: string, isActive: boolean) {
|
||||
await this.ensure(id);
|
||||
const product = await this.prisma.product.update({
|
||||
where: { id },
|
||||
data: { isActive },
|
||||
include: {
|
||||
company: { select: { name: true } },
|
||||
catalog: { select: { code: true, name: true } },
|
||||
},
|
||||
});
|
||||
return this.map(product);
|
||||
}
|
||||
|
||||
private pickNumber(
|
||||
primary?: number | null,
|
||||
alias?: number | null,
|
||||
): number | null {
|
||||
if (primary !== undefined) return primary;
|
||||
if (alias !== undefined) return alias;
|
||||
return null;
|
||||
}
|
||||
|
||||
private async ensure(id: string) {
|
||||
const product = await this.prisma.product.findUnique({ where: { id } });
|
||||
if (!product) throw new NotFoundException('محصول یافت نشد');
|
||||
return product;
|
||||
}
|
||||
|
||||
private map(product: {
|
||||
id: string;
|
||||
companyId: string;
|
||||
catalogId: string;
|
||||
name: string;
|
||||
code: string;
|
||||
color: string;
|
||||
description: string | null;
|
||||
minMeterage: number;
|
||||
orderMultiplier: number;
|
||||
basePrice: number | null;
|
||||
stock: number | null;
|
||||
isActive: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
company: { name: string };
|
||||
catalog: { code: string; name: string };
|
||||
}) {
|
||||
return {
|
||||
id: product.id,
|
||||
companyId: product.companyId,
|
||||
companyName: product.company.name,
|
||||
catalogId: product.catalogId,
|
||||
catalogCode: product.catalog.code,
|
||||
catalogName: product.catalog.name,
|
||||
name: product.name,
|
||||
code: product.code,
|
||||
productCode: product.code,
|
||||
color: product.color,
|
||||
description: product.description ?? undefined,
|
||||
minMeterage: product.minMeterage,
|
||||
orderMultiplier: product.orderMultiplier,
|
||||
orderStep: product.orderMultiplier,
|
||||
basePrice: product.basePrice,
|
||||
price: product.basePrice,
|
||||
stock: product.stock,
|
||||
internalStock: product.stock ?? 0,
|
||||
isActive: product.isActive,
|
||||
createdAt: product.createdAt.toISOString(),
|
||||
updatedAt: product.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user