From 9bd2849695359b1c2e9e19c7e7c284ae8b6ebc72 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Wed, 18 Sep 2024 18:05:16 +0530 Subject: [PATCH] feat: apply events to billing Views --- .../adapters/output/db/postgres/bill_view.rs | 45 +++++++++++++------ .../output/db/postgres/line_item_view.rs | 19 ++++++++ .../adapters/output/db/postgres/store_view.rs | 22 ++++++--- 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/src/billing/adapters/output/db/postgres/bill_view.rs b/src/billing/adapters/output/db/postgres/bill_view.rs index 462b8a4..71c73cc 100644 --- a/src/billing/adapters/output/db/postgres/bill_view.rs +++ b/src/billing/adapters/output/db/postgres/bill_view.rs @@ -52,27 +52,44 @@ impl Default for BillView { } } +impl BillView { + fn merge(&mut self, bill: &Bill) { + self.created_time = bill.created_time().clone(); + self.store_id = *bill.store_id(); + self.bill_id = *bill.bill_id(); + + self.token_number = *bill.token_number() as i32; + + self.total_price_minor = bill.total_price().as_ref().map(|t| *t.minor() as i32); + self.total_price_major = bill.total_price().as_ref().map(|t| *t.major() as i32); + self.total_price_currency = bill + .total_price() + .as_ref() + .map(|t| t.currency().to_string()); + } +} + // This updates the view with events as they are committed. // The logic should be minimal here, e.g., don't calculate the account balance, // design the events to carry the balance information instead. impl View for BillView { fn update(&mut self, event: &EventEnvelope) { - if let BillingEvent::BillAdded(val) = &event.payload { - self.created_time = val.bill().created_time().clone(); - self.store_id = *val.bill().store_id(); - self.bill_id = *val.bill().bill_id(); + match &event.payload { + BillingEvent::BillAdded(val) => { + self.merge(val.bill()); - self.token_number = *val.bill().token_number() as i32; + self.deleted = false; + } + BillingEvent::BillUpdated(e) => self.merge(e.new_bill()), + BillingEvent::BillTotalPriceComputed(e) => { + let total_price = e.total_price().clone(); + self.total_price_minor = Some(*total_price.minor() as i32); + self.total_price_major = Some(*total_price.major() as i32); + self.total_price_currency = Some(total_price.currency().to_string()); + } + BillingEvent::BillDeleted(e) => self.deleted = true, - self.total_price_minor = val.bill().total_price().as_ref().map(|t| *t.minor() as i32); - self.total_price_major = val.bill().total_price().as_ref().map(|t| *t.major() as i32); - self.total_price_currency = val - .bill() - .total_price() - .as_ref() - .map(|t| t.currency().to_string()); - - self.deleted = false; + _ => (), } } } diff --git a/src/billing/adapters/output/db/postgres/line_item_view.rs b/src/billing/adapters/output/db/postgres/line_item_view.rs index fd7484c..c98a875 100644 --- a/src/billing/adapters/output/db/postgres/line_item_view.rs +++ b/src/billing/adapters/output/db/postgres/line_item_view.rs @@ -134,6 +134,25 @@ impl View for LineItemView { self.deleted = false; } + BillingEvent::LineItemUpdated(val) => { + let new = val.new_line_item(); + self.product_name = new.product_name().into(); + self.product_id = *new.product_id(); + self.line_item_id = *new.line_item_id(); + + self.quantity_major_number = *new.quantity().major().number() as i32; + self.quantity_minor_number = *new.quantity().minor().number() as i32; + self.quantity_major_unit = new.quantity().major().unit().to_string(); + self.quantity_minor_unit = new.quantity().minor().unit().to_string(); + + self.price_per_unit_major = *new.price_per_unit().major() as i32; + self.price_per_unit_minor = *new.price_per_unit().minor() as i32; + self.price_per_unit_currency = new.price_per_unit().currency().to_string(); + + self.created_time = new.created_time().clone(); + self.bill_id = *new.bill_id(); + } + BillingEvent::LineItemDeleted(_) => self.deleted = true, _ => (), } } diff --git a/src/billing/adapters/output/db/postgres/store_view.rs b/src/billing/adapters/output/db/postgres/store_view.rs index f8ee34f..acb6f80 100644 --- a/src/billing/adapters/output/db/postgres/store_view.rs +++ b/src/billing/adapters/output/db/postgres/store_view.rs @@ -32,12 +32,22 @@ pub struct StoreView { // design the events to carry the balance information instead. impl View for StoreView { fn update(&mut self, event: &EventEnvelope) { - if let BillingEvent::StoreAdded(val) = &event.payload { - self.name = val.name().into(); - self.address = val.address().clone(); - self.store_id = *val.store_id(); - self.owner = *val.owner(); - self.deleted = false; + match &event.payload { + BillingEvent::StoreAdded(val) => { + self.name = val.name().into(); + self.address = val.address().clone(); + self.store_id = *val.store_id(); + self.owner = *val.owner(); + self.deleted = false; + } + BillingEvent::StoreUpdated(e) => { + let val = e.new_store(); + self.name = val.name().into(); + self.address = val.address().clone(); + self.store_id = *val.store_id(); + self.owner = *val.owner(); + } + _ => (), } } }