浏览代码

Remove old orders when they are updated in order to be replaced

Lee Morgan 6 年之前
父节点
当前提交
ad370f6903
共有 2 个文件被更改,包括 13 次插入6 次删除
  1. 11 5
      controllers/renderer.js
  2. 2 1
      models/transaction.js

+ 11 - 5
controllers/renderer.js

@@ -3,8 +3,7 @@ const ObjectId = require("mongoose").Types.ObjectId;
 
 const Merchant = require("../models/merchant");
 const Transaction = require("../models/transaction");
-const transaction = require("../models/transaction");
-const ingredient = require("../models/ingredient");
+// const ingredient = require("../models/ingredient");
 
 module.exports = {
     /*
@@ -45,7 +44,7 @@ module.exports = {
                     const subscriptionCheck = axios.get(`${process.env.CLOVER_ADDRESS}/v3/apps/${process.env.SUBLINE_CLOVER_APPID}/merchants/${merchant.posId}/billing_info?access_token=${merchant.posAccessToken}`);
                     const transactionRetrieval = axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=modifiedTime>=${merchant.lastUpdatedTime}&expand=lineItems&expand=payment&access_token=${merchant.posAccessToken}`);
                     await Promise.all([subscriptionCheck, transactionRetrieval])
-                        .then((response)=>{
+                        .then(async (response)=>{
                             if(response[0].data.status !== "ACTIVE"){
                                 req.session.error = "SUBSCRIPTION EXPIRED.  PLEASE RENEW ON CLOVER";
                                 return res.redirect("/");
@@ -63,10 +62,10 @@ module.exports = {
                                 let newTransaction = new Transaction({
                                     merchant: merchant._id,
                                     date: new Date(order.createdTime),
-                                    device: order.device.id
+                                    device: order.device.id,
+                                    posId: order.id
                                 });
 
-
                                 //Go through lineItems from Clover
                                 //Get the appropriate recipe from Subline
                                 //Add it to the transaction or increment if existing
@@ -115,6 +114,13 @@ module.exports = {
 
                             merchant.lastUpdatedTime = updatedTime;
 
+                            //Remove any existing orders so that they can ber replaced
+                            let ids = [];
+                            for(let i = 0; i < transactions.length; i++){
+                                ids.push(transactions[i].posId);
+                            }
+                            Transaction.deleteMany({posId: {$in: ids}});
+
                             promiseArray.push(Transaction.create(transactions));
                         })
                         .catch((err)=>{

+ 2 - 1
models/transaction.js

@@ -20,7 +20,8 @@ const TransactionSchema = new mongoose.Schema({
             type: Number,
             min: [0, "Must be a positive number"]
         }
-    }]
+    }],
+    posId: String
 });
 
 module.exports = mongoose.model("Transaction", TransactionSchema);