Skip to content

Instantly share code, notes, and snippets.

@frogermcs
Created November 5, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frogermcs/63e62b4aea7796c7bc8ea2bbb7931f25 to your computer and use it in GitHub Desktop.
Save frogermcs/63e62b4aea7796c7bc8ea2bbb7931f25 to your computer and use it in GitHub Desktop.
class TimeManager {
constructor(firebase, geoTz, moment, userManager) {
this.firebase = firebase;
this.geoTz = geoTz; //geo-tz library
this.moment = moment; //moment-timezone library
this.userManager = userManager;
}
getTodayStartTimestampForAssistantUser(assistantUserId) {
return this.getAssistantUserTimeData(assistantUserId)
.then(userTimeData => {
if (userTimeData) {
return this.moment.tz(userTimeData.timezone).startOf("day").toDate();
} else {
return this.moment.tz(this.moment.tz.guess()).startOf("day").toDate();
}
});
}
getPlatformTime(timeFormat = 'h:mm a') {
const localTimezone = this.moment.tz.guess();
return Promise.resolve(this.moment.tz(localTimezone).format(timeFormat));
}
getTimeZoneFromCoordinates(coordinates) {
return this.geoTz.tz(coordinates.latitude, coordinates.longitude);
}
saveAssistantUserTimezone(assistantUserId, timezone) {
return this._dbRefToAssistantUserTimeDataPromise(assistantUserId)
.then(ref => ref.set({timezone: timezone}));
}
getAssistantUserTimeData(assistantUserId) {
return this._dbRefToAssistantUserTimeDataPromise(assistantUserId)
.then(ref => ref.once('value'))
.then(snapshot => snapshot.val());
}
_dbRefToAssistantUserTimeDataPromise(assistantUserId) {
return this.userManager.ensureAuthUser()
.then(() => this.firebase.database().ref('userTime/' + assistantUserId));
}
}
module.exports = TimeManager;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment