order = %{
"_csrf_token" => "gwsgw",
"custom_tags" => "",
"id" => "62212",
"products" => %{
"123456" => %{
"category" => "3",
"tag" => ["hero", "superstar"],
"title" => "Product title"
},
"234567" => %{"category" => "5", "title" => "Product title"},
"345678" => %{
"category" => "8",
"tag" => ["creation", "dynasty"],
"title" => "Product title"
},
"456789" => %{
"category" => "5",
"title" => "Product title"
}
},
"order_id" => "193885",
"custom_message" => "Nothing to declare",
"tags_title" => %{"tag" => ["preview"]},
"allcats" => "5"
}
%{
"_csrf_token" => "gwsgw",
"allcats" => "5",
"custom_message" => "Nothing to declare",
"custom_tags" => "",
"id" => "62212",
"order_id" => "193885",
"products" => %{
"123456" => %{"category" => "3", "tag" => ["hero", "superstar"], "title" => "Product title"},
"234567" => %{"category" => "5", "title" => "Product title"},
"345678" => %{"category" => "8", "tag" => ["creation", "dynasty"], "title" => "Product title"},
"456789" => %{"category" => "5", "title" => "Product title"}
},
"tags_title" => %{"tag" => ["preview"]}
}
associated_products = %{
"123456" => [
%{
"id" => "4984944",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "123456"
},
%{
"id" => "3511064",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "123456"
}
],
"234567" => [
%{
"id" => "3511075",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "234567"
},
%{
"id" => "3511076",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "234567"
}
],
"345678" => [
%{
"id" => "3511082",
"has_stock" => true,
"name" => "Other product Name",
"product_id" => "345678"
},
%{
"id" => "3511083",
"has_stock" => false,
"name" => "Other product Name",
"product_id" => "345678"
}
]
}
%{
"123456" => [
%{
"has_stock" => false,
"id" => "4984944",
"name" => "Other product Name",
"product_id" => "123456"
},
%{
"has_stock" => true,
"id" => "3511064",
"name" => "Other product Name",
"product_id" => "123456"
}
],
"234567" => [
%{
"has_stock" => true,
"id" => "3511075",
"name" => "Other product Name",
"product_id" => "234567"
},
%{
"has_stock" => false,
"id" => "3511076",
"name" => "Other product Name",
"product_id" => "234567"
}
],
"345678" => [
%{
"has_stock" => true,
"id" => "3511082",
"name" => "Other product Name",
"product_id" => "345678"
},
%{
"has_stock" => false,
"id" => "3511083",
"name" => "Other product Name",
"product_id" => "345678"
}
]
}
Enum.reduce(order["products"], order, fn {product_id, _product}, order ->
associated =
associated_products
|> Map.values()
|> List.flatten()
|> Enum.filter(&(&1["product_id"] == product_id))
update_in(order, ["products", product_id], fn product ->
Map.put(product, "associated_products", associated)
end)
end)
%{
"_csrf_token" => "gwsgw",
"allcats" => "5",
"custom_message" => "Nothing to declare",
"custom_tags" => "",
"id" => "62212",
"order_id" => "193885",
"products" => %{
"123456" => %{
"associated_products" => [
%{
"has_stock" => false,
"id" => "4984944",
"name" => "Other product Name",
"product_id" => "123456"
},
%{
"has_stock" => true,
"id" => "3511064",
"name" => "Other product Name",
"product_id" => "123456"
}
],
"category" => "3",
"tag" => ["hero", "superstar"],
"title" => "Product title"
},
"234567" => %{
"associated_products" => [
%{
"has_stock" => true,
"id" => "3511075",
"name" => "Other product Name",
"product_id" => "234567"
},
%{
"has_stock" => false,
"id" => "3511076",
"name" => "Other product Name",
"product_id" => "234567"
}
],
"category" => "5",
"title" => "Product title"
},
"345678" => %{
"associated_products" => [
%{
"has_stock" => true,
"id" => "3511082",
"name" => "Other product Name",
"product_id" => "345678"
},
%{
"has_stock" => false,
"id" => "3511083",
"name" => "Other product Name",
"product_id" => "345678"
}
],
"category" => "8",
"tag" => ["creation", "dynasty"],
"title" => "Product title"
},
"456789" => %{"associated_products" => [], "category" => "5", "title" => "Product title"}
},
"tags_title" => %{"tag" => ["preview"]}
}