let processAd = function()

in source/services/marketing/lib/marketing.js [116:205]


  let processAd = function(record,poi,cb){
      var params = {
          TableName: marketingTable,
          KeyConditionExpression: 'trip_id = :tripid and poi_id = :poiid',
          ExpressionAttributeValues: {
                      ':tripid': record.trip_id,
                      ':poiid': poi.poi_id
                  }

      };
      let docClient = new AWS.DynamoDB.DocumentClient(dynamoConfig);
      docClient.query(params, function(err, adata){
          if (err) {
              console.log(err);
              return cb(err, null);
              }
          if(adata) {
              console.log(adata);
              let _exist = _.find(adata.Items, function(item) {
                  return item.poi_id === poi.poi_id;
              });
              console.log(_exist);

              if (!_exist){
                      console.log('has not yet received this ad');
                      let _advertisement = {
                          vin: record.vin,
                          trip_id: record.trip_id,
                          created_at: moment().utc().format(),
                          updated_at: moment().utc().format(),
                          identified_at: moment(record.timestamp).utc().format(),
                          poi_id: poi.poi_id,
                          message: poi.message,
                          action: 'none'
                      };

                      let params = {
                          TableName: marketingTable,
                          Item: _advertisement
                      };

                      let docClient = new AWS.DynamoDB.DocumentClient(dynamoConfig);
                      docClient.put(params, function(err, data){
                          if (err) {
                              console.log(err);
                              return cb(err, null);
                          }
                          if (data){

                              console.log(data);
                              let _adInfo = poi.message;

                              let _mobile = [
                                  '*Notification from',
                                  poi.poi,
                                  '-',
                                  _adInfo
                              ].join(' ');

                              let _hud = _adInfo;

                              let _message = {
                                  type: 'info',
                                  mobile: _mobile,
                                  mqtt: _hud
                              };

                              sendNotification(record.vin, _message, function(err, msg_data){
                                  if (err) {
                                      console.log(err);
                                      return cb(err, null);
                                  }
                                  console.log(msg_data);
                                  return cb(null, _advertisement);
                              });
                          }
                      });
                  } else {
                      console.log('already exists');
                      return cb(null, {});
                  }
          } else {
              return cb({
                  error: {
                      message: 'Error occured querying anomaly table.'
                  }
              }, null);
          }
      });
  };