Monday 12 June 2017

How to map data from postgresql to mongoJSON

i have pasted is my code that will do 1) fetching data from mongodb database based on monthfirstday and monthlastday later it will sort based on the condition and push to the perticular array its perfect 2)am using 2 database here a) mongo and b)postgresql so i need to get the university_id which is returned by the data of point 1 by using that university_id i need to display universityName which is stored in postgresql db a user can enrolled to more than 20 universities so how to fetch the university name from PG and map in to the JSON data which is return by mongo

 const today = new Date();
    const previous = new Date(today.getTime() - (24 * 1 * 60 * 60 * 1000));
    const weekFirstDay = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 2);
    const weekLastDay = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay() + 8);
    const monthFirstDay = new Date(today.getFullYear(), today.getMonth(), 2);
    const monthLastDay = new Date(today.getFullYear(), today.getMonth() + 1, 1);
    previous.setUTCHours(0, 0, 0, 0);
    weekFirstDay.setUTCHours(0, 0, 0, 0);
    weekLastDay.setUTCHours(0, 0, 0, 0);
    monthFirstDay.setUTCHours(0, 0, 0, 0);
    monthLastDay.setUTCHours(0, 0, 0, 0);
    entries.find({
        $and: [{
            $or: [{
                "times.0.date": {
                    $gte: monthFirstDay,
                    $lte: monthLastDay
                }
            }, {
                "times.date": {
                    $gte: monthFirstDay,
                    $lte: monthLastDay
                }
            }]
        }, {
            "email": data.email,
            "user_id": parseInt(data.user_id)
        }]
    }).toArray().then(function(timer) {
        let today_entry = [];
        let previous_entry = [];
        let week_entry = [];
        let month_entry = [];
        let week_hrs = 0;
        let month_hrs = 0;
        let previous_hrs = 0;
        let today_hrs = 0;
        timer.map(function(tim) {
  dbs.query('Select "universityName" from universities where _id = $1', [data.user_id], function(err, univers) {
    if (err) return callback(new Error('error'));
            if (tim.times.length > 0) {
                var ms = 0;
                var start = null;
                tim.times.map(function(item) {

                    if ((item.state == 'start' || item.state == 'resume') && !start) {
                        start = item;
                    } else if ((item.state == 'pause' || item.state == 'stop') && start) {
                        ms = ms + moment(item.date) - moment(start.date);
                        start = null;
                    }
                })
                tim.total_hours = moment.utc(ms).format("HH:mm");
                tim.universityName = univers.rows[0];-------------------------->
                if ((monthFirstDay.getDate() <= tim.times[0].date.getDate()) && (monthLastDay.getDate() >= tim.times[0].date.getDate())) {
                    month_hrs = month_hrs + ms;
                    month_entry.push(tim);
                }
                if ((tim.times[0].date.getDate() == today.getDate())) {
                    today_hrs = today_hrs + ms;
                    today_entry.push(tim);
                }
                if ((tim.times[0].date.getDate() == previous.getDate())) {
                    previous_hrs = previous_hrs + ms;
                    previous_entry.push(tim);
                }
                if ((weekFirstDay.getDate() <= tim.times[0].date.getDate()) && (weekLastDay.getDate() >= tim.times[0].date.getDate())) {
                    week_hrs = week_hrs + ms;
                    week_entry.push(tim);
                }
            }
       })
        });
        return callback(null, {
            today: today_entry,
            today_hrs: moment.utc(today_hrs).format("HH:mm"),
            previous: previous_entry,
            previous_hrs: moment.utc(previous_hrs).format("HH:mm"),
            week_hrs: moment.utc(week_hrs).format("HH:mm"),
            month_hrs: moment.utc(month_hrs).format("HH:mm")
        })
    }).catch(function(err) {
        return callback(new Error(err));
    })

output for my code:

{
  "today": [],
  "today_hrs": "00:00",
  "previous": [
    {
      "_id": "591453340a59301b9471e3d1",
      "university_id": "7",
      "times": [
        {
          "state": "start",
          "date": "2017-05-11T08:12:00.000Z"
        },
        {
          "state": "pause",
          "date": "2017-05-11T09:12:00.000Z"
        },
        {
          "state": "resume",
          "date": "2017-05-11T10:12:00.000Z"
        },
        {
          "state": "stop",
          "date": "2017-05-11T11:12:00.000Z"
        }
      ],
      "email": "time@time.com",
      "total_hours": "02:00"
    },
     "previous": [
    {
      "_id": "591453340a59301b9471e3d1",
      "university_id": "6",
      "times": [
        {
          "state": "start",
          "date": "2017-05-11T08:12:00.000Z"
        },
        {
          "state": "pause",
          "date": "2017-05-11T09:12:00.000Z"
        },
        {
          "state": "resume",
          "date": "2017-05-11T10:12:00.000Z"
        },
        {
          "state": "stop",
          "date": "2017-05-11T11:12:00.000Z"
        }
      ],
      "email": "time@time.com",
      "total_hours": "02:00"
    }
    ]

expected output:

{
  "today": [],
  "today_hrs": "00:00",
  "previous": [
    {
      "_id": "591453340a59301b9471e3d1",
      "university_id": "7",
      "times": [
        {
          "state": "start",
          "date": "2017-05-11T08:12:00.000Z"
        },
        {
          "state": "pause",
          "date": "2017-05-11T09:12:00.000Z"
        },
        {
          "state": "resume",
          "date": "2017-05-11T10:12:00.000Z"
        },
        {
          "state": "stop",
          "date": "2017-05-11T11:12:00.000Z"
        }
      ],
      "email": "time@time.com",
      "total_hours": "02:00",
      "universityName":"AAAAA"------------>
    },
     "previous": [
    {
      "_id": "591453340a59301b9471e3d1",
      "university_id": "6",
      "times": [
        {
          "state": "start",
          "date": "2017-05-11T08:12:00.000Z"
        },
        {
          "state": "pause",
          "date": "2017-05-11T09:12:00.000Z"
        },
        {
          "state": "resume",
          "date": "2017-05-11T10:12:00.000Z"
        },
        {
          "state": "stop",
          "date": "2017-05-11T11:12:00.000Z"
        }
      ],
      "email": "time@time.com",
      "total_hours": "02:00",
      "universityName":"CCCC" ----------------->
    }
    ]



via Schüler

No comments:

Post a Comment