Created
November 5, 2017 13:43
-
-
Save frogermcs/63e62b4aea7796c7bc8ea2bbb7931f25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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