From b0e124ddd35ea029c2b773fca3eed1ddcacc4a16 Mon Sep 17 00:00:00 2001 From: Seb Date: Mon, 8 Jan 2024 22:58:49 -0800 Subject: [PATCH] fit: add activity age guard --- packages/fitness/src/LoggedWorkout.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/fitness/src/LoggedWorkout.js b/packages/fitness/src/LoggedWorkout.js index 73f5468..d35cfc8 100644 --- a/packages/fitness/src/LoggedWorkout.js +++ b/packages/fitness/src/LoggedWorkout.js @@ -220,6 +220,12 @@ const isInactive = (user) => { return isAfter(limit, lastActive); }; +const isTooOld = (activity) => { + const started = new Date(activity.start_date); + const age = differenceInHours(new Date(), started); + return age > 48; +}; + // -- strava api const fetchToken = (refreshToken) => @@ -283,13 +289,11 @@ const postWorkout = (discord, db) => async (stravaId, activityId) => { } const [activity, streams] = data; - const started = new Date(activity.start_date); - const age = differenceInHours(new Date(), started); - // if (age > 48) { - // return new Error( - // `Activity is too old (${age} hours). It was started ${started.toLocaleDateString()} and today is ${new Date().toLocaleDateString()}` - // ); - // } + if (isTooOld(activity)) { + return new Error( + `Activity is too old. It was started ${started.toLocaleDateString()} and today is ${new Date().toLocaleDateString()}` + ); + } const workouts = loggedWorkoutCollection(db); const workout = workoutFromActivity(user, activity, streams);