Browse Source

Working with tags

Cesar Rodas 1 year ago
parent
commit
9aacbe5eca
5 changed files with 60 additions and 2 deletions
  1. 2 0
      client.js
  2. 12 2
      src/main.rs
  3. 16 0
      utxo/src/ledger.rs
  4. 1 0
      utxo/src/lib.rs
  5. 29 0
      utxo/src/transaction/mod.rs

+ 2 - 0
client.js

@@ -24,6 +24,7 @@ async function deposit(account, amount, asset) {
       memo: "deposit",
       account,
       cents: amount.toString(),
+      tags: ['deposit'],
       asset,
       status: 'pending',
     })
@@ -92,6 +93,7 @@ async function change_status(id, s_status) {
 async function test() {
   let d = (await deposit(addr1, 1512312, "BTC/8"));
   dbg(d);
+  return
   dbg(await change_status(d._id, 'settled'));
   d = (await deposit(addr2, 1001234, "USD/4"));
   dbg(await change_status(d._id, 'settled'));

+ 12 - 2
src/main.rs

@@ -19,6 +19,7 @@ pub struct Deposit {
     #[serde(flatten)]
     pub amount: AnyAmount,
     pub memo: String,
+    pub tags: Vec<String>,
     pub status: Status,
 }
 
@@ -27,7 +28,7 @@ impl Deposit {
         self,
         ledger: &Ledger,
     ) -> Result<verax::Transaction, verax::Error> {
-        ledger
+        let zdeposit = ledger
             ._inner
             .deposit(
                 &self.account,
@@ -35,7 +36,16 @@ impl Deposit {
                 self.status,
                 self.memo,
             )
-            .await
+            .await?;
+
+        Ok(if !self.tags.is_empty() {
+            ledger
+                ._inner
+                .set_tags(&zdeposit.id, self.tags, "Update tags".to_owned())
+                .await?
+        } else {
+            zdeposit
+        })
     }
 }
 

+ 16 - 0
utxo/src/ledger.rs

@@ -296,6 +296,22 @@ where
         &self.config.status
     }
 
+    /// Updates a transaction and updates their tags to this given set
+    pub async fn set_tags(
+        &self,
+        transaction_id: &TxId,
+        tags: Vec<String>,
+        reason: String,
+    ) -> Result<Transaction, Error> {
+        Ok(self
+            .config
+            .storage
+            .get_transaction(transaction_id)
+            .await?
+            .set_tags(&self.config, tags, reason)
+            .await?)
+    }
+
     /// Attempts to change the status of a given transaction id. On success the
     /// new transaction object is returned, otherwise an error is returned.
     pub async fn change_status(

+ 1 - 0
utxo/src/lib.rs

@@ -39,6 +39,7 @@ mod transaction;
 
 #[cfg(test)]
 pub use self::storage::test as storage_test;
+
 pub use self::{
     amount::{Amount, AnyAmount, HumanAmount},
     asset::Asset,

+ 29 - 0
utxo/src/transaction/mod.rs

@@ -162,6 +162,35 @@ impl Transaction {
         &self.id
     }
 
+    /// Updates the transaction tags
+    pub async fn set_tags<S>(
+        self,
+        config: &Config<S>,
+        new_tags: Vec<String>,
+        reason: String,
+    ) -> Result<Self, Error>
+    where
+        S: Storage + Sync + Send,
+    {
+        let new_revision = Revision {
+            transaction_id: self.revision.transaction_id,
+            changelog: reason,
+            previous: Some(self.revision_id),
+            tags: new_tags,
+            status: self.revision.status,
+            created_at: Utc::now(),
+        };
+
+        let mut new_transaction = Transaction {
+            id: self.id,
+            revision_id: new_revision.rev_id()?,
+            transaction: self.transaction,
+            revision: new_revision,
+        };
+        new_transaction.persist(config).await?;
+        Ok(new_transaction)
+    }
+
     /// Prepares a new revision to change the transaction status
     ///
     /// If the status transaction is not allowed, it will return an error.