Skip to content

Persist a new request to the data_requests table only if the patient id, project id combination does not exist. #12

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
statuses: read
with:
image-prefix: "samply/"
components: '[ "routine-connector" ]'
components: '[ "transFAIR" ]'
architectures: '[ "amd64" ]'
test-via-script: true
push-to: ${{ (github.ref_protected == true || github.event_name == 'workflow_dispatch') && 'dockerhub' || 'none' }}
Expand Down

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

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

This file was deleted.

This file was deleted.

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

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

This file was deleted.

6 changes: 3 additions & 3 deletions dev/test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function start_bg() {

# Test Config Setup
export INSTITUTE_TTP_URL="http://localhost:8081";
export INSTITUTE_TTP_API_KEY="routine-connector-password";
export INSTITUTE_TTP_API_KEY="transFAIR-password";
export PROJECT_ID_SYSTEM="PROJECT_1_ID"
export FHIR_REQUEST_URL="http://localhost:8085"
export FHIR_INPUT_URL="http://localhost:8086"
Expand Down Expand Up @@ -38,8 +38,8 @@ function start_bg() {
fi
done
done
chmod +x artifacts/binaries-amd64/routine-connector
artifacts/binaries-amd64/routine-connector &
chmod +x artifacts/binaries-amd64/transFAIR
artifacts/binaries-amd64/transFAIR &
sleep 10
}

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
ML_DB_USER: mainzelliste
ML_DB_HOST: mainzelliste-db
ML_DB_PASS: "${ML_DB_PASS:-my-secret-db-password}"
ML_ROUTINE_CONNECTOR_PASSPHRASE: "${ML_ROUTINE_CONNECTOR_PASSPHRASE:-routine-connector-password}"
ML_ROUTINE_CONNECTOR_PASSPHRASE: "${ML_ROUTINE_CONNECTOR_PASSPHRASE:-transFAIR-password}"
ML_DIZ_PASSPHRASE: "${ML_DIZ_PASSPHRASE:-diz-password}"
ML_LOG_LEVEL: "${ML_LOG_LEVEL:-info}"
configs:
Expand Down
6 changes: 4 additions & 2 deletions migrations/20240614130457_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ VALUES ('Created', 1),
('Error', 4);

CREATE TABLE IF NOT EXISTS data_requests (
id CHAR(36) PRIMARY KEY NOT NULL,
status CHAR(16) NOT NULL DEFAULT ('Created') REFERENCES request_status(Type)
id CHAR(36) PRIMARY KEY NOT NULL,
exchange_id CHAR(36) NOT NULL,
project_id CHAR(36) NULL,
status CHAR(16) NOT NULL DEFAULT ('Created') REFERENCES request_status(Type)
)
16 changes: 10 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ pub struct Config {
// Definition of the fhir server and credentials used for communicating data requests to the dic
#[clap(long, env)]
pub fhir_request_url: Url,
#[clap(long, env)]
pub fhir_request_credentials: Option<Auth>,
#[clap(long, env, default_value = "")]
pub fhir_request_credentials: Auth,
// Definition of the fhir server and credentials used for reading data from the dic
#[clap(long, env)]
pub fhir_input_url: Url,
#[clap(long, env)]
pub fhir_input_credentials: Option<Auth>,
#[clap(long, env, default_value = "")]
pub fhir_input_credentials: Auth,
// Definition of the fhir server and credentials used for adding data to the project data
#[clap(long, env)]
pub fhir_output_url: Url,
#[clap(long, env)]
pub fhir_output_credentials: Option<Auth>,
#[clap(long, env, default_value = "")]
pub fhir_output_credentials: Auth,
}

#[derive(Debug, Clone)]
pub enum Auth {
None,
Basic {
user: String,
pw: String,
Expand All @@ -46,6 +47,9 @@ impl FromStr for Auth {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Ok(Self::None);
}
let (user, pw) = s.split_once(":").ok_or("Credentials should be in the form of '<user>:<pw>'")?;
Ok(Self::Basic { user: user.to_owned(), pw: pw.to_owned() })
}
Expand Down
84 changes: 84 additions & 0 deletions src/data_access/data_requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use super::models::DataRequest;
use sqlx::SqlitePool;
use tracing::debug;

pub async fn get_all(db_pool: &SqlitePool) -> Result<Vec<DataRequest>, sqlx::Error> {
let data_request = sqlx::query_as!(
DataRequest,
r#"SELECT id, exchange_id, project_id, status as "status: _" FROM data_requests;"#,
)
.fetch_all(db_pool)
.await;

data_request
}

pub async fn get_by_id(db_pool: &SqlitePool, id: &str) -> Result<Option<DataRequest>, sqlx::Error> {
let data_request = sqlx::query_as!(
DataRequest,
r#"SELECT id, exchange_id, project_id, status as "status: _" FROM data_requests WHERE id = $1;"#,
id
)
.fetch_optional(db_pool)
.await;

data_request
}

pub async fn get_by(
db_pool: &SqlitePool,
exchange_id: &str,
project_id: Option<&str>,
) -> Result<Option<DataRequest>, sqlx::Error> {
let data_request = if project_id.is_some() {
sqlx::query_as!(
DataRequest,
r#"SELECT id, exchange_id, project_id, status as "status: _" FROM data_requests WHERE exchange_id = $1 AND project_id = $2;"#,
exchange_id, project_id
)
.fetch_optional(db_pool)
.await
} else {
sqlx::query_as!(
DataRequest,
r#"SELECT id, exchange_id, project_id, status as "status: _" FROM data_requests WHERE exchange_id = $1;"#,
exchange_id
)
.fetch_optional(db_pool)
.await
};

debug!("exchange_id: {exchange_id}, project id: {}, data request: {:?}", project_id.unwrap_or(""), data_request.is_err());
data_request
}

pub async fn exists(db_pool: &SqlitePool, exchange_id: &str, project_id: Option<&str>) -> bool {
let data_request = get_by(db_pool, exchange_id, project_id).await.unwrap_or(None);
data_request.is_some()
}

pub async fn insert(db_pool: &SqlitePool, data_request: &DataRequest) -> Result<i64, sqlx::Error> {
let query = if data_request.project_id.is_some() {
sqlx::query!(
"INSERT INTO data_requests (id, exchange_id, project_id, status) VALUES ($1, $2, $3, $4)",
data_request.id,
data_request.exchange_id,
data_request.project_id,
data_request.status
)
} else {
sqlx::query!(
"INSERT INTO data_requests (id, exchange_id, status) VALUES ($1, $2, $3)",
data_request.id,
data_request.exchange_id,
data_request.status
)
};

let insert_query_result = query
.execute(db_pool)
.await
.map(|qr| qr.last_insert_rowid());

insert_query_result
}
2 changes: 2 additions & 0 deletions src/data_access/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod data_requests;
pub mod models;
Loading
Loading