feat(torob): add TorobController in backend for /api/torob/products endpoint
This commit is contained in:
1 parent
f2535b4aee
commit
fadea346ff
13 files changed
+11584
-11119
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ProductsService } from './products.service';
|
||||
import { ProductsController } from './products.controller';
|
||||
import { TorobController } from './torob.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [ProductsController],
|
||||
controllers: [ProductsController, TorobController],
|
||||
providers: [ProductsService],
|
||||
})
|
||||
export class ProductsModule {}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation } from '@nestjs/swagger';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@ApiTags('Torob Integration - وبسرویس اتصال به ترب')
|
||||
@Controller('torob')
|
||||
export class TorobController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
@Get('products')
|
||||
@ApiOperation({ summary: 'دریافت محصولات جهت وبسرویس خودکار ترب (Torob API)' })
|
||||
async getTorobProducts(
|
||||
@Query('page') pageQuery?: string,
|
||||
@Query('page_size') pageSizeQuery?: string,
|
||||
) {
|
||||
const page = Math.max(1, parseInt(pageQuery || '1', 10));
|
||||
const pageSize = Math.min(100, Math.max(10, parseInt(pageSizeQuery || '100', 10)));
|
||||
const baseUrl = (process.env.FRONTEND_URL || 'https://canina.ir').replace(/\/$/, '');
|
||||
|
||||
const whereCondition = {
|
||||
noIndex: false,
|
||||
stockStatus: { not: 'DISCONTINUED' as const },
|
||||
};
|
||||
|
||||
const [totalCount, products] = await Promise.all([
|
||||
this.prisma.product.count({ where: whereCondition }),
|
||||
this.prisma.product.findMany({
|
||||
where: whereCondition,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
}),
|
||||
]);
|
||||
|
||||
const maxPages = Math.ceil(totalCount / pageSize) || 1;
|
||||
|
||||
const results = products.map((product) => {
|
||||
const slug = product.slug || product.artNo || product.id;
|
||||
const pageUrl = `${baseUrl}/shop/${slug}`;
|
||||
const productId = String(product.artNo || product.id || slug);
|
||||
const nameFa = product.nameFa || '';
|
||||
const nameEn = product.nameEn || '';
|
||||
const title = nameEn ? `${nameFa} (${nameEn})` : nameFa;
|
||||
|
||||
let imageLink = '';
|
||||
if (product.ogImage) {
|
||||
imageLink = product.ogImage.startsWith('http')
|
||||
? product.ogImage
|
||||
: `${baseUrl}${product.ogImage.startsWith('/') ? product.ogImage : `/${product.ogImage}`}`;
|
||||
} else if (product.imageUrl) {
|
||||
imageLink = product.imageUrl.startsWith('http')
|
||||
? product.imageUrl
|
||||
: `${baseUrl}/uploads/${product.imageUrl.replace(/^\/?(uploads\/)?/, '')}`;
|
||||
}
|
||||
|
||||
const isInStock = product.stockStatus !== 'DISCONTINUED' && String(product.stockStatus || '').toLowerCase().includes('in');
|
||||
const price = Number(product.priceValue || 0);
|
||||
const oldPrice = Number(product.priceValue || 0);
|
||||
|
||||
return {
|
||||
product_id: productId,
|
||||
page_unique_code: productId,
|
||||
title,
|
||||
subtitle: nameEn || product.scientificTagline || '',
|
||||
page_url: pageUrl,
|
||||
price,
|
||||
old_price: oldPrice,
|
||||
availability: isInStock ? 'instock' : 'outofstock',
|
||||
image_link: imageLink,
|
||||
guarantee: 'ضمانت اصالت ۱۰۰٪ کمپانی Canina آلمان و اصالت کالا',
|
||||
category_name: product.categorySlug || 'مکمل حیوانات خانگی',
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
count: totalCount,
|
||||
max_pages: maxPages,
|
||||
results,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user