/* Update Data to IOS Users*/
'use strict';
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://10.0.0.5:27017/";
var pgsqlUtils = require('./../../utils/pgsql.utils');
//Get Data from Mongo
MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    var dbo = db.db("aiproddb");
         let skip = parseInt(process.argv[2]);
         let query = {did:'a573704d-7ffd-4a81-8b2f-454f0a50e79d'};
          
         dbo.collection("app_users5fa41afe6fc1e678a8344311").find().skip(skip).limit(10000).toArray(function(err, result) {
             if (err) throw err;
             result.forEach(element => {
                if(element.did){
                    let queryCitus = "select did,segment->'UCIC' as UCIC from events_5fa41afe6fc1e678a8344311 where key='Login' and did ='" + element.did + "'";
                    // Citus Query
                    pgsqlUtils.executeQuery(queryCitus)
                        .then(res =>{
                            let countData = JSON.parse(JSON.stringify(res));  
                             
                            let queryCitus = "update app_users_5fa41afe6fc1e678a8344311 SET _custom_userid = '" + countData[0][0].ucic + "' where did = '" + element.did + "'";
                                    // Citus Query
                            let userObj = {"_custom" : { "UserId" : "[ " + countData[0][0].ucic + " ]" }}
                             
                            pgsqlUtils.executeQuery(queryCitus)
                                .then(res =>{
                                    dbo.collection("app_users5fa41afe6fc1e678a8344311").updateOne({'did':element.did},{'$set':userObj},function(err, result) {
                                        if (err) throw err;
                                         
                                    });
                                   
                                })
                                .catch(error =>{
                                     
                                });
                    })
                }
             });
         });
});

