Skip to content

Commit

Permalink
Merge pull request #898 from golemfactory/event-api/pay-invoice-event…
Browse files Browse the repository at this point in the history
…s-fix

[pay] use backward compatible event types against DB
  • Loading branch information
tworec authored Dec 22, 2020
2 parents 75cb9a3 + fabbcf3 commit 899776b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
14 changes: 8 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions core/payment/src/dao/debit_note_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ impl<'c> AsDao<'c> for DebitNoteEventDao<'c> {
}

impl<'c> DebitNoteEventDao<'c> {
pub async fn create<T: Serialize>(
pub async fn create<T: Serialize + Send + 'static>(
&self,
debit_note_id: String,
owner_id: NodeId,
event_type: DebitNoteEventType,
details: Option<T>,
) -> DbResult<()> {
let event = WriteObj::new(debit_note_id, owner_id, event_type, details)?;
do_with_transaction(self.pool, move |conn| {
diesel::insert_into(write_dsl::pay_debit_note_event)
.values(event)
.execute(conn)?;
Ok(())
create(debit_note_id, owner_id, event_type, details, conn)
})
.await
}
Expand Down
8 changes: 2 additions & 6 deletions core/payment/src/dao/invoice_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ impl<'c> AsDao<'c> for InvoiceEventDao<'c> {
}

impl<'c> InvoiceEventDao<'c> {
pub async fn create<T: Serialize>(
pub async fn create<T: Serialize + Send + 'static>(
&self,
invoice_id: String,
owner_id: NodeId,
event_type: InvoiceEventType,
details: Option<T>,
) -> DbResult<()> {
let event = WriteObj::new(invoice_id, owner_id, event_type, details)?;
do_with_transaction(self.pool, move |conn| {
diesel::insert_into(write_dsl::pay_invoice_event)
.values(event)
.execute(conn)?;
Ok(())
create(invoice_id, owner_id, event_type, details, conn)
})
.await
}
Expand Down
10 changes: 8 additions & 2 deletions core/payment/src/models/debit_note_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl WriteObj {
Ok(Self {
debit_note_id,
owner_id,
event_type: serde_json::to_string(&event_type)?,
event_type: event_type.to_string(),
details,
})
}
Expand All @@ -53,6 +53,12 @@ impl TryFrom<ReadObj> for DebitNoteEvent {
type Error = DbError;

fn try_from(event: ReadObj) -> DbResult<Self> {
let event_type = event.event_type.parse().map_err(|e| {
DbError::Integrity(format!(
"DebitNoteEvent type `{}` parsing failed: {}",
&event.event_type, e
))
})?;
// TODO Attach details when event_type=REJECTED
let _details = match event.details {
Some(s) => Some(json_from_str(&s)?),
Expand All @@ -61,7 +67,7 @@ impl TryFrom<ReadObj> for DebitNoteEvent {
Ok(Self {
debit_note_id: event.debit_note_id,
event_date: Utc.from_utc_datetime(&event.timestamp),
event_type: serde_json::from_str(&event.event_type)?,
event_type,
})
}
}
11 changes: 9 additions & 2 deletions core/payment/src/models/invoice_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl WriteObj {
Ok(Self {
invoice_id,
owner_id,
event_type: serde_json::to_string(&event_type)?,
event_type: event_type.to_string(),
details,
})
}
Expand All @@ -53,12 +53,19 @@ impl TryFrom<ReadObj> for InvoiceEvent {
type Error = DbError;

fn try_from(event: ReadObj) -> DbResult<Self> {
let event_type = event.event_type.parse().map_err(|e| {
DbError::Integrity(format!(
"InvoiceEvent type `{}` parsing failed: {}",
event.event_type, e
))
})?;

// TODO Attach details when event_type=REJECTED
let _details = match event.details {
Some(s) => Some(json_from_str(&s)?),
None => None,
};
let event_type = serde_json::from_str(&event.event_type)?;

Ok(Self {
invoice_id: event.invoice_id,
event_date: Utc.from_utc_datetime(&event.timestamp),
Expand Down

0 comments on commit 899776b

Please sign in to comment.