how to properly perform query by date in Firestore? - node.js

I am trying to make a cronjob to delete an event that has been passed by using http trigger. today is 7th September, so the event at 5 september ( 6fqmROcWD7K1pTFtXmJJ )like the picture below shall be captured in the query snapshot. but the document length is zero
here is the code I use:
export const cronJobDeletingPastEvents = functions.https.onRequest(async (request,response) => {
const dateNow = Date.now()
const pastEventsSnapshot = await admin.firestore().collection("events").where('dateTimeFinish', '<', dateNow).get()
console.log(pastEventsSnapshot.docs.length) // <---- the result is zero
// perform some actions ....
}
I don't know what went wrong in here, but I guess this is because dateNow is number, but the field dateTimeFinish in firestore is Timestamp object. but i dont know how to fix that

The following will work:
const dateNow = new Date()
const pastEventsSnapshot = await admin.firestore().collection("events").where('dateTimeFinish', '<', dateNow).get()
...
The followings would also work:
const dateNow = new Date('2018-09-07T01:25:54.000Z')
const dateNow = new Date('2018-09-07')

Related

how to calculate duration with cloud functions

i want to calculate duration for a given task/event. I am using cloud functions. I have four fields in flutter app;
Start date
Start time
End date
End time
how can i calculate the duration. Please help. i am using typescript for my cloud functions.
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
admin.initializeApp()
export const updateCharts =
functions.firestore.document('users/{UserId}/count/{uid}')
.onWrite(async(change, _) => await updateStats(change))
async function updateStats (change:
functions.Change<functions.firestore.DocumentSnapshot>){
const chartRating = change.after.ref.parent
let title = (await chartRating.where('Title', '==', 'Game').get()).size;
//duration
const restaurantRef = chartRating.parent!
console.log('{restaurantRef.path} now has ${Title}')
await restaurantRef.update({
Title: Title,
})
// place this code where you want to measure from.
var startTime = new Date().getTime();
...
// place this code where you want to get the difference
var diff = new Date().getTime() - startTime;
Use the getTime method to get the time in total milliseconds since 1970-01-01.

Firebase Admin in Cloud Functions data manipulation problem

Firebase Realtime Database structure
freepacks contains 2 important elements:
current, that is the quizpack ID that I will download from the mobile app (retrieving it from quizpacks).
history, that is a node where I add all the quizpacks ID that I put in current over time with a scheduled function in Cloud Functions.
What I need to do EVERY TIME THE CLOUD FUNCTION EXECUTES
Step 1: Add value of current in history with the current timestamp.
Step 2: Substitute the current value with another quizpack ID that is not in history.
Done
How I tried to accomplish this target
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.scheduledFunction = functions.pubsub.schedule('* * * * *').onRun((context) => {
// Current timestamp
const dt = new Date();
const timestamp = `${
(dt.getMonth()+1).toString().padStart(2, '0')}/${
dt.getDate().toString().padStart(2, '0')}/${
dt.getFullYear().toString().padStart(4, '0')} ${
dt.getHours().toString().padStart(2, '0')}:${
dt.getMinutes().toString().padStart(2, '0')}:${
dt.getSeconds().toString().padStart(2, '0')}`
// Download the entire node 'freepacks'
return admin.database().ref('quiz/freepacks').once('value').then((currentSnap) => {
const currentPack = currentSnap.val().current;
// Add current to history
admin.database().ref('quiz/freepacks/history/' + currentPack).set(timestamp);
// Download entire history node
admin.database().ref('quiz/freepacks/history').once('value').then((historySnap) => {
const history = historySnap.val();
console.log('HISTORY: ' + history);
// Download entire quizpacks node
admin.database().ref('quiz/quizpacks').once('value').then((quizpacksSnap) => {
for(quizpack in Object.keys(quizpacksSnap.val())) {
console.log('ITERANDO: ' + quizpack);
// Add the new current if it isn't in history
if (historySnap[quizpack] == undefined) {
admin.database().ref('quiz/freepacks/current').set(quizpack);
break;
}
}
});
})
});
});
What I get from the previous code
Starting point:
First execution: history updates well but updating current didn't work
Second execution ad so on...
current doesn't update anymore (it is stuck on 0)
My experience with JavaScript and Firebase Admin is ~0... What is the problem with my code? Thanks in advance for the help!
First thing is all read/write operations return promises hence you should handle them. In this answer I used async-await syntax. .ref("quiz/freepacks") fetches the complete node i.e. current and the history so you don't have to query the history node again as in original code. Other changes are just Javascript tweaks such as using .find() instead of for-loop and so on..
Try changing your function to:
exports.scheduledFunction = functions.pubsub
.schedule("* * * * *")
.onRun(async (context) => {
// Getting Date
const dt = new Date();
const timestamp = `${(dt.getMonth() + 1).toString().padStart(2, "0")}/${dt
.getDate()
.toString()
.padStart(2, "0")}/${dt.getFullYear().toString().padStart(4, "0")} ${dt
.getHours()
.toString()
.padStart(2, "0")}:${dt.getMinutes().toString().padStart(2, "0")}:${dt
.getSeconds()
.toString()
.padStart(2, "0")}`;
// Download the entire node 'freepacks'
const currentSnap = await firebase
.database()
.ref("quiz/freepacks")
.once("value");
// Checking current free pack ID and array of previous free packs
const currentPack = currentSnap.val().current || "default";
const freeHistoryIDs = [
...Object.keys(currentSnap.val().history || {}),
currentPack,
];
// Add current free pack to free history
await firebase
.database()
.ref("quiz/freepacks/history/" + currentPack)
.set(timestamp);
// Download entire quizpacks node
const quizpacksSnap = await firebase
.database()
.ref("quiz/quizpacks")
.once("value");
const quizPackIDs = Object.keys(quizpacksSnap.val() || {});
const newPack = quizPackIDs.find((id) => !freeHistoryIDs.includes(id));
console.log(newPack);
if (!newPack) {
console.log("No new pack found")
}
return firebase.database().ref("quiz/freepacks/current").set(newPack || "none");
});

Pub/Sub Cloud Function does not Update Document in Subcollection

I am trying to update a field in my document in Firestore. The general location of the document would be "/games/{userId}/userGames/{gameId}. And in this game, there is a property called "status" which changes accordingly to the games start and end time.
As you can guess, the if the start time is bigger than the "now" timestamp and the status is "TO_BE_PLAYED", the game will begin and the status will be 1, "BEING_PLAYED". Also, if the end time is bigger than the "now" timestamp and the status is "BEING_PLAYED", the game will end, therefore the status will be 2, "PLAYED". I want to create a cloud function that is capable to do so.
However, even if the function logs output 'ok', the values are never updated. Unfortunately, I do not have that much experience in Javascript too.
THE CODE
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const STATUS_PLAYED = 2;
const STATUS_BEING_PLAYED = 1;
const STATUS_TO_BE_PLAYED = 0;
exports.handleBeingPlayedGames = functions.runWith({memory: "2GB"}).pubsub.schedule('* * * * *')
.timeZone('Europe/Istanbul') // Users can choose timezone - default is America/Los_Angeles
.onRun(async () => {
// current time & stable
// was Timestamp.now();
const now = admin.firestore.Timestamp.fromDate( new Date());
const querySnapshot = await db.collection("games").get();
const promises = [];
querySnapshot.forEach( doc => {
const docRef = doc.ref;
console.log(docRef);
promises.push(docRef.collection("userGames").where("status", "==", STATUS_BEING_PLAYED).where("endtime", "<", now).get());
});
const snapshotArrays = await Promise.all(promises);
const promises1 = [];
snapshotArrays.forEach( snapArray => {
snapArray.forEach(snap => {
promises1.push(snap.ref.update({
"status": STATUS_PLAYED,
}));
});
});
return Promise.all(promises1);
});
exports.handleToBePlayedGames = functions.runWith({memory: "2GB"}).pubsub.schedule('* * * * *')
.onRun(async () => {
// current time & stable
// was Timestamp.now();
const now = admin.firestore.Timestamp.fromDate(new Date());
const querySnapshot = await db.collection("games").get();
const promises = [];
querySnapshot.forEach( async doc => {
const docData = await doc.ref.collection("userGames").where("status", "==", STATUS_TO_BE_PLAYED).where("startTime", ">", now).get();
promises.push(docData);
});
const snapshotArrays = await Promise.all(promises);
const promises1 = [];
snapshotArrays.forEach( snapArray => {
snapArray.forEach(snap => {
promises1.push(snap.ref.update({
"status": STATUS_BEING_PLAYED,
}));
});
});
return Promise.all(promises1);
});
Okay, so this answer goes to lurkers trying to solve this problem.
First I tried to solve this problem by brute force and not including much thinking and tried to acquire the value in subcollection. However, as I searched, I've found that denormalizing (flattening) data actually solves the problem a bit.
I created a new directory under /status/{gameId} with the properties
endTime, startTime, and status field and I actually did it on a single level by using promises. Sometimes denormalizing data can be your savior.
How can startTime be greater than now? Is it set by default to a date in the future?
My current assumption is that a game cannot set it's status to STATUS_BEING_PLAYED because of the inconsistency with startTime. Moreover, a game cannot have the status STATUS_PLAYED because it depends on having STATUS_BEING_PLAYED, which cannot have.
My recommendation would be to set the field startTime and endTime to null by default. If you do so you can check if a game has to be set to STATUS_BEING_PLAYED with this:
doc.ref.collection("userGames")
.where("status", "==", STATUS_TO_BE_PLAYED)
.where("startTime", "<", now)
.where("endTime", "==", null)
.get();
You could check if a game has to be on STATUS_PLAYED with this (exactly as you did):
docRef.collection("userGames")
.where("status", "==", STATUS_BEING_PLAYED)
.where("endtime", "<", now)
.get();
Now there's something that you should wonder, is this the best approach to change a game's status? You are querying the whole game library of a user every single minute as you know read operations are charged so this approach would imply meaningful charges. Maybe you should simply use update the game's status when the game is started and closed.
Also notice that the equals operation is ==, not =.

Moment is getting the wrong date in Node JS mssql query

I am trying to pass moment dates as variables in a query. When I console log the dates are correct but when the query runs it gets the current date -1...
Here is the code:
const dateEnd = moment().format('YYYY-MM-DD 23:59:59');
const dateStart = moment().format('YYYY-MM-DD 00:00:00');
// console.log(dateEnd)<-- They come back correct
// console.log(dateStart)
// process.exit();
let pool = await sql.connect(dbconfig)
//Get records from table
let getRecords = await pool.request()
.input('dateStart', sql.Date, dateStart)
.input('incrm', sql.VarChar, 'yes')
.input('dateEnd', sql.Date, dateEnd)
.query('select * from database.dbo.table where someValue in (231312,7132133) and incrm=#incrm and processeddate between #dateStart and #dateEnd and email is not null')
The node-mssql library expects dates to be passed as Date objects.
Try this, if you want the current day in the same time zone as the machine you're running this code from:
const dateStart = moment().startOf('day').toDate();
const dateEnd = moment().endOf('day').toDate();
Or, if you want the current UTC day, then:
const dateStart = moment.utc().startOf('day').toDate();
const dateEnd = moment.utc().endOf('day').toDate();
Or, if you want the current day in a particular time zone (say US Central Time), then use the moment-timezone add-on library:
const dateStart = moment.tz('America/Chicago').startOf('day').toDate();
const dateEnd = moment.tz('America/Chicago').endOf('day').toDate();
All three of these assume that the processeddate is in terms of UTC in your database.
You should probably also pass sql.DateTime2 instead of sql.Date.

Compare two Firestore Timestamps in Cloud Functions

I'm writing update function in Firestore and I want to compare two Timestamp. I've tried multiple things but not working. Can you point me correct way of comparing two Timestamp in firestore.
exports.updateFunction = functions.firestore
.document('xyz/{xyzId}')
.onUpdate((change, context) => {
var updatedXYZ = change.after.data();
var oldXYZ = change.before.data();
var newTimestamp = updatedXYZ.timing;
var oldTimestamp = oldXYZ.timing;
// I've tried following things but not working
var result = newTimestamp === oldTimestamp; // not working
var result = new Date(newTimestamp) - new Date(oldTimestamp); // not working
return true;
});
I want to check two Timestamp is same or not.
Consult the API docs for Firestore's Timestamp object. Timestamp has a method called isEqual() that will compare two timestamps.
var result = newTimestamp.isEqual(oldTimestamp);
You need to add this line
admin.firestore().settings( { timestampsInSnapshots: true })
in your index.js file.
After adding this line cloud function will read your timestamp field as Timestamp Datatype. Now you can compare two timestamps using
var result = newTimestamp.isEqual(oldTimestamp);
//I assume you have yourTimestampInMilliseconds
const currentTimestamp = Date.now();
console.log("(currentTimestamp <= yourTimestampInMilliseconds)= "+ (currentTimestamp <= yourTimestampInMilliseconds));
console.log("(currentTimestamp > yourTimestampInMilliseconds)= "+ (currentTimestamp > yourTimestampInMilliseconds));

Resources