Skip to content

Commit

Permalink
Balance and trx prep
Browse files Browse the repository at this point in the history
  • Loading branch information
kartonrad committed May 18, 2023
1 parent eae3bbf commit 7871574
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/src/db_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ impl UserDebtExt for UserDebt {
finally, they are both joined.
*/

// TODO Migration strategy: UNION the trx table to the debt table (debtor_id, creditor_id, owed)
let dtrs: Vec<DebtTableRecord> = sqlx::query_as!( DebtTableRecord , r#"
WITH debt_table AS (
SELECT debtor_id, creditor_id, (amount/nr_shares)::NUMERIC(16,2) as owed
Expand Down
6 changes: 6 additions & 0 deletions api/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ async fn get_wg_users_public(params: web::Path<(i32,)>) -> Result<impl Responder

#[get("/my_wg/costs")]
async fn get_wg_costs(member: WGMemberIdentity, query: web::Query<BalanceInput>) -> Result<impl Responder, DatabaseError> {
// TODO MIGRATION STRAT: Dings endpunkt mit enum CostOrTrx rückgabe
// er fetched von beiden tabellen und mischt die dann hier in die richtige reihenfolge!
let cost = Cost::get_all_balance(member.identity.id, member.wg_id, query.balance.unwrap_or(0)).await?;

Ok( HttpResponse::Ok()
Expand Down Expand Up @@ -347,6 +349,9 @@ async fn post_wg_costs_balance(WGMemberIdentity{identity, wg_id} : WGMemberIdent
async fn get_wg_costs_balance(identity: Identity) -> Result<impl Responder, DatabaseError> {
let costs_opt =
if let Some(wg_id) = identity.wg {
/// TODO Migration Strat: rename to "i_paid_cost, i_recieved_cost" and add "i_paid_trx, i_recieved_trx"
/// This will make sure that info is there
let balances = sqlx::query_as!(Balance, r#"
SELECT equal_balances.id, equal_balances.balanced_on, equal_balances.initiator_id, equal_balances.wg_id,
coalesce( sum(costs.amount), 0) as total_unified_spending,
Expand Down Expand Up @@ -388,6 +393,7 @@ async fn get_wg_costs_over_time(member: WGMemberIdentity, params: web::Path<(Str
my_total_spending: Option<Decimal>
}

// needs no modification for trx
let balances = sqlx::query_as!(DBRegularSpending, r#"
SELECT
date_trunc($3, added_on) as time_bucket ,
Expand Down
10 changes: 10 additions & 0 deletions api/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
- add FromRequest for WGIdentity (requires WG) ✔
- DELETE cost ✔

***
### Priority
* Add trx table
* Change GEt /my_wg/costs or add another to take enum
* Change GET /my_wg/costs/stats (tally) to union trxs
* Change GET /my_wg/costs/balances to also aggregate trxs
* make no changes to over_time, that shit is fine
* add endpoints to add and remove trxs

***
- continue refactoring backend: balance and stats
- Add cost_shares.amount field, and update POST cost (and in future POST shares) accordingly, to guard that all shares add up exactly to costs.amount
- The result will allow for far more flexible and inexpensive querying of data
Expand Down
2 changes: 2 additions & 0 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub fn LoggedInApp<'a>(cx: Scope, member: &'a WGMember) -> Element {
Route { to: "/costs/detail", Layout { CostDetailScreen {} } }
Route { to: "/costs/tally", Layout { TopTabs {} CostTallyScreen {} } }
Route { to: "/costs/balance", Layout { CostBalanceDetailScreen {} } }
Route { to: "/costs/balance/confirm", CostBalanceConfirmScreen {} }
Route { to: "/costs/stats", Layout { TopTabs {} CostStatScreen {} } }

Route { to: "/settings", Layout { SettingScreen {} } }
Expand All @@ -128,6 +129,7 @@ pub fn LoggedInApp<'a>(cx: Scope, member: &'a WGMember) -> Element {
}



fn TopTabs(cx: Scope) -> Element {
cx.render(rsx!(
nav {
Expand Down
65 changes: 65 additions & 0 deletions app/src/screens/costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ pub fn CostTallyScreen(cx: Scope) -> Element {
render!(
Tallys {
}
Link {
to: "/costs/balance/confirm",

div {
background_color: "green",
color: "white",
"Komplett abrechnen"
}
}
//trx_obj
h3 {
class: "cost_seperator",
Expand Down Expand Up @@ -534,6 +543,62 @@ pub fn CostBalanceDetailScreen(cx: Scope) -> Element {
)
}

pub fn CostBalanceConfirmScreen(cx: Scope) -> Element {
let router = use_router(cx);
let http = use_context::<HTTP>(cx)?;

let send_balance = move |evt: Event<MouseData>| {
evt.stop_propagation();
cx.spawn({
to_owned![router, http];

async move {
let req = http.post(format!("{API_URL}/api/my_wg/costs/balance"));
let result = req.send().await;
match result.map(|res| res.error_for_status()) {
Ok(res) => {
trace!("SUCCESSFULLY ADDED BALANCE!");
router.pop_route();
},
Err(err) => {
error!("Couldn't add entry!! {err}");
}
}
}
})
//evt.values
};

render!(
div {
background_color: "crimson",
position: "fixed",
top: "0",
left: "0",
right: "0",
bottom: "0",
text_align: "center",
color: "white",

h2 { "Willst du wirklich alles abrechnen?" }
h4 { "Der aktuelle Unterschied wird auf 0 gesetzt." }

button {
value: "JA",
onclick: send_balance,

"JA"
}
button {
value: "Nein",
onclick: move |_| {router.pop_route();},

"Nein"
}
}
)
}

// ========== Components
#[inline_props]
fn TransactionEntry(cx: Scope, trx: BalancingTransaction) -> Element {
Expand Down

0 comments on commit 7871574

Please sign in to comment.