Skip to content

Commit

Permalink
Add missing allocation id foreign key reference to payment documents
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Sep 16, 2024
1 parent b73a653 commit e277890
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 83 deletions.
12 changes: 7 additions & 5 deletions core/payment/migrations/2024-08-12-171012_extended_order/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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))
Expand Down
80 changes: 3 additions & 77 deletions core/payment/src/dao/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ pub fn resolve_invoices(args: &ResolveInvoiceArgs) -> DbResult<Option<String>> {
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::<String>),
Expand All @@ -634,6 +635,7 @@ pub fn resolve_invoices(args: &ResolveInvoiceArgs) -> DbResult<Option<String>> {
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::<String>),
dsl::activity_id.eq(activity_id),
Expand Down Expand Up @@ -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<BatchPayment> {
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::<NodeId, Vec<BatchPaymentObligation>>::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<String>,
Option<String>,
Option<String>,
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,
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions core/payment/src/models/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub activity_id: String,
Expand Down
3 changes: 2 additions & 1 deletion core/payment/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Text>,
activity_id -> Nullable<Text>,
Expand Down

0 comments on commit e277890

Please sign in to comment.