Ejemplo n.º 1
0
        private static async Task <TableResult> CreateNewHousehold(string newHouseholdPk, UserDetails ownerDetails, string notConfirmedMemberLogin, CloudTable table, TraceWriter log)
        {
            string       money     = null;
            List <Money> moneyList = null;

            try
            {
                log.Info("InviteToHousehold: converting user wallets to household money");
                var walletsList = JsonConvert.DeserializeObject <List <Wallet> >(ownerDetails.Wallets);
                moneyList = Aggregation.MergeWallets(walletsList);
                money     = JsonConvert.SerializeObject(moneyList);
            }
            catch (Exception ex)
            {
                log.Error("InviteToHousehold: cannot convert user wallets to household money", ex);
                return(null);
            }

            var household = new Household()
            {
                PartitionKey = newHouseholdPk,
                RowKey       = newHouseholdPk,
                Members      = JsonConvert.SerializeObject(new List <Member>()
                {
                    new Member()
                    {
                        Login         = ownerDetails.Login,
                        WalletSummary = moneyList,
                        Tmr           = CalorieRateCalculator.Tmr(
                            sex: ownerDetails.Sex.Value,
                            bodyWeightKg: ownerDetails.Weight.Value,
                            heightCm: ownerDetails.Height.Value,
                            ageYears: AgeUtil.GetAge(dateOfBirth: ownerDetails.DateOfBirth.Value),
                            pal: ownerDetails.Pal.Value
                            )
                    },
                    new Member()
                    {
                        Login       = notConfirmedMemberLogin,
                        Uncorfirmed = true
                    }
                }),
                MoneyAggregated      = money,
                CategoriesAggregated = ownerDetails.Categories
            };

            try
            {
                log.Info($"InviteToHousehold: adding new household {newHouseholdPk}");
                TableOperation insertTableOperation = TableOperation.InsertOrReplace(household);
                return(await table.ExecuteAsync(insertTableOperation));
            }
            catch (Exception ex)
            {
                log.Error($"Failed to add household {newHouseholdPk}", ex);
                return(null);
            }
        }
Ejemplo n.º 2
0
        private static async Task <bool> SetThatReceiverIsConfirmedAndAddHisWalletsAndCategories(Household household, UserDetails to, CloudTable table, TraceWriter log)
        {
            try
            {
                log.Info("SetThatReceiverIsConfirmedAndAddHisWallets");
                var userWallets = JsonConvert.DeserializeObject <List <Wallet> >(to.Wallets);

                // members
                var members = JsonConvert.DeserializeObject <List <Member> >(household.Members);
                var member  = members.First(x => x.Login == to.Login);
                member.Uncorfirmed = null;
                var walletAggregated = Aggregation.MergeWallets(userWallets);
                member.WalletSummary = walletAggregated;
                member.Tmr           = CalorieRateCalculator.Tmr(to.Sex.Value, to.Weight.Value, to.Height.Value, AgeUtil.GetAge(to.DateOfBirth.Value), to.Pal.Value);
                household.Members    = JsonConvert.SerializeObject(members);
                log.Info($"No problem with editing new member in household. His wallet summary is: {member.WalletSummary} and TMR is {member.Tmr}");

                // wallets
                var householdMoney = JsonConvert.DeserializeObject <List <Money> >(household.MoneyAggregated);
                var merged         = Aggregation.MergeWallets(householdMoney, userWallets);
                household.MoneyAggregated = JsonConvert.SerializeObject(merged);
                log.Info($"No problem with aggregating wallets. BEFORE: household - {household.MoneyAggregated}; new member - {to.Wallets}; AFTER: {household.MoneyAggregated}");

                // categories
                var householdCategories = JsonConvert.DeserializeObject <List <Category> >(household.CategoriesAggregated);
                var newUserCategories   = JsonConvert.DeserializeObject <List <Category> >(to.Categories);
                var categoriesMerged    = Aggregation.MergeCategories(members, householdCategories, newUserCategories);
                household.CategoriesAggregated = JsonConvert.SerializeObject(categoriesMerged);
                log.Info($"No problem with aggregating categories. BEFORE: household - {household.CategoriesAggregated}; new member - {to.Categories}; AFTER: {household.CategoriesAggregated}");

                var updateTableOperation = TableOperation.Replace(household);
                await table.ExecuteAsync(updateTableOperation);

                log.Info($"Updated household with id {household.PartitionKey}");

                var userAsHouseholdMember = new UserDetails(to);
                userAsHouseholdMember.PartitionKey = household.PartitionKey;
                var insertTableOperation = TableOperation.Insert(userAsHouseholdMember);
                await table.ExecuteAsync(insertTableOperation);

                log.Info("Inserted user details as household member's details");
                return(true);
            }
            catch (Exception ex)
            {
                log.Error($"Cannot add user to a household. Exception: {ex.ToString()}", ex);
                return(false);
            }
        }