Skip to main content

PDI--- Delete Object in a json file sourced from a column if conditions meet

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'));
//   }
// });

PDI Flow

Via Active questions tagged javascript - Stack Overflow https://ift.tt/IfxcoWk

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data