In PDI, I am trying to compare data from a query(STG_FULFILLMENT_QTY_AGG) with data in Json array being sourced from a column json in Sales Queues table input. When the order_number and extid is same we compare the count(fullfilments.line_items) and quantity(which is the sum of fullfilments.line_items.quantity) . If the order_number and ext_if combination match along with count and sum above, we leave the json struct as it is. If not, we remove the refund. the code works fine in Visual Studio. I am new to pentaho and I think the code needs to be changed as only core js works with this tool.
In the Orders file are sample json structure, for example Order_number (66 in this case) need to calculate and compare the count of line items(6 in this case) along with the Quantity of items(7 in this case), if it doesn't match need to remove Object Refund along with its elements, else No Changes.
``````Sample File````````
[
{
"app_id": 111,
"fulfillments": [{
"id": 376,
"quantity": 2
},
{
"id": 992,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}],
"line_items": [
{
"id": 376,
"quantity": 2
},
{
"id": 992,
"quantity": 1
},
{
"id": 929,
"quantity": 2
},
{
"id": 768,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}
],
"name": "#59",
"number": 6,
"order_number": 66,
"order_id": 111,
"refunds": [
{
"id": 80,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
},
{
"app_id": 111,
"fulfillments": [{
"id": 376,
"quantity": 2
},
{
"id": 992,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}],
"line_items": [
{
"id": 376,
"quantity": 2
},
{
"id": 992,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}
],
"name": "#59",
"number": 6,
"order_number": 67,
"order_id": 114,
"refunds": [
{
"id": 81,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
},
{
"app_id": 111,
"fulfillments": [{
"id": 376,
"quantity": 2
},
{
"id": 768,
"quantity": 1
},
{
"id": 929,
"quantity": 2
},
{
"id": 768,
"quantity": 2
}],
"line_items": [
{
"id": 376,
"quantity": 2
},
{
"id": 768,
"quantity": 1
},
{
"id": 929,
"quantity": 2
},
{
"id": 768,
"quantity": 2
}
],
"name": "#59",
"number": 6,
"order_number": 68,
"order_id": 113,
"refunds": [
{
"id": 80,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
},
{
"app_id": 111,
"fulfillments": [{
"id": 376,
"quantity": 2
},
{
"id": 992,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 738,
"quantity": 1
},
{
"id": 739,
"quantity": 3
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}],
"line_items": [
{
"id": 376,
"quantity": 2
},
{
"id": 992,
"quantity": 1
},
{
"id": 929,
"quantity": 1
},
{
"id": 738,
"quantity": 1
},
{
"id": 739,
"quantity": 3
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}
],
"name": "#59",
"number": 6,
"order_number": 69,
"order_id": 101,
"refunds": [
{
"id": 80,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
},
{
"app_id": 111,
"fulfillments": [{
"id": 376,
"quantity": 2
},
{
"id": 768,
"quantity": 3
}],
"line_items": [
{
"id": 376,
"quantity": 2
},
{
"id": 768,
"quantity": 3
}
],
"name": "#59",
"number": 6,
"order_number": 70,
"order_id": 119,
"refunds": [
{
"id": 80,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
},
{
"app_id": 111,
"fulfillments": [{
"id": 376,
"quantity": 2
},
{
"id": 929,
"quantity": 1
},
{
"id": 768,
"quantity": 1
}],
"line_items": [
{
"id": 376,
"quantity": 2
},
{
"id": 929,
"quantity": 2 // two orders were placed but only one fullfilled.
},
{
"id": 768,
"quantity": 1
}
],
"name": "#59",
"number": 6,
"order_number": 71,
"order_id": 117,
"refunds": [
{
"id": 80,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
},
{
"app_id": 111,
"fulfillments": [{
"id": 768,
"quantity": 1
}],
"line_items": [
{
"id": 768,
"quantity": 1
}
],
"name": "#59",
"number": 6,
"order_number": 72,
"order_id": 909,
"refunds": [
{
"id": 80,
"created_at": "2000-06-17T14:31:06-04:00"
}
]
}
]
`````````````````````````````````````````````````Code`````````````````
/**
* orders.json file has some sample orders
* resultset.json file has results accourding to the orders
* After comparision, order number #68 and #70 and #72 are not matcing, hence we are revomg the refund key for those orders.
*/
const orders = require('./orders.json');
function compare(order) {
let isMatched = false;
let resultSet = require('./resultset.json');
let result = resultSet.find(function (item) {
return item.order_number === order.order_number;
});
if (
result &&
result.line_items_count === order.items &&
result.quantity === order.quantity
) {
isMatched = true;
}
return isMatched;
}
function fixOrders(orders) {
orders.map(function (order) {
let { order_number, line_items } = order;
let quantity = line_items.reduce(function (quantity, line_item) {
return (quantity += line_item.quantity);
}, 0);
if (!compare({ order_number, items: line_items.length, quantity })) {
delete order.refunds;
}
});
return orders;
}
let fixedOrders = fixOrders(orders);
console.log(fixedOrders);
// store in output.js
//========================================
// var fs = require('fs');
// fs.writeFile('outputFile.json', JSON.stringify(fixedOrders), (err) => {
// if (err) console.log(err);
// else {
// console.log('File written successfully\n');
// // console.log('The written has the following contents:');
// // console.log(fs.readFileSync('outputFile.json', 'utf8'));
// }
// });
Via Active questions tagged javascript - Stack Overflow https://ift.tt/IfxcoWk
Comments
Post a Comment