public void GivenEmptyGoalObjectWithValidUserObject_WhenGetGoalResultForProjectIsEnvoked_ThenReturnsNull()
        {
            // Arrange
            var mockConfiguration       = new Mock <IConfiguration>();
            var mockConfigurationHelper = new Mock <ConfigurationHelper>(mockConfiguration.Object);
            var mockCryptHelper         = new Mock <CryptHelper>(mockConfigurationHelper.Object);
            var mockTogglHttpClient     = new Mock <TogglHttpClient>(mockCryptHelper.Object);

            mockTogglHttpClient.Setup(s => s.PostAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <HttpContent>()))
            .ReturnsAsync(string.Empty);

            var sut = new TogglHelper(mockTogglHttpClient.Object);

            // Act
            var result = sut.GetGoalResultForProject(_emptyGoal, _validUser, _startDate, _endDate);

            // Assert
            Assert.Null(result);
        }
        public void GivenValidObjectsWithTogglResultData_WhenGetGoalResultForProjectIsEnvoked_ThenReturnsParsedResults()
        {
            // Arrange
            var validJsonResponseString = "{  \"seconds\": 12360,  \"rates\": null,  \"graph\": [    {      \"seconds\": 0,      \"by_rate\": null    },    {      \"seconds\": 1560,      \"by_rate\": null    },    {      \"seconds\": 0,      \"by_rate\": null    },    {      \"seconds\": 3600,      \"by_rate\": null    },    {      \"seconds\": 0,      \"by_rate\": null    },    {      \"seconds\": 7200,      \"by_rate\": null    },    {      \"seconds\": 0,      \"by_rate\": null    }  ],  \"resolution\": \"day\"}";

            var mockConfiguration       = new Mock <IConfiguration>();
            var mockConfigurationHelper = new Mock <ConfigurationHelper>(mockConfiguration.Object);
            var mockCryptHelper         = new Mock <CryptHelper>(mockConfigurationHelper.Object);
            var mockTogglHttpClient     = new Mock <TogglHttpClient>(mockCryptHelper.Object);

            mockTogglHttpClient.Setup(s => s.PostAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <HttpContent>()))
            .ReturnsAsync(validJsonResponseString);

            var sut = new TogglHelper(mockTogglHttpClient.Object);

            // Act
            var result             = sut.GetGoalResultForProject(_validGoal, _validUser, _startDate, _endDate);
            var resultTrackiedlist = result.TrackedEntryDetails.ToList();

            // Assert
            Assert.NotNull(result);
            Assert.Equal(_validGoal.Id, result.GoalId);
            Assert.Equal(_validGoal.GoalFriendlyName, result.GoalFriendlyName);

            // We have all tracked entries
            Assert.NotEmpty(result.TrackedEntryDetails);
            Assert.Equal(7, result.TrackedEntryDetails.Count());

            // Using our sample result the 2nd entry should match the following
            Assert.True(resultTrackiedlist[1].GoalMet);
            Assert.Equal("1560000", resultTrackiedlist[1].GoalTrackedDurationInMs);
            Assert.Equal("00h:26m:00s", resultTrackiedlist[1].GoalTrackedDurationParsed);
            Assert.Equal(DateTime.Parse("2019-05-06T08:30:39.9120605+01:00").ToLongDateString(), resultTrackiedlist[1].Date.ToLongDateString());

            // And the last should match these
            Assert.False(resultTrackiedlist[6].GoalMet);
            Assert.Equal("0", resultTrackiedlist[6].GoalTrackedDurationInMs);
            Assert.Equal("00h:00m:00s", resultTrackiedlist[6].GoalTrackedDurationParsed);
            Assert.Equal(DateTime.Parse("2019-05-11T08:30:39.9120605+01:00").ToLongDateString(), resultTrackiedlist[6].Date.ToLongDateString());
        }
        public async Task <CustomApiResult> GetGoalResults()
        {
            string nameIdentifier = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

            var usr = await _userService.GetByNameIdentifier(nameIdentifier);

            // If no user, die
            if (usr == null || string.IsNullOrWhiteSpace(usr.TogglApiKey))
            {
                return(new CustomApiResult(_responseHelper.NotFound("No user found")));
            }

            // Get their saved goals
            var goals = await _goalService.GetForUserId(usr.Id);

            if (goals?.Count() > 0)
            {
                var goalResults = new List <GoalResult>();

                var _now = DateTime.Now;

                // calculate start of week
                int diff         = (7 + (_now.DayOfWeek - DayOfWeek.Monday)) % 7;
                var _startOfWeek = _now.AddDays(-1 * diff).Date;
                var _endOfWeek   = _startOfWeek.AddDays(6);

                // Hit toggl and retrieve our result objects for each goal
                foreach (var userGoal in goals)
                {
                    goalResults.Add(_TogglHelper.GetGoalResultForProject(userGoal, usr, _startOfWeek,
                                                                         _endOfWeek));
                }

                // All done, return
                return(new CustomApiResult(_responseHelper.ValidResponse(goalResults.ToArray())));
            }

            return(new CustomApiResult(_responseHelper.NotFound("No results found")));
        }
Ejemplo n.º 4
0
        public async Task <CustomApiResult> GetGoalsMet(DateTime?startDate = null, DateTime?endDate = null)
        {
            string nameIdentifier = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;

            var usr = await _userService.GetByNameIdentifier(nameIdentifier);

            // If no user, die
            if (usr == null || string.IsNullOrWhiteSpace(usr.TogglApiKey))
            {
                return(new CustomApiResult(_responseHelper.NotFound("User not found")));
            }

            // Deal with date defaults
            if (startDate == null || startDate == DateTime.MinValue || startDate == DateTime.MaxValue)
            {
                startDate = DateTime.UtcNow.AddDays(-7);
            }

            if (endDate == null || endDate == DateTime.MinValue || endDate == DateTime.MaxValue)
            {
                endDate = DateTime.UtcNow;
            }

            // Make sure they are the correct way round
            if (startDate > endDate)
            {
                var temp = startDate;
                startDate = endDate;
                endDate   = temp;
            }

            // Get their saved goals
            var goals = await _goalService.GetForUserId(usr.Id);

            if (goals == null || goals.Count() < 1)
            {
                return(new CustomApiResult(_responseHelper.NotFound("No goals found")));
            }

            var goalResults = new List <GoalResult>();

            // Hit toggl and retrieve our result objects for each goal
            foreach (var userGoal in goals)
            {
                goalResults.Add(_TogglHelper.GetGoalResultForProject(userGoal, usr, (DateTime)startDate,
                                                                     (DateTime)endDate));
            }

            var totalGoalsMet = 0;
            var totalGoalsSet = 0;

            foreach (var goal in goalResults)
            {
                totalGoalsMet += goal.TrackedEntryDetails.Count(e => e.GoalMet == true);
                totalGoalsSet += goal.TrackedEntryDetails.Count();
            }

            // All done, return
            return(new CustomApiResult(_responseHelper.ValidResponse(new GoalStats
            {
                GoalsAchieved = totalGoalsMet,
                GoalsAttempted = totalGoalsSet,
                StartDate = startDate,
                EndDate = endDate
            })));
        }