From d61a86b5b3da7865f54e76add449a0445da41056 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Fri, 20 Sep 2024 17:33:02 +0200 Subject: [PATCH] Better order behaviour. Update date shows when last payment was done --- .../2024-08-12-171012_extended_order/up.sql | 3 ++- core/payment/src/dao/batch.rs | 15 ++++++++------- core/payment/src/models/batch.rs | 6 ++++-- core/payment/src/schema.rs | 5 +++-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/core/payment/migrations/2024-08-12-171012_extended_order/up.sql b/core/payment/migrations/2024-08-12-171012_extended_order/up.sql index 397cd36f8..371226a93 100644 --- a/core/payment/migrations/2024-08-12-171012_extended_order/up.sql +++ b/core/payment/migrations/2024-08-12-171012_extended_order/up.sql @@ -3,7 +3,8 @@ -- Your SQL goes here CREATE TABLE pay_batch_order( id VARCHAR (50) NOT NULL, - ts DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')), + created_ts DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')), + updated_ts DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')), owner_id VARCHAR(50) NOT NULL, payer_addr VARCHAR(50) NOT NULL, platform VARCHAR(50) NOT NULL, diff --git a/core/payment/src/dao/batch.rs b/core/payment/src/dao/batch.rs index 2a4d7b747..543b7bbc4 100644 --- a/core/payment/src/dao/batch.rs +++ b/core/payment/src/dao/batch.rs @@ -10,7 +10,7 @@ use ya_core_model::NodeId; use ya_persistence::executor::{ do_with_transaction, readonly_transaction, AsDao, ConnType, PoolType, }; -use ya_persistence::types::BigDecimalField; +use ya_persistence::types::{AdaptTimestamp, BigDecimalField}; use crate::error::{DbError, DbResult}; use crate::models::allocation::AllocationExpenditureObj; @@ -736,12 +736,12 @@ impl<'c> BatchDao<'c> { .filter(dsl::owner_id.eq(node_id)) .into_boxed(); if let Some(date) = after_timestamp { - query = query.filter(dsl::ts.gt(date)) + query = query.filter(dsl::created_ts.gt(date)) } if let Some(items) = max_items { query = query.limit(items.into()) } - query = query.order_by(dsl::ts.desc()); + query = query.order_by(dsl::created_ts.desc()); Ok(query.load(conn)?) }) .await @@ -887,7 +887,8 @@ impl<'c> BatchDao<'c> { Ok(query .select(( - order_dsl::ts, + order_dsl::created_ts, + order_dsl::updated_ts, order_item_dsl::order_id, order_item_dsl::owner_id, order_item_dsl::payee_addr, @@ -898,7 +899,7 @@ impl<'c> BatchDao<'c> { aggr_item_dsl::activity_id, aggr_item_dsl::debit_note_id, )) - .order_by(order_dsl::ts.desc()) + .order_by(order_dsl::created_ts.desc()) .load(conn)?) }) .await @@ -975,10 +976,10 @@ impl<'c> BatchDao<'c> { .get_result::(conn)?; let updated_amount = current_order.paid_amount + current_order_item.amount; - + let now = Utc::now().adapt(); diesel::update(dsl::pay_batch_order) .filter(dsl::id.eq(&order_id).and(dsl::owner_id.eq(owner_id))) - .set(dsl::paid_amount.eq(updated_amount)) + .set((dsl::paid_amount.eq(updated_amount), dsl::updated_ts.eq(now))) .execute(conn)?; Ok(true) diff --git a/core/payment/src/models/batch.rs b/core/payment/src/models/batch.rs index 74b3271d2..e3f4e1453 100644 --- a/core/payment/src/models/batch.rs +++ b/core/payment/src/models/batch.rs @@ -63,7 +63,8 @@ pub struct BatchPayment { #[serde(rename_all = "camelCase")] pub struct DbBatchOrder { pub id: String, - pub ts: NaiveDateTime, + pub created_ts: NaiveDateTime, + pub updated_ts: NaiveDateTime, pub owner_id: NodeId, pub payer_addr: String, pub platform: String, @@ -102,7 +103,8 @@ pub struct DbBatchOrderItemFullInfo { #[derive(Queryable, Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct DbAgreementBatchOrderItem { - pub ts: NaiveDateTime, + pub created_ts: NaiveDateTime, + pub updated_ts: NaiveDateTime, pub order_id: String, pub owner_id: String, pub payee_addr: String, diff --git a/core/payment/src/schema.rs b/core/payment/src/schema.rs index 2c2da5af3..baf7b5ded 100644 --- a/core/payment/src/schema.rs +++ b/core/payment/src/schema.rs @@ -78,7 +78,8 @@ table! { table! { pay_batch_order (owner_id, id) { id -> Text, - ts -> Timestamp, + created_ts -> Timestamp, + updated_ts -> Timestamp, owner_id -> Text, payer_addr -> Text, platform -> Text, @@ -88,7 +89,7 @@ table! { } table! { - pay_batch_order_item (owner_id, order_id, payee_addr) { + pay_batch_order_item (owner_id, order_id, payee_addr, allocation_id) { order_id -> Text, owner_id -> Text, payee_addr -> Text,