소스 검색

Fetch transactions for an account.

Lee Morgan 11 달 전
부모
커밋
9d9e3abc46
4개의 변경된 파일63개의 추가작업 그리고 12개의 파일을 삭제
  1. 13 0
      src/views/js/data/Account.js
  2. 32 12
      src/views/js/data/Transaction.js
  3. 1 0
      src/views/js/pages/CreateTransaction.js
  4. 17 0
      src/views/js/pages/Home.js

+ 13 - 0
src/views/js/data/Account.js

@@ -27,6 +27,14 @@ export default class Account{
         return Format.centsToDollars(this._balance);
     }
 
+    get isPopulated(){
+        return this._populated;
+    }
+
+    set isPopulated(v){
+        if(typeof v === "boolean") this._populated = v;
+    }
+
     incomeTotal(){
         let income = 0;
         for(let i = 0; i < this._income.length; i++){
@@ -99,6 +107,11 @@ export default class Account{
         this.sortTransactions();
     }
 
+    addManyTransactions(transactions){
+        this._transactions.push(...transactions);
+        this.sortTransactions();
+    }
+
     removeTransaction(transaction){
         if(transaction instanceof Transaction){
             for(let i = 0; i < this._transactions.length; i++){

+ 32 - 12
src/views/js/data/Transaction.js

@@ -2,7 +2,7 @@ import Format from "../Format.js";
 import Notifier from "../Notifier.js";
 
 export default class Transaction{
-    constructor(parent, id, iv, date, data,){
+    constructor(parent, id, iv, date, data){
         this._parent = parent;
         this._id = id;
         this._iv = iv;
@@ -39,20 +39,41 @@ export default class Transaction{
         );
     }
 
-    static decrypt(transaction){
-        let data = encryptionHandler.decrypt(transaction.data, transaction.iv);
+    static async fetch(account, from, to){
+        let response;
+        try{
+            response = await fetch("/api/transaction/search", {
+                method: "POST",
+                headers: {"Content-Type": "application/json"},
+                body: JSON.stringify({account, from, to})
+            });
+            response = await response.json();
+
+            const promises = [];
+            for(let i = 0; i < response.length; i++){
+                promises.push(Transaction.decrypt(response[i]));
+            }
+
+            return await Promise.all(promises);
+        }catch(e){
+            new Notifier("error", "Something went wrong, try refreshing the page");
+        }
+
+        if(response.error){
+            new Notifier("error", response.error.message);
+            return;
+        }
+    }
+
+    static async decrypt(transaction){
+        let data = await encryptionHandler.decrypt(transaction.data, transaction.iv);
 
         return new Transaction(
+            user.account,
             transaction.id,
-            transaction.account,
-            transaction.date,
-            data.amount,
-            data.tags,
-            data.location,
-            data.note,
             transaction.iv,
-            data.type,
-            data.typeId
+            transaction.date,
+            data
         );
     }
 
@@ -96,7 +117,6 @@ export default class Transaction{
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 new Notifier("error", "Something went wrong, try refreshing the page");
                 this._parent.removeTransaction(this);
             });

+ 1 - 0
src/views/js/pages/CreateTransaction.js

@@ -11,6 +11,7 @@ export default class CreateTransaction extends Page{
             user.account.listBills(),
             user.account.listAllowances()
         );
+
     }
 
     async submit(event){

+ 17 - 0
src/views/js/pages/Home.js

@@ -2,6 +2,7 @@ import Page from "./Page.js";
 import Elem from "../Elem.js";
 import Format from "../Format.js";
 import Notifier from "../Notifier.js";
+import Transaction from "../data/Transaction.js";
 
 export default class Home extends Page{
     constructor(){
@@ -10,6 +11,22 @@ export default class Home extends Page{
         const month = date.toLocaleString("en-US", {month: "long"});
         let logoutSvg = '<svg width="24px" height="24px" stroke-width="2.5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" color="currentColor"><path d="M12 12H19M19 12L16 15M19 12L16 9" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path><path d="M19 6V5C19 3.89543 18.1046 3 17 3H7C5.89543 3 5 3.89543 5 5V19C5 20.1046 5.89543 21 7 21H17C18.1046 21 19 20.1046 19 19V18" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"></path></svg>';
 
+        if(!user.account.isPopulated){
+            let from = new Date();
+            from.setDate(1);
+            from = Format.transactionDate(from);
+            let to = new Date();
+            to = Format.transactionDate(to);
+            
+            Transaction.fetch(user.account.id, from, to)
+                .then((transactions)=>{
+                    user.account.addManyTransactions(transactions);
+                    user.account.isPopulated = true;
+                })
+                .catch((err)=>{
+                    new Notifier("error", "Unable to retrieve transactions");
+                });
+        }
         this.render(month, logoutSvg);
     }