Skip to content

Commit

Permalink
events for invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
wkargul committed Apr 5, 2022
1 parent f172a0f commit 7381e92
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- This file should undo anything in `up.sql`
DROP VIEW pay_debit_note_event_read;
DROP VIEW pay_invoice_event_read;

CREATE VIEW pay_debit_note_event_read AS
SELECT
Expand All @@ -16,3 +17,16 @@ FROM
INNER JOIN pay_agreement agr ON dne.owner_id = agr.owner_id AND act.agreement_id = agr.id;


CREATE VIEW pay_invoice_event_read AS
SELECT
ie.invoice_id,
ie.owner_id,
ie.event_type,
ie.timestamp,
ie.details,
agr.app_session_id
FROM
pay_invoice_event ie
INNER JOIN pay_invoice inv ON ie.owner_id = inv.owner_id AND ie.invoice_id = inv.id
INNER JOIN pay_agreement agr ON ie.owner_id = agr.owner_id AND inv.agreement_id = agr.id;

17 changes: 17 additions & 0 deletions core/payment/migrations/2022-04-04-095553_role_id-in-events/up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- Your SQL goes here
DROP VIEW pay_debit_note_event_read;
DROP VIEW pay_invoice_event_read;

CREATE VIEW pay_debit_note_event_read AS
SELECT
Expand All @@ -17,3 +18,19 @@ FROM
INNER JOIN pay_agreement agr ON dne.owner_id = agr.owner_id AND act.agreement_id = agr.id;


CREATE VIEW pay_invoice_event_read AS
SELECT
inv.role,
ie.invoice_id,
ie.owner_id,
ie.event_type,
ie.timestamp,
ie.details,
agr.app_session_id
FROM
pay_invoice_event ie
INNER JOIN pay_invoice inv ON ie.owner_id = inv.owner_id AND ie.invoice_id = inv.id
INNER JOIN pay_agreement agr ON ie.owner_id = agr.owner_id AND inv.agreement_id = agr.id;



1 change: 0 additions & 1 deletion core/payment/src/dao/debit_note_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl<'c> DebitNoteEventDao<'c> {
requestor_events: Vec<Cow<'static, str>>,
provider_events: Vec<Cow<'static, str>>,
) -> DbResult<Vec<DebitNoteEvent>> {
// TODO: filter out by _requestor_events, _provider_events
readonly_transaction(self.pool, move |conn| {
let mut query = read_dsl::pay_debit_note_event_read
.filter(read_dsl::owner_id.eq(node_id))
Expand Down
21 changes: 16 additions & 5 deletions core/payment/src/dao/invoice_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use chrono::NaiveDateTime;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
use serde::Serialize;
use std::borrow::Cow;
use std::collections::HashSet;
use std::convert::TryInto;
use ya_client_model::payment::{InvoiceEvent, InvoiceEventType};
use ya_client_model::NodeId;
use ya_persistence::executor::{
do_with_transaction, readonly_transaction, AsDao, ConnType, PoolType,
};
use ya_persistence::types::Role;

pub fn create<T: Serialize>(
invoice_id: String,
Expand Down Expand Up @@ -57,10 +59,9 @@ impl<'c> InvoiceEventDao<'c> {
after_timestamp: Option<NaiveDateTime>,
max_events: Option<u32>,
app_session_id: Option<String>,
_requestor_events: Vec<Cow<'static, str>>,
_provider_events: Vec<Cow<'static, str>>,
requestor_events: Vec<Cow<'static, str>>,
provider_events: Vec<Cow<'static, str>>,
) -> DbResult<Vec<InvoiceEvent>> {
// TODO: filter out by _requestor_events, _provider_events
readonly_transaction(self.pool, move |conn| {
let mut query = read_dsl::pay_invoice_event_read
.filter(read_dsl::owner_id.eq(node_id))
Expand All @@ -76,8 +77,18 @@ impl<'c> InvoiceEventDao<'c> {
query = query.limit(limit.into());
}
let events: Vec<ReadObj> = query.load(conn)?;
events.into_iter().map(TryInto::try_into).collect()
let requestor_events: HashSet<Cow<'static, str>> =
requestor_events.into_iter().collect();
let provider_events: HashSet<Cow<'static, str>> = provider_events.into_iter().collect();
events
.into_iter()
.filter(|e| match e.role {
Role::Requestor => requestor_events.contains(e.event_type.as_str()),
Role::Provider => provider_events.contains(e.event_type.as_str()),
})
.map(TryInto::try_into)
.collect()
})
.await
.await
}
}
2 changes: 2 additions & 0 deletions core/payment/src/models/invoice_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::Serialize;
use std::convert::TryFrom;
use ya_client_model::payment::{InvoiceEvent, InvoiceEventType};
use ya_client_model::NodeId;
use ya_persistence::types::Role;

#[derive(Debug, Identifiable, Insertable)]
#[table_name = "pay_invoice_event"]
Expand Down Expand Up @@ -41,6 +42,7 @@ impl WriteObj {
#[table_name = "pay_invoice_event_read"]
#[primary_key(invoice_id, event_type)]
pub struct ReadObj {
pub role: Role,
pub invoice_id: String,
pub owner_id: NodeId,
pub event_type: String,
Expand Down
3 changes: 1 addition & 2 deletions core/payment/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ table! {

table! {
pay_invoice_event_read (invoice_id, event_type) {
role -> Text,
invoice_id -> Text,
owner_id -> Text,
event_type -> Text,
Expand Down Expand Up @@ -204,12 +205,10 @@ allow_tables_to_appear_in_same_query!(
pay_allocation,
pay_debit_note,
pay_debit_note_event,
pay_debit_note_event_read,
pay_document_status,
pay_event_type,
pay_invoice,
pay_invoice_event,
pay_invoice_event_read,
pay_invoice_x_activity,
pay_order,
pay_payment,
Expand Down

0 comments on commit 7381e92

Please sign in to comment.