Skip to content

Commit

Permalink
update(product filter): filter with parent category return all produc…
Browse files Browse the repository at this point in the history
…ts under this category
  • Loading branch information
anamulhaque22 committed Dec 17, 2024
1 parent 0417030 commit ee54166
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
5 changes: 3 additions & 2 deletions admin-client/components/AddProduct/CategoryInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ const groupCategoriesByParent = (categories) => {

// Step 2: Populate the result with the top-level categories
categories.forEach((category) => {
if (category.isVisibleInMenu) {
if (!category?.parentCategory) {
result.push(categoriesMap.get(category.id));
}

// If it has a parentCategory, push it into the parent's subCategory
if (category.parentCategory) {
if (category?.parentCategory?.id) {
const parent = categoriesMap.get(category.parentCategory.id);
if (parent) {
parent.subCategory.push(categoriesMap.get(category.id));
Expand All @@ -37,6 +37,7 @@ export default function CategoryInput({ setValue, getValues, name }) {
useEffect(() => {
const fetchData = async () => {
const { status, data } = await fetchCategories();
console.log({ status, data });
if (status === HTTP_CODES.OK) {
setCategories(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpStatus, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Category } from 'src/categories/domain/category';
import { NullableType } from 'src/utils/types/nullable.type';
import { Equal, Repository } from 'typeorm';
import { DataSource, Equal, Repository } from 'typeorm';
import { CategoryRepository } from '../category.repositoty';
import { CategoryEntity } from '../entities/category.entity';
import { CategoryMapper } from '../mappers/cateogry.mapper';
Expand All @@ -11,6 +11,7 @@ export class CategoryRepositoryImpl implements CategoryRepository {
constructor(
@InjectRepository(CategoryEntity)
private categoryRepository: Repository<CategoryEntity>,
private readonly dataSource: DataSource,
) {}

async create(data) {
Expand All @@ -20,6 +21,7 @@ export class CategoryRepositoryImpl implements CategoryRepository {
this.categoryRepository.create(toPersistence),
);
}

async getCategoryWithSubCategories(id: Category['id']): Promise<Category[]> {
if (!id) {
throw new NotFoundException({
Expand All @@ -29,9 +31,27 @@ export class CategoryRepositoryImpl implements CategoryRepository {
},
});
}
const category = await this.categoryRepository.find({
where: { parentCategory: Equal(Number(id)) },
});
// const category = await this.categoryRepository.find({
// where: { parentCategory: Equal(Number(id)) },
// });

const category = await this.dataSource.query(
`
WITH RECURSIVE all_categories AS (
SELECT id, name, "parentCategoryId"
FROM categories
WHERE id = $1
UNION ALL
SELECT t.id, t.name, t."parentCategoryId"
FROM categories t
INNER JOIN categories d ON t."parentCategoryId" = d.id
)
SELECT * FROM all_categories;`,
[id],
);

return category.map((category) => CategoryMapper.toDomain(category));
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/database/seeds/users/users-seed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class UsersSeedService {

if (!countAdmin) {
const salt = await bcrypt.genSalt();
const password = await bcrypt.hash('Zaqmlp@567', salt);
const password = await bcrypt.hash('secret', salt);

await this.repository.save(
this.repository.create({
firstName: 'Super',
lastName: 'Admin',
email: 'anamul.ah00@gmail.com',
email: 'email@example.com',
password,
role: {
id: RoleEnum.admin,
Expand Down
1 change: 1 addition & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function bootstrap() {
origin: [
configService.getOrThrow('app.frontendCustomerDomain', { infer: true }),
'http://localhost:3000',
'http://localhost:3001',
'https://clothing-shop-client.vercel.app',
],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'],
Expand Down

0 comments on commit ee54166

Please sign in to comment.