Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // USAGE
  2. // Require `itoolkitHandler` into the file
  3. // Parameters are an object with arrays
  4. // filters filter out the displayed value when logging, does not modify the actual value
  5. //
  6. // itoolkit.calliPgm(params, program, filters);
  7. //
  8. // params:[Object] An object with keys representing the value's name passed in or returned
  9. //    example: const params = {
  10. //                         name:   ["data", "type"],
  11. //                         number: ["", "6p0"],
  12. //                         yesNo:  ["", "7p2"],
  13. //                        }      
  14. //                        
  15. //         data: [String, Number] The data needs to match the type
  16. //         type: [String] This is a string designating the datatype for the RPG program | Reference the chart below fo more information
  17. //        
  18. //         ------------------------------------
  19. //         | Type   | syntax |    eg    | eg  |
  20. //         ------------------------------------
  21. //         ------------------------------------
  22. //         | Number |   npn  | 12345.67 | 7p2 |
  23. //         ------------------------------------
  24. //         | String |    nA  | "hello"  | 5A  |
  25. //         ------------------------------------
  26. //
  27. // program: {name: <programName>, lib: <libraryName.};
  28. //    name: [String] name of the program
  29. //    lib: [String] name of the library the program resides
  30. //
  31. // (optional) filters: {name: callback};
  32. //    name: [String] name of the parameter to modify (Needs to be the same key as in params)
  33. //    callback: [Function] callback to modify the string value of the data being passed in
  34. //      example: {creditCardNumber: ccnum => `XXXX-XXXX-XXXX-${ccnum.slice(-4)}
  35. //
  36.  
  37. const itoolkit = require('itoolkit');
  38. const logger = require('./config/winston.js');
  39.  
  40. let conn;
  41. connectitoolkit();
  42.  
  43. function connectitoolkit() {
  44.   if (!conn) {
  45.     logger.info(`itoolkit setting up DBPool..`);
  46.     try {
  47.       conn = new itoolkit.iConn("*LOCAL", "LOWANG", "ZILLA");
  48.       conn.debug(false);
  49.     } catch (error) {
  50.       logger.error("iConn connection issue: ", error);
  51.     }
  52.   }
  53.   return conn;
  54. }
  55.  
  56. // Matches "npn" OR "nA"
  57. const validParameterRegex = /\d+A|\d+p\d+/;
  58.  
  59. exports.calliPgm = function(params, program, filters) {
  60.   logHandler(params, program, filters);
  61.   return new Promise((resolve, reject) => {
  62.     try {
  63.       const conn = connectitoolkit();
  64.       const pgm = new itoolkit.iPgm(program.name, { lib: program.lib });
  65.  
  66.       Object.values(params).forEach(param => {
  67.         if(!validParameterRegex.test(param[1])) {
  68.           return reject(Error(`Invalid parameter descriptor "${param[1]}" passed to calliPgm, should match "nA" for strings and "npn" for numeric parameters`));
  69.         }
  70.  
  71.         pgm.addParam(param[0], param[1])
  72.       });
  73.  
  74.       conn.add(pgm);
  75.  
  76.       conn.run(xml => {
  77.         const result = itoolkit.xmlToJson(xml)[0];
  78.         if (!result.success) return reject(new Error(xml));
  79.         const formattedData = Object.keys(params).reduce((accumulator, key, index) => {
  80.           accumulator[key] = result.data[index].value;
  81.           return accumulator;
  82.         }, {});
  83.         resolve(formattedData);
  84.       });
  85.     } catch(error) {
  86.       reject(error);
  87.     }
  88.   });
  89. }
  90.  
  91. // filters is an object with keys of the same name as 'params', and callbacks to modify the displayed value.
  92. function logHandler(params, program, filters={}) {
  93.   // change the value of the parameter to match the return value of the callback
  94.   // This is used to mask stuff like credit card numbers.
  95.   const filtered = Object.entries(filters).reduce((acc, [key, filter]) => {
  96.     acc[key] = [filter(params[key][0]), params[key][1]];
  97.     return acc;
  98.   }, {});
  99.  
  100.   // Using object spreading to over-write the parameters with the filtered ones
  101.   logger.info(`Calling "${program.lib}.${program.name}" with: ${JSON.stringify({...params, ...filtered}, null, process.env.NODE_ENV === 'development' ? 1 : 0)}`);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement