//Synchronizes clients notepad public async Task <List <NotepadModel> > SynchronizeNotepadAsync(UserModel user) { 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(); } user = await _userRepository.GetByIdAsync(user.Id, true); return(await _notepadRepository.GetAllNoteByHomeIdAsync(user.Home.Id)); }
//User request to quit home public async Task LeaveHomeAsync(UserModel user, int newAdminId) { 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(); } 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); UserInformationModel userFirstName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await firstNameInfo).Id); UserInformationModel userLastName = await _userInformationRepository.GetUserInformationByIdAsync(user.Id, (await lastNameInfo).Id); List <UserExpenseModel> userExpenses = await _userExpenseRepository.GetAllUserExpenseByUserIdAsync(user.Id); foreach (var ue in userExpenses) { _userExpenseRepository.Delete(ue); } if (home.Users.Count != 1) { if (user.Position == (int)UserPosition.Admin) { UserModel newAdmin = await _userRepository.GetByIdAsync(newAdminId); if (newAdmin == null || newAdmin.Home.Id != user.Home.Id) { CustomException errors = new CustomException((int)HttpStatusCode.BadRequest); errors.AddError("Friendship Not Found", "Friendship not found for admin assignment"); errors.Throw(); } newAdmin.Position = (int)UserPosition.Admin; home.Admin = newAdmin; } home.Users.Remove(user); user.Home = null; user.Position = (int)UserPosition.HasNotHome; _homeRepository.Update(home); _userRepository.Update(user); //Home friends notification foreach (var u in home.Users) { FriendshipModel friendship = await _friendshipRepository.GetFriendshipByIdAsync(user.Id, u.Id); FCMModel fcm = new FCMModel(u.DeviceId, new Dictionary <string, object>()); fcm.notification.Add("title", "Evden Ayrılma"); if (friendship.User1.Id == user.Id) { if (friendship.Debt > 0) { fcm.notification.Add("body", String.Format("{0} {1} evden ayrılıyor. Alacağınız : {2:c}", userFirstName.Value, userLastName.Value, friendship.Debt)); } else if (friendship.Debt == 0) { fcm.notification.Add("body", String.Format("{0} {1} evden ayrılıyor. Borcunuz veya alacağınız bulunmamaktadır.", userFirstName.Value, userLastName.Value)); } else { fcm.notification.Add("body", String.Format("{0} {1} evden ayrılıyor. Borcunuz : {2:c}", userFirstName.Value, userLastName.Value, -friendship.Debt)); } } else { if (friendship.Debt > 0) { fcm.notification.Add("body", String.Format("{0} {1} evden ayrılıyor. Borcunuz : {2:c}", userFirstName.Value, userLastName.Value, friendship.Debt)); } else if (friendship.Debt == 0) { fcm.notification.Add("body", String.Format("{0} {1} evden ayrılıyor. Borcunuz veya alacağınız bulunmamaktadır.", userFirstName.Value, userLastName.Value)); } else { fcm.notification.Add("body", String.Format("{0} {1} evden ayrılıyor. Alacağınız : {2:c}", userFirstName.Value, userLastName.Value, -friendship.Debt)); } } await _fcmService.SendFCMAsync(fcm); fcm = new FCMModel(u.DeviceId, type: "LeaveHome"); fcm.data.Add("LeaverId", user.Id); await _fcmService.SendFCMAsync(fcm); _friendshipRepository.Delete(friendship); } } else { ShoppingListModel shoppingList = await _shoppingListRepository.GetShoppingListByHomeIdAsync(user.Home.Id); List <NotepadModel> notepad = await _notepadRepository.GetAllNoteByHomeIdAsync(user.Home.Id); List <ExpenseModel> expenses = await _expenseRepository.GetAllExpensesByHomeIdAsync(user.Home.Id); _shoppingListRepository.Delete(shoppingList); foreach (var note in notepad) { _notepadRepository.Delete(note); } foreach (var expense in expenses) { _expenseRepository.Delete(expense); } user.Home = null; user.Position = (int)UserPosition.HasNotHome; _userRepository.Update(user); _homeRepository.Delete(home); } }