Skip to content

Commit

Permalink
events for debit-note
Browse files Browse the repository at this point in the history
  • Loading branch information
prekucki committed Apr 4, 2022
1 parent 26e2336 commit f172a0f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DROP VIEW pay_debit_note_event_read;

CREATE VIEW pay_debit_note_event_read AS
SELECT
dn.role_id,
dn.role,
dne.debit_note_id,
dne.owner_id,
dne.event_type,
Expand Down
18 changes: 15 additions & 3 deletions core/payment/src/dao/debit_note_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::{DebitNoteEvent, DebitNoteEventType};
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>(
debit_note_id: String,
Expand Down Expand Up @@ -57,8 +59,8 @@ impl<'c> DebitNoteEventDao<'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<DebitNoteEvent>> {
// TODO: filter out by _requestor_events, _provider_events
readonly_transaction(self.pool, move |conn| {
Expand All @@ -76,7 +78,17 @@ impl<'c> DebitNoteEventDao<'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
}
Expand Down
2 changes: 2 additions & 0 deletions core/payment/src/models/debit_note_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::{DebitNoteEvent, DebitNoteEventType};
use ya_client_model::NodeId;
use ya_persistence::types::Role;

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

table! {
pay_debit_note_event_read (debit_note_id, event_type) {
role -> Text,
debit_note_id -> Text,
owner_id -> Text,
event_type -> Text,
Expand Down

0 comments on commit f172a0f

Please sign in to comment.