-
Notifications
You must be signed in to change notification settings - Fork 16
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
6204 name insurance join #6395
Merged
+523
β7
Merged
6204 name insurance join #6395
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2f138ee
WIP name_insurance_join
jmbrunskill 1474fed
name_link_id
jmbrunskill f76a920
Merge branch '6203-sync-and-translate-for-insuranceprovider-table' inβ¦
jmbrunskill 59daed1
sync translations
jmbrunskill abff4d8
Add test push records
jmbrunskill 5563b00
Fix tests
jmbrunskill 87c2bec
Remove debug statement
jmbrunskill 7659b77
Fix postgres ordering
jmbrunskill e047b05
Merge branch 'develop' into 6204-name-insurance-join
jmbrunskill 6ef0d08
Refresh dates
jmbrunskill ae154ee
Merge branch 'develop' into 6204-name-insurance-join
jmbrunskill 4e02630
Fix Postgres type
jmbrunskill 2a8b6cb
Decimal percentages in nameInsuranceJoin
jmbrunskill 33cc234
Merge branch 'develop' into 6204-name-insurance-join
jmbrunskill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
server/repository/src/db_diesel/name_insurance_join_row.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use super::{ | ||
ChangeLogInsertRow, ChangelogRepository, ChangelogTableName, RowActionType, StorageConnection, | ||
}; | ||
|
||
use crate::{repository_error::RepositoryError, Upsert}; | ||
use diesel::prelude::*; | ||
use diesel_derive_enum::DbEnum; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(DbEnum, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] | ||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] | ||
#[DbValueStyle = "SCREAMING_SNAKE_CASE"] | ||
pub enum InsurancePolicyType { | ||
#[default] | ||
Personal, | ||
Business, | ||
} | ||
|
||
table! { | ||
name_insurance_join (id) { | ||
id -> Text, | ||
name_link_id -> Text, | ||
insurance_provider_id -> Text, | ||
policy_number_person -> Nullable<Text>, | ||
policy_number_family -> Nullable<Text>, | ||
policy_number -> Text, | ||
policy_type -> crate::db_diesel::name_insurance_join_row::InsurancePolicyTypeMapping, | ||
discount_percentage -> Double, | ||
expiry_date -> Date, | ||
is_active -> Bool, | ||
entered_by_id -> Nullable<Text>, | ||
} | ||
} | ||
|
||
#[derive( | ||
Clone, Insertable, Queryable, Debug, PartialEq, AsChangeset, Default, Serialize, Deserialize, | ||
)] | ||
#[diesel(table_name = name_insurance_join)] | ||
pub struct NameInsuranceJoinRow { | ||
pub id: String, | ||
pub name_link_id: String, | ||
pub insurance_provider_id: String, | ||
pub policy_number_person: Option<String>, | ||
pub policy_number_family: Option<String>, | ||
pub policy_number: String, | ||
pub policy_type: InsurancePolicyType, | ||
pub discount_percentage: f64, | ||
pub expiry_date: chrono::NaiveDate, | ||
pub is_active: bool, | ||
pub entered_by_id: Option<String>, | ||
} | ||
|
||
pub struct NameInsuranceJoinRowRepository<'a> { | ||
connection: &'a StorageConnection, | ||
} | ||
|
||
impl<'a> NameInsuranceJoinRowRepository<'a> { | ||
pub fn new(connection: &'a StorageConnection) -> Self { | ||
NameInsuranceJoinRowRepository { connection } | ||
} | ||
|
||
pub fn find_one_by_id( | ||
&self, | ||
id: &str, | ||
) -> Result<Option<NameInsuranceJoinRow>, RepositoryError> { | ||
let result = name_insurance_join::table | ||
.filter(name_insurance_join::id.eq(id)) | ||
.first(self.connection.lock().connection()) | ||
.optional()?; | ||
Ok(result) | ||
} | ||
|
||
pub fn upsert_one(&self, row: &NameInsuranceJoinRow) -> Result<i64, RepositoryError> { | ||
diesel::insert_into(name_insurance_join::table) | ||
.values(row) | ||
.on_conflict(name_insurance_join::id) | ||
.do_update() | ||
.set(row) | ||
.execute(self.connection.lock().connection())?; | ||
self.insert_changelog(&row.id, RowActionType::Upsert) | ||
} | ||
|
||
fn insert_changelog(&self, uid: &str, action: RowActionType) -> Result<i64, RepositoryError> { | ||
let row = ChangeLogInsertRow { | ||
table_name: ChangelogTableName::NameInsuranceJoin, | ||
record_id: uid.to_string(), | ||
row_action: action, | ||
store_id: None, | ||
name_link_id: None, | ||
}; | ||
|
||
ChangelogRepository::new(self.connection).insert(&row) | ||
} | ||
} | ||
|
||
impl Upsert for NameInsuranceJoinRow { | ||
fn upsert(&self, con: &StorageConnection) -> Result<Option<i64>, RepositoryError> { | ||
let change_log = NameInsuranceJoinRowRepository::new(con).upsert_one(self)?; | ||
Ok(Some(change_log)) | ||
} | ||
|
||
// Test only | ||
fn assert_upserted(&self, con: &StorageConnection) { | ||
assert_eq!( | ||
NameInsuranceJoinRowRepository::new(con).find_one_by_id(&self.id), | ||
Ok(Some(self.clone())) | ||
) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
server/repository/src/migrations/v2_06_00/add_name_insurance_join.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::migrations::*; | ||
|
||
pub(crate) struct Migrate; | ||
|
||
impl MigrationFragment for Migrate { | ||
fn identifier(&self) -> &'static str { | ||
"add_name_insurance_join" | ||
} | ||
|
||
fn migrate(&self, connection: &StorageConnection) -> anyhow::Result<()> { | ||
let policy_type = if cfg!(feature = "postgres") { | ||
"insurance_policy_type" | ||
} else { | ||
"TEXT" | ||
}; | ||
|
||
if cfg!(feature = "postgres") { | ||
// Postgres changelog variant | ||
sql!( | ||
connection, | ||
r#" | ||
ALTER TYPE changelog_table_name ADD VALUE IF NOT EXISTS 'name_insurance_join'; | ||
"# | ||
)?; | ||
|
||
// Postgres enum variant for policy_type | ||
sql!( | ||
connection, | ||
r#" | ||
CREATE TYPE insurance_policy_type AS ENUM ('PERSONAL', 'BUSINESS'); | ||
"# | ||
)?; | ||
} | ||
|
||
sql!( | ||
connection, | ||
r#" | ||
CREATE TABLE name_insurance_join ( | ||
id TEXT NOT NULL PRIMARY KEY, | ||
name_link_id TEXT NOT NULL REFERENCES name_link(id), | ||
insurance_provider_id TEXT NOT NULL REFERENCES insurance_provider(id), | ||
policy_number_person TEXT, | ||
policy_number_family TEXT, | ||
policy_number TEXT NOT NULL, | ||
policy_type {policy_type} NOT NULL, | ||
discount_percentage {DOUBLE} NOT NULL, | ||
expiry_date DATE NOT NULL, | ||
is_active BOOLEAN NOT NULL, | ||
entered_by_id TEXT | ||
); | ||
"# | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
server/service/src/sync/test/test_data/name_insurance_join.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use repository::name_insurance_join_row::{InsurancePolicyType, NameInsuranceJoinRow}; | ||
|
||
use crate::sync::{ | ||
test::{TestSyncIncomingRecord, TestSyncOutgoingRecord}, | ||
translations::name_insurance_join::{LegacyInsurancePolicyType, LegacyNameInsuranceJoinRow}, | ||
}; | ||
use serde_json::json; | ||
|
||
const TABLE_NAME: &str = "nameInsuranceJoin"; | ||
|
||
const NAME_INSURANCE_JOIN_1: (&str, &str) = ( | ||
"NAME_INSURANCE_JOIN_1_ID", | ||
r#"{ | ||
"ID": "NAME_INSURANCE_JOIN_1_ID", | ||
"discountRate": 30, | ||
"enteredByID": "", | ||
"expiryDate": "2026-01-23", | ||
"insuranceProviderID": "INSURANCE_PROVIDER_1_ID", | ||
"isActive": true, | ||
"nameID": "1FB32324AF8049248D929CFB35F255BA", | ||
"policyNumberFamily": "888", | ||
"policyNumberFull": "888", | ||
"policyNumberPerson": "", | ||
"type": "personal" | ||
}"#, | ||
); | ||
|
||
fn name_insurance_join_1() -> TestSyncIncomingRecord { | ||
TestSyncIncomingRecord::new_pull_upsert( | ||
TABLE_NAME, | ||
NAME_INSURANCE_JOIN_1, | ||
NameInsuranceJoinRow { | ||
id: NAME_INSURANCE_JOIN_1.0.to_owned(), | ||
name_link_id: "1FB32324AF8049248D929CFB35F255BA".to_owned(), | ||
insurance_provider_id: "INSURANCE_PROVIDER_1_ID".to_owned(), | ||
policy_number_person: None, | ||
policy_number_family: Some("888".to_owned()), | ||
policy_number: "888".to_owned(), | ||
policy_type: InsurancePolicyType::Personal, | ||
discount_percentage: 30.0, | ||
expiry_date: "2026-01-23".parse().unwrap(), | ||
is_active: true, | ||
entered_by_id: None, | ||
}, | ||
) | ||
} | ||
|
||
const NAME_INSURANCE_JOIN_2: (&str, &str) = ( | ||
"NAME_INSURANCE_JOIN_2_ID", | ||
r#"{ | ||
"ID": "NAME_INSURANCE_JOIN_2_ID", | ||
"discountRate": 20.5, | ||
"enteredByID": "", | ||
"expiryDate": "2027-01-01", | ||
"insuranceProviderID": "INSURANCE_PROVIDER_1_ID", | ||
"isActive": true, | ||
"nameID": "1FB32324AF8049248D929CFB35F255BA", | ||
"policyNumberFamily": "", | ||
"policyNumberFull": "777", | ||
"policyNumberPerson": "777", | ||
"type": "business" | ||
}"#, | ||
); | ||
|
||
fn name_insurance_join_2() -> TestSyncIncomingRecord { | ||
TestSyncIncomingRecord::new_pull_upsert( | ||
TABLE_NAME, | ||
NAME_INSURANCE_JOIN_2, | ||
NameInsuranceJoinRow { | ||
id: NAME_INSURANCE_JOIN_2.0.to_owned(), | ||
name_link_id: "1FB32324AF8049248D929CFB35F255BA".to_owned(), | ||
insurance_provider_id: "INSURANCE_PROVIDER_1_ID".to_owned(), | ||
policy_number_person: Some("777".to_owned()), | ||
policy_number_family: None, | ||
policy_number: "777".to_owned(), | ||
policy_type: InsurancePolicyType::Business, | ||
discount_percentage: 20.5, | ||
expiry_date: "2027-01-01".parse().unwrap(), | ||
is_active: true, | ||
entered_by_id: None, | ||
}, | ||
) | ||
} | ||
|
||
pub(crate) fn test_pull_upsert_records() -> Vec<TestSyncIncomingRecord> { | ||
vec![name_insurance_join_1(), name_insurance_join_2()] | ||
} | ||
|
||
pub(crate) fn test_push_records() -> Vec<TestSyncOutgoingRecord> { | ||
vec![ | ||
TestSyncOutgoingRecord { | ||
record_id: NAME_INSURANCE_JOIN_1.0.to_string(), | ||
table_name: TABLE_NAME.to_string(), | ||
push_data: json!(LegacyNameInsuranceJoinRow { | ||
ID: NAME_INSURANCE_JOIN_1.0.to_string(), | ||
nameID: "1FB32324AF8049248D929CFB35F255BA".to_string(), | ||
insuranceProviderID: "INSURANCE_PROVIDER_1_ID".to_string(), | ||
discountRate: 30.0, | ||
enteredByID: None, | ||
expiryDate: "2026-01-23".parse().unwrap(), | ||
isActive: true, | ||
policyNumberFamily: Some("888".to_string()), | ||
policyNumberFull: "888".to_string(), | ||
policyNumberPerson: None, | ||
policyType: LegacyInsurancePolicyType::Personal, | ||
}), | ||
}, | ||
TestSyncOutgoingRecord { | ||
record_id: NAME_INSURANCE_JOIN_2.0.to_string(), | ||
table_name: TABLE_NAME.to_string(), | ||
push_data: json!(LegacyNameInsuranceJoinRow { | ||
ID: NAME_INSURANCE_JOIN_2.0.to_string(), | ||
nameID: "1FB32324AF8049248D929CFB35F255BA".to_string(), | ||
insuranceProviderID: "INSURANCE_PROVIDER_1_ID".to_string(), | ||
discountRate: 20.5, | ||
enteredByID: None, | ||
expiryDate: "2027-01-01".parse().unwrap(), | ||
isActive: true, | ||
policyNumberFamily: None, | ||
policyNumberFull: "777".to_string(), | ||
policyNumberPerson: Some("777".to_string()), | ||
policyType: LegacyInsurancePolicyType::Business, | ||
}), | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, probably want to make "person" and "family" just boolean flags in OMS, and just translate them to OG?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've checked in mSupply, it looks like if both fields are filled in, they're combined in to the
full
field.E.g. 123-321
So I think we probably want to keep it how it is...