//User requests to admin for joining home
        public async Task JoinHomeRequestAsync(UserModel user, string joinHomeName)
        {
            Task <InformationModel> firstNameInfo = _informationRepository.GetInformationByInformationNameAsync("FirstName");
            Task <InformationModel> lastNameInfo  = _informationRepository.GetInformationByInformationNameAsync("LastName");

            if (user.Position != (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("User Has Home", "User has home already");
                errors.Throw();
            }

            var home = await _homeRepository.GetByHomeNameAsync(joinHomeName, true);

            if (home == null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Name Not Exist", "Home name has not exist");
                errors.Throw();
            }

            //User waiting for admin's accept

            UserInformationModel firstName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id);

            UserInformationModel lastName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id);

            FCMModel fcm = new FCMModel(home.Admin.DeviceId, new Dictionary <string, object>());

            fcm.notification.Add("title", "Eve Katılma İsteği");
            fcm.notification.Add("body", String.Format("{0} {1}({2}) evinize katılmak istiyor.", firstName.Value, lastName.Value, user.Username));

            await _fcmService.SendFCMAsync(fcm);

            fcm = new FCMModel(home.Admin.DeviceId, type: "JoinHomeRequest");

            fcm.data.Add("RequesterId", user.Id);
            fcm.data.Add("RequesterUsername", user.Username);
            fcm.data.Add("RequesterName", firstName.Value);
            fcm.data.Add("RequesterLastName", lastName.Value);

            await _fcmService.SendFCMAsync(fcm);
        }
Example #2
0
        //Registers new user
        public async Task RegisterNewUserAsync(RegistrationModel registrationForm)
        {
            UserModel            user            = new UserModel();
            InformationModel     info            = new InformationModel();
            UserInformationModel userInformation = new UserInformationModel();

            user.Username = registrationForm.Username;
            user.Password = registrationForm.Password;
            user.Status   = (int)UserStatus.NotValid;
            user.Home     = null;
            user.Position = (int)UserPosition.HasNotHome;

            _userRepository.Insert(user);

            info = await _informationRepository.GetInformationByInformationNameAsync("FirstName");

            userInformation.Information = info;
            userInformation.User        = user;
            userInformation.Value       = registrationForm.FirstName;
            _userInformationRepository.Insert(userInformation);
            userInformation = null;

            userInformation = new UserInformationModel();
            info            = await _informationRepository.GetInformationByInformationNameAsync("LastName");

            userInformation.Information = info;
            userInformation.User        = user;
            userInformation.Value       = registrationForm.LastName;
            _userInformationRepository.Insert(userInformation);
            userInformation = null;

            userInformation = new UserInformationModel();
            info            = await _informationRepository.GetInformationByInformationNameAsync("Email");

            userInformation.Information = info;
            userInformation.User        = user;
            userInformation.Value       = registrationForm.Email;
            _userInformationRepository.Insert(userInformation);
            userInformation = null;

            userInformation = new UserInformationModel();
            info            = await _informationRepository.GetInformationByInformationNameAsync("PhoneNumber");

            userInformation.Information = info;
            userInformation.User        = user;
            userInformation.Value       = registrationForm.PhoneNumber;
            _userInformationRepository.Insert(userInformation);
            userInformation = null;

            userInformation = new UserInformationModel();
            info            = await _informationRepository.GetInformationByInformationNameAsync("ReportCount");

            userInformation.Information = info;
            userInformation.User        = user;
            userInformation.Value       = "0";
            _userInformationRepository.Insert(userInformation);
            userInformation = null;

            userInformation = new UserInformationModel();
            info            = await _informationRepository.GetInformationByInformationNameAsync("RegistrationDate");

            userInformation.Information = info;
            userInformation.User        = user;
            userInformation.Value       = String.Format("{0:u}", registrationForm.RegistrationDate);
            _userInformationRepository.Insert(userInformation);
        }
Example #3
0
        //User adds expense
        public async Task AddExpenseAsync(UserModel user, ExpenseModel expense, List <int> participants)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            if ((expense.EType > (int)ExpenseType.Others) || (expense.EType < 0))
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Expense Type Not Exist", "Expense type number not valid");
                errors.Throw();
            }

            if (expense.Cost <= 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Expense Cost Not Valid", "The expense cost must be bigger than 0");
                errors.Throw();
            }

            participants = participants.Distinct().ToList();

            if (participants.Count == 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Participants Not Exist", "You must add participants for this expense");
                errors.Throw();
            }

            Task <InformationModel> firstNameInfo = _informationRepository.GetInformationByInformationNameAsync("FirstName");
            Task <InformationModel> lastNameInfo  = _informationRepository.GetInformationByInformationNameAsync("LastName");

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            //Friend not found
            foreach (var p in participants)
            {
                if (home.Users.SingleOrDefault(u => u.Id == p) == null)
                {
                    CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                    errors.AddError("Friendship Not Found", "Friendship not found for lend");
                    errors.Throw();
                }
            }

            expense.LastUpdated = DateTime.UtcNow;
            expense.Home        = home;
            expense.Author      = await _userRepository.GetByIdAsync(user.Id);;

            //Author informations
            UserInformationModel userFirstName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id);

            UserInformationModel userLastName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id);

            //Lend
            if (expense.EType == (int)ExpenseType.Lend)
            {
                if (participants.SingleOrDefault(p => p.Equals(user.Id)) != 0)
                {
                    CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                    errors.AddError("Lend Error", "You cant lend yourself");
                    errors.Throw();
                }

                ExpenseModel borrowExpense = new ExpenseModel((int)ExpenseType.Borrow,
                                                              expense.Cost / participants.Count,
                                                              expense.Author,
                                                              expense.Home,
                                                              expense.LastUpdated,
                                                              expense.Title,
                                                              expense.Content);

                _expenseRepository.Insert(expense);
                _expenseRepository.Insert(borrowExpense);

                //Borrow for all participants
                foreach (var p in participants)
                {
                    var to = await _userRepository.GetByIdAsync(p);

                    await _homeService.TransferMoneyToFriendAsync(user, to, borrowExpense.Cost);

                    await _userExpenseRepository.InsertAsync(new UserExpenseModel(to, borrowExpense));

                    //Send fcm to other participants
                    FCMModel fcmBorrow = new FCMModel(to.DeviceId, new Dictionary <string, object>());
                    fcmBorrow.notification.Add("title", "Nakit Aktarımı");
                    fcmBorrow.notification.Add("body", String.Format("{0} {1} tarafından {2:c} nakit alındı.",
                                                                     userFirstName.Value,
                                                                     userLastName.Value,
                                                                     borrowExpense.Cost));
                    await _fcmService.SendFCMAsync(fcmBorrow);

                    //Send fcm to other participants
                    fcmBorrow = new FCMModel(to.DeviceId, type: "AddExpense");
                    fcmBorrow.data.Add("Content", borrowExpense);
                    fcmBorrow.data.Add("Author", expense.Author.Username);
                    fcmBorrow.data.Add("Participants", participants);

                    await _fcmService.SendFCMAsync(fcmBorrow);
                }

                Task insertUEL = _userExpenseRepository.InsertAsync(new UserExpenseModel(user, expense));

                //Send fcm to user
                FCMModel fcmLend = new FCMModel(user.DeviceId, type: "AddExpense");
                fcmLend.data.Add("Content", expense);
                fcmLend.data.Add("Author", expense.Author.Username);
                fcmLend.data.Add("Participants", participants);

                await _fcmService.SendFCMAsync(fcmLend);

                await insertUEL;
            }

            else
            {
                expense.Cost = expense.Cost / participants.Count;
                _expenseRepository.Insert(expense);

                foreach (var p in participants)
                {
                    //Paid for friends
                    if (p != user.Id)
                    {
                        var to = await _userRepository.GetByIdAsync(p);

                        await _homeService.TransferMoneyToFriendAsync(user, to, expense.Cost);

                        await _userExpenseRepository.InsertAsync(new UserExpenseModel(to, expense));

                        //Send fcm to other participants
                        FCMModel fcmExpense = new FCMModel(to.DeviceId, new Dictionary <string, object>());
                        fcmExpense.notification.Add("title", String.Format("Yeni Gider : \"{0}\"", expense.Title));
                        fcmExpense.notification.Add("body", String.Format("{0} {1} tarafından {2:c} ödendi.",
                                                                          userFirstName.Value,
                                                                          userLastName.Value,
                                                                          expense.Cost));

                        await _fcmService.SendFCMAsync(fcmExpense);

                        //Send fcm to other participants
                        fcmExpense = new FCMModel(to.DeviceId, type: "AddExpense");

                        fcmExpense.data.Add("Content", expense);
                        fcmExpense.data.Add("Author", expense.Author.Username);
                        fcmExpense.data.Add("Participants", participants);

                        await _fcmService.SendFCMAsync(fcmExpense);
                    }
                    else
                    {
                        Task insertUE = _userExpenseRepository.InsertAsync(new UserExpenseModel(user, expense));

                        //Send fcm to user
                        FCMModel fcmExpense = new FCMModel(user.DeviceId, type: "AddExpense");
                        fcmExpense.data.Add("Content", expense);
                        fcmExpense.data.Add("Author", expense.Author.Username);
                        fcmExpense.data.Add("Participants", participants);

                        await _fcmService.SendFCMAsync(fcmExpense);

                        await insertUE;
                    }
                }
                //Expense author is not exist in participants
                if (participants.SingleOrDefault(pr => pr == user.Id) == 0)
                {
                    //Send fcm to user
                    FCMModel fcmExpense = new FCMModel(user.DeviceId, type: "AddExpense");
                    fcmExpense.data.Add("Content", expense);
                    fcmExpense.data.Add("Author", expense.Author.Username);
                    fcmExpense.data.Add("Participants", participants);

                    await _fcmService.SendFCMAsync(fcmExpense);
                }
            }
        }