Skip to content

Commit

Permalink
Feat: set timestamp when creating transaction (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianim authored Feb 2, 2023
2 parents f430326 + 65bc38f commit 283a838
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
21 changes: 12 additions & 9 deletions src/resources/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ servers:
- url: /api/v1

paths:

/auth:
get:
get:
tags:
- auth
summary: get authentication config
Expand All @@ -26,7 +25,7 @@ paths:
local:
type: object
properties:
enabled:
enabled:
type: boolean
proxy:
type: object
Expand All @@ -49,18 +48,17 @@ paths:
required: true
responses:
200:
$ref: '#/components/responses/AuthenticationResponse'
$ref: "#/components/responses/AuthenticationResponse"

/auth/proxy:
post:
tags:
tags:
- auth
summary: get a token from the proxy provider
description: get a token by sending an empty request to the proxy provider
responses:
200:
$ref: '#/components/responses/AuthenticationResponse'

$ref: "#/components/responses/AuthenticationResponse"

/user:
get:
Expand Down Expand Up @@ -103,7 +101,7 @@ paths:
deprecated: true
tags:
- user
summary: use `/auth/local` instead!
summary: use `/auth/local` instead!
description: just used for backwards compatibility
requestBody:
description: username and password
Expand All @@ -114,7 +112,7 @@ paths:
required: true
responses:
200:
$ref: '#/components/responses/AuthenticationResponse'
$ref: "#/components/responses/AuthenticationResponse"

/group:
get:
Expand Down Expand Up @@ -405,6 +403,11 @@ components:
description:
type: string
example: "Bread"
timestamp:
type: number
format: unix timestamp (seconds)
required: false
example: 1675350727

responses:
AuthenticationResponse:
Expand Down
2 changes: 2 additions & 0 deletions src/routes/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct TransactionCreationRequest {
debtor_ids: Vec<String>,
amount: u32,
description: String,
timestamp: Option<u32>,
}

#[get("/")]
Expand Down Expand Up @@ -112,6 +113,7 @@ async fn create_group_tansaction(
transaction_creation_request.debtor_ids.to_owned(),
transaction_creation_request.amount,
transaction_creation_request.description.to_owned(),
transaction_creation_request.timestamp,
)
.await
{
Expand Down
31 changes: 23 additions & 8 deletions src/services/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl GroupService {
debtor_ids: Vec<String>,
amount: u32,
description: String,
timestamp: Option<u32>,
) -> Result<Transaction, TransactionCreationError> {
let members = self
._get_group_members(&group_id)
Expand All @@ -216,7 +217,14 @@ impl GroupService {
}

Ok(self
._create_transaction_with_debt(group_id, creditor_id, debtor_ids, amount, description)
._create_transaction_with_debt(
group_id,
creditor_id,
debtor_ids,
amount,
description,
timestamp,
)
.await)
}

Expand Down Expand Up @@ -364,21 +372,27 @@ impl GroupService {
group_id: &str,
creditor_id: String,
description: String,
timestamp: Option<u32>,
) -> String {
let new_transaction_id = uuid::Uuid::new_v4().to_string();

let new_transaction = model::transaction::ActiveModel {
id: ActiveValue::Set(new_transaction_id.to_owned()),
group_id: ActiveValue::Set(group_id.to_owned()),
creditor_id: ActiveValue::Set(creditor_id.to_owned()),
timestamp: ActiveValue::Set(
let timestamp: i32 = timestamp
.unwrap_or(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs()
.try_into()
.unwrap(),
),
)
.try_into()
.unwrap();

let new_transaction = model::transaction::ActiveModel {
id: ActiveValue::Set(new_transaction_id.to_owned()),
group_id: ActiveValue::Set(group_id.to_owned()),
creditor_id: ActiveValue::Set(creditor_id.to_owned()),
timestamp: ActiveValue::Set(timestamp),
description: ActiveValue::Set(description.to_owned()),
};

Expand All @@ -397,9 +411,10 @@ impl GroupService {
debtor_ids: Vec<String>,
amount: u32,
description: String,
timestamp: Option<u32>,
) -> Transaction {
let transaction_id = self
._create_transaction(&group_id, creditor_id, description)
._create_transaction(&group_id, creditor_id, description, timestamp)
.await;

let debts = self
Expand Down

0 comments on commit 283a838

Please sign in to comment.