Bladeren bron

Bugfix: Enhance category deletion logic to support multiple categoryIDs and improve error messages

BedirhanOZCAN 1 maand geleden
bovenliggende
commit
b1d4ee0d09

+ 2 - 2
src/actions/auth/login/index.ts

@@ -44,8 +44,8 @@ const login = async (input: LoginInput): Promise<LoginResult> => {
 
     if (!user.isApproved) {
         return {
-            message: "account-under-review", // TODO buraya da mesaj olarak onay sürecindesiniz gibisinde mesaj verilecek fronttan kullanmak için
-            code: 200, // TODO BUNUN YERİNE HTTPSTATUS CODE ŞEKLİNDE BAKILACAK
+            message: "your-account-is-currently-under-review-we-will-get-back-to-you",
+            code: 200,
         };
     }
 

+ 5 - 13
src/actions/menu/addProduct/index.ts

@@ -1,5 +1,5 @@
 import {
-    Menu 
+    Menu
 } from "../../../models/Menu";
 import {
     Product
@@ -24,10 +24,10 @@ const addProduct = async (body: any, context: { userID: string }): Promise<AddPr
             userID
         } = context;
 
-        const normalizedCategoryIDs = Array.isArray(categoryIDs) 
-            ? categoryIDs 
+        const normalizedCategoryIDs = Array.isArray(categoryIDs)
+            ? categoryIDs
             : categoryIDs ? [categoryIDs] : [];
-    
+
         const newProduct = await Product.create({
             categoryIDs: normalizedCategoryIDs,
             userID: userID,
@@ -45,19 +45,11 @@ const addProduct = async (body: any, context: { userID: string }): Promise<AddPr
         });
 
         if (activeMenu) {
-            const existingCategoryIDs = new Set(
-                activeMenu.products.flatMap((p: any) => p.categoryIDs.map((id: any) => id.toString()))
-            );
-
-            const newCategoryIDs = normalizedCategoryIDs.filter(
-                (id: string) => !existingCategoryIDs.has(id.toString())
-            );
-
             await Menu.findByIdAndUpdate(activeMenu._id, {
                 $push: {
                     products: {
                         productID: newProduct._id,
-                        categoryIDs: newCategoryIDs,
+                        categoryIDs: normalizedCategoryIDs,
                         isViewPrice: true
                     }
                 }

+ 34 - 18
src/actions/menu/deleteCategory/index.ts

@@ -1,51 +1,67 @@
 import {
-    Category 
+    Category
 } from "../../../models/Category";
 import {
-    Product 
+    Product
 } from "../../../models/Product";
 import {
-    DeleteCategoryInput,
-    DeleteCategoryResult 
+    DeleteCategoryResult,
+    DeleteCategoryInput
 } from "./types";
 
 const deleteCategory = async (userID: string, input: DeleteCategoryInput): Promise<DeleteCategoryResult> => {
     try {
         const {
-            categoryID 
-        } = input;  
+            categoryIDs
+        } = input;
 
-        const category = await Category.findOne({
-            _id: categoryID,
-            userID
-        });
-        if (!category) {
+        const normalizedCategoryIDs = Array.isArray(categoryIDs)
+            ? categoryIDs
+            : categoryIDs ? [categoryIDs] : [];
+
+        if (normalizedCategoryIDs.length === 0) {
             return {
-                message: "category-not-found",
-                code: 404 
+                message: "categoryIDs-cannot-be-empty",
+                code: 400
             };
         }
 
         const productInCategory = await Product.findOne({
-            categoryID
+            categoryIDs: {
+                $in: normalizedCategoryIDs
+            }
         });
+
         if (productInCategory) {
             return {
-                message: "category-has-products",
+                message: "one-or-more-categories-have-products",
                 code: 409
             };
         }
-        await Category.findByIdAndDelete(categoryID);
+
+        const result = await Category.deleteMany({
+            _id: {
+                $in: normalizedCategoryIDs
+            },
+            userID: userID
+        });
+
+        if (result.deletedCount === 0) {
+            return {
+                message: "categories-not-found-or-unauthorized",
+                code: 404
+            };
+        }
 
         return {
-            message: "category-deleted",
+            message: "categories-deleted",
             code: 200
         };
     } catch (error) {
         console.error("DeleteCategory error:", error);
         return {
             message: "internal-server-error",
-            code: 500 
+            code: 500
         };
     }
 };

+ 3 - 8
src/actions/menu/deleteCategory/types.ts

@@ -1,18 +1,13 @@
 import {
-    IsNotEmpty, IsString, 
-    Validate
+    IsNotEmpty
 } from "class-validator";
 
 export class DeleteCategoryInput {
-    @IsString()
-    @IsNotEmpty()
-    categoryID!: string;
+    @IsNotEmpty({ message: "categoryIDs-is-required" })
+    categoryIDs!: string | string[];
 }
 
 export interface DeleteCategoryResult {
     message: string;
     code: number;
-    payload?: {
-        categoryID: string;
-    };
 }

+ 11 - 9
src/actions/menu/deleteProduct/index.ts

@@ -1,5 +1,6 @@
+import mongoose from "mongoose";
 import {
-    Menu 
+    Menu
 } from "../../../models/Menu";
 import {
     Product
@@ -21,10 +22,12 @@ const deleteProduct = async (body: any, context: { userID: string }): Promise<De
         const normalizedProductIDs = Array.isArray(productIDs)
             ? productIDs
             : productIDs ? [productIDs] : [];
-            
+
+        const objectIdArray = normalizedProductIDs.map(id => new mongoose.Types.ObjectId(id));
+
         const result = await Product.deleteMany({
             _id: {
-                $in: normalizedProductIDs 
+                $in: objectIdArray
             },
             userID: userID
         });
@@ -38,19 +41,18 @@ const deleteProduct = async (body: any, context: { userID: string }): Promise<De
 
         await Menu.updateMany(
             {
-                userID: userID 
+                userID: userID
             },
             {
                 $pull: {
                     products: {
                         productID: {
-                            $in: normalizedProductIDs 
-                        } 
-                    } 
-                } 
+                            $in: objectIdArray
+                        }
+                    }
+                }
             }
         );
-        
         return {
             message: "product-deleted-successfully",
             code: 200

+ 24 - 5
src/actions/menu/getProducts/index.ts

@@ -7,7 +7,7 @@ import {
     GetProductsInput
 } from "./types";
 import {
-    Menu 
+    Menu
 } from "../../../models/Menu";
 
 const getProducts = async (query: GetProductsInput, context: { userID: string }): Promise<GetProductsResult> => {
@@ -17,7 +17,8 @@ const getProducts = async (query: GetProductsInput, context: { userID: string })
         } = context;
 
         const {
-            categoryIDs
+            categoryIDs,
+            search
         } = query;
 
         const matchStage: any = {
@@ -25,11 +26,29 @@ const getProducts = async (query: GetProductsInput, context: { userID: string })
         };
 
         if (categoryIDs) {
+            const normalizedCatIDs = Array.isArray(categoryIDs) ? categoryIDs : [categoryIDs];
             matchStage.categoryIDs = {
-                $in: [new mongoose.Types.ObjectId(categoryIDs)]
+                $in: normalizedCatIDs.map(id => new mongoose.Types.ObjectId(id))
             };
         }
 
+        if (search) {
+            matchStage.$or = [
+                {
+                    title: {
+                        $regex: search,
+                        $options: "i"
+                    }
+                },
+                {
+                    description: {
+                        $regex: search,
+                        $options: "i"
+                    }
+                }
+            ];
+        }
+
         const activeMenu = await Menu.findOne({
             userID: new mongoose.Types.ObjectId(userID),
             isActive: true
@@ -43,9 +62,9 @@ const getProducts = async (query: GetProductsInput, context: { userID: string })
         }
         const activeProductIDs = activeMenu.products.map((p) => p.productID);
         matchStage._id = {
-            $in: activeProductIDs 
+            $in: activeProductIDs
         };
-        
+
         const products = await Product.aggregate([
             {
                 $match: matchStage

+ 4 - 1
src/actions/menu/getProducts/types.ts

@@ -4,9 +4,12 @@ import {
 } from "class-validator";
 
 export class GetProductsInput {
+    @IsOptional()
+    categoryIDs?: string | string[];
+
     @IsString()
     @IsOptional()
-    categoryIDs?: string;
+    search?: string;
 }
 
 export type GetProductsResult = {