Skip to content

Commit

Permalink
Added peer id to document payment
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Sep 24, 2024
1 parent 09fbd40 commit 6603d92
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 35 deletions.
27 changes: 18 additions & 9 deletions core/payment/migrations/2024-08-12-171012_extended_order/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,24 @@ CREATE TABLE pay_payment(
signature BLOB,
signed_bytes TEXT,

CONSTRAINT pay_payment_pk PRIMARY KEY (id, role, owner_id, peer_id)
CONSTRAINT pay_payment_pk PRIMARY KEY (id, owner_id, peer_id)
);

INSERT INTO pay_payment (id, owner_id, peer_id, payee_addr, payer_addr, payment_platform, role, amount, timestamp, details, send_payment, signature, signed_bytes)
SELECT id, owner_id, peer_id, payee_addr, payer_addr, payment_platform, role, amount, timestamp, details, send_payment, signature, signed_bytes
FROM pay_payment_old;

CREATE TABLE pay_payment_document(
owner_id VARCHAR(50) NOT NULL,
payment_id VARCHAR(50) NOT NULL,
owner_id VARCHAR(50) NOT NULL,
peer_id VARCHAR(50) NOT NULL,
agreement_id VARCHAR(50) NOT NULL,
invoice_id VARCHAR(50),
activity_id VARCHAR(50),
debit_note_id VARCHAR(50),
amount VARCHAR(32) NOT NULL,

CONSTRAINT pay_payment_document_pk PRIMARY KEY (owner_id, payment_id, agreement_id, activity_id),
CONSTRAINT pay_payment_document_pk PRIMARY KEY (payment_id, owner_id, peer_id, agreement_id, activity_id),
CONSTRAINT pay_payment_document_fk1 FOREIGN KEY (owner_id, payment_id) REFERENCES pay_payment(owner_id, id),
CONSTRAINT pay_payment_document_fk2 FOREIGN KEY (owner_id, activity_id) REFERENCES pay_activity(owner_id, id),
CONSTRAINT pay_payment_document_fk3 FOREIGN KEY (owner_id, agreement_id) REFERENCES pay_agreement(owner_id, id),
Expand All @@ -166,39 +167,47 @@ CREATE TABLE pay_payment_document(
INSERT INTO pay_payment_document (
owner_id,
payment_id,
peer_id,
agreement_id,
invoice_id,
activity_id,
debit_note_id,
amount)
SELECT pap.owner_id,
pap.payment_id,
pp.peer_id,
pa.agreement_id,
null,
pap.activity_id,
null,
pap.amount
FROM pay_activity_payment pap
LEFT JOIN pay_activity pa
on pap.activity_id = pa.id;
on pap.activity_id = pa.id
LEFT JOIN pay_payment_old pp
on pap.payment_id = pp.id and pap.owner_id = pp.owner_id;


INSERT INTO pay_payment_document (
owner_id,
payment_id,
peer_id,
agreement_id,
invoice_id,
activity_id,
debit_note_id,
amount)
SELECT owner_id,
payment_id,
agreement_id,
SELECT pap.owner_id,
pap.payment_id,
pp.peer_id,
pap.agreement_id,
null,
null,
null,
amount
FROM pay_agreement_payment;
pap.amount
FROM pay_agreement_payment as pap
LEFT JOIN pay_payment_old pp
on pap.payment_id = pp.id and pap.owner_id = pp.owner_id;

DROP TABLE pay_activity_payment;
DROP TABLE pay_agreement_payment;
Expand Down
6 changes: 3 additions & 3 deletions core/payment/src/dao/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn increase_amount_scheduled(

pub fn increase_amount_paid(
activity_id: &String,
owner_id: &NodeId,
owner_id: NodeId,
amount: &BigDecimalField,
conn: &ConnType,
) -> DbResult<()> {
Expand Down Expand Up @@ -121,12 +121,12 @@ pub fn increase_amount_paid(
})
.collect::<Result<Vec<_>, _>>()?;

debit_note::update_status(&debit_note_ids, owner_id, &DocumentStatus::Settled, conn)?;
debit_note::update_status(&debit_note_ids, owner_id, DocumentStatus::Settled, conn)?;

for debit_note_id in debit_note_ids {
debit_note_event::create(
debit_note_id,
*owner_id,
owner_id,
DebitNoteEventType::DebitNoteSettledEvent,
conn,
)?;
Expand Down
8 changes: 4 additions & 4 deletions core/payment/src/dao/agreement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn increase_amount_scheduled(

pub fn set_amount_accepted(
agreement_id: &String,
owner_id: &NodeId,
owner_id: NodeId,
total_amount_accepted: &BigDecimalField,
conn: &ConnType,
) -> DbResult<()> {
Expand Down Expand Up @@ -145,7 +145,7 @@ impl Error for IncreaseAmountPaidError {

pub fn increase_amount_paid(
agreement_id: &String,
owner_id: &NodeId,
owner_id: NodeId,
amount: &BigDecimalField,
conn: &ConnType,
) -> DbResult<()> {
Expand Down Expand Up @@ -178,10 +178,10 @@ pub fn increase_amount_paid(
.map_err(|e| DbError::Query(format!("Failed to parse amount from invoice: {}", e)))?;

if invoice_amount <= total_amount_paid.0 {
invoice::update_status(&invoice_id, owner_id, &DocumentStatus::Settled, conn)?;
invoice::update_status(&invoice_id, owner_id, DocumentStatus::Settled, conn)?;
invoice_event::create(
invoice_id,
*owner_id,
owner_id,
InvoiceEventType::InvoiceSettledEvent,
conn,
)?;
Expand Down
6 changes: 3 additions & 3 deletions core/payment/src/dao/debit_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ macro_rules! query {

pub fn update_status(
debit_note_ids: &Vec<String>,
owner_id: &NodeId,
status: &DocumentStatus,
owner_id: NodeId,
status: DocumentStatus,
conn: &ConnType,
) -> DbResult<()> {
diesel::update(
Expand Down Expand Up @@ -313,7 +313,7 @@ impl<'c> DebitNoteDao<'c> {
.execute(conn)?;
}

update_status(&vec![debit_note_id.clone()], &owner_id, &status, conn)?;
update_status(&vec![debit_note_id.clone()], owner_id, status, conn)?;
activity::set_amount_accepted(&activity_id, &owner_id, &amount, conn)?;
for event in events {
debit_note_event::create(debit_note_id.clone(), owner_id, event, conn)?;
Expand Down
14 changes: 7 additions & 7 deletions core/payment/src/dao/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ macro_rules! query {

pub fn update_status(
invoice_id: &String,
owner_id: &NodeId,
status: &DocumentStatus,
owner_id: NodeId,
status: DocumentStatus,
conn: &ConnType,
) -> DbResult<()> {
diesel::update(
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<'c> InvoiceDao<'c> {

pub async fn mark_received(&self, invoice_id: String, owner_id: NodeId) -> DbResult<()> {
do_with_transaction(self.pool, "invoice_dao_mark_received", move |conn| {
update_status(&invoice_id, &owner_id, &DocumentStatus::Received, conn)
update_status(&invoice_id, owner_id, DocumentStatus::Received, conn)
})
.await
}
Expand Down Expand Up @@ -331,8 +331,8 @@ impl<'c> InvoiceDao<'c> {
.execute(conn)?;
}

update_status(&invoice_id, &owner_id, &status, conn)?;
agreement::set_amount_accepted(&agreement_id, &owner_id, &amount, conn)?;
update_status(&invoice_id, owner_id, status, conn)?;
agreement::set_amount_accepted(&agreement_id, owner_id, &amount, conn)?;

for event in events {
invoice_event::create(invoice_id.clone(), owner_id, event, conn)?;
Expand Down Expand Up @@ -432,7 +432,7 @@ impl<'c> InvoiceDao<'c> {
.find((&invoice_id, &owner_id))
.select((dsl::agreement_id, dsl::amount, dsl::role))
.first(conn)?;
update_status(&invoice_id, &owner_id, &DocumentStatus::Rejected, conn)?;
update_status(&invoice_id, owner_id, DocumentStatus::Rejected, conn)?;
if role == Role::Requestor {
diesel::update(
dsl::pay_invoice
Expand Down Expand Up @@ -503,7 +503,7 @@ impl<'c> InvoiceDao<'c> {

agreement::compute_amount_due(&agreement_id, &owner_id, conn)?;

update_status(&invoice_id, &owner_id, &DocumentStatus::Cancelled, conn)?;
update_status(&invoice_id, owner_id, DocumentStatus::Cancelled, conn)?;
invoice_event::create(
invoice_id,
owner_id,
Expand Down
17 changes: 11 additions & 6 deletions core/payment/src/dao/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub struct PaymentDao<'c> {
fn insert_activity_payments(
activity_payments: Vec<ActivityPayment>,
payment_id: &str,
owner_id: &NodeId,
owner_id: NodeId,
peer_id: NodeId,
conn: &ConnType,
) -> DbResult<()> {
log::trace!("Inserting activity payments...");
Expand Down Expand Up @@ -60,9 +61,10 @@ fn insert_activity_payments(
agreement_id: agreement_id.clone(),
invoice_id: None,
activity_id: Some(activity_payment.activity_id.clone()),
owner_id: *owner_id,
owner_id,
amount,
debit_note_id: None,
peer_id,
})
.execute(conn)
.map(|_| ())?;
Expand All @@ -74,7 +76,8 @@ fn insert_activity_payments(
fn insert_agreement_payments(
agreement_payments: Vec<AgreementPayment>,
payment_id: &str,
owner_id: &NodeId,
owner_id: NodeId,
peer_id: NodeId,
conn: &ConnType,
) -> DbResult<()> {
log::trace!("Inserting agreement payments...");
Expand All @@ -90,9 +93,10 @@ fn insert_agreement_payments(
agreement_id: agreement_payment.agreement_id,
invoice_id: None,
activity_id: None,
owner_id: *owner_id,
owner_id,
amount,
debit_note_id: None,
peer_id,
})
.execute(conn)
.map(|_| ())?;
Expand All @@ -117,6 +121,7 @@ impl<'c> PaymentDao<'c> {
let payment_id = payment.id.clone();
let owner_id = payment.owner_id;
let amount = payment.amount.clone();
let peer_id = payment.peer_id;

do_with_transaction(self.pool, "payment_dao_insert", move |conn| {
log::trace!("Inserting payment...");
Expand All @@ -125,8 +130,8 @@ impl<'c> PaymentDao<'c> {
.execute(conn)?;
log::trace!("Payment inserted.");

insert_activity_payments(activity_payments, &payment_id, &owner_id, conn)?;
insert_agreement_payments(agreement_payments, &payment_id, &owner_id, conn)?;
insert_activity_payments(activity_payments, &payment_id, owner_id, peer_id, conn)?;
insert_agreement_payments(agreement_payments, &payment_id, owner_id, peer_id, conn)?;

Ok(())
})
Expand Down
1 change: 1 addition & 0 deletions core/payment/src/models/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl ReadObj {
#[primary_key(owner_id, payment_id, agreement_id, activity_id)]
pub struct DocumentPayment {
pub owner_id: NodeId,
pub peer_id: NodeId,
pub payment_id: String,
pub agreement_id: String,
pub invoice_id: Option<String>,
Expand Down
7 changes: 4 additions & 3 deletions core/payment/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ table! {
}

table! {
pay_payment (id, role, owner_id, peer_id) {
pay_payment (id, owner_id, peer_id) {
id -> Text,
owner_id -> Text,
peer_id -> Text,
Expand All @@ -230,9 +230,10 @@ table! {
}

table! {
pay_payment_document (owner_id, payment_id, agreement_id, activity_id) {
owner_id -> Text,
pay_payment_document (payment_id, owner_id, peer_id, agreement_id, activity_id) {
payment_id -> Text,
owner_id -> Text,
peer_id -> Text,
agreement_id -> Text,
invoice_id -> Nullable<Text>,
activity_id -> Nullable<Text>,
Expand Down

0 comments on commit 6603d92

Please sign in to comment.