How to write firebase function sending message after 5 minutes? - google-cloud-firestore

I'm writing firebase functions using pubsub and scheduler.
I want to publishMessage after 5 minutes later from timilimitScheduler function start.
But timelimitScheduler send message briefly. So cancelReservation function worked and reservation cancelled in 10 seconds...
exports.timelimitScheduler = functions.firestore// eslint-disable-line
.document('Reservation/{documentId}')// eslint-disable-line
.onCreate((snapshot, context) => {// eslint-disable-line
const newReservation = snapshot.data();// eslint-disable-line
const userId = newReservation.userId;// eslint-disable-line
const shopId = newReservation.shopId;// eslint-disable-line
const topicName = 'reservation-cancellation';// eslint-disable-line
const reservationId = context.params.documentId;// eslint-disable-line
const data = JSON.stringify({ reservationId });// eslint-disable-line
const pubSubClient = new PubSub();// eslint-disable-line
const delaySeconds = 300;// eslint-disable-line
const publishTime = new Date(Date.now() + delaySeconds * 1000);// eslint-disable-line
console.log('Scheduled publish time:', publishTime);// eslint-disable-line
const message = {// eslint-disable-line
data: Buffer.from(data),// eslint-disable-line
attributes: {// eslint-disable-line
scheduledTime: publishTime.getTime().toString(),// eslint-disable-line
},// eslint-disable-line
};// eslint-disable-line
return pubSubClient// eslint-disable-line
.topic(topicName)// eslint-disable-line
.publishMessage(message)// eslint-disable-line
.then((messageId) => {// eslint-disable-line
console.log(`Message ${messageId} published at ${publishTime}`);// eslint-disable-line
return null;// eslint-disable-line
})// eslint-disable-line
.catch((error) => {// eslint-disable-line
console.error(`Error publishing message: ${error}`);// eslint-disable-line
throw error;// eslint-disable-line
});// eslint-disable-line
});// eslint-disable-line
exports.cancelReservation = functions.pubsub.topic('reservation-cancellation')// eslint-disable-line
.onPublish((message) => {// eslint-disable-line
const reservationId = message.json.reservationId;// eslint-disable-line
const scheduledTime = parseInt(message.attributes.scheduledTime, 10);// eslint-disable-line
const delaySeconds = Math.floor((Date.now() - scheduledTime) / 1000);// eslint-disable-line
console.log(`Canceling reservation ${reservationId} (delayed by ${delaySeconds} seconds)`);// eslint-disable-line
return admin.firestore().collection('Reservation').doc(reservationId).get()// eslint-disable-line
.then((reservationDoc) => {// eslint-disable-line
const reservation = reservationDoc.data();// eslint-disable-line
if (reservation.state !== '예약접수') {// eslint-disable-line
return;// eslint-disable-line
}// eslint-disable-line
return admin.firestore().collection('Reservation').doc(reservationId).update({// eslint-disable-line
state: '예약취소',// eslint-disable-line
});// eslint-disable-line
})// eslint-disable-line
.catch((error) => {// eslint-disable-line
console.error(`Error canceling reservation: ${error}`);// eslint-disable-line
throw error;// eslint-disable-line
});// eslint-disable-line
});// eslint-disable-line

Cloud Tasks work great for this. In Firebase you can Enqueue functions with Cloud Tasks which essentially allows you to programmatically queue a task to start at a specified time. The key property for that is scheduleTime which is "time from now" that the function will start.

Related

MongooseError: Operation `users.findOne()` buffering timed out after 10000ms. How do I resolve this?

Error:
MongooseError: Operation `users.findOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (D:\Web Dev\Traversy\storybooks\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:153:23)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
GET /auth/google/callback?code=4%2F0ARtbsJqsdI35M7-9iSldOa1xWXs9SXFW09MohqzZ94_K5I5jpc1YTKge6lGKuS8-jPRyZA&scope=profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile - - ms - -
What I tried to do:
mongoose.connect(...) code is loading except this is my code where I tried to comment out: useFindAndModify: true
db.js
const mongoose = require('mongoose')
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
// useFindAndModify: true
})
console.log(`MongoDB Connected: ${conn.connection.host}`)
} catch(err) {
console.error(err)
process.exit(1)
}
}
module.exports = connectDB
auth.js
const express = require('express')
const passport = require('passport')
const router = express.Router()
// #desc Auth with Google
// #route GET /auth/google
router.get('/google', passport.authenticate('google', { scope: ['profile'] }))
// #desc Google auth callback
// #route GET /auth/google/callback
router.get('/google/callback', passport.authenticate('google', { failureRedirect: '/' }),
(req, res) => {
res.redirect('/dashboard')
})
module.exports = router
passport.js
const GoogleStrategy = require('passport-google-oauth20').Strategy
const mongoose = require('mongoose')
const User = require('../models/User')
module.exports = function (passport) {
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback'
},
async (accessToken, refreshToken, profile, done) => {
// console.log(profile)
const newUser = {
googleId: profile.id,
displayName: profile.displayName,
firstName: profile.name.givenName,
lastName: profile.name.familyName,
image: profile.photos[0].value
}
try {
let user = await User.findOne({ googleId: profile.id })
if (user) {
done(null, user)
} else {
user = await User.create(newUser)
done(null, user)
}
} catch (err) {
console.error(err)
}
}))
passport.serializeUser((user, done) => done(null, user.id))
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => done(err, user))
})
}
App.js
const path = require('path')
const express = require('express')
const dotenv = require('dotenv')
const morgan = require('morgan')
const exphbs = require('express-handlebars')
const passport = require('passport')
const session = require('express-session')
const connectDB = require('./config/db')
// Load config
dotenv.config({ path: './config/config.env' })
// Passport config
require('./config/passport')(passport)
connectDB()
const app = express()
// Logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// Handlebars (Source: https://www.npmjs.com/package/express-handlebars)
app.engine('.hbs', exphbs.engine({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', '.hbs')
// Sessions
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false
}))
// Passport middleware
app.use(passport.initialize())
app.use(passport.session())
// Static folder
app.use(express.static(path.join(__dirname, 'public')))
// Routes
app.use('/', require('./routes/index'))
app.use('/auth', require('./routes/auth'))
const PORT = process.env.PORT || 3000
app.listen(PORT, console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`))

Error: connect ECONNREFUSED 127.0.0.1:8000 in Express Js ( When try to Test with Postman )

