From 457004df3ff58e59f09626f0ecce41f9ad7c265a Mon Sep 17 00:00:00 2001 From: Brian McGonagill Date: Fri, 16 Aug 2024 12:33:27 -0500 Subject: [PATCH] Fix bug with updating linked menu items and products. --- client/MenuItems/addProdToListModal.js | 5 +-- imports/api/menuProdLinks.js | 44 ++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/client/MenuItems/addProdToListModal.js b/client/MenuItems/addProdToListModal.js index 548fc15..7204281 100644 --- a/client/MenuItems/addProdToListModal.js +++ b/client/MenuItems/addProdToListModal.js @@ -53,9 +53,6 @@ Template.addProdToListModal.events({ event.preventDefault(); let selectedItems = Session.get("itemsSelected"); let listId = $("#chooseList").val(); - // console.log(" calling meteor method with items: "); - // console.dir(selectedItems); - // console.log(" and list id: "+ listId); Meteor.call('add.itemsFromMenuItem', selectedItems, listId, function(err, result) { if (err) { console.log(" ERROR adding menu components to list: " + err); @@ -64,4 +61,4 @@ Template.addProdToListModal.events({ } }); } -}); \ No newline at end of file +}); diff --git a/imports/api/menuProdLinks.js b/imports/api/menuProdLinks.js index ea3c0ac..042755b 100644 --- a/imports/api/menuProdLinks.js +++ b/imports/api/menuProdLinks.js @@ -22,12 +22,42 @@ Meteor.methods({ throw new Meteor.Error('You are not allowed to add menu and product links. Make sure you are logged in with valid user credentials.'); }; - MenuProdLinks.insert({ - menuItemId: menuItemId, - menuItemName: menuItemName, - products: prodNameArray, - dateCreated: Date(), - createdBy: this.userId + let linkExists = MenuProdLinks.findOne({ menuItemId: menuItemId }); + + if (linkExists) { + console.log("sending to update method instead."); + Meteor.call('update.menuPordLInks', menuItemId, menuItemName, prodNameArray, function(err, result) { + if (err) { + console.log(" ERROR moving to the update method: " + err); + } else { + console.log("Successfully updated the menu prod links.") + } + }); + } else { + return MenuProdLinks.insert({ + menuItemId: menuItemId, + menuItemName: menuItemName, + products: prodNameArray, + dateCreated: Date(), + createdBy: this.userId + }); + } + }, + 'update.menuPordLInks' (menuItemId, menuItemName, prodNameArray) { + check(menuItemId, String); + check(menuItemName, String); + check(prodNameArray, [Object]); + + if (!this.userId) { + throw new Meteor.Error('You are not allowed to add menu and product links. Make sure you are logged in with valid user credentials.'); + }; + + return MenuProdLinks.update({ menuItemId: menuItemId }, { + $set: { + products: prodNameArray, + dateUpdated: Date(), + updatedBy: this.userId + } }); - } + }, });