public void UpdatePlanToProfileValidate(Guid planId, string oId, PersonalizedPlanViewModel personalizedPlan)
        {
            //arrange
            var profileResponse = userProfileBusinessLogic.GetUserProfileDataAsync(oId);

            profileResponse.ReturnsForAnyArgs(ShareTestData.UserProfileWithoutPlanId);
            var dbResponse = dbService.GetItemAsync <PersonalizedPlanViewModel>(personalizedPlan.PersonalizedPlanId.ToString(), dbSettings.ActionPlansCollectionId);

            dbResponse.ReturnsForAnyArgs <PersonalizedPlanViewModel>(personalizedPlan);
            var response1 = personalizedPlanBusinessLogic.GetPersonalizedPlanAsync(personalizedPlan.PersonalizedPlanId);

            Document       updatedDocument = new Document();
            JsonTextReader reader          = new JsonTextReader(new StringReader(ShareTestData.userProfileWithSharedResource));

            updatedDocument.LoadFrom(reader);

            dbService.UpdateItemAsync <UserProfile>(
                Arg.Any <string>(),
                Arg.Any <UserProfile>(),
                Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument);

            dbService.CreateItemAsync <SharedResources>(
                Arg.Any <SharedResources>(),
                Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument);

            //act
            dynamic response = personalizedPlanBusinessLogic.UpdatePlanToProfile(planId, oId, personalizedPlan);

            //assert
            Assert.Equal("RanToCompletion", response.Status.ToString());
        }
        public async Task <PersonalizedPlanViewModel> MapViewModel(UnprocessedPersonalizedPlan personalizedPlanStepsInScope)
        {
            var actionPlan = new PersonalizedPlanViewModel();

            foreach (var unprocessedTopic in personalizedPlanStepsInScope.UnprocessedTopics)
            {
                var resourceDetails = new List <Resource>();
                var resourceIDs     = unprocessedTopic.UnprocessedSteps.SelectMany(x => x.ResourceIds);
                if (resourceIDs != null || resourceIDs.Any())
                {
                    var resourceValues = resourceIDs.Select(x => x.ToString()).Distinct().ToList();
                    var resourceData   = await dynamicQueries.FindItemsWhereInClauseAsync(cosmosDbSettings.ResourcesCollectionId, Constants.Id, resourceValues);

                    resourceDetails = JsonUtilities.DeserializeDynamicObject <List <Resource> >(resourceData);
                }

                var topic = await topicsResourcesBusinessLogic.GetTopic(unprocessedTopic.Name);

                actionPlan.Topics.Add(new PlanTopic
                {
                    TopicId            = Guid.Parse(topic.Id),
                    TopicName          = topic.Name,
                    Icon               = topic.Icon,
                    AdditionalReadings = await GetAdditionalReadings(topic.Id),
                    Steps              = GetSteps(unprocessedTopic.UnprocessedSteps, resourceDetails)
                });
            }
            actionPlan.PersonalizedPlanId = Guid.NewGuid();
            actionPlan.IsShared           = false;
            return(actionPlan);
        }
        public void GetPersonalizedPlanAsyncValidate(PersonalizedPlanViewModel personalizedPlan, dynamic expectedData)
        {
            //arrange
            var dbResponse = dbService.GetItemAsync <PersonalizedPlanViewModel>(personalizedPlan.PersonalizedPlanId.ToString(), dbSettings.ActionPlansCollectionId);

            dbResponse.ReturnsForAnyArgs <PersonalizedPlanViewModel>(personalizedPlan);
            var response = personalizedPlanBusinessLogic.GetPersonalizedPlanAsync(personalizedPlan.PersonalizedPlanId);

            //act
            var actualResult   = JsonConvert.SerializeObject(response.Result);
            var expectedResult = JsonConvert.SerializeObject(expectedData);

            //assert
            Assert.Equal(expectedResult, actualResult);
        }
        public async Task UpdatePlanToProfile(Guid planId, string oId, PersonalizedPlanViewModel personalizedPlan)
        {
            UserProfile userProfile = await this.userProfileBusinessLogic.GetUserProfileDataAsync(oId);

            if (userProfile?.PersonalizedActionPlanId != null && userProfile?.PersonalizedActionPlanId != Guid.Empty)
            {
                var userExistingPersonalizedPlan = await GetPersonalizedPlanAsync(userProfile.PersonalizedActionPlanId);

                var  i         = 0;
                bool isMatched = false;
                foreach (var planTopic in personalizedPlan.Topics)
                {
                    foreach (var topic in userExistingPersonalizedPlan.Topics.ToArray())
                    {
                        if (topic.TopicName == planTopic.TopicName)
                        {
                            isMatched = true;
                            break;
                        }
                        i++;
                    }
                    if (isMatched)
                    {
                        userExistingPersonalizedPlan.Topics[i] = planTopic;
                    }
                    else
                    {
                        userExistingPersonalizedPlan.Topics.Add(planTopic);
                    }
                }
                await backendDatabaseService.UpdateItemAsync(
                    userExistingPersonalizedPlan.PersonalizedPlanId.ToString(), userExistingPersonalizedPlan, cosmosDbSettings.ActionPlansCollectionId);
            }
            else
            {
                userProfile.PersonalizedActionPlanId = planId;
                await backendDatabaseService.UpdateItemAsync(userProfile.Id, userProfile, cosmosDbSettings.ProfilesCollectionId);
            }
        }
        public async Task <Document> UpsertPersonalizedPlanAsync(UserPlan userPlan)
        {
            try
            {
                PersonalizedPlanViewModel personalizedPlan = userPlan.PersonalizedPlan;
                string  oId      = userPlan.UserId;
                dynamic response = null;

                var userPersonalizedPlan = await GetPersonalizedPlanAsync(personalizedPlan.PersonalizedPlanId);

                if (userPersonalizedPlan == null || userPersonalizedPlan?.PersonalizedPlanId == Guid.Empty)
                {
                    var newPlan = await backendDatabaseService.CreateItemAsync(personalizedPlan, cosmosDbSettings.ActionPlansCollectionId);

                    if (!Guid.TryParse(newPlan.Id, out Guid guid))
                    {
                        return(response);
                    }
                    response = newPlan;
                }
                else
                {
                    response = await backendDatabaseService.UpdateItemAsync(
                        personalizedPlan.PersonalizedPlanId.ToString(), personalizedPlan, cosmosDbSettings.ActionPlansCollectionId);
                }
                if (!userPlan.saveActionPlan)
                {
                    await UpdatePlanToProfile(personalizedPlan.PersonalizedPlanId, oId, personalizedPlan);
                }
                return(response);
            }
            catch
            {
                return(null);
            }
        }