Skip to content
Snippets Groups Projects
chatRoutes.js 4.12 KiB
Newer Older
Fajar Nugroho's avatar
Fajar Nugroho committed
const express = require('express');
const Chat = require('../models/Chat.js');
const chatRouter = express.Router();
const async = require('async');
Fajar Nugroho's avatar
Fajar Nugroho committed
const messaging = require('../controllers/messaging');
const request = require('request');
const admin = require('firebase-admin');
const getFCMToken = require('../controllers/getFCMToken');
const User = require('../models/User');


Fajar Nugroho's avatar
Fajar Nugroho committed

chatRouter.route('/').get((req, res) => {
Fajar Nugroho's avatar
Fajar Nugroho committed
  // res.setHeader('Allow-Control-Access-Origin', '*');
Fajar Nugroho's avatar
Fajar Nugroho committed
  const uCustomer = req.query.customer;
  const uDriver = req.query.driver;
  console.log("masuk");
  Chat.find({ customer : uCustomer, driver : uDriver}, (err, result) => {
    if (err) return console.error(err);
    return res.json({
      'url' : '/chat',
      'desc' : 'get chat history',
Fajar Nugroho's avatar
Fajar Nugroho committed
      'status' : 200,
      'data' : result[0]
Fajar Nugroho's avatar
Fajar Nugroho committed
    });
  });
});

Fajar Nugroho's avatar
Fajar Nugroho committed
chatRouter.route('/send').post((req, res) => {
Fajar Nugroho's avatar
Fajar Nugroho committed
  // res.setHeader('Allow-Control-Access-Origin', '*');
Fajar Nugroho's avatar
Fajar Nugroho committed
  const data = {};
Fajar Nugroho's avatar
Fajar Nugroho committed
  console.log(req.body);
Fajar Nugroho's avatar
Fajar Nugroho committed
  data.uCustomer = parseInt(req.body.customer);
  data.uDriver = parseInt(req.body.driver);
Fajar Nugroho's avatar
Fajar Nugroho committed
  data.chat = {
Fajar Nugroho's avatar
Fajar Nugroho committed
    sender : parseInt(req.body.chat.sender),
Fajar Nugroho's avatar
Fajar Nugroho committed
    message : req.body.chat.message
  }
Fajar Nugroho's avatar
Fajar Nugroho committed
  const recvUser = req.body.chat.sender == data.uCustomer ? data.uDriver : data.uCustomer;
  // const recvToken = getFCMToken(recvUser);
  // console.log(recvToken);
  const payload = {
    "notification" : {
Fajar Nugroho's avatar
Fajar Nugroho committed
     "title": req.body.chat.title,
Fajar Nugroho's avatar
Fajar Nugroho committed
     "body": req.body.chat.message,
   }
  }
Fajar Nugroho's avatar
Fajar Nugroho committed
  console.log(typeof(recvUser));
Fajar Nugroho's avatar
Fajar Nugroho committed

Fajar Nugroho's avatar
Fajar Nugroho committed
  User.findOne({ userId : recvUser }, (err, result) => {
Fajar Nugroho's avatar
Fajar Nugroho committed
    if (err) return console.error(err);
Fajar Nugroho's avatar
Fajar Nugroho committed
    console.log("hasil query" + result);
Fajar Nugroho's avatar
Fajar Nugroho committed
    const recvToken = result.token;
    console.log(recvToken);
Fajar Nugroho's avatar
Fajar Nugroho committed
    if (req.body.chat.title == "order" || req.body.chat.title == "complete") {
      messaging.sendToDevice(recvToken, payload)
        .then((res) => {
          console.log("order terkirim");
          return res.json({
            url : '/chat/save',
            desc : 'send chat',
            status : 200,
            data : {}
          });
        }).catch((err) => {
          return res.json({
            url : '/chat/save',
            desc : 'send chat',
            status : 500,
            data : err
          });
        })
    } else {
      messaging.sendToDevice(recvToken, payload)
        .then((response) => {
          console.log(response);
          async.waterfall([
            (flowCallback) => {
              Chat.find({ customer : data.uCustomer, driver : data.uDriver}, (err, result) => {
Fajar Nugroho's avatar
Fajar Nugroho committed
                if (err) return flowCallback(err);
Fajar Nugroho's avatar
Fajar Nugroho committed
                result.length > 0 ? data.exist = 1 : data.exist = 0;
Fajar Nugroho's avatar
Fajar Nugroho committed
                return flowCallback();
              });
Fajar Nugroho's avatar
Fajar Nugroho committed
            },
            (flowCallback) => {
              if (data.exist == 1) {
                Chat.update({ customer : data.uCustomer, driver : data.uDriver},
                { '$push' : { content : data.chat}}, (err, result) => {
                  if (err) return flowCallback(err);
                  return flowCallback();
                });
              } else {
                Chat.create({
                  customer : data.uCustomer,
                  driver : data.uDriver,
                  content : [data.chat]
                }, (err, result) => {
                  if (err) return flowCallback(err);
                  return flowCallback();
                });
              }
            }
          ], (err, result) => {
            if (err) {
              console.error(err);
              return res.json({
                url : '/chat/save',
                desc : 'save chat history',
                status : 500,
                data : {}
              });
Fajar Nugroho's avatar
Fajar Nugroho committed
            } else {
Fajar Nugroho's avatar
Fajar Nugroho committed
              return res.json({
                url : '/chat/save',
                desc : 'save chat history',
                status : 200,
                data : 'OK'
Fajar Nugroho's avatar
Fajar Nugroho committed
              });
            }
Fajar Nugroho's avatar
Fajar Nugroho committed
          });
        })
        .catch((err) => {
          console.error(err);
          return res.json({
            url : '/chat/save',
            desc : 'send chat',
            status : 500,
            data : {}
          });
Fajar Nugroho's avatar
Fajar Nugroho committed
        });
Fajar Nugroho's avatar
Fajar Nugroho committed
      }
    });
Fajar Nugroho's avatar
Fajar Nugroho committed

Fajar Nugroho's avatar
Fajar Nugroho committed
});

Fajar Nugroho's avatar
Fajar Nugroho committed

Fajar Nugroho's avatar
Fajar Nugroho committed
module.exports = chatRouter;