Ejemplo n.º 1
0
        public async Task <object> GetGiftsHistoryAsync(string userid, string token)
        {
            List <ToDoItem> todo = new List <ToDoItem>();

            GrocedyAPI.DataModels.Zoho.Subscriptions.Subscription        subscription = null;//= new DataModels.Zoho.Subscriptions.Subscription();
            List <GrocedyAPI.DataModels.Zoho.Subscriptions.Subscription> gifts        = new List <GrocedyAPI.DataModels.Zoho.Subscriptions.Subscription>();

            var usermetas = await unitOfWork.UsersMetaRepository.Find(u => u.UserId == long.Parse(userid));

            var userMappings = await unitOfWork.ZohoCustomerMappingRepository.Find(u => u.UserId == long.Parse(userid));

            string CustomerID = string.Empty;

            if (userMappings.Count() == 0)
            {
                //Not customer yet OR No Subscriptions found
                CustomerID = string.Empty;
            }
            else
            {
                CustomerID = userMappings.First().ZohoCustomerId;
            }

            var history = await this.httpClientFactory
                          .GetAsync <GetSubscriptionsResponse>("https://subscriptions.zoho.com/api/v1/subscriptions?filter_by=SubscriptionStatus.ACTIVE",
                                                               HttpClientFactory.APIType.ZohoSubscription,
                                                               token);

            gifts = (from all in (from s in history.result.Subscriptions
                                  from c in s.CustomFields
                                  where (c.Label == "CreatedBy" && c.Value == userid)
                                  select s)
                     from p in all.CustomFields
                     where (p.Label == "IsGift" && p.Value == "true")
                     select all).ToList();

            if (gifts.Count > 0)
            {
                gifts.ForEach(g =>
                {
                    g.Date = $"{Convert.ToDateTime(g.CurrentTermStartsAt).ToString("MMMM")} {Convert.ToDateTime(g.CurrentTermStartsAt).ToString("yyyy")} - {Convert.ToDateTime(g.CurrentTermEndsAt).ToString("MMMM")}  {Convert.ToDateTime(g.CurrentTermEndsAt).ToString("yyyy")} ";
                });
            }
            return(gifts.Where(g => g.Status == "expired").ToList());
        }
