Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Load the AWS SDK for Node.js
  2. var AWS = require('aws-sdk');
  3. const axios = require('axios');
  4. // Set the regionsss
  5. AWS.config.update({ region: 'us-west-2' });
  6.  
  7. const MongoClient = require("mongodb").MongoClient;
  8. const MONGODB_URI =
  9.   "mongodb://machrus:Kolok8080@mapiddev-shard-00-00-n8uqn.mongodb.net:27017,mapiddev-shard-00-01-n8uqn.mongodb.net:27017,mapiddev-shard-00-02-n8uqn.mongodb.net:27017/mapiddb?ssl=true&replicaSet=MapidDev-shard-0&authSource=admin&retryWrites=true";
  10.  
  11.  
  12. exports.handler = function(event, context, callback) {
  13.  
  14.     var username = event.body.username;
  15.     var userid = event.body.userid;
  16.     var longitude = parseFloat(event.body.longitude);
  17.     var latitude = parseFloat(event.body.latitude);
  18.     var image_url = event.body.image_url;
  19.     var images = event.body.images;
  20.     var hashtags = event.body.hashtags;
  21.     var project_id = event.body.project_id;
  22.     var layer_id = event.body.layer_id;
  23.     var cognitoid = event.body.cognitoid;
  24.     var fields = event.body.fields;
  25.  
  26.     /*var username = "yacob89";
  27.     var longitude = parseFloat("0.1");
  28.     var latitude = parseFloat("0.2");
  29.     var image_url = "dfaff";
  30.     var hashtags = "coba coba aja #17anbersamaMAPID #woiwoi";
  31.     var project_id = "5d6cf3fc2e1c920007ad7169";
  32.     var layer_id = "5d88696a62a2810008e32099";*/
  33.     var mongo = require('mongodb');
  34.  
  35.     var image1 = "-";
  36.     var image2 = "-";
  37.     var image3 = "-";
  38.     var image4 = "-";
  39.     var imagesmodel = [];
  40.  
  41.     var x;
  42.     for (x = 0; x < images.length; x++) {
  43.         var image_id = new mongo.ObjectID();
  44.         var string_image_id = "feed_image_" + image_id;
  45.         if (x === 0) {
  46.             image1 = images[0];
  47.         }
  48.         if (x === 1) {
  49.             image2 = images[1];
  50.         }
  51.         if (x === 2) {
  52.             image3 = images[2];
  53.         }
  54.         if (x === 3) {
  55.             image4 = images[3];
  56.         }
  57.         imagesmodel.push({ _id: string_image_id, createdAt: new Date(), image_url: images[x] });
  58.     }
  59.  
  60.     if (image_url === "") {
  61.         image_url = "-";
  62.     }
  63.     if (userid === "") {
  64.         userid = "-";
  65.     }
  66.  
  67.     var hashtagsArray = hashtags.match(/#[a-zA-Z0-9]+/gi);
  68.     console.log("Hashtagarray: ", hashtagsArray);
  69.  
  70.     if (hashtagsArray === null) {
  71.         hashtagsArray = [];
  72.     }
  73.  
  74.     var res = {
  75.         statusCode: 200,
  76.         headers: {
  77.             "Content-Type": "*/*",
  78.             "Access-Control-Allow-Origin": "*", // Required for CORS support to work
  79.             "Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS
  80.         }
  81.     };
  82.  
  83.  
  84.     var projectid = new mongo.ObjectID(project_id);
  85.     var layerid = new mongo.ObjectID(layer_id);
  86.     var featureid = new mongo.ObjectID();
  87.  
  88.     if (project_id === "undefined" || layer_id === "undefined") {
  89.         MongoClient.connect(MONGODB_URI, function(err, db) {
  90.             if (err) {
  91.                 res.body = err;
  92.                 callback(null, res);
  93.             }
  94.             else {
  95.                 var dbo = db.db("mapiddb");
  96.                 // Insert a single document
  97.                 dbo.collection("feeds").insertOne({ createdAt: new Date(), description: hashtags, image_url: image_url, username: username, userid: userid, longitude: longitude, latitude: latitude, flag: 0, layer_id: layer_id, project_id: project_id, cognitoid: cognitoid, hashtags: hashtagsArray, images: imagesmodel, comments: [], location: { type: "Point", coordinates: [longitude, latitude] } }, function(err, result) {
  98.                     if (err) {
  99.                         console.log(err);
  100.                         res.body = { message: "error", error: JSON.stringify(err) };
  101.                         callback(null, res.body);
  102.                         db.close();
  103.                     }
  104.                     else {
  105.                         console.log(result);
  106.                         res.body = { message: "success", _id: result.insertedId };
  107.                         callback(null, res.body);
  108.                         db.close();
  109.                     }
  110.                 });
  111.             }
  112.         });
  113.     }
  114.     else {
  115.         axios.get("https://hnjp62bwxh.execute-api.us-west-2.amazonaws.com/GeoDev/getprojectbyid", {
  116.             params: {
  117.                 _id: project_id
  118.             }
  119.         }).then(res2 => {
  120.             const stories = res2.data.layers;
  121.             console.log("Jumlah data: ", res2.data.layers.length);
  122.             var found = stories;
  123.             var spss = found.find(obj => {
  124.                 return obj._id === layer_id
  125.             })
  126.             console.log("found: ", spss.geojson.features.length);
  127.             var generated_id = spss.geojson.features.length;
  128.             if (stories) {
  129.                 console.log("Ceritanya adalah: ", stories);
  130.  
  131.                 MongoClient.connect(MONGODB_URI, function(err, db) {
  132.                     if (err) {
  133.                         res.body = err;
  134.                         callback(null, res);
  135.                     }
  136.                     else {
  137.                         var dbo = db.db("mapiddb");
  138.                         var insertedId;
  139.  
  140.                         // Insert a single document
  141.                         dbo.collection("feeds").insertOne({ createdAt: new Date(), description: hashtags, fields: fields, image_url: image_url, username: username, userid: userid, longitude: longitude, latitude: latitude, flag: 0, cognitoid: cognitoid, layer_id: layer_id, project_id: project_id, hashtags: hashtagsArray, images: imagesmodel, comments: [], location: { type: "Point", coordinates: [longitude, latitude] } }, function(err, result) {
  142.                             if (err) {
  143.                                 console.log(err);
  144.                                 res.body = { message: "error", error: JSON.stringify(err) };
  145.                                 callback(null, res.body);
  146.                                 db.close();
  147.                             }
  148.                             else {
  149.                                 console.log(result);
  150.                                 insertedId = result.insertedId;
  151.                                 // Setelah insert ke feeds, insert new layer di MDCs
  152.                                 fields["mapid_id"] = generated_id;
  153.                                 fields["feed_id"] = insertedId;
  154.                                 fields["createdAt"] = new Date();
  155.                                 fields["username"] = username;
  156.                                 fields["icon-image"] = "star";
  157.                                 fields["description"] = hashtags;
  158.                                 fields["image1"] = image1;
  159.                                 fields["image2"] = image2;
  160.                                 fields["image3"] = image3;
  161.                                 fields["image4"] = image4;
  162.  
  163.                                 var myquery = { "_id": projectid };
  164.                                 var newvalues = { $push: { "layers.$[elem].geojson.features": { id: featureid, type: "Feature", properties: fields, geometry: { coordinates: [longitude, latitude], type: "Point" } } } };
  165.  
  166.                                 dbo.collection("projects").updateOne(myquery, newvalues, { arrayFilters: [{ "elem._id": { $eq: layerid } }] }, function(err, result) {
  167.                                     if (err) {
  168.                                         console.log(err);
  169.                                         res.body = { message: "error", error: JSON.stringify(err) };
  170.                                         callback(null, res.body);
  171.                                         db.close();
  172.                                     }
  173.                                     else {
  174.                                         console.log(result);
  175.                                         res.body = { message: "success", _id: insertedId };
  176.                                         callback(null, res.body);
  177.                                         db.close();
  178.                                     }
  179.                                 });
  180.                             }
  181.                         });
  182.                     }
  183.                 });
  184.             }
  185.             else {
  186.                 res.body = { message: "error" };
  187.                 callback(null, res.body);
  188.             }
  189.         });
  190.     }
  191. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement