Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database reading issue. #114

Open
saife245 opened this issue May 27, 2019 · 0 comments
Open

database reading issue. #114

saife245 opened this issue May 27, 2019 · 0 comments

Comments

@saife245
Copy link

Please help me to get out of this,

'use strict';
const http = require('http');
// Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');
// Your Google Cloud Platform project ID
const projectId = 'trying-self';
// Instantiates a client
const datastore = Datastore({
projectId: projectId
});
/*
//declaration of data read
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp({
credential:admin.credential.applicationDefault(),
databaseURL:'ws://trying-self.firebaseio.com/',
});
process.env.DEBUG = 'dialogflow:debug';
/
const kind = 'Details';
exports.dialogflowFirebaseFulfillment = (req, res) => {
console.log('Dialogflow Request body: ' + JSON.stringify(req.body));
let type = req.body.queryResult.outputContexts[1].parameters['prod.original'];
let salary = req.body.queryResult.outputContexts[1].parameters['number.original'];
let name = req.body.queryResult.outputContexts[1].parameters['given-name.original'];
let phone_number = req.body.queryResult.outputContexts[1].parameters['phone-number.original'];
let email = req.body.queryResult.outputContexts[1].parameters['email.original'];
let install = req.body.queryResult.outputContexts[1].parameters['number1.original'];
let scor = req.body.queryResult.outputContexts[1].parameters['number2.original'];
let limit = req.body.queryResult.outputContexts[1].parameters['number3.original'];
let respo = req.body.queryResult.outputContexts[1].parameters['resp.original'];
let turn = req.body.queryResult.outputContexts[1].parameters['number4.original'];
let loan = req.body.queryResult.outputContexts[1].parameters['number5.original'];
let btype = req.body.queryResult.outputContexts[1].parameters['btype.original'];
let bank = req.body.queryResult.outputContexts[1].parameters['bank.original'];
let branch = req.body.queryResult.outputContexts[1].parameters['branch.original'];
console.log('looking for ' + type);
console.log('salary is '+ salary);
console.log('name is '+ name);
console.log('phone number is '+ phone_number);
console.log('email id is '+ email);
console.log('installment is '+ install);
console.log('credit score is '+ scor);
console.log('credit card limit is '+ limit);
console.log(respo);
console.log('turn over is '+ turn);
console.log('requirement is'+ loan);
console.log('i have my' +btype);
console.log('bank is '+ bank);
console.log('branch is '+ branch);
function randomIntInc (low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
let ticketnum = randomIntInc(11111,99999);
// The Cloud Datastore key for the new entity
function d_ratio(install, limit, salary){
return (((0.05
parseFloat(limit))+parseFloat(install))/parseFloat(salary))100;
}
const dbr = parseFloat(d_ratio(install,limit, salary));
const taskKey = datastore.key(kind);
// Prepares the new entity
if (type.toUpperCase() == 'CREDIT CARD'){
if (respo.toUpperCase()=='NO'){
const dbr = 10;
//dbr = 0;
limit = parseInt(0);
install = parseInt(0);
}else{
const dbr = parseFloat(d_ratio(install,limit, salary));
}
}
/

function handleswift(bank){
return admin.database().ref('swift').once('value').then((snapshot) => {
var value = snapshot.child(bank).val();
if(value !== null){
res.send(JSON.stringify({ 'fulfillmentText': "swift code is " + value}));
}
});
}

function handleswift(bank, branch){
return datastore.ref('swift').once('value').then((snapshot) => {
var value = snapshot.child(branch).val();
if(value !== null){
res.send(JSON.stringify({ 'fulfillmentText': "swift code is" +value}));
}
});
}
if (type.toLowerCase() =='swift code'){
handleswift(bank, branch);
}
const kind2 = 'swift';
const taskKey2 = datastore.key(kind2);
//const value = taskKey2.collection(kind2).doc(bank).field(branch).get()
const task2 = {key:taskKey, data};
if (type.toLowerCase() == 'swift code'){
//handleswift(bank);
//admin.collection('swift').doc(bank).get()
datastore.collection('swift').doc(bank).field(branch).get()
.then(doc=>{
if (!doc.exists){
res.send(JSON.stringify({ 'fulfillmentText': "No such documents"}));
}else{
res.send(JSON.stringify({ 'fulfillmentText': "swift code is " + doc}));
}
})
.catch((err) => {
console.error('ERROR:', err);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': "Error occurred while saving, try again later", 'displayText': "Error occurred while saving, try again later" }));
})
}*/

const task = {
key: taskKey,
data: {
  //description: ticketDescription,
  //username: username,
  //phoneNumber: phone_number,
  LOOK : type,
  Salary: salary,
  username: name,
  phoneNumber: phone_number,
  emailId : email,
  DBR: dbr,
  INSTALLMENT: install,
  credit: scor,
  limit: limit,
  ticketNumber: ticketnum,
  turn_Over : turn,
  Loan_Amount:loan
}

};
console.log("incidence is " , task);
// Saves the entity
datastore.save(task)
.then(() => {
console.log(Saved ${task.key}: ${task.data.description});
res.setHeader('Content-Type', 'application/json');
//Response to send to Dialogflow
//res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your ticket, the ticket number is " + ticketnum + ". Someone from the helpdesk will reach out to you within 24 hours."}));
//res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your ticket, the ticket number is " + ticketnum + ". Someone from the helpdesk will reach out to you within 24 hours.", 'fulfillmentMessages': "I have successfully logged your ticket, the ticket number is " + ticketnum + ". Someone from the helpdesk will reach out to you within 24 hours."}));
if (type.toLowerCase() == 'swift code'){
const query1 = datastore.createQuery('swift').filter('bank', '=', 'SBI');
if (bank == "SBI") {
return datastore.runQuery(query1).then(results => {
req.ask(results[0][0].code);
});
}
}
if ((type.toUpperCase() == 'PERSONAL LOAN')||(type.toUpperCase() == 'HOME LOAN')){
if (parseFloat(salary) >= 5000){
if (respo.toUpperCase()=='NO'){
if (parseFloat(scor) >= 750){
res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your eligibility, the ticket number is " + ticketnum + ".Someone from the helpdesk will reach out to you within 24 hours."}));
}else{
res.send(JSON.stringify({ 'fulfillmentText': "You are not eligible for loan because your credit score is low. Our agent will get back to you."}));
}
}else{
if (parseFloat(dbr) < 50){
if (parseFloat(scor) >= 750){
res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your eligibility, the ticket number is " + ticketnum + ".Someone from the helpdesk will reach out to you within 24 hours."}));
}else{
res.send(JSON.stringify({ 'fulfillmentText': "You are not eligible for loan because your credit score is low. Our agent will get back to you."}));
}
}else{
res.send(JSON.stringify({ 'fulfillmentText': "You are not eligible for loan because your DBR(debt burden ratio) is greater then 50%. Our Agent will contact you to help."}));
}
}
}else{
res.send(JSON.stringify({ 'fulfillmentText': "You are not eligible for loan as your salary is low, thanks for visiting. Our Agent will contact you to help."}));
}
}
if (type.toUpperCase() == 'CREDIT CARD'){
if (parseFloat(salary) >= 5000){
if (respo.toUpperCase()=='NO'){
res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your eligibility, the ticket number is " + ticketnum + ".Someone from the helpdesk will reach out to you within 24 hours."}));
}else{
if (parseFloat(dbr) < 50){
res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your eligibility, the ticket number is " + ticketnum + ".Someone from the helpdesk will reach out to you within 24 hours."}));
}else{
res.send(JSON.stringify({ 'fulfillmentText': "You are not eligible for loan because your DBR(debt burden ratio) is greater then 50%. Our Agent will contact you to help."}));
}
}
}else{
res.send(JSON.stringify({ 'fulfillmentText': "You are not eligible for loan as your salary is low, thanks for visiting. Our Agent will contact you to help."}));
}
}
if (type.toLowerCase() == 'business loan'){
if (respo.toLowerCase() =='yes'){
res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your eligibility, the ticket number is " + ticketnum + ".Someone from the helpdesk will reach out to you within 24 hours."}));
}else{
res.send(JSON.stringify({ 'fulfillmentText': "someone from the helpdesk will reach out to you within 24 hours."}));
}
}/*
if (type.toLowerCase() == 'swift code'){
//handleswift(bank);
admin.collection('swift').doc(bank).get()
.then(doc=>{
if (!doc.exists){
res.send(JSON.stringify({ 'fulfillmentText': "No such documents"}));
}else{
res.send(JSON.stringify({ 'fulfillmentText': "swift code is " + doc}));
}
})
.catch((err) => {
console.error('ERROR:', err);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': "Error occurred while saving, try again later", 'displayText': "Error occurred while saving, try again later" }));
})
}*/
})
.catch((err) => {
console.error('ERROR:', err);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': "Error occurred while saving, try again later", 'displayText': "Error occurred while saving, try again later" }));
});
}

here we store the user details in 'Details' when they visit for query and read the data from 'swift' and give the result to the user when they ask for swift code.

It write the user details in datastore but unable to read the database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant