Sfoglia il codice sorgente

Bugfix: Update menu actions to handle active state correctly and adjust default value for isActive in menu schema

emrecevik106 4 settimane fa
parent
commit
c3b941f0f0

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

@@ -85,7 +85,7 @@ const register = async (input: RegisterInput): Promise<RegisterResult> => {
             isActive: true,
             products: []
         });
-        /* 
+        
         const starterPlan = await Plan.findOne({
             title: "Starter"
         });
@@ -108,7 +108,7 @@ const register = async (input: RegisterInput): Promise<RegisterResult> => {
             endDate: new Date(new Date().setFullYear(new Date().getFullYear() + 1)),
             status: "active",
             price: starterPlan!.type.find((t: { type: string; price: number }) => t.type === "yearly")?.price ?? 0
-        }); */
+        }); 
 
         await smsSend({
             userID: newUser._id.toString()

+ 15 - 1
src/actions/menu/addMenu/index.ts

@@ -17,7 +17,7 @@ const addMenu = async (userID: string, menuLimit: number, input: AddMenuInput):
         } = input;
 
         const menuCount = await Menu.countDocuments({
-            userID
+            userID: new mongoose.Types.ObjectId(userID)
         });
         
         if (menuCount >= menuLimit) {
@@ -27,6 +27,20 @@ const addMenu = async (userID: string, menuLimit: number, input: AddMenuInput):
             };
         }
 
+        if (isActive) {
+            await Menu.updateMany(
+                {
+                    userID: new mongoose.Types.ObjectId(userID),
+                    isActive: true 
+                },
+                {
+                    $set: {
+                        isActive: false 
+                    } 
+                }
+            );
+        }
+
         const newMenu = await Menu.create({
             userID: new mongoose.Types.ObjectId(userID), 
             isViewPrice,

+ 2 - 2
src/actions/menu/deleteMenu/index.ts

@@ -51,9 +51,9 @@ const deleteMenu = async (userID: string, input: DeleteMenuInput): Promise<Delet
             isActive: true
         });
 
-        if (activeMenuCount - deletingActiveCount < 1) {
+        if (deletingActiveCount > 0) {
             return {
-                message: "cannot-delete-last-active-menu",
+                message: "cannot-delete-active-menu-deactivate-first",
                 code: 400
             };
         }

+ 16 - 0
src/actions/menu/updateMenu/index.ts

@@ -38,6 +38,22 @@ const updateMenu = async (userID: string, input: UpdateMenuInput): Promise<Updat
             }
         }
 
+        if (updateFields.isActive === true) {
+            await Menu.updateMany(
+                {
+                    userID: objectUserID,
+                    _id: {
+                        $ne: objectMenuID 
+                    }
+                },
+                {
+                    $set: {
+                        isActive: false 
+                    }
+                }
+            );
+        }
+
         const updatedMenu = await Menu.findOneAndUpdate(
             {
                 _id: objectMenuID,

+ 1 - 1
src/models/Menu.ts

@@ -31,7 +31,7 @@ const menuSchema = new Schema<IMenu>(
         },
         isActive: {
             type: Boolean,
-            default: true
+            default: false
         },
         isViewPrice: {
             type: Boolean,