Merge pull request 'feat: apply events to billing Views' (#111) from billing-view into master
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Reviewed-on: #111
This commit is contained in:
Aravinth Manivannan 2024-09-18 18:34:33 +05:30
commit 7d66d9d5b8
3 changed files with 66 additions and 20 deletions

View file

@ -52,28 +52,45 @@ 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<Bill> for BillView {
fn update(&mut self, event: &EventEnvelope<Bill>) {
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();
self.token_number = *val.bill().token_number() as i32;
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());
match &event.payload {
BillingEvent::BillAdded(val) => {
self.merge(val.bill());
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,
_ => (),
}
}
}

View file

@ -134,6 +134,25 @@ impl View<LineItem> 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,
_ => (),
}
}

View file

@ -32,13 +32,23 @@ pub struct StoreView {
// design the events to carry the balance information instead.
impl View<Store> for StoreView {
fn update(&mut self, event: &EventEnvelope<Store>) {
if let BillingEvent::StoreAdded(val) = &event.payload {
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();
}
_ => (),
}
}
}