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
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Reviewed-on: #111
This commit is contained in:
commit
7d66d9d5b8
3 changed files with 66 additions and 20 deletions
|
@ -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.
|
// This updates the view with events as they are committed.
|
||||||
// The logic should be minimal here, e.g., don't calculate the account balance,
|
// The logic should be minimal here, e.g., don't calculate the account balance,
|
||||||
// design the events to carry the balance information instead.
|
// design the events to carry the balance information instead.
|
||||||
impl View<Bill> for BillView {
|
impl View<Bill> for BillView {
|
||||||
fn update(&mut self, event: &EventEnvelope<Bill>) {
|
fn update(&mut self, event: &EventEnvelope<Bill>) {
|
||||||
if let BillingEvent::BillAdded(val) = &event.payload {
|
match &event.payload {
|
||||||
self.created_time = val.bill().created_time().clone();
|
BillingEvent::BillAdded(val) => {
|
||||||
self.store_id = *val.bill().store_id();
|
self.merge(val.bill());
|
||||||
self.bill_id = *val.bill().bill_id();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,6 +134,25 @@ impl View<LineItem> for LineItemView {
|
||||||
|
|
||||||
self.deleted = false;
|
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,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,12 +32,22 @@ pub struct StoreView {
|
||||||
// design the events to carry the balance information instead.
|
// design the events to carry the balance information instead.
|
||||||
impl View<Store> for StoreView {
|
impl View<Store> for StoreView {
|
||||||
fn update(&mut self, event: &EventEnvelope<Store>) {
|
fn update(&mut self, event: &EventEnvelope<Store>) {
|
||||||
if let BillingEvent::StoreAdded(val) = &event.payload {
|
match &event.payload {
|
||||||
self.name = val.name().into();
|
BillingEvent::StoreAdded(val) => {
|
||||||
self.address = val.address().clone();
|
self.name = val.name().into();
|
||||||
self.store_id = *val.store_id();
|
self.address = val.address().clone();
|
||||||
self.owner = *val.owner();
|
self.store_id = *val.store_id();
|
||||||
self.deleted = false;
|
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();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue