feat: complete specialist review and initial agency alignment for Canina Iran
This commit is contained in:
1 parent
5e8a919dd0
commit
0a04bac013
14 files changed
+269
-113
No files matched your search
@@ -0,0 +1,4 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
|
||||
export const ROLES_KEY = 'roles';
|
||||
export const Roles = (...roles: string[]) => SetMetadata(ROLES_KEY, roles);
|
||||
@@ -0,0 +1,31 @@
|
||||
import { Injectable, CanActivate, ExecutionContext, ForbiddenException } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { ROLES_KEY } from './roles.decorator';
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(private reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const requiredRoles = this.reflector.getAllAndOverride<string[]>(ROLES_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
|
||||
if (!requiredRoles || requiredRoles.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const { user } = context.switchToHttp().getRequest();
|
||||
if (!user || !user.role) {
|
||||
throw new ForbiddenException('شما دسترسی لازم برای این بخش را ندارید');
|
||||
}
|
||||
|
||||
const hasRole = requiredRoles.includes(user.role);
|
||||
if (!hasRole) {
|
||||
throw new ForbiddenException('سطح دسترسی شما کافی نیست');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { GetProductsDto } from './dto/get-products.dto';
|
||||
export class ProductsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll(filters: GetProductsDto) {
|
||||
async findAll(filters: GetProductsDto, userRole?: string) {
|
||||
const { category, petType, search, symptom, page = 1, limit = 10, sortBy = 'createdAt', sortOrder = 'desc' } = filters;
|
||||
|
||||
const whereClause: any = {};
|
||||
@@ -32,6 +32,8 @@ export class ProductsService {
|
||||
// If symptom filter is already applied, extend via AND to also search names/desc
|
||||
// If not, use OR across names, description AND symptoms
|
||||
const searchConditions = [
|
||||
{ artNo: { contains: search, mode: 'insensitive' } },
|
||||
{ barcode: { contains: search, mode: 'insensitive' } },
|
||||
{ nameFa: { contains: search, mode: 'insensitive' } },
|
||||
{ nameEn: { contains: search, mode: 'insensitive' } },
|
||||
{ description: { contains: search, mode: 'insensitive' } },
|
||||
@@ -59,7 +61,7 @@ export class ProductsService {
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
const [rawProducts, total] = await Promise.all([
|
||||
this.prisma.product.findMany({
|
||||
where: whereClause,
|
||||
skip,
|
||||
@@ -73,6 +75,15 @@ export class ProductsService {
|
||||
this.prisma.product.count({ where: whereClause }),
|
||||
]);
|
||||
|
||||
const isWholesaleOrAdmin = userRole === 'User_Wholesale' || userRole === 'ADMIN';
|
||||
const data = rawProducts.map(p => {
|
||||
if (!isWholesaleOrAdmin) {
|
||||
const { wholesalePrice, ...rest } = p;
|
||||
return rest;
|
||||
}
|
||||
return p;
|
||||
});
|
||||
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
@@ -84,15 +95,23 @@ export class ProductsService {
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(idOrSlug: string) {
|
||||
async findOne(idOrSlug: string, userRole?: string) {
|
||||
const isUuid = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(idOrSlug);
|
||||
return this.prisma.product.findFirst({
|
||||
const product = await this.prisma.product.findFirst({
|
||||
where: isUuid ? { id: idOrSlug } : { slug: idOrSlug },
|
||||
include: {
|
||||
ingredientList: true,
|
||||
symptoms: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!product) return null;
|
||||
const isWholesaleOrAdmin = userRole === 'User_Wholesale' || userRole === 'ADMIN';
|
||||
if (!isWholesaleOrAdmin) {
|
||||
const { wholesalePrice, ...rest } = product;
|
||||
return rest;
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
async getActiveFilters() {
|
||||
|
||||
Reference in new issue
Block a user