Core Javascript Question -- Delete Object in a json file sourced from a column if conditions meet

Viewed 42

the reason I need the code to work in core javascript is the tool we use, it uses Rhino from mozilla, so it cannot contain objects or methods related to manipulation of web pages.

I am trying to compare data from two json files using core javascript, When the order_number and extid is same we compare the count(fullfilments.line_items) and quantity(which is the sum of all the quantities of all line items under that fulfillment, fullfilments.line_items.quantity) . If the order_number and ext_id combination match along with count and sum above, do nothing. If no match, 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": 39828,
            "order_id": 450625,
            "receipt": {},
            "service": "manual",
            "shipment_status": null,
            "status": "success",
            "updated_at": "2022-05-24",
            "line_items": 
            [{
                "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": [],
        "name": "#59",
        "number": 6,
        "order_number": 66,
        "ext_id": 110,
        "refunds": [
            {
                "id": 80,
                "created_at": "2000-06-17T14:31:06-04:00"
            }
        ]
    },
    {
        "app_id": 111,
        "fulfillments": [
        {
            "id": 398000,
            "order_id": 450005,
            "receipt": {},
            "service": "manual",
            "shipment_status": null,
            "status": "success",
            "updated_at": "2022-05-24",
            "line_items": 
            [{
                "id": 376,
                "quantity": 2
            },
            {
                "id": 992,
                "quantity": 1
            },
            {
                "id": 929,
                "quantity": 1
            },
            {
                "id": 768,
                "quantity": 1
            }
           ]
        }
        ],
        "line_items": [],
        "name": "#59",
        "number": 6,
        "order_number": 67,
        "ext_id": 114,
        "refunds": [
            {
                "id": 81,
                "created_at": "2000-06-17T14:31:06-04:00"
            }
        ]
    },
    {
        "app_id": 111,
        "fulfillments": [
        {
            "id": 39828,
            "order_id": 450625,
            "receipt": {},
            "service": "manual",
            "shipment_status": null,
            "status": "success",
            "updated_at": "2022-05-24",
            "line_items": 
            [{
                "id": 376,
                "quantity": 2
            },
            {
                "id": 768,
                "quantity": 1
            },
            {
                "id": 929,
                "quantity": 2
            },
            {
                "id": 768,
                "quantity": 2
            }
           ]
        }
        ],
        "line_items": [],
        "name": "#59",
        "number": 6,
        "order_number": 68,
        "ext_id": 113,
        "refunds": [
            {
                "id": 80,
                "created_at": "2000-06-17T14:31:06-04:00"
            }
        ]
    }
]

```````````````````````````````json````````````` //resultset file content

[ { "order_number": 66, "extid":110, "line_items_count": 6, "quantity": 7 }, { "order_number": 67, "extid":114, "line_items_count": 4, "quantity": 7 }, { "order_number": 68, "extid":113, "line_items_count": 4, "quantity": 6 } ]

`````````````````````````````````````````````````Code`````````````````

/**
 * orders.json file has some sample orders
 * resultset.json file has results from sql Lookup to the 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'));
//   }
// });

[PDI Flow][1]


  [1]: https://i.stack.imgur.com/3OzQE.png
0 Answers
Related