Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rust types for new events tables #137

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion database/bindings/EventType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.

export type EventType = "AppConnect" | "AppDisconnect" | "ClientConnectInit" | "ClientConnectResolve" | "ClientDisconnect" | "SignMessage" | "SignTransaction" | "SignAndSendTransaction" | "ChangeWallet" | "ChangeNetwork";
export type EventType = "AppConnect" | "AppDisconnect" | "ClientConnect" | "ClientDisconnect" | "SignMessage" | "SignTransaction" | "SignAndSendTransaction" | "ChangeWallet" | "ChangeNetwork";
5 changes: 3 additions & 2 deletions database/migrations/0012_events_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ CREATE TABLE event_client_connect(

CREATE TABLE event_client_disconnect(
event_id BIGINT PRIMARY KEY REFERENCES events(event_id),
client_id TEXT NOT NULL,
disconnected_session_id TEXT NOT NULL
);

Expand Down Expand Up @@ -63,7 +64,7 @@ CREATE TABLE event_change_wallet(
wallet_name TEXT NOT NULL,
wallet_type TEXT NOT NULL,
old_wallet_address TEXT NOT NULL,
new_wallet_address TEXT NOT NULL
new_wallet_address TEXT
);

CREATE TABLE event_change_network(
Expand All @@ -72,5 +73,5 @@ CREATE TABLE event_change_network(
request_id TEXT NOT NULL,
request_status request_status_enum NOT NULL,
old_network TEXT NOT NULL,
new_network TEXT NOT NULL
new_network TEXT
);
2 changes: 1 addition & 1 deletion database/src/tables/events/app_connect/table_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub const EVENT_APP_CONNECT_KEYS: &str =

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AppConnectEvent {
pub event_id: String,
pub event_id: i64,
pub app_id: String,
pub session_id: String,
pub device_metadata: String,
Expand Down
4 changes: 3 additions & 1 deletion database/src/tables/events/app_connect/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ impl Db {
pub async fn create_new_event_app_connect(
&self,
tx: &mut Transaction<'_, Postgres>,
event_id: i64,
session_id: &String,
device_metadata: &String,
lang: &String,
timezone: &String,
new_session: bool,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {EVENT_APP_CONNECT_TABLE_NAME} ({EVENT_APP_CONNECT_KEYS}) VALUES (DEFAULT, $1, $2, $3, $4, $5)"
"INSERT INTO {EVENT_APP_CONNECT_TABLE_NAME} ({EVENT_APP_CONNECT_KEYS}) VALUES ($1, $2, $3, $4, $5, $6)"
);

let query_result = query(&query_body)
.bind(event_id)
.bind(session_id)
.bind(device_metadata)
.bind(lang)
Expand Down
9 changes: 7 additions & 2 deletions database/src/tables/events/app_disconnect/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ impl Db {
pub async fn create_new_event_app_disconnect(
&self,
tx: &mut Transaction<'_, Postgres>,
event_id: i64,
session_id: &String,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {EVENT_APP_DISCONNECT_TABLE_NAME} ({EVENT_APP_DISCONNECT_KEYS}) VALUES (DEFAULT, $1)"
"INSERT INTO {EVENT_APP_DISCONNECT_TABLE_NAME} ({EVENT_APP_DISCONNECT_KEYS}) VALUES ($1, $2)"
);

let query_result = query(&query_body).bind(session_id).execute(&mut **tx).await;
let query_result = query(&query_body)
.bind(event_id)
.bind(session_id)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Expand Down
2 changes: 2 additions & 0 deletions database/src/tables/events/change_network/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod table_struct;
pub mod update;
29 changes: 29 additions & 0 deletions database/src/tables/events/change_network/table_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::structs::request_status::RequestStatus;
use sqlx::{postgres::PgRow, FromRow, Row};

pub const EVENT_CHANGE_NETWORK_TABLE_NAME: &str = "event_change_network";
pub const EVENT_CHANGE_NETWORK_KEYS: &str =
"event_id, session_id, request_id, request_status, old_network, new_network";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ChangeNetworkEvent {
pub event_id: i64,
pub session_id: String,
pub request_id: String,
pub request_status: RequestStatus,
pub old_network: String,
pub new_network: Option<String>,
}

impl FromRow<'_, PgRow> for ChangeNetworkEvent {
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> {
Ok(ChangeNetworkEvent {
event_id: row.get("event_id"),
session_id: row.get("session_id"),
request_id: row.get("request_id"),
request_status: row.get("request_status"),
old_network: row.get("old_network"),
new_network: row.get("new_network"),
})
}
}
62 changes: 62 additions & 0 deletions database/src/tables/events/change_network/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::structs::request_status::RequestStatus;
use crate::{
db::Db,
structs::db_error::DbError,
tables::events::change_network::table_struct::{
EVENT_CHANGE_NETWORK_KEYS, EVENT_CHANGE_NETWORK_TABLE_NAME,
},
};
use sqlx::{query, Postgres, Transaction};

impl Db {
pub async fn create_new_event_change_network(
&self,
tx: &mut Transaction<'_, Postgres>,
event_id: i64,
session_id: &String,
request_id: &String,
old_network: &String,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {EVENT_CHANGE_NETWORK_TABLE_NAME} ({EVENT_CHANGE_NETWORK_KEYS}) VALUES ($1, $2, $3, $4, $5, NULL)"
);

let query_result = query(&query_body)
.bind(event_id)
.bind(session_id)
.bind(request_id)
.bind(RequestStatus::Pending)
.bind(old_network)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}

pub async fn update_event_change_network(
&self,
tx: &mut Transaction<'_, Postgres>,
request_id: &String,
request_status: RequestStatus,
new_network: Option<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_CHANGE_NETWORK_TABLE_NAME} SET request_status = $1, new_network = $2 WHERE request_id = $3",
);

let query_result = query(&query_body)
.bind(request_status)
.bind(new_network)
.bind(request_id)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}
}
2 changes: 2 additions & 0 deletions database/src/tables/events/change_wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod table_struct;
pub mod update;
35 changes: 35 additions & 0 deletions database/src/tables/events/change_wallet/table_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::structs::request_status::RequestStatus;
use sqlx::{postgres::PgRow, FromRow, Row};

pub const EVENT_CHANGE_WALLET_TABLE_NAME: &str = "event_change_wallet";
pub const EVENT_CHANGE_WALLET_KEYS: &str =
"event_id, session_id, request_id, request_status, network, wallet_name, wallet_type, old_wallet_address, new_wallet_address";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ChangeWalletEvent {
pub event_id: i64,
pub session_id: String,
pub request_id: String,
pub request_status: RequestStatus,
pub network: String,
pub wallet_name: String,
pub wallet_type: String,
pub old_wallet_address: String,
pub new_wallet_address: Option<String>,
}

impl FromRow<'_, PgRow> for ChangeWalletEvent {
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> {
Ok(ChangeWalletEvent {
event_id: row.get("event_id"),
session_id: row.get("session_id"),
request_id: row.get("request_id"),
request_status: row.get("request_status"),
network: row.get("network"),
wallet_name: row.get("wallet_name"),
wallet_type: row.get("wallet_type"),
old_wallet_address: row.get("old_wallet_address"),
new_wallet_address: row.get("new_wallet_address"),
})
}
}
68 changes: 68 additions & 0 deletions database/src/tables/events/change_wallet/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::structs::request_status::RequestStatus;
use crate::{
db::Db,
structs::db_error::DbError,
tables::events::change_wallet::table_struct::{
EVENT_CHANGE_WALLET_KEYS, EVENT_CHANGE_WALLET_TABLE_NAME,
},
};
use sqlx::{query, Postgres, Transaction};