Ejemplo n.º 2
0
        public async Task <object> GetHomeScreenDataAsync(string userid, string token)
        {
            List <ToDoItem> todo = new List <ToDoItem>();

            GrocedyAPI.DataModels.Zoho.Subscriptions.Subscription        subscription = null;//= new DataModels.Zoho.Subscriptions.Subscription();
            List <GrocedyAPI.DataModels.Zoho.Subscriptions.Subscription> gifts        = new List <GrocedyAPI.DataModels.Zoho.Subscriptions.Subscription>();

            var usermetas = await unitOfWork.UsersMetaRepository.Find(u => u.UserId == long.Parse(userid));

            var userMappings = await unitOfWork.ZohoCustomerMappingRepository.Find(u => u.UserId == long.Parse(userid));

            string CustomerID = string.Empty;

            if (userMappings.Count() == 0)
            {
                //Not customer yet OR No Subscriptions found
                CustomerID = string.Empty;
            }
            else
            {
                CustomerID = userMappings.First().ZohoCustomerId;
            }
            if (usermetas.Count() > 0)
            {
                if (usermetas.Where(u => u.MetaKey == "billing_address_1").Count() == 0 ||
                    usermetas.Where(u => u.MetaKey == "billing_address_2").Count() == 0)
                {
                    todo.Add(new ToDoItem()
                    {
                        Name = "Add your delivery details", Value = "1"
                    });
                }
            }

            if (string.IsNullOrWhiteSpace(CustomerID))
            {
                var history = await this.httpClientFactory
                              .GetAsync <GetSubscriptionsResponse>("https://subscriptions.zoho.com/api/v1/subscriptions?filter_by=SubscriptionStatus.ACTIVE",
                                                                   HttpClientFactory.APIType.ZohoSubscription,
                                                                   token);

                gifts = (from all in (from s in history.result.Subscriptions
                                      from c in s.CustomFields
                                      where (c.Label == "CreatedBy" && c.Value == userid)
                                      select s)
                         from p in all.CustomFields
                         where (p.Label == "IsGift" && p.Value == "true")
                         select all).ToList();

                if (gifts.Count > 0)
                {
                    gifts.ForEach(g =>
                    {
                        g.Date   = $"{Convert.ToDateTime(g.CurrentTermStartsAt).ToString("MMMM")} {Convert.ToDateTime(g.CurrentTermStartsAt).ToString("yyyy")} - {Convert.ToDateTime(g.CurrentTermEndsAt).ToString("MMMM")}  {Convert.ToDateTime(g.CurrentTermEndsAt).ToString("yyyy")} ";
                        g.IsGift = true;
                    });
                }
                //if (gifts.Count == 0)
                //{
                todo.Add(new ToDoItem()
                {
                    Name = "Send a Gift", Value = "2"
                });
                //}
                return(new
                {
                    Status = true,
                    ZohoSubscriptionToken = string.Empty,
                    ToDoList = todo,
                    Subscriptions = subscription,
                    Gifts = gifts
                });
            }
            else
            {
                var history = await this.httpClientFactory
                              .GetAsync <GetSubscriptionsResponse>("https://subscriptions.zoho.com/api/v1/subscriptions?filter_by=SubscriptionStatus.ACTIVE",
                                                                   HttpClientFactory.APIType.ZohoSubscription,
                                                                   token);

                if (history.result.Code == 0)
                {
                    var allSubscriptions = history.result
                                           .Subscriptions
                                           .Where(s => s.CustomerId == CustomerID)
                                           .ToList();



                    gifts = (from all in (from s in history.result.Subscriptions
                                          from c in s.CustomFields
                                          where (c.Label == "CreatedBy" && c.Value == userid)
                                          select s)
                             from p in all.CustomFields
                             where (p.Label == "IsGift" && p.Value == "true")
                             select all).ToList();

                    if (gifts.Count > 0)
                    {
                        gifts.ForEach(g =>
                        {
                            g.Date = $"{Convert.ToDateTime(g.CurrentTermStartsAt).ToString("MMMM")} {Convert.ToDateTime(g.CurrentTermStartsAt).ToString("yyyy")} - {Convert.ToDateTime(g.CurrentTermEndsAt).ToString("MMMM")}  {Convert.ToDateTime(g.CurrentTermEndsAt).ToString("yyyy")} ";
                        });
                    }
                    if (allSubscriptions.Count > 0)
                    {
                        subscription = (from s in allSubscriptions
                                        from c in s.CustomFields
                                        where (c.Label == "IsGift" && c.Value == "false")
                                        select s).OrderByDescending(s => Convert.ToDateTime(s.CreatedAt)).First();

                        subscription.Date = $"{Convert.ToDateTime(subscription.CurrentTermStartsAt).ToString("MMMM")} {Convert.ToDateTime(subscription.CurrentTermStartsAt).ToString("yyyy")} - {Convert.ToDateTime(subscription.CurrentTermEndsAt).ToString("MMMM")}  {Convert.ToDateTime(subscription.CurrentTermEndsAt).ToString("yyyy")} ";
                    }
                    else
                    {
                        //subscription = new DataModels.Zoho.Subscriptions.Subscription();
                    }
                    //  if (gifts.Count==0)
                    //{
                    todo.Add(new ToDoItem()
                    {
                        Name = "Send a Gift", Value = "2"
                    });
                    //}
                    return(new
                    {
                        Status = true,
                        ZohoSubscriptionToken = string.IsNullOrWhiteSpace(history.token) ? string.Empty : history.token,
                        ToDoList = todo,
                        Subscriptions = subscription,
                        Gifts = gifts
                    });
                }
                else
                {
                    todo.Add(new ToDoItem()
                    {
                        Name = "Send a Gift", Value = "2"
                    });
                    return(new
                    {
                        Status = true,
                        ZohoSubscriptionToken = history.token,
                        ToDoList = todo,
                        Subscriptions = subscription,
                        Gifts = gifts
                    });
                }
            }
        }