Просмотр исходного кода

Create the transaction model for frontend.

Lee Morgan 11 месяцев назад
Родитель
Сommit
1d6ceace21

+ 2 - 1
src/dto/transaction.rs

@@ -4,7 +4,8 @@ use serde::Deserialize;
 pub struct CreateInput {
     pub account: String,
     pub date: String,
-    pub data: String
+    pub data: String,
+    pub iv: String
 }
 
 #[derive(Deserialize)]

+ 8 - 4
src/models/transaction.rs

@@ -14,7 +14,8 @@ pub struct Transaction {
     pub id: ObjectId,
     pub account: ObjectId,
     pub data: String,
-    pub date: String
+    pub date: String,
+    pub iv: String
 }
 
 #[derive(Serialize, Deserialize)]
@@ -22,7 +23,8 @@ pub struct ResponseTransaction {
     pub id: String,
     pub account: String,
     pub data: String,
-    pub date: String
+    pub date: String,
+    pub iv: String
 }
 
 impl Transaction {
@@ -34,7 +36,8 @@ impl Transaction {
             id: ObjectId::new(),
             account: ObjectId::parse_str(input.account)?,
             data: input.data,
-            date: input.date
+            date: input.date,
+            iv: input.iv
         };
 
         let insert_one_result = collection.insert_one(transaction).await?;
@@ -125,7 +128,8 @@ impl Transaction {
             id: self.id.to_hex(),
             account: self.account.to_hex(),
             data: self.data.clone(),
-            date: self.date.clone()
+            date: self.date.clone(),
+            iv: self.iv.clone()
         }
     }
 }

+ 13 - 0
src/views/js/Format.js

@@ -0,0 +1,13 @@
+export default class Format{
+    static currency(num){
+        return "$" + num.toFixed(2);
+    }
+
+    static transactionDate(date){
+        let year = date.getFullYear();
+        let month = date.getMonth() + 1;
+        let day = date.getDate();
+
+        return `${year}-${month}-${day}`;
+    }
+}

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

@@ -1,5 +1,7 @@
 import EncryptionHandler from "../EncryptionHandler.js";
 import Notifier from "../Notifier.js";
+import Format from "../Format.js";
+import Transaction from "./Transaction.js";
 
 export default class Account{
     constructor(id, iv, data){
@@ -11,6 +13,27 @@ export default class Account{
         this._bills = data.bills;
         this._allowances = data.allowances;
         this._transactions = [];
+        this._populated = false;
+    }
+
+    get balance(){
+        return this.toDollars(this._balance);
+    }
+
+    incomeTotal(){
+        let income = 0;
+        for(let i = 0; i < this._income.length; i++){
+            income += this._income[i].amount;
+        }
+        return this.toDollars(income);
+    }
+
+    billsTotal(){
+        let bills = 0;
+        for(let i = 0; i < this._bills.length; i++){
+            bills += this._bills[i].amount;
+        }
+        return this.toDollars(bills);
     }
 
     static async create(name, balance){
@@ -89,6 +112,41 @@ export default class Account{
         return Math.round(num * 100);
     }
 
+    toDollars(num){
+        return num / 100;
+    }
+
+    async populateTransactions(){
+        if(this._populated) return;
+
+        let from = new Date();
+        from.setDate(1);
+        const to = new Date();
+        let response;
+        try{
+            response = await fetch("/api/transaction/search", {
+                method: "POST",
+                headers: {"Content-Type": "application/json"},
+                body: JSON.stringify({
+                    account: this._id,
+                    from: Format.transactionDate(from),
+                    to: Format.transactionDate(to)
+                })
+            });
+        }catch(e){
+            new Notifier("error", "Something went wrong, try refreshing the page");
+        }
+
+        if(response.error){
+            new Notifier("error", response.error.message);
+        }else{
+            for(let i = 0; i < response.length; i++){
+                this._transactions = Transaction.decrypt(response[i]);
+            }
+            this._populated = true;
+        }
+    }
+
     async save(){
         const data = {
             name: this._name,

+ 31 - 0
src/views/js/data/Transaction.js

@@ -0,0 +1,31 @@
+export default class Transaction{
+    constructor(id, account, date, amount, tags, location, note, iv, type, typeId){
+        this._id = id;
+        this._account = account;
+        this._date = date;
+        this._type = type;
+        this._typeId = typeId;
+        this._amount = amount;
+        this._tags = tags;
+        this._location = location;
+        this._note = note;
+        this._iv = iv;
+    }
+
+    static decrypt(transaction){
+        let data = encryptionHandler.decrypt(transaction.data, transaction.iv);
+
+        return new Transaction(
+            transaction.id,
+            transaction.account,
+            transaction.date,
+            data.amount,
+            data.tags,
+            data.location,
+            data.note,
+            transaction.iv,
+            data.type,
+            data.typeId
+        );
+    }
+}

+ 1 - 0
src/views/js/index.js

@@ -60,5 +60,6 @@ fetch("/api/user", {
         currentPage = new Home();
     })
     .catch((err)=>{
+        console.log(err);
         currentPage = new Login();
     });

+ 36 - 2
src/views/js/pages/Home.js

@@ -1,14 +1,17 @@
 import Page from "./Page.js";
 import Elem from "../Elem.js";
+import Format from "../Format.js";
 
 export default class Home extends Page{
     constructor(){
         super("Home");
+        const date = new Date();
+        const month = date.toLocaleString("en-US", {month: "long"});
 
-        this.render();
+        this.render(month);
     }
 
-    render(){
+    render(month){
         new Elem("div")
             .addClass("buttonBox")
             .append(new Elem("button")
@@ -17,5 +20,36 @@ export default class Home extends Page{
                 .onclick(()=>{changePage("addMenu")})
             )
             .appendTo(this.container);
+
+        new Elem("h1")
+            .text(month)
+            .appendTo(this.container);
+
+        new Elem("dl")
+            .append(new Elem("dt")
+                .text("Balance: ")
+            )
+            .append(new Elem("dd")
+                .text(Format.currency(user.account.balance))
+            )
+            .appendTo(this.container);
+
+        new Elem("dl")
+            .append(new Elem("dt")
+                .text("Income: ")
+            )
+            .append(new Elem("dd")
+                .text(Format.currency(user.account.incomeTotal()))
+            )
+            .appendTo(this.container);
+
+        new Elem("dl")
+            .append(new Elem("dt")
+                .text("Bills: ")
+            )
+            .append(new Elem("dd")
+                .text(Format.currency(user.account.billsTotal()))
+            )
+            .appendTo(this.container);
     }
 }