I´m building an integration package for a legacy system where I receive all data, including the data structure definition (metadata) using lists.
Fields (or properties) in metadata list changes depeding on what type of record is being retrieved. The example below shows a customer record, but it may be a product record, or a stock item record - then I would receive different metadata.
I need to dynamically build the object structure from the metadata and then fetch its data from the given lists.
metadata list for a customer call to legacy system:
[
{
field: "customer.id",
type: "string"
},
{
field: "customer.name",
type: "string"
},
{
field: "customer.credit.rate",
type: "string"
},
{
field: "customer.credit.card.limit",
type: "number"
},
{
field: "customer.credit.card.due",
type: "number"
},
{
field: "customer.credit.card.paymentDay",
type: "number"
},
{
field: "customer.address.street",
type: "string"
},
{
field: "customer.address.number",
type: "string"
},
{
field: "customer.address.zip",
type: "string"
}
]
metadata list for a product call to legacy system:
[
{
field: "product.id",
type: "string"
},
{
field: "product.name",
type: "string"
},
{
field: "product.sale.customer.id",
type: "string"
},
{
field: "product.sale.customer.name",
type: "number"
},
{
field: "product.sale.price",
type: "number"
}
]
products list:
[
{ id: "1", product_id: "1", name: "Spounge" },
{ id: "2", product_id: "2", name: "CD Box" },
]
customers list:
[
{ id: "1", name: "John"},
{ id: "2", name: "Mary"}
]
credits list:
[
{ id: "1", customer_id: "1", rate: "A" },
{ id: "2", customer_id: "2", rate: "B" },
]
cards list:
[
{ id: "1", credit_id: "1", limit: 10000, due: 0, paymentDay: 15 },
{ id: "2", credit_id: "2", limit: 1000, due: 1000, paymentDay: 2 },
]
address list:
[
{ id: "1", customer_id: "1", street: "46th St", number: "33", zip: "12312" },
{ id: "2", customer_id: "2", street: "55th Avenue", number: "349", zip: "39458" },
]
products list:
[
{ id: "1", product_id: "1", name: "Spounge" },
{ id: "2", product_id: "2", name: "CD Box" },
]
customers list:
[
{ id: "1", name: "John"},
{ id: "2", name: "Mary"}
]
credits list:
[
{ id: "1", customer_id: "1", rate: "A" },
{ id: "2", customer_id: "2", rate: "B" },
]
cards list:
[
{ id: "1", credit_id: "1", limit: 10000, due: 0, paymentDay: 15 },
{ id: "2", credit_id: "2", limit: 1000, due: 1000, paymentDay: 2 },
]
sales list:
[
{ id: "1", customer_id: "1", product_id: "1", price: 123.44 },
{ id: "2", customer_id: "2", product_id: "2", price: 3.22 },
]
My goal is to have that data in a single javascript array of objects:
Example for the customer call to legacy system:
customers = [
{
id: 1,
name: "John",
credit {
rate: "A",
card {
limit: 10000,
due: 0,
paymentDay: 15
}
},
address {
street: "46th St",
number: "33",
zip: "12312"
}
},
{
id: 2,
name: "Mary",
credit {
rate: "B",
card {
limit: 1000,
due: 1000,
paymentDay: 2
}
},
address {
street: "55th Avenue",
number: "349",
zip: "39458"
}
}
]
Example for the product call to legacy system:
customers = [
{
id: 1,
name: "Spounge",
sale {
price: 123.44,
customer {
id: "1",
name: "John"
}
}
},
{
id: 2,
name: "CD Box",
sale {
price: 3.22,
customer {
id: "2",
name: "Mary"
}
}
}
]
I´m really very confused finding the right way of doing this. What would be the best approach to start from? Start from building a nested object and filling it up? Any suggested algorithm?
NOTE: The legacy system changes the data based on the type of object is being read and data availability. So, metadata list and the lists are not fixed. For every API call into the legacy system I receive a different metadata array and different lists, depending on what data is available and the query is being made, so I need a generic algorithm to build the object based on the given data.