Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. const toJson        = require('xml2js').parseString ;
  3. const mysql         = require('mysql') ;
  4. const format        = require('dateformat') ;
  5.  
  6. const con = mysql.createConnection({
  7.     host: "HOST",
  8.     user: "USERNAME",
  9.     password: "PASSWORD",
  10.     database: "DB"
  11. });
  12.  
  13. module.exports.parseResult = function(result) {
  14.     var xmlResult = result ;
  15.     toJson(xmlResult, function(error, result){
  16.  
  17.         jsonObj = result.Instructions.Instruction ;
  18.         for ( i=0; i < jsonObj.length ; i++ ) {
  19.            
  20.             var thisRef = jsonObj[i].Ref.toString() ;
  21.             if ( thisRef.indexOf('-FR') > 0 ) {
  22.  
  23.                 // CREATE PLACEHOLDER
  24.                 createRecord(thisRef);
  25.                
  26.                 // PARSE REFERRER INFO
  27.                 var referrerRef = jsonObj[i].Referrer[0].Ref.toString() ;
  28.                 var referrerName = jsonObj[i].Referrer[0].Description.toString() ;
  29.                 var referrerNameNameNumber = jsonObj[i].Referrer[0].Address[0].NameNumber.toString() ;
  30.                 var referrerNameAddLine1 = jsonObj[i].Referrer[0].Address[0].Line1.toString() ;
  31.                 var referrerNameAddLine2 = jsonObj[i].Referrer[0].Address[0].Line2.toString() ;
  32.                 var referrerNameTownCity = jsonObj[i].Referrer[0].Address[0].TownCity.toString() ;
  33.                 var referrerNameCounty = jsonObj[i].Referrer[0].Address[0].County.toString() ;
  34.                 var referrerNamePostCode = jsonObj[i].Referrer[0].Address[0].PostCode.toString() ;
  35.                 var referrerTelephoneNo = jsonObj[i].Referrer[0].PhoneNumbers[0].Daytime.toString() ;
  36.                 var referrerMobileNo = jsonObj[i].Referrer[0].PhoneNumbers[0].Mobile.toString() ;
  37.                 var referrerFaxNo = jsonObj[i].Referrer[0].PhoneNumbers[0].Fax.toString() ;
  38.                 var referrerEmailAddress = jsonObj[i].Referrer[0].EmailAddress.toString() ;
  39.  
  40.                 // PARSE CUSTOMER DETAILS
  41.                 var addressName = jsonObj[i].Customers.toString() ;
  42.                 var counter = 0 ;
  43.                 var numberOfBorrowers = jsonObj[i].Customer.length ;
  44.                 for ( x=0; x < numberOfBorrowers; x++ ) {
  45.                     var counter = counter + 1 ;
  46.                     var clientUpdateSql = 'UPDATE auto_SRMinstructs SET '+
  47.                                           'borrower'+ counter +'_title = "' + jsonObj[i].Customer[x].Title.toString() + '", ' +
  48.                                           'borrower'+ counter +'_forenames = "' + jsonObj[i].Customer[x].Forename.toString() + '", ' +
  49.                                           'borrower'+ counter +'_surname = "' + jsonObj[i].Customer[x].Surname.toString() + '", ' +
  50.                                           'borrower'+ counter +'_housenumber = "' + jsonObj[i].Customer[x].Address[0].NameNumber.toString() + '", ' +
  51.                                           'borrower'+ counter +'_streetname = "' + jsonObj[i].Customer[x].Address[0].Line1.toString() + '", ' +
  52.                                           'borrower'+ counter +'_districtname = "' + jsonObj[i].Customer[x].Address[0].Line2.toString() + '", ' +
  53.                                           'borrower'+ counter +'_posttown = "' + jsonObj[i].Customer[x].Address[0].TownCity.toString() + '", ' +
  54.                                           'borrower'+ counter +'_county = "' + jsonObj[i].Customer[x].Address[0].County.toString() + '", ' +
  55.                                           'borrower'+ counter +'_postcode1 = "' + jsonObj[i].Customer[x].Address[0].PostCode.toString() + '", ' +
  56.                                           'borrower'+ counter +'_tel_home = "' + jsonObj[i].Customer[x].PhoneNumbers[0].Daytime.toString() + '", ' +
  57.                                           'borrower'+ counter +'_tel_office = "' + jsonObj[i].Customer[x].PhoneNumbers[0].Evening.toString() + '", ' +
  58.                                           'borrower'+ counter +'_tel_mobile = "' + jsonObj[i].Customer[x].PhoneNumbers[0].Mobile.toString() + '", ' +
  59.                                           'borrower'+ counter +'_email = "' + jsonObj[i].Customer[x].EmailAddress.toString() + '" ' +
  60.                                           'WHERE SRinstruction_id = "' + thisRef + '" ;' ;
  61.                     // addClients(clientUpdateSql) ;
  62.                 }
  63.  
  64.                 // CASE DETAILS
  65.                 var transType = jsonObj[i].Case[0].TransactionType.toString() ;
  66.                 var feeGuard = jsonObj[i].Case[0].feeGuard.toString() ;
  67.                 var createDate = jsonObj[i].Case[0].CreateDate.toString() ;
  68.                 var onHold = jsonObj[i].Case[0].OnHold.toString() ;
  69.                 var tenureType = jsonObj[i].Case[0].TenureType.toString() ;
  70.                 var propertyNameNumber = jsonObj[i].Case[0].Property[0].Address[0].NameNumber.toString() ;
  71.                 var propertyLine1 = jsonObj[i].Case[0].Property[0].Address[0].Line1.toString() ;
  72.                 var propertyLine2 = jsonObj[i].Case[0].Property[0].Address[0].Line2.toString() ;
  73.                 var propertyTownCity = jsonObj[i].Case[0].Property[0].Address[0].TownCity.toString() ;
  74.                 var propertyCounty = jsonObj[i].Case[0].Property[0].Address[0].County.toString() ;
  75.                 var propertyPostCode = jsonObj[i].Case[0].Property[0].Address[0].PostCode.toString() ;
  76.                 var propertyPrice = jsonObj[i].Case[0].Property[0].Price.toString() ;
  77.                 var propertyMortgageAmount = jsonObj[i].Case[0].Property[0].MortgageAmount.toString() ;
  78.                 var propertyMortgageLender = jsonObj[i].Case[0].Property[0].MortgageLender.toString() ;
  79.                
  80.                 // DISBURSEMENTS
  81.                 if ( jsonObj[i].Disbursements != '' ) {
  82.                     var hmlrLandRegistryFee = jsonObj[i].Disbursements[0].hmlrLandRegistryFee.toString() ;
  83.                     var landRegistryOfficialCopies = jsonObj[i].Disbursements[0].landRegistryOfficialCopies.toString() ;
  84.                     var hmlrSearchFee = jsonObj[i].Disbursements[0].hmlrSearchFee.toString() ;
  85.                     var bankruptcySearchFee = jsonObj[i].Disbursements[0].bankruptcySearchFee.toString() ;
  86.                     var electronicIDCheckFee = jsonObj[i].Disbursements[0].electronicIDCheckFee.toString() ;
  87.                     var rmSearchIdentityFee = jsonObj[i].Disbursements[0].rmSearchIdentityFee.toString() ;
  88.                     var searchIndemnityFee = jsonObj[i].Disbursements[0].searchIndemnityFee.toString() ;
  89.                 }
  90.  
  91.                 // FEES
  92.                 var sortFee = jsonObj[i].Fees[0].sortFee.toString() ;
  93.                 var brokerFee = jsonObj[i].Fees[0].brokerFee.toString() ;
  94.                 var solicitorFee = jsonObj[i].Fees[0].solicitorFee.toString() ;
  95.                 var totalExVat = jsonObj[i].Fees[0].totalExVat.toString() ;
  96.                 var totalIncVat = jsonObj[i].Fees[0].totalIncVat.toString() ;
  97.                
  98.                 // CASE NOTES
  99.                 var caseNotes = jsonObj[i].Note.toString() ;
  100.  
  101.                 // MISC VARIABLES
  102.                 var matterDesc = 'R/M OF ' + propertyNameNumber.toUpperCase() ;
  103.                 var instructionDate = new Date() ;
  104.                 var instructionDate = format(instructionDate, 'yyyy-mm-dd') ;
  105.  
  106.                 // INSERT INSTRUCTION
  107.                 var mainInsert = 'UPDATE auto_SRMinstructs SET ' +
  108.                                'instruction_type = "R",' +
  109.                                'worktype_code = "CON",' +
  110.                                'mattertype_code = "VRM",' +
  111.                                'matter_desc = "'+matterDesc+'",' +
  112.                                'client_type_code = "",' +
  113.                                'client_origin = "SRM",' +
  114.                                'matter_origin = "SRM",' +
  115.                                'dept_code = "REM",' +
  116.                                'lead_contact_id = "",' +
  117.                                'contact_type = "L",' +
  118.                                'trantype = "R",' +
  119.                                'instruction_date = "'+instructionDate+'",' +
  120.                                'accepted_ind = "N",' +
  121.                                'number_of_borrowers = "'+numberOfBorrowers+'",' +
  122.                                'property_name = "'+propertyNameNumber+'",' +
  123.                                'property_street = "'+propertyLine1+'",' +
  124.                                'property_district = "'+propertyLine2+'",' +
  125.                                'property_post_town = "'+propertyTownCity+'",' +
  126.                                'property_county = "'+propertyCounty+'",' +
  127.                                'property_postcode = "'+propertyPostCode+'",' +
  128.                                'property_country = "United Kingdom",' +
  129.                                'customer_note = "'+caseNotes+'",' +
  130.                                'hmlrLandRegistryFee = "'+hmlrLandRegistryFee+'",' +
  131.                                'landRegistryOfficialCopies = "'+landRegistryOfficialCopies+'",' +
  132.                                'hmlrSearchFee = "'+hmlrSearchFee+'",' +
  133.                                'bankruptcySearchFee = "'+bankruptcySearchFee+'",' +
  134.                                'electronicIDCheckFee = "'+electronicIDCheckFee+'",' +
  135.                                'rmSearchIdentityFee = "'+rmSearchIdentityFee+'",' +
  136.                                'searchIndemnityFee = "'+searchIndemnityFee+'",' +
  137.                                'sortFee = "'+sortFee+'",' +
  138.                                'brokerFee = "'+brokerFee+'",' +
  139.                                'solicitorFee = "'+solicitorFee+'",' +
  140.                                'totalExVat = "'+totalExVat+'",' +
  141.                                'totalIncVat = "'+totalIncVat+'"' +
  142.                                'WHERE SRinstruction_id = "' + thisRef + '"' ;
  143.  
  144.                 // DO INSERTS
  145.                 createInstruction(mainInsert);
  146.             }
  147.         }
  148.     })
  149. }
  150.  
  151. // CREATE PLACEHOLDER ROW IN TABLE
  152. function createRecord(ref) {
  153.     var placeHolderSql = 'INSERT INTO auto_SRMinstructs (SRinstruction_id) VALUES ("'+ref+'")' ;
  154.     con.connect(function(){
  155.         con.query(placeHolderSql, function(error, result){
  156.             console.log('[+] Place holder for '+ref+' has been created.') ;
  157.         }) ;
  158.     }) ;
  159. }
  160.  
  161. // UPDATE BORROWER DETAILS
  162. function addClients(query) {
  163.     con.connect(function(){
  164.         con.query(query, function(error, result){
  165.             console.log(error)
  166.         }) ;
  167.     })
  168. }
  169.  
  170. // MAIN UPDATE
  171. function createInstruction(query) {
  172.     con.connect(function(){
  173.         con.query(query.toString(), function(error, result){
  174.             if (error) { throw error }
  175.             console.log(result) ;
  176.         })                 
  177.     })
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement