feat(backend): implement Phase A database models and controllers for cross-boundary dependencies
Deploy Canina / deploy (push) Failing after 28s

This commit is contained in:
parsa committed 2026-08-06 23:45:51 +03:30
1 parent 74fcaa6a4e
commit 1ae4c99ffc
26 files changed
+1205 -345

No files matched your search

+22 -114
View File
@@ -1,10 +1,13 @@
import {
Controller,
Get,
Patch,
Body,
Query,
Param,
NotFoundException,
HttpStatus,
UseGuards,
} from '@nestjs/common';
import { ProductsService } from './products.service';
import { GetProductsDto } from './dto/get-products.dto';
@@ -15,62 +18,21 @@ import {
ApiOkResponse,
ApiNotFoundResponse,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';
@ApiTags('Products - مدیریت محصولات دارویی')
@Controller('products')
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: 'خطای داخلی سرور',
schema: {
example: {
success: false,
message: 'خطای داخلی سرور',
code: 'SERVER_ERROR',
details: {},
},
},
})
export class ProductsController {
constructor(private readonly productsService: ProductsService) {}
@Get()
@ApiOperation({ summary: 'لیست و فیلتر محصولات' })
@ApiOkResponse({
description: 'لیست محصولات متناسب با فیلترها (دسته، پت و جستجو)',
schema: {
example: {
data: [
{
id: 'a1b2c3d4-1234-5678-abcd-ef1234567890',
artNo: 'canhydrox-gag',
name: 'Canhydrox GAG (کنهیدروکس)',
scientificTagline: 'برای تقویت مفاصل و استخوان‌ها',
description:
'کنهیدروکس محصولی بی‌نظیر برای مفاصل و سیستم حرکتی سگ‌ها...',
shortDescription: 'تقویت مفاصل و غضروف‌ها',
category: 'سیستم حرکتی و مفاصل',
categorySlug: 'joints',
priceValue: '1750000.00',
priceDisplay: '۱,۷۵۰,۰۰۰ تومان',
unit: 'عدد قرص',
packageSize: '120.00',
dosageLogic: 'یک قرص به ازای هر ۱۰ کیلوگرم وزن بدن سگ',
suitableFor: 'سگ',
imageUrl: 'https://example.com/canhydrox.png',
createdAt: '2026-05-26T18:10:00.000Z',
ingredients: [],
symptoms: [],
},
],
meta: {
total: 1,
page: 1,
lastPage: 1,
limit: 10,
},
},
},
})
findAll(@Query() query: GetProductsDto) {
return this.productsService.findAll(query);
}
@@ -79,16 +41,6 @@ export class ProductsController {
@ApiOperation({
summary: 'دریافت فیلترهای فعال محصولات (دسته، علائم، نوع پت)',
})
@ApiOkResponse({
description: 'لیست فیلترهای پویا استخراج شده از دیتابیس',
schema: {
example: {
categories: [{ id: '1', name: 'مفاصل و استخوان', slug: 'joints' }],
symptoms: ['لنگش', 'ریزش مو'],
petTypes: ['سگ', 'گربه', 'هر دو'],
},
},
})
getActiveFilters() {
return this.productsService.getActiveFilters();
}
@@ -97,72 +49,28 @@ export class ProductsController {
@ApiOperation({
summary: 'دریافت فیلترها و راهکارهای ناوبری (دسته با علائم مربوطه)',
})
@ApiOkResponse({
description: 'ساختار درختی فیلترها و تگ‌های درمانی واقعی متصل به محصولات',
schema: {
example: [
{
id: '1',
name: 'مفاصل و استخوان',
slug: 'joints',
symptoms: ['درد مفاصل', 'لنگش'],
},
],
},
})
getNavigationFilters() {
return this.productsService.getNavigationFilters();
}
@Get(':id/dosage-config')
@ApiOperation({ summary: 'دریافت پیکربندی دوز مصرف محصول' })
getDosageConfig(@Param('id') id: string) {
return this.productsService.getDosageConfig(id);
}
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('Admin')
@Patch(':id/dosage-config')
@ApiOperation({
summary: 'ویرایش پیکربندی دوز مصرف محصول (نیازمند دسترسی ادمین)',
})
updateDosageConfig(@Param('id') id: string, @Body() body: any) {
return this.productsService.updateDosageConfig(id, body);
}
@Get(':id')
@ApiOperation({ summary: 'دریافت اطلاعات محصول با شناسه یکتا (ID)' })
@ApiOkResponse({
description: 'اطلاعات کامل محصول شامل مواد تشکیل‌دهنده و علائم مرتبط',
schema: {
example: {
id: 'a1b2c3d4-1234-5678-abcd-ef1234567890',
artNo: 'canhydrox-gag',
name: 'Canhydrox GAG (کنهیدروکس)',
scientificTagline: 'برای تقویت مفاصل و استخوان‌ها',
description: 'کنهیدروکس محصولی بی‌نظیر برای مفاصل...',
shortDescription: 'تقویت مفاصل و غضروف‌ها',
category: 'سیستم حرکتی و مفاصل',
categorySlug: 'joints',
priceValue: '1750000.00',
priceDisplay: '۱,۷۵۰,۰۰۰ تومان',
unit: 'عدد قرص',
packageSize: '120.00',
dosageLogic: 'یک قرص به ازای هر ۱۰ کیلوگرم وزن بدن سگ',
suitableFor: 'سگ',
imageUrl: 'https://example.com/canhydrox.png',
createdAt: '2026-05-26T18:10:00.000Z',
ingredients: [
{
productId: 'a1b2c3d4-1234-5678-abcd-ef1234567890',
ingredient: 'صدف لب‌سبز',
},
],
symptoms: [
{
productId: 'a1b2c3d4-1234-5678-abcd-ef1234567890',
symptom: 'لنگیدن',
},
],
},
},
})
@ApiNotFoundResponse({
description: 'محصول با شناسه ارسال شده پیدا نشد',
schema: {
example: {
success: false,
message:
'Product with ID a1b2c3d4-1234-5678-abcd-ef1234567890 not found',
code: 'NOT_FOUND',
details: {},
},
},
})
async findOne(@Param('id') id: string) {
const product = await this.productsService.findOne(id);
if (!product) {
+30 -10
View File
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { GetProductsDto } from './dto/get-products.dto';
import { Prisma } from '@prisma/client';
@Injectable()
export class ProductsService {
@@ -21,7 +22,7 @@ export class ProductsService {
sortOrder = 'desc',
} = filters;
const andConditions: any[] = [];
const andConditions: Prisma.ProductWhereInput[] = [];
if (requiresRx !== undefined && requiresRx !== null && requiresRx !== '') {
andConditions.push({
@@ -75,7 +76,7 @@ export class ProductsService {
});
}
const whereClause: any =
const whereClause: Prisma.ProductWhereInput =
andConditions.length > 0 ? { AND: andConditions } : {};
const skip = (page - 1) * limit;
@@ -102,13 +103,11 @@ export class ProductsService {
const isAdmin = userRole === 'ADMIN' || userRole === 'SuperAdmin';
const data = rawProducts.map((p) => {
// Remove buyPrice for all non-admins
const { buyPrice, ...withoutBuyPrice } = p;
const { buyPrice: _b, wholesalePrice: _w, ...publicProduct } = p;
if (!isWholesaleOrAdmin) {
const { wholesalePrice, ...publicProduct } = withoutBuyPrice;
return publicProduct;
}
return isAdmin ? p : withoutBuyPrice;
return isAdmin ? p : { ...publicProduct, wholesalePrice: p.wholesalePrice };
});
return {
@@ -143,12 +142,11 @@ export class ProductsService {
userRole === 'SuperAdmin';
const isAdmin = userRole === 'ADMIN' || userRole === 'SuperAdmin';
const { buyPrice, ...withoutBuyPrice } = product;
const { buyPrice: _b, wholesalePrice: _w, ...publicProduct } = product;
if (!isWholesaleOrAdmin) {
const { wholesalePrice, ...publicProduct } = withoutBuyPrice;
return publicProduct;
}
return isAdmin ? product : withoutBuyPrice;
return isAdmin ? product : { ...publicProduct, wholesalePrice: product.wholesalePrice };
}
async getActiveFilters() {
@@ -220,4 +218,26 @@ export class ProductsService {
};
});
}
async getDosageConfig(id: string) {
const product = await this.prisma.product.findUnique({
where: { id },
select: { dosageConfig: true, dosageLogic: true },
});
if (!product) {
throw new NotFoundException(`Product with ID ${id} not found`);
}
return product.dosageConfig || {};
}
async updateDosageConfig(id: string, dosageConfig: Prisma.InputJsonValue) {
const product = await this.prisma.product.findUnique({ where: { id } });
if (!product) {
throw new NotFoundException(`Product with ID ${id} not found`);
}
return this.prisma.product.update({
where: { id },
data: { dosageConfig },
});
}
}