瀏覽代碼

Backend working to remove transactions

Lee Morgan 6 年之前
父節點
當前提交
401745768f

+ 0 - 9
controllers/renderer.js

@@ -37,14 +37,6 @@ module.exports = {
         Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
             .populate("inventory.ingredient")
             .populate("recipes")
-            // .populate({
-            //     path: "recipes",
-            //     model: "Recipe",
-            //     populate: {
-            //         path: "ingredients.ingredient",
-            //         model: "Ingredient"
-            //     }
-            // })
             .then((merchant)=>{
                 if(merchant.pos === "clover"){
                     axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
@@ -146,7 +138,6 @@ module.exports = {
                         }},
                         {$sort: {date: 1}},
                         {$project: {
-                            _id: 0,
                             date: 1,
                             recipes: 1
                         }}

+ 18 - 1
controllers/transactionData.js

@@ -1,5 +1,4 @@
 const Transaction = require("../models/transaction");
-const Order = require("../models/order");
 const Merchant = require("../models/merchant");
 
 module.exports = {
@@ -56,5 +55,23 @@ module.exports = {
             .catch((err)=>{
                 return;
             });
+    },
+
+    /*
+    DELETE - Remove a transaction from the database
+    */
+    remove: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Transaction.deleteOne({_id: req.params.id})
+            .then((response)=>{
+                return res.json({});
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to delete the transaction");
+            });
     }
 }

+ 5 - 4
routes.js

@@ -35,12 +35,13 @@ module.exports = function(app){
     app.post("/order", orderData.createOrder);
     app.delete("/order/:id", orderData.removeOrder);
 
+    //Transactions
+    app.delete("/transaction/:id", transactionData.remove);
+    app.get("/populatesometransactions", transactionData.populate);
+
     //Other
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
     app.get("/cloverlogin", otherData.cloverRedirect);
-    app.get("/cloverauth*", otherData.cloverAuth);
-
-    //Transactions
-    app.get("/populatesometransactions", transactionData.populate);
+    app.get("/cloverauth*", otherData.cloverAuth);   
 }

+ 10 - 12
views/dashboardPage/Merchant.js

@@ -29,25 +29,16 @@ class Recipe{
     }
 }
 
-/*
-parent: merchant associated with,
-date: date created,
-recipes: [{
-    recipe: Recipe Object,
-    quantity: quantity of the recipe
-}
-*/
 class Transaction{
-    constructor(date, recipes, parent){
+    constructor(id, date, recipes, parent){
+        this.id = id;
         this.parent = parent;
         this.date = new Date(date);
         this.recipes = [];
 
         for(let i = 0; i < recipes.length; i++){
-            let found = false;
             for(let j = 0; j < parent.recipes.length; j++){
                 if(recipes[i].recipe === parent.recipes[j].id){
-                    found = true;
                     this.recipes.push({
                         recipe: parent.recipes[j],
                         quantity: recipes[i].quantity
@@ -112,9 +103,9 @@ class Merchant{
             ));
         }
 
-        
         for(let i = 0; i < transactions.length; i++){
             this.transactions.push(new Transaction(
+                transactions[i]._id,
                 transactions[i].date,
                 transactions[i].recipes,
                 this
@@ -225,6 +216,13 @@ class Merchant{
         closeSidebar();
     }
 
+    // editTransactions(transaction, remove = false){
+    //     for(let )
+    //     if(remove){
+
+    //     }
+    // }
+
     /*
     Gets the indices of two dates from transactions
     Inputs

+ 30 - 0
views/dashboardPage/sidebars/sidebars.js

@@ -865,7 +865,11 @@ let newRecipeComp = {
 }
 
 let transactionDetailsComp = {
+    transaction: {},
+
     display: function(transaction){
+        this.transaction = transaction;
+
         let recipeList = document.getElementById("transactionRecipes");
         let template = document.getElementById("transactionRecipe").content.children[0];
         let totalRecipes = 0;
@@ -898,5 +902,31 @@ let transactionDetailsComp = {
         document.getElementById("totalPrice").innerText = `$${(totalPrice / 100).toFixed(2)}`;
 
         openSidebar(document.getElementById("transactionDetails"));
+    },
+
+    remove: function(){
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch(`/transaction/${this.transaction.id}`, {
+            method: "delete",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+        })
+            .then(response => response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    //update the transactions on the front end
+                }
+            })
+            .catch((err)=>{
+                banner.createError("Something went wrong, please refresh the page");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }

+ 8 - 7
views/dashboardPage/sidebars/transactionDetails.ejs

@@ -7,14 +7,15 @@
             </svg>
         </button>
 
-        <button class="iconButton" onclick="transactionDetailsComp.edit()">
-            <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
-                <path d="M12 20h9"></path>
-                <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
-            </svg>
-        </button>
-
         <% if(merchant.pos === "none"){ %>
+            <button class="iconButton" onclick="transactionDetailsComp.edit()">
+                <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
+                    <path d="M12 20h9"></path>
+                    <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path>
+                </svg>
+            </button>
+
+        
             <button class="iconButton" onclick="transactionDetailsComp.remove()">
                 <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                     <polyline points="3 6 5 6 21 6"></polyline>