Skip to content

Commit

Permalink
Merge pull request #265 from capcodigital/feature/fitbit-issues
Browse files Browse the repository at this point in the history
Feature/fitbit issues
  • Loading branch information
andrewkentish authored Jun 28, 2024
2 parents aeb8e7d + c1a8ec8 commit 582b76e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
49 changes: 21 additions & 28 deletions server/controllers/fitbit.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ function buildRequest(requestDetail, requestOptions) {

function updateUser(user) {
var today = new Date();
// Temporary fix for strange date issue
if (user.expires_in && (user.expires_in.getTime() < today.getTime() || user.expires_in.getFullYear() > today.getFullYear())) {
console.log("FitBit Token Expired:" + user.name);
postOptions.path = "/oauth2/token?" + "grant_type=refresh_token&refresh_token=" + user.refresh_token;
Expand All @@ -306,35 +305,29 @@ function updateUser(user) {
.then((result) => {

if (result.errors && result.errors[0]) {
console.log("Token Refresh Error: " + result.errors[0].message);
}

var date = new Date();
var datemillis = date.getTime();
console.log("Re-register request Error for " + user.name + " : " + err);
} else {
var date = new Date();
var datemillis = date.getTime();

var expiresTime = new Date(result.expires_in*1000);
var expiresTimeMillis = expiresTime.getTime();
var expiresTime = new Date(result.expires_in*1000);
var expiresTimeMillis = expiresTime.getTime();

var expiration = new Date();
expiration.setTime(datemillis + expiresTimeMillis);
var expiration = new Date();
expiration.setTime(datemillis + expiresTimeMillis);

user.access_token = result.access_token;
user.refresh_token = result.refresh_token;
user.expires_in = expiration;
user.access_token = result.access_token;
user.refresh_token = result.refresh_token;
user.expires_in = expiration;

user.markModified('access_token');
user.markModified('refresh_token');
user.markModified('expires_in');
user.markModified('access_token');
user.markModified('refresh_token');
user.markModified('expires_in');

console.log("Successfully obtained new FitBit Token for:" + user.name);
console.log("Successfully obtained new FitBit Token for:" + user.name);

getStats(user).then((results) => {
user.save()
.then((updatedUser) => {
}).catch((err) => {
console.log(err);
});
});
updateAccessTokens(user);
}

}).catch((err) => {
console.log(user.name + " : " + err);
Expand Down Expand Up @@ -368,7 +361,7 @@ function getStats(user, date) {
if (result.summary) {
user.activities[date] = result;
} else {
console.log("No FitBit result summary for: " + user.name + " Response: " + result.toString());
console.log("No FitBit result summary for: " + user.name + " Response: " + JSON.stringify(result));
}

console.log("Updating FitBit Stats for: " + user.name);
Expand Down Expand Up @@ -532,11 +525,11 @@ function updateAccessTokens(user) {
});

}).catch((err) => {
console.log(err);
console.log("Error saving user tokens " + user.name + " : " + err);
});
}
}).catch((err) => {
console.log("Error updating existing useer access tokens during re-registration");
console.log("Error updating existing user access tokens during re-registration - " + user.name);
});
}

Expand All @@ -545,7 +538,7 @@ function updateAccessTokens(user) {
*/
function updateEveryInterval(minutes) {

console.log("Begin stats refresh every " + minutes + " minutes");
console.log("Begin FitBit stats refresh every " + minutes + " minutes");
var millis = minutes * 60 * 1000;

setInterval(function(){
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/strava.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ function updateUser(user) {
// If token is expired refresh access token and get a new refresh token
var newReq2 = buildRequest("Update token for " + user.name, userOptions, function(err, result) {
if (err) {
console.log(user.name + " : " + err.message);
console.log("Strava token update error for " + user.name + " : " + err.message);
} else if (result.errors && result.errors.length > 0) {
console.log(user.name + " : " + JSON.stringify(result.errors[0]));
console.log("Strava token update error for " + user.name + " : " + JSON.stringify(result.errors[0]));
} else {

user.access_token = result.access_token;
Expand Down Expand Up @@ -431,7 +431,7 @@ function updateUser(user) {
*/
function updateEveryInterval(minutes) {

console.log("Begin stats refresh every " + minutes + " minutes");
console.log("Begin Strava stats refresh every " + minutes + " minutes");
var millis = minutes * 60 * 1000;

setInterval(function(){
Expand Down

0 comments on commit 582b76e

Please sign in to comment.