Skip to content

Commit

Permalink
chore: add timestamps to journal
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts committed Oct 10, 2024
1 parent 08778df commit a99325e
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 42 deletions.
3 changes: 3 additions & 0 deletions cala-ledger-core-types/src/journal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use super::primitives::*;
Expand All @@ -6,6 +7,8 @@ use super::primitives::*;
pub struct JournalValues {
pub id: JournalId,
pub version: u32,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub name: String,
pub status: Status,
pub description: Option<String>,
Expand Down
8 changes: 8 additions & 0 deletions cala-ledger-outbox-client/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ impl TryFrom<proto::Journal> for JournalValues {
let res = Self {
id: journal.id.parse()?,
version: journal.version,
created_at: journal
.created_at
.ok_or(CalaLedgerOutboxClientError::MissingField)?
.into(),
modified_at: journal
.modified_at
.ok_or(CalaLedgerOutboxClientError::MissingField)?
.into(),
name: journal.name,
status,
description: journal.description,
Expand Down

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

This file was deleted.

24 changes: 19 additions & 5 deletions cala-ledger/src/journal/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,17 @@ impl Journal {
}

pub fn update(&mut self, builder: impl Into<JournalUpdate>) {
let mut builder = builder.into();
if builder.modified_at.is_none() {
builder.modified_at(chrono::Utc::now());
}

let JournalUpdateValues {
name,
modified_at,
status,
description,
} = builder
.into()
.build()
.expect("JournalUpdateValues always exist");
} = builder.build().expect("JournalUpdateValues always exist");
let mut updated_fields = Vec::new();

if let Some(name) = name {
Expand All @@ -88,6 +91,8 @@ impl Journal {
updated_fields.push("description".to_string());
}

self.values.modified_at = modified_at;

if !updated_fields.is_empty() {
self.events.push(JournalEvent::Updated {
values: self.values.clone(),
Expand Down Expand Up @@ -116,6 +121,8 @@ impl Journal {
#[derive(Builder, Debug, Default)]
#[builder(name = "JournalUpdate", default)]
pub struct JournalUpdateValues {
#[builder(private)]
modified_at: chrono::DateTime<chrono::Utc>,
#[builder(setter(into, strip_option))]
pub name: Option<String>,
#[builder(setter(into, strip_option))]
Expand All @@ -127,6 +134,7 @@ pub struct JournalUpdateValues {
impl From<(JournalValues, Vec<String>)> for JournalUpdate {
fn from((values, fields): (JournalValues, Vec<String>)) -> Self {
let mut builder = JournalUpdate::default();
builder.modified_at(values.modified_at);

for field in fields {
match field.as_str() {
Expand Down Expand Up @@ -177,6 +185,8 @@ impl TryFrom<EntityEvents<JournalEvent>> for Journal {
pub struct NewJournal {
#[builder(setter(into))]
pub id: JournalId,
#[builder(private)]
pub(super) created_at: chrono::DateTime<chrono::Utc>,
#[builder(setter(into))]
pub(super) name: String,
#[builder(setter(into), default)]
Expand All @@ -187,7 +197,9 @@ pub struct NewJournal {

impl NewJournal {
pub fn builder() -> NewJournalBuilder {
NewJournalBuilder::default()
let mut builder = NewJournalBuilder::default();
builder.created_at(chrono::Utc::now());
builder
}

pub(super) fn initial_events(self) -> EntityEvents<JournalEvent> {
Expand All @@ -197,6 +209,8 @@ impl NewJournal {
values: JournalValues {
id: self.id,
version: 1,
created_at: self.created_at,
modified_at: self.created_at,
name: self.name,
status: self.status,
description: self.description,
Expand Down
13 changes: 9 additions & 4 deletions cala-ledger/src/journal/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ impl JournalRepo {
new_journal: NewJournal,
) -> Result<Journal, JournalError> {
let id = new_journal.id;
let created_at = new_journal.created_at;
sqlx::query!(
r#"INSERT INTO cala_journals (id, name)
VALUES ($1, $2)"#,
r#"INSERT INTO cala_journals (id, created_at, name)
VALUES ($1, $2, $3)"#,
id as JournalId,
created_at,
new_journal.name,
)
.execute(&mut **db)
.await?;
let mut events = new_journal.initial_events();
events.persist(db).await?;
events.persist_at(db, created_at).await?;
let journal = Journal::try_from(events)?;
Ok(journal)
}
Expand All @@ -52,7 +54,10 @@ impl JournalRepo {
)
.execute(&mut **db)
.await?;
journal.events.persist(db).await?;
journal
.events
.persist_at(db, journal.values().modified_at)
.await?;
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions cala-ledger/src/outbox/server/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ impl From<JournalValues> for proto::Journal {
JournalValues {
id,
version,
created_at,
modified_at,
name,
status,
description,
Expand All @@ -255,6 +257,8 @@ impl From<JournalValues> for proto::Journal {
proto::Journal {
id: id.to_string(),
version,
created_at: Some(created_at.into()),
modified_at: Some(modified_at.into()),
name,
status: status as i32,
description,
Expand Down

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

This file was deleted.

8 changes: 5 additions & 3 deletions proto/ledger/outbox_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ message JournalUpdated {
message Journal {
string id = 1;
uint32 version = 2;
string name = 3;
Status status = 4;
optional string description = 5;
google.protobuf.Timestamp created_at = 3;
google.protobuf.Timestamp modified_at = 4;
string name = 5;
Status status = 6;
optional string description = 7;
}

message TxTemplateCreated {
Expand Down

0 comments on commit a99325e

Please sign in to comment.