impl Db {
pub async fn create_new_event_change_wallet(
&self,
tx: &mut Transaction<'_, Postgres>,
event_id: &i64,
session_id: &String,
request_id: &String,
network_id: &String,
wallet_name: &String,
wallet_type: &String,
old_wallet_address: &String,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {EVENT_CHANGE_WALLET_TABLE_NAME} ({EVENT_CHANGE_WALLET_KEYS}) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NULL)"
);

let query_result = query(&query_body)
.bind(event_id)
.bind(session_id)
.bind(request_id)
.bind(RequestStatus::Pending)
.bind(network_id)
.bind(wallet_name)
.bind(wallet_type)
.bind(old_wallet_address)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}

pub async fn update_event_change_wallet(
&self,
tx: &mut Transaction<'_, Postgres>,
request_id: &String,
request_status: RequestStatus,
new_wallet_address: Option<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_CHANGE_WALLET_TABLE_NAME} SET request_status = $1, new_wallet_address = $2 WHERE request_id = $3",
);

let query_result = query(&query_body)
.bind(request_status)
.bind(new_wallet_address)
.bind(request_id)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}
}
2 changes: 2 additions & 0 deletions database/src/tables/events/client_connect/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod table_struct;
pub mod update;
32 changes: 32 additions & 0 deletions database/src/tables/events/client_connect/table_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use sqlx::{postgres::PgRow, FromRow, Row};

pub const EVENT_CLIENT_CONNECT_TABLE_NAME: &str = "event_client_connect";
pub const EVENT_CLIENT_CONNECT_KEYS: &str =
"event_id, client_id, session_id, addresses, wallet_name, wallet_type, session_type, success";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClientConnectEvent {
pub event_id: String,
pub client_id: String,
pub session_id: String,
pub addresses: Vec<String>,
pub wallet_name: String,
pub wallet_type: String,
pub session_type: String,
pub success: bool,
}

impl FromRow<'_, PgRow> for ClientConnectEvent {
fn from_row(row: &sqlx::postgres::PgRow) -> std::result::Result<Self, sqlx::Error> {
Ok(ClientConnectEvent {
event_id: row.get("event_id"),
client_id: row.get("client_id"),
session_id: row.get("session_id"),
addresses: row.get("addresses"),
wallet_name: row.get("wallet_name"),
wallet_type: row.get("wallet_type"),
session_type: row.get("session_type"),
success: row.get("success"),
})
}
}
66 changes: 66 additions & 0 deletions database/src/tables/events/client_connect/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::{
db::Db,
structs::db_error::DbError,
tables::events::client_connect::table_struct::{
EVENT_CLIENT_CONNECT_KEYS, EVENT_CLIENT_CONNECT_TABLE_NAME,
},
};
use sqlx::{query, Postgres, Transaction};

impl Db {
pub async fn create_new_event_client_connect(
&self,
tx: &mut Transaction<'_, Postgres>,
event_id: i64,
client_id: &String,
session_id: &String,
wallet_name: &String,
wallet_type: &String,
session_type: &String,
) -> Result<(), DbError> {
let query_body = format!(
"INSERT INTO {EVENT_CLIENT_CONNECT_TABLE_NAME} ({EVENT_CLIENT_CONNECT_KEYS}) VALUES ($1, $2, $3, NULL, $4, $5, $6, false)"
);

let query_result = query(&query_body)
.bind(event_id)
.bind(client_id)
.bind(session_id)
.bind(wallet_name)
.bind(wallet_type)
.bind(session_type)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}

pub async fn update_event_client_connect_success(
&self,
tx: &mut Transaction<'_, Postgres>,
client_id: &String,
session_id: &String,
success: bool,
new_addresses: Vec<String>,
) -> Result<(), DbError> {
let query_body = format!(
"UPDATE {EVENT_CLIENT_CONNECT_TABLE_NAME} SET success = $1, addresses = $2 WHERE client_id = $3 AND session_id = $4 AND success = false"
);

let query_result = query(&query_body)
.bind(success)
.bind(&new_addresses)
.bind(client_id)
.bind(session_id)
.execute(&mut **tx)
.await;

match query_result {
Ok(_) => Ok(()),
Err(e) => Err(e).map_err(|e| e.into()),
}
}
}
2 changes: 2 additions & 0 deletions database/src/tables/events/client_disconnect/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod table_struct;
pub mod update;
Loading
Loading