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 3277924b3..574e860a3 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 @@ -25,7 +25,7 @@ CREATE TABLE pay_batch_order_item( payment_id VARCHAR(50), paid BOOLEAN NOT NULL DEFAULT FALSE, - CONSTRAINT pay_batch_order_item_pk PRIMARY KEY (owner_id, order_id, payee_addr), + CONSTRAINT pay_batch_order_item_pk PRIMARY KEY (owner_id, order_id, payee_addr, allocation_id), CONSTRAINT pay_batch_order_item_fk1 FOREIGN KEY (owner_id, order_id) REFERENCES pay_batch_order(owner_id, id), CONSTRAINT pay_batch_order_item_fk2 FOREIGN KEY (owner_id, allocation_id) REFERENCES pay_allocation(owner_id, id) ); @@ -37,20 +37,22 @@ CREATE TABLE pay_batch_order_item_document( order_id VARCHAR(50) NOT NULL, owner_id VARCHAR(50) NOT NULL, payee_addr VARCHAR(50) NOT NULL, + allocation_id VARCHAR(50) NOT NULL, agreement_id VARCHAR(50) NOT NULL, invoice_id VARCHAR(50) NULL, activity_id VARCHAR(50) NULL, debit_note_id VARCHAR(50) NULL, amount VARCHAR(32) NOT NULL, - CONSTRAINT pay_batch_order_item_agreement_pk PRIMARY KEY (owner_id, order_id, payee_addr, agreement_id, activity_id), - CONSTRAINT pay_batch_order_item_agreement_fk1 FOREIGN KEY (owner_id, order_id, payee_addr) REFERENCES pay_batch_order_item(owner_id, order_id, payee_addr), + CONSTRAINT pay_batch_order_item_agreement_pk PRIMARY KEY (owner_id, order_id, payee_addr, allocation_id, agreement_id, activity_id), + CONSTRAINT pay_batch_order_item_agreement_fk1 FOREIGN KEY (owner_id, order_id, payee_addr, allocation_id) REFERENCES pay_batch_order_item(owner_id, order_id, payee_addr, allocation_id), CONSTRAINT pay_batch_order_item_agreement_fk2 FOREIGN KEY (owner_id, agreement_id) REFERENCES pay_agreement(owner_id, id), CONSTRAINT pay_batch_order_item_agreement_fk3 FOREIGN KEY (owner_id, activity_id) REFERENCES pay_activity(owner_id, id), - CONSTRAINT pay_batch_order_item_agreement_fk4 FOREIGN KEY (owner_id, invoice_id) + CONSTRAINT pay_batch_order_item_agreement_fk4 FOREIGN KEY (owner_id, allocation_id) REFERENCES pay_allocation(owner_id, id), + CONSTRAINT pay_batch_order_item_agreement_fk5 FOREIGN KEY (owner_id, invoice_id) REFERENCES pay_invoice(owner_id, id) ON DELETE SET NULL, - CONSTRAINT pay_batch_order_item_agreement_fk5 FOREIGN KEY (owner_id, debit_note_id) + CONSTRAINT pay_batch_order_item_agreement_fk6 FOREIGN KEY (owner_id, debit_note_id) REFERENCES pay_debit_note(owner_id, id) ON DELETE SET NULL, CHECK ((invoice_id IS NULL) <> (debit_note_id IS NULL)) diff --git a/core/payment/src/dao/batch.rs b/core/payment/src/dao/batch.rs index de1fe3ce2..121bf13ee 100644 --- a/core/payment/src/dao/batch.rs +++ b/core/payment/src/dao/batch.rs @@ -613,6 +613,7 @@ pub fn resolve_invoices(args: &ResolveInvoiceArgs) -> DbResult> { dsl::order_id.eq(&order_id), dsl::owner_id.eq(owner_id), dsl::payee_addr.eq(&payee_addr), + dsl::allocation_id.eq(allocation_id), dsl::agreement_id.eq(agreement_id), dsl::invoice_id.eq(id), dsl::activity_id.eq(None::), @@ -634,6 +635,7 @@ pub fn resolve_invoices(args: &ResolveInvoiceArgs) -> DbResult> { dsl::order_id.eq(&order_id), dsl::owner_id.eq(owner_id), dsl::payee_addr.eq(&payee_addr), + dsl::allocation_id.eq(allocation_id), dsl::agreement_id.eq(agreement_id), dsl::invoice_id.eq(None::), dsl::activity_id.eq(activity_id), @@ -784,83 +786,6 @@ impl<'c> BatchDao<'c> { }).await } - pub async fn get_batch_order_payments( - &self, - order_id: String, - owner_id: NodeId, - payee_addr: String, - ) -> DbResult { - readonly_transaction(self.pool, "get_batch_order_payments", move |conn| { - use crate::schema::pay_agreement::dsl as pa; - use crate::schema::pay_batch_order_item::dsl as di; - use crate::schema::pay_batch_order_item_document::dsl as d; - - let (amount,) = di::pay_batch_order_item - .filter( - di::order_id - .eq(&order_id) - .and(di::payee_addr.eq(&payee_addr)) - .and(di::owner_id.eq(&owner_id)), - ) - .select((di::amount,)) - .get_result::<(BigDecimalField,)>(conn)?; - - let mut peer_obligation = HashMap::>::new(); - - for (payee_id, agreement_id, invoice_id, activity_id, debit_note_id, amount) in - d::pay_batch_order_item_document - .filter(d::order_id.eq(order_id).and(d::payee_addr.eq(payee_addr))) - .inner_join( - pa::pay_agreement - .on(d::owner_id.eq(pa::owner_id).and(d::agreement_id.eq(pa::id))), - ) - .select(( - pa::peer_id, - d::agreement_id, - d::invoice_id, - d::activity_id, - d::debit_note_id, - d::amount, - )) - .load::<( - NodeId, - String, - Option, - Option, - Option, - BigDecimalField, - )>(conn)? - { - let obligation = if let Some(activity_id) = activity_id { - BatchPaymentObligation::DebitNote { - debit_note_id, - amount: amount.0, - agreement_id, - activity_id, - } - } else if let Some(invoice_id) = invoice_id { - BatchPaymentObligation::Invoice { - amount: amount.0, - agreement_id, - id: invoice_id, - } - } else { - return Err(DbError::Integrity("No invoice or activity id".to_string())); - }; - peer_obligation - .entry(payee_id) - .or_default() - .push(obligation); - } - - Ok(BatchPayment { - amount: amount.0, - peer_obligation, - }) - }) - .await - } - pub async fn get_unsent_batch_items( &self, owner_id: NodeId, @@ -919,6 +844,7 @@ impl<'c> BatchDao<'c> { aggr_item_dsl::pay_batch_order_item_document.on(order_item_dsl::order_id .eq(aggr_item_dsl::order_id) .and(order_item_dsl::owner_id.eq(aggr_item_dsl::owner_id)) + .and(order_item_dsl::allocation_id.eq(aggr_item_dsl::allocation_id)) .and(order_item_dsl::payee_addr.eq(aggr_item_dsl::payee_addr))), ) .inner_join( diff --git a/core/payment/src/models/batch.rs b/core/payment/src/models/batch.rs index 9c98f35df..8a80817e8 100644 --- a/core/payment/src/models/batch.rs +++ b/core/payment/src/models/batch.rs @@ -120,6 +120,7 @@ pub struct DbBatchOrderItemAgreement { pub order_id: String, pub owner_id: NodeId, pub payee_addr: String, + pub allocation_id: String, pub agreement_id: String, pub invoice_id: Option, pub activity_id: String, diff --git a/core/payment/src/schema.rs b/core/payment/src/schema.rs index 7e22219d5..1a31f67f2 100644 --- a/core/payment/src/schema.rs +++ b/core/payment/src/schema.rs @@ -101,10 +101,11 @@ table! { } table! { - pay_batch_order_item_document (owner_id, order_id, payee_addr, agreement_id) { + pay_batch_order_item_document (owner_id, order_id, payee_addr, allocation_id, agreement_id, activity_id) { order_id -> Text, owner_id -> Text, payee_addr -> Text, + allocation_id -> Text, agreement_id -> Text, invoice_id -> Nullable, activity_id -> Nullable,