Files
canina/backend/src/admin/categories.service.ts
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation.
- Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings.
- Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors.
- Fixed unmount lifecycle guards, promise error handling, and component edge cases.
2026-10-13 16:01:00 +00:00

147 lines
4.3 KiB
TypeScript

import { Injectable, NotFoundException, OnModuleInit } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { PrismaService } from "../prisma/prisma.service";
export class CategoryQuery {
page?: number | string;
limit?: number | string;
search?: string;
}
@Injectable()
export class CategoriesService implements OnModuleInit {
constructor(private prisma: PrismaService) {}
async onModuleInit() {
await this.seedDefaultCategories();
}
private async seedDefaultCategories() {
try {
const defaultCategories = [
{
name: "مفاصل و استخوان",
slug: "joints",
description: "مکمل‌های ارتوپدی و مفاصل حاوی عصاره صدف سبز و GAG",
},
{
name: "سیستم ایمنی و گوارش",
slug: "immune",
description: "تقویت سیستم دفاعی بدن و پروبیوتیک‌های تخصصی",
},
{
name: "ویتامین‌ها و انرژی‌بخش‌ها",
slug: "energy",
description: "مولتی‌ویتامین‌ها و تونیک‌های انرژی‌بخش",
},
{
name: "مراقبت‌های ویژه (پوست، دندان و چشم)",
slug: "special-care",
description: "محلول‌های نقره، قطره‌های شستشو و ژل دندان",
},
{
name: "تقویت عمومی",
slug: "general",
description: "مکمل‌های پایه‌ای و روزانه سگ و گربه",
},
{
name: "تغذیه تخصصی",
slug: "nutrition",
description: "شیر خشک، پودر خون گاو و مکمل‌های تخصصی پرورش",
},
{
name: "مکمل‌های غذایی و درمانی",
slug: "supplements",
description: "طیف کامل مکمل‌های گرید دارویی Canina",
},
];
for (const cat of defaultCategories) {
const savedCat = await this.prisma.category.upsert({
where: { slug: cat.slug },
update: { name: cat.name, description: cat.description },
create: {
name: cat.name,
slug: cat.slug,
description: cat.description,
},
});
// Automatically connect products with matching categorySlug
await this.prisma.product.updateMany({
where: {
categorySlug: cat.slug,
},
data: {
categoryId: savedCat.id,
},
});
}
} catch (err) {
console.warn(
"[CategoriesService] Failed to seed default categories:",
err,
);
}
}
async getCategories(query: CategoryQuery = {}) {
const page = Number(query.page) || 1;
const limit = Number(query.limit) || 10;
const skip = (page - 1) * limit;
const where: Prisma.CategoryWhereInput = query.search
? { name: { contains: query.search, mode: "insensitive" } }
: {};
const [data, total] = await Promise.all([
this.prisma.category.findMany({
where,
skip,
take: limit,
orderBy: { createdAt: "desc" },
}),
this.prisma.category.count({ where }),
]);
return {
data,
meta: {
total,
page,
limit,
lastPage: Math.ceil(total / limit),
},
};
}
async getAllCategoriesRaw() {
return this.prisma.category.findMany({ orderBy: { name: "asc" } });
}
async createCategory(data: Prisma.CategoryCreateInput) {
const nameStr = data.name || "";
const slugStr = data.slug || nameStr.replace(/\s+/g, "-").toLowerCase();
return this.prisma.category.create({
data: {
...data,
slug: slugStr,
},
});
}
async updateCategory(id: string, data: Prisma.CategoryUpdateInput) {
const category = await this.prisma.category.findUnique({ where: { id } });
if (!category) throw new NotFoundException("Category not found");
return this.prisma.category.update({ where: { id }, data });
}
async deleteCategory(id: string) {
const category = await this.prisma.category.findUnique({ where: { id } });
if (!category) throw new NotFoundException("Category not found");
return this.prisma.category.delete({ where: { id } });
}
}