Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 777fc8bbf8 | |||
| 803d62b2fb | |||
| 8845dcdef2 | |||
| 40e08cbf09 | |||
| c7372891d8 |
@ -146,6 +146,9 @@
|
||||
"modified_at": {
|
||||
"type": "string",
|
||||
"format": "date-time"
|
||||
},
|
||||
"organization_id": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
@ -168,7 +171,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
],
|
||||
"grouped_fields": {
|
||||
"entity": [
|
||||
@ -178,7 +182,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
]
|
||||
},
|
||||
"lookup_fields": [],
|
||||
@ -345,6 +350,10 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"organization_id": {
|
||||
"type": "string",
|
||||
"const": "ffffffff-ffff-ffff-ffff-ffffffffffff"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -368,7 +377,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
],
|
||||
"grouped_fields": {
|
||||
"person": [
|
||||
@ -396,7 +406,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
]
|
||||
},
|
||||
"lookup_fields": [
|
||||
@ -446,7 +457,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
],
|
||||
"grouped_fields": {
|
||||
"order": [
|
||||
@ -462,7 +474,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
]
|
||||
},
|
||||
"lookup_fields": [
|
||||
@ -504,7 +517,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
],
|
||||
"grouped_fields": {
|
||||
"order_line": [
|
||||
@ -521,7 +535,8 @@
|
||||
"created_by",
|
||||
"modified_at",
|
||||
"modified_by",
|
||||
"archived"
|
||||
"archived",
|
||||
"organization_id"
|
||||
]
|
||||
},
|
||||
"lookup_fields": [],
|
||||
@ -3126,10 +3141,26 @@
|
||||
"type": "invoice",
|
||||
"number": "INV-1001",
|
||||
"total": 200.0,
|
||||
"metadata_line": {"price": 50},
|
||||
"metadata_lines": [{"price": 25}],
|
||||
"metadata_nested_line": {"line": {"price": 75}},
|
||||
"metadata_nested_lines": {"lines": [{"price": 100}]}
|
||||
"metadata_line": {
|
||||
"price": 50
|
||||
},
|
||||
"metadata_lines": [
|
||||
{
|
||||
"price": 25
|
||||
}
|
||||
],
|
||||
"metadata_nested_line": {
|
||||
"line": {
|
||||
"price": 75
|
||||
}
|
||||
},
|
||||
"metadata_nested_lines": {
|
||||
"lines": [
|
||||
{
|
||||
"price": 100
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"expect": {
|
||||
"success": true,
|
||||
@ -3304,6 +3335,354 @@
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Test organization_id syntactic sugar permutations",
|
||||
"action": "merge",
|
||||
"data": {
|
||||
"type": "order",
|
||||
"organization_id": "parent-org-id",
|
||||
"customer": {
|
||||
"type": "person",
|
||||
"first_name": "Const",
|
||||
"last_name": "Person"
|
||||
},
|
||||
"lines": [
|
||||
{
|
||||
"type": "order_line"
|
||||
},
|
||||
{
|
||||
"type": "order_line",
|
||||
"organization_id": "explicit-org-id"
|
||||
}
|
||||
]
|
||||
},
|
||||
"schema_id": "order",
|
||||
"expect": {
|
||||
"success": true,
|
||||
"sql": [
|
||||
[
|
||||
"INSERT INTO agreego.\"entity\" (",
|
||||
" \"created_at\",",
|
||||
" \"created_by\",",
|
||||
" \"id\",",
|
||||
" \"modified_at\",",
|
||||
" \"modified_by\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" '{{uuid:person_id}}',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'person'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"organization\" (",
|
||||
" \"id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{uuid:person_id}}',",
|
||||
" 'person'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"user\" (",
|
||||
" \"id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{uuid:person_id}}',",
|
||||
" 'person'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"person\" (",
|
||||
" \"first_name\",",
|
||||
" \"id\",",
|
||||
" \"last_name\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" 'Const',",
|
||||
" '{{uuid:person_id}}',",
|
||||
" 'Person',",
|
||||
" 'person'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.change (",
|
||||
" \"old\",",
|
||||
" \"new\",",
|
||||
" entity_id,",
|
||||
" id,",
|
||||
" kind,",
|
||||
" modified_at,",
|
||||
" modified_by",
|
||||
")",
|
||||
"VALUES (",
|
||||
" NULL,",
|
||||
" '{",
|
||||
" \"first_name\":\"Const\",",
|
||||
" \"last_name\":\"Person\",",
|
||||
" \"type\":\"person\"",
|
||||
" }',",
|
||||
" '{{uuid:person_id}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'create',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"entity\" (",
|
||||
" \"created_at\",",
|
||||
" \"created_by\",",
|
||||
" \"id\",",
|
||||
" \"modified_at\",",
|
||||
" \"modified_by\",",
|
||||
" \"organization_id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" '{{uuid:order_id}}',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'parent-org-id',",
|
||||
" 'order'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"order\" (",
|
||||
" \"customer_id\",",
|
||||
" \"id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{uuid:person_id}}',",
|
||||
" '{{uuid:order_id}}',",
|
||||
" 'order'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"entity\" (",
|
||||
" \"created_at\",",
|
||||
" \"created_by\",",
|
||||
" \"id\",",
|
||||
" \"modified_at\",",
|
||||
" \"modified_by\",",
|
||||
" \"organization_id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" '{{uuid:line1_id}}',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'parent-org-id',",
|
||||
" 'order_line'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"order_line\" (",
|
||||
" \"id\",",
|
||||
" \"order_id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{uuid:line1_id}}',",
|
||||
" '{{uuid:order_id}}',",
|
||||
" 'order_line'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.change (",
|
||||
" \"old\",",
|
||||
" \"new\",",
|
||||
" entity_id,",
|
||||
" id,",
|
||||
" kind,",
|
||||
" modified_at,",
|
||||
" modified_by",
|
||||
")",
|
||||
"VALUES (",
|
||||
" NULL,",
|
||||
" '{",
|
||||
" \"order_id\":\"{{uuid:order_id}}\",",
|
||||
" \"organization_id\":\"parent-org-id\",",
|
||||
" \"type\":\"order_line\"",
|
||||
" }',",
|
||||
" '{{uuid:line1_id}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'create',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"entity\" (",
|
||||
" \"created_at\",",
|
||||
" \"created_by\",",
|
||||
" \"id\",",
|
||||
" \"modified_at\",",
|
||||
" \"modified_by\",",
|
||||
" \"organization_id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" '{{uuid:line2_id}}',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'explicit-org-id',",
|
||||
" 'order_line'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.\"order_line\" (",
|
||||
" \"id\",",
|
||||
" \"order_id\",",
|
||||
" \"type\"",
|
||||
")",
|
||||
"VALUES (",
|
||||
" '{{uuid:line2_id}}',",
|
||||
" '{{uuid:order_id}}',",
|
||||
" 'order_line'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.change (",
|
||||
" \"old\",",
|
||||
" \"new\",",
|
||||
" entity_id,",
|
||||
" id,",
|
||||
" kind,",
|
||||
" modified_at,",
|
||||
" modified_by",
|
||||
")",
|
||||
"VALUES (",
|
||||
" NULL,",
|
||||
" '{",
|
||||
" \"order_id\":\"{{uuid:order_id}}\",",
|
||||
" \"organization_id\":\"explicit-org-id\",",
|
||||
" \"type\":\"order_line\"",
|
||||
" }',",
|
||||
" '{{uuid:line2_id}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'create',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"INSERT INTO agreego.change (",
|
||||
" \"old\",",
|
||||
" \"new\",",
|
||||
" entity_id,",
|
||||
" id,",
|
||||
" kind,",
|
||||
" modified_at,",
|
||||
" modified_by",
|
||||
")",
|
||||
"VALUES (",
|
||||
" NULL,",
|
||||
" '{",
|
||||
" \"customer_id\":\"{{uuid:person_id}}\",",
|
||||
" \"organization_id\":\"parent-org-id\",",
|
||||
" \"type\":\"order\"",
|
||||
" }',",
|
||||
" '{{uuid:order_id}}',",
|
||||
" '{{uuid}}',",
|
||||
" 'create',",
|
||||
" '{{timestamp}}',",
|
||||
" '{{uuid}}'",
|
||||
")"
|
||||
],
|
||||
[
|
||||
"SELECT pg_notify('entity', '{",
|
||||
" \"complete\":{",
|
||||
" \"created_at\":\"{{timestamp}}\",",
|
||||
" \"created_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"customer_id\":\"{{uuid:person_id}}\",",
|
||||
" \"id\":\"{{uuid:order_id}}\",",
|
||||
" \"modified_at\":\"{{timestamp}}\",",
|
||||
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"organization_id\":\"parent-org-id\",",
|
||||
" \"type\":\"order\"",
|
||||
" },",
|
||||
" \"new\":{",
|
||||
" \"customer_id\":\"{{uuid:person_id}}\",",
|
||||
" \"organization_id\":\"parent-org-id\",",
|
||||
" \"type\":\"order\"",
|
||||
" }",
|
||||
" }')"
|
||||
],
|
||||
[
|
||||
"SELECT pg_notify('entity', '{",
|
||||
" \"complete\":{",
|
||||
" \"created_at\":\"{{timestamp}}\",",
|
||||
" \"created_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"first_name\":\"Const\",",
|
||||
" \"id\":\"{{uuid:person_id}}\",",
|
||||
" \"last_name\":\"Person\",",
|
||||
" \"modified_at\":\"{{timestamp}}\",",
|
||||
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"type\":\"person\"",
|
||||
" },",
|
||||
" \"new\":{",
|
||||
" \"first_name\":\"Const\",",
|
||||
" \"last_name\":\"Person\",",
|
||||
" \"type\":\"person\"",
|
||||
" }",
|
||||
" }')"
|
||||
],
|
||||
[
|
||||
"SELECT pg_notify('entity', '{",
|
||||
" \"complete\":{",
|
||||
" \"created_at\":\"{{timestamp}}\",",
|
||||
" \"created_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"id\":\"{{uuid:line1_id}}\",",
|
||||
" \"modified_at\":\"{{timestamp}}\",",
|
||||
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"order_id\":\"{{uuid:order_id}}\",",
|
||||
" \"organization_id\":\"parent-org-id\",",
|
||||
" \"type\":\"order_line\"",
|
||||
" },",
|
||||
" \"new\":{",
|
||||
" \"order_id\":\"{{uuid:order_id}}\",",
|
||||
" \"organization_id\":\"parent-org-id\",",
|
||||
" \"type\":\"order_line\"",
|
||||
" }",
|
||||
" }')"
|
||||
],
|
||||
[
|
||||
"SELECT pg_notify('entity', '{",
|
||||
" \"complete\":{",
|
||||
" \"created_at\":\"{{timestamp}}\",",
|
||||
" \"created_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"id\":\"{{uuid:line2_id}}\",",
|
||||
" \"modified_at\":\"{{timestamp}}\",",
|
||||
" \"modified_by\":\"00000000-0000-0000-0000-000000000000\",",
|
||||
" \"order_id\":\"{{uuid:order_id}}\",",
|
||||
" \"organization_id\":\"explicit-org-id\",",
|
||||
" \"type\":\"order_line\"",
|
||||
" },",
|
||||
" \"new\":{",
|
||||
" \"order_id\":\"{{uuid:order_id}}\",",
|
||||
" \"organization_id\":\"explicit-org-id\",",
|
||||
" \"type\":\"order_line\"",
|
||||
" }",
|
||||
" }')"
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -129,6 +129,25 @@ impl Merger {
|
||||
crate::drop::Drop::success_with_val(stripped_val)
|
||||
}
|
||||
|
||||
fn inject_organization_id(
|
||||
relative: &mut serde_json::Map<String, Value>,
|
||||
entity_fields: &serde_json::Map<String, Value>,
|
||||
schema: &Arc<crate::database::schema::Schema>,
|
||||
) {
|
||||
if !relative.contains_key("organization_id") {
|
||||
if let Some(org_id) = entity_fields.get("organization_id") {
|
||||
if let Some(compiled_props) = schema.obj.compiled_properties.get() {
|
||||
if let Some(org_schema) = compiled_props.get("organization_id") {
|
||||
if org_schema.obj.const_.is_some() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
relative.insert("organization_id".to_string(), org_id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn merge_internal(
|
||||
&self,
|
||||
mut schema: Arc<crate::database::schema::Schema>,
|
||||
@ -313,11 +332,7 @@ impl Merger {
|
||||
let parent_is_source = edge.forward;
|
||||
|
||||
if parent_is_source {
|
||||
if !relative.contains_key("organization_id") {
|
||||
if let Some(org_id) = entity_fields.get("organization_id") {
|
||||
relative.insert("organization_id".to_string(), org_id.clone());
|
||||
}
|
||||
}
|
||||
Self::inject_organization_id(&mut relative, &entity_fields, &rel_schema);
|
||||
|
||||
let mut merged_relative = match self.merge_internal(
|
||||
rel_schema.clone(),
|
||||
@ -338,11 +353,7 @@ impl Merger {
|
||||
);
|
||||
entity_response.insert(relation_name, Value::Object(merged_relative));
|
||||
} else {
|
||||
if !relative.contains_key("organization_id") {
|
||||
if let Some(org_id) = entity_fields.get("organization_id") {
|
||||
relative.insert("organization_id".to_string(), org_id.clone());
|
||||
}
|
||||
}
|
||||
Self::inject_organization_id(&mut relative, &entity_fields, &rel_schema);
|
||||
|
||||
Self::apply_entity_relation(
|
||||
&mut relative,
|
||||
@ -401,22 +412,6 @@ impl Merger {
|
||||
if let Some(compiled_edges) = schema.obj.compiled_edges.get() {
|
||||
if let Some(edge) = compiled_edges.get(&relation_name) {
|
||||
if let Some(relation) = self.db.relations.get(&edge.constraint) {
|
||||
let mut relative_responses = Vec::new();
|
||||
for relative_item_val in relative_arr {
|
||||
if let Value::Object(mut relative_item) = relative_item_val {
|
||||
if !relative_item.contains_key("organization_id") {
|
||||
if let Some(org_id) = entity_fields.get("organization_id") {
|
||||
relative_item.insert("organization_id".to_string(), org_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Self::apply_entity_relation(
|
||||
&mut relative_item,
|
||||
&relation.source_columns,
|
||||
&relation.destination_columns,
|
||||
&entity_fields,
|
||||
);
|
||||
|
||||
let mut item_schema = rel_schema.clone();
|
||||
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) =
|
||||
&rel_schema.obj.type_
|
||||
@ -428,8 +423,20 @@ impl Merger {
|
||||
}
|
||||
}
|
||||
|
||||
let mut relative_responses = Vec::new();
|
||||
for relative_item_val in relative_arr {
|
||||
if let Value::Object(mut relative_item) = relative_item_val {
|
||||
Self::inject_organization_id(&mut relative_item, &entity_fields, &item_schema);
|
||||
|
||||
Self::apply_entity_relation(
|
||||
&mut relative_item,
|
||||
&relation.source_columns,
|
||||
&relation.destination_columns,
|
||||
&entity_fields,
|
||||
);
|
||||
|
||||
let merged_relative = match self.merge_internal(
|
||||
item_schema,
|
||||
item_schema.clone(),
|
||||
Value::Object(relative_item),
|
||||
notifications,
|
||||
)? {
|
||||
|
||||
@ -8188,3 +8188,9 @@ fn test_merger_0_14() {
|
||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 14).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_merger_0_15() {
|
||||
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
|
||||
crate::tests::runner::run_test_case(&path, 0, 15).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user