I am really new to the industry and have this error when trying to check the database connection via API reuests with postman..... Please help me to settle this issue...
I just want to check the mongodb database by sendng API requests. Still I cannot identify the error and I am following a set of tutorials and occure this issue... Anyone can help me to identify the mistake it's highly appreciated....
{ this is dummy text to avoid please add more details...
Here is my code...
const app = express();
const { MongoClient } = require('mongodb');
const PORT = process.env.PORT || 8000;
// Initialize middleware
// we used to install body parser but now it's a built in middleware
// Function of express. It parses incoming JSONpayload
// app.use(express.json({extended:false}));
app.use(express.json({ extended: false }));
// Test Routs
// app.get("/", (req,res)=>res.send("Hello Aruna !!!"));
// app.post("/", (req,res)=>res.send(`Hello ${req.body.name} `));
// app.get("/hello/:name", (req.res)=>res.send(`Hello ${req.params.name}`))
app.get('/api/articles/:name', async (req, res) => {
try {
const articleName = req.params.name;
const client = await MongoClient.connect('mongodb://localhost:27017');
const db = client.db('mernblog');
const articlesinfo = db
.collection('articles')
.findOne({ name: articleName });
res.status(200).jason(articlesinfo);
client.close();
} catch (error) {
res.status(500).jason({ message: 'Error connecting to database', error });
}
});
app.post('/api/articles/:name/add-comments', (req, res) => {
const { username, text } = req.body;
const articleName = req.params.name;
articlesinfo[articleName].comments.push({ username, text });
res.status(200).send(articlesinfo[articleName]);
});
app.post('/', (req, res) => res.send(`Hello ${req.body.name}`));
app.get('/hello/:name', (req, res) => res.send(`Hello ${req.params.name}`));
app.listen(PORT, () => console.log(`Server is running at port ${PORT}`));
Server.js
Terminal
Error and API request in Postman
You have a typo in your code: jason should be json.
Other tips, you should handle your DB connection in a separate method and change your post request since articlesinfo is not a global variable:
const app = express();
const { MongoClient } = require('mongodb');
const PORT = process.env.PORT || 8000;
const client = new MongoClient('mongodb://localhost:27017');
const connectDB = async () => {
try {
await client.connect();
console.log('Successfully connected to DB')
} catch (err) {
await client.close();
console.log('Error connecting to DB');
process.exit(1);
}
}
// Initialize middleware
// we used to install body parser but now it's a built in middleware
// Function of express. It parses incoming JSONpayload
// app.use(express.json({extended:false}));
app.use(express.json({ extended: false }));
// Test Routs
// app.get("/", (req,res)=>res.send("Hello Aruna !!!"));
// app.post("/", (req,res)=>res.send(`Hello ${req.body.name} `));
// app.get("/hello/:name", (req.res)=>res.send(`Hello ${req.params.name}`))
app.get('/api/articles/:name', async (req, res) => {
try {
const articleName = req.params.name;
const db = client.db('mernblog');
const articlesinfo = db
.collection('articles')
.findOne({ name: articleName });
res.status(200).json(articlesinfo);
client.close();
} catch (error) {
res.status(500).json({ message: 'Error connecting to database', error });
}
});
app.post('/api/articles/:name/add-comments', (req, res) => {
const { username, text } = req.body;
const articleName = req.params.name;
const db = client.db('mernblog');
const articlesinfo = db
.collection('articles')
.updateOne({ name: articleName }, { $push: { comments: { username, text } } });
res.status(200).send(articlesinfo);
});
app.post('/', (req, res) => res.send(`Hello ${req.body.name}`));
app.get('/hello/:name', (req, res) => res.send(`Hello ${req.params.name}`));
connectDB();
app.listen(PORT, () => console.log(`Server is running at port ${PORT}`));

Express mongoDB Integration testing for private/secured routes

I am trying to run a integration test for one of the express routes in the application.
The routed is a protected route allows user to create supplier when user is authenticated.
I am trying to login user before making a request to the 'api/v1/supplier' (protected route) route but not able to login user before calling the Login API give 500 error back, the Login API is working as expected when tested separately.
Here is the test that I am trying. request help!
process.env.NODE_ENV = 'development';
const expect = require('chai').expect;
const request = require('supertest');
const app = require('../../../app.js');
const conn = require('../../../db/index.js');
describe('POST /api/v1/supplier ', () => {
let token = '';
before(done => {
conn
.connect()
.then(done())
.catch(err => done(err));
});
after(done => {
conn
.close()
.then(done())
.catch(err => done(err));
});
it('Error, on unauthorized POST Supplier request', done => {
request(app)
.post('/api/v1/users/login')
.send({ email: 'sgrmhdk00#gmail.com', password: '12345678' })
.end(function(err, res) {
token = res.body.token;
});
request(app)
.post('/api/v1/supplier')
.set('Authorization', 'Bearer' + token)
.send({ supplierID: '1234567' })
.then(res => {
const body = res.body;
expect(body).to.contain.property('status');
expect(body).to.contain.property('error');
done();
})
.catch(err => done(err));
});
});
db/index.js
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const DB_URI = 'mongodb://localhost:27017/myapp';
function connect() {
return new Promise((resolve, reject) => {
dotenv.config({ path: './config.env' });
const setDatabase = () => {
if (process.env.NODE_ENV === 'development') {
const DB = process.env.DATABASE;
return DB;
} else {
const DB = process.env.DATABASE_PRODUCTION.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
return DB;
}
};
const DB = setDatabase();
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
.then(() => console.log('DB connection successful!'));
});
}
function close() {
return mongoose.disconnect();
}
module.exports = { connect, close };
You need to call you API with token, but the two calls are asynchronous, you need call the second method inside the end of the first:
it('Error, on unauthorized POST Supplier request', done => {
request(app)
.post('/api/v1/users/login')
.send({ email: 'sgrmhdk00#gmail.com', password: '12345678' })
.end(function(err, res) {
if(err){
done(err)
return
}
token = res.body.token;
request(app)
.post('/api/v1/supplier')
.set('Authorization', 'Bearer' + token)
.send({ supplierID: '1234567' })
.then(res => {
const body = res.body;
expect(body).to.contain.property('status');
expect(body).to.contain.property('error');
done();
})
.catch(err => done(err));
});
});

agent.add not working but console.log working

In below code, "agent.add" is not working but "console.log" is working. I have added promise with resolve and reject, but still its not working. I have tried different ways but multiple response from firestore, not able to send it to user. Able to see logs in firebase but not in dialogflow.
const {Firestore} = require('#google-cloud/firestore');
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const admin = require('firebase-admin');
admin.initializeApp();
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging
statements
const firestore = new Firestore();
const settings = {/* your settings... */
timestampsInSnapshots: true};
firestore.settings(settings);
exports.dialogflowFirebaseFulfillment =
functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' +
JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
const db = admin.firestore();
```
function welcome(agent) {
agent.add(`Welcome to my agent!`); // not working
console.log("Welcome Agent");
const num = agent.parameters.number;
let usersRef = db.collection('add');
let query = usersRef.where('Number', '==', num);
return new Promise((resolve,reject)=>{ // not working
return query.get()
.then(querySnapshot => {
if (querySnapshot.empty) {/*
const timestamp = querySnapshot.get('created_at');
const date = timestamp.toDate();*/
console.log('No matching documents.');
agent.add(`No Matching Documents`);
return;
}
querySnapshot.forEach(doc => {
const line1 = doc.get('Line1');
const line2 = doc.get('Line2');
const explain = doc.get('explanation');
console.log('Line1: ', line1); //this works
console.log('Line2: ', line2); //this works
console.log('explain: ', explain); //this works
agent.add(`your response is ` +doc.get('Line1')); //not working
agent.add(`Final Response - ${line2}`); //not working
agent.add(doc.get('explanation')); //not working
});
resolve('resolved');
})
.catch(err => {
console.log('Error getting documents', err);
reject(err);
});
});
}
Issue is resolved now. Added return statement in the last agent.add and it is working. Thanks.
agent.add(your response is +doc.get('Line1'));
agent.add(Final Response - ${line2});
return agent.add(doc.get('explanation'));

Passport: Error: passport.initialize() middleware not in use;

I'm have an express server with MongoDB and Mongoose, and using passport to authenticate with JWT, but getting an error as in the title.
I'm following the passport-jwt documentation, but am still getting the error. What am I doing wrong?
Here is the error message when doing GET call on localhost3090 with a valid JWT:
::1 - - [16/Mar/2018:05:35:47 +0000] "GET / HTTP/1.1" 500 1677 "-" "PostmanRuntime/7.1.1"
Error: passport.initialize() middleware not in use
at IncomingMessage.req.login.req.logIn (/Users/okadachikara/react-courses/projects/server/node_modules/passport/lib/http/request.js:46:34)
at JwtStrategy.strategy.success (/Users/okadachikara/react-courses/projects/server/node_modules/passport/lib/middleware/authenticate.js:248:13)
at verified (/Users/okadachikara/react-courses/projects/server/node_modules/passport-jwt/lib/strategy.js:115:41)
at /Users/okadachikara/react-courses/projects/server/services/passport.js:34:7
at /Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/model.js:3930:16
at _init (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/query.js:2007:5)
at model.Document.init (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/document.js:393:5)
at completeOne (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/query.js:1993:12)
at Immediate.<anonymous> (/Users/okadachikara/react-courses/projects/server/node_modules/mongoose/lib/query.js:1520:11)
at Immediate._onImmediate (/Users/okadachikara/react-courses/projects/server/node_modules/mquery/lib/utils.js:119:16)
at runCallback (timers.js:773:18)
at tryOnImmediate (timers.js:734:5)
at processImmediate [as _immediateCallback] (timers.js:711:5)
My server/controllers/authentication.js:
const User = require('../models/user');
const jwt = require('jwt-simple');
const config = require('../config');
function tokenForUser(user) {
const timestamp = new Date().getTime();
return jwt.encode({ sub: user.id, iat: timestamp }, config.secret);
}
exports.signup = function (req, res, next) {
const email = req.body.email;
const password = req.body.password;
if (!email || !password) {
return res.status(422).send({ error: 'You must provide an email and
password' });
}
// see if user with the given email exists
User.findOne({ email: email }, function (err, existingUser) {
if (err) { return next(err); }
if (existingUser) {
return res.status(422).send({ error: 'A user with that email
already exists' });
}
const user = new User({
email: email,
password: password
});
user.save(function (err) {
if (err) { return next(err); }
res.json({ token: tokenForUser(user), iat: jwt.iat });
});
});
};
My server/services/passport.js
const passport = require('passport');
const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config');
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromHeader('authorization'),
secretOrKey: config.secret
};
const jwtLogin = new JwtStrategy(jwtOptions, function (payload, done) {
User.findById(payload.sub, function (err, user) {
if (err) { return done(err, false); }
if (user) {
done(null, user);
} else {
done(null, false);
}
});
});
passport.use(jwtLogin);
My server/router.js
const passport = require('passport');
const Authentication = require('./controllers/authentication');
const passportService = require('./services/passport');
const requireAuth = passport.authenticate('jwt', { sesssion: false });
module.exports = function (app) {
app.get('/', requireAuth, function (req, res) {
res.send({ hi: 'there' });
});
app.post('/signup', Authentication.signup);
};
You need to initialize the passport module before using it:
let app = express();
app.use(passport.initialize());