Skip to content

Commit

Permalink
Better order behaviour. Update date shows when last payment was done
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Sep 20, 2024
1 parent bfbd9c3 commit d61a86b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 8 additions & 7 deletions core/payment/src/dao/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -975,10 +976,10 @@ impl<'c> BatchDao<'c> {
.get_result::<DbBatchOrder>(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)
Expand Down
6 changes: 4 additions & 2 deletions core/payment/src/models/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions core/payment/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d61a86b

Please sign in to comment.