private void CloseSession(Session session) { var renewSession = SessionService.Get(session.ID); if (renewSession.IsClientClose) { SessionsQuenue.Remove(session); return; } var clientWallet = UserWalletService.GetUserWallet(session.Problem.User); var specialistWallet = UserWalletService.GetUserWallet(session.Specialist.User); clientWallet.Balance -= session.Reward; specialistWallet.Balance += session.Reward; session.IsClientClose = true; session.Status = SessionStatus.Success; session.ClientCloseDate = DateTime.UtcNow; SessionService.Update(session); UserWalletService.Update(clientWallet); UserWalletService.Update(specialistWallet); SessionsQuenue.Remove(session); }
public IActionResult GetMyWallet() { var user = UserService.Get(long.Parse(User.Identity.Name)); if (user == null) { return(NotFound(new ResponseModel { Success = false, Message = "Пользователь не найден" })); } var wallet = UserWalletService.GetUserWallet(user); var activeSessions = SessionService.GetActiveSessions(user); return(Ok(new DataResponse <UserWalletViewModel> { Data = new UserWalletViewModel(wallet, activeSessions.Sum(x => x.Reward)) })); }
public IActionResult StartSession(long id, long sessionID) { var user = UserService.Get(long.Parse(User.Identity.Name)); if (user == null) { return(NotFound(new ResponseModel { Success = false, Message = "Пользователь не найден" })); } var problem = ProblemService.Get(id); if (problem == null) { return(NotFound(new ResponseModel { Success = false, Message = "Проблема не найдена" })); } var session = SessionService.Get(sessionID); if (session == null) { return(NotFound(new ResponseModel { Success = false, Message = "Сессия не найдена" })); } if (session.Specialist == null) { return(Ok(new ResponseModel { Success = false, Message = "Не выбран специалист" })); } var wallet = UserWalletService.GetUserWallet(user); if (wallet == null) { return(NotFound(new ResponseModel { Success = false, Message = "Кошелек не найден" })); } var activeSessions = SessionService.GetActiveSessions(user); var lockedBalance = activeSessions.Sum(x => x.Reward); if ((wallet.Balance - lockedBalance) < session.Reward) { return(Ok(new ResponseModel { Success = false, Message = "Недостаточно средств" })); } session.Status = SessionStatus.Started; session.Date = DateTime.UtcNow; SessionService.Update(session); return(Ok(new ResponseModel())); }
public IActionResult CloseSession(long id, long sessionID) { var user = UserService.Get(long.Parse(User.Identity.Name)); if (user == null) { return(NotFound(new ResponseModel { Success = false, Message = "Пользователь не найден" })); } var problem = ProblemService.Get(id); if (problem == null) { return(NotFound(new ResponseModel { Success = false, Message = "Проблема не найдена" })); } var session = SessionService.Get(sessionID); if (session == null) { return(NotFound(new ResponseModel { Success = false, Message = "Сессия не найдена" })); } var wallet = UserWalletService.GetUserWallet(user); if (wallet == null) { return(NotFound(new ResponseModel { Success = false, Message = "Кошелек не найден" })); } var specialistWallet = UserWalletService.GetUserWallet(session.Specialist.User); if (specialistWallet == null) { return(NotFound(new ResponseModel { Success = false, Message = "Кошелек не найден" })); } if (!session.IsSpecialistClose) { return(BadRequest(new ResponseModel { Success = false, Message = "Сессию не завершил специалист" })); } wallet.Balance -= session.Reward; UserWalletService.Update(wallet); specialistWallet.Balance += session.Reward; UserWalletService.Update(specialistWallet); session.IsClientClose = true; session.ClientCloseDate = DateTime.UtcNow; session.Status = SessionStatus.Success; SessionService.Update(session); var clientActiveSessions = SessionService.GetActiveSessions(session.Problem.User); NotificationsService.SendUpdateToUser( session.Problem.User.ID, SocketMessageType.BalanceUpdate, new UserWalletViewModel(wallet, clientActiveSessions.Sum(x => x.Reward))); NotificationsService.SendUpdateToUser( session.Specialist.ID, SocketMessageType.BalanceUpdate, new UserWalletViewModel(specialistWallet, 0)); return(Ok(new ResponseModel())); }
public async Task <IActionResult> CreatePayment([FromBody] CreatePaymentRequest request) { var user = UserService.Get(long.Parse(User.Identity.Name)); if (user == null) { return(NotFound(new ResponseModel { Success = false, Message = "Пользователь не найден" })); } var wallet = UserWalletService.GetUserWallet(user); if (wallet == null) { return(NotFound(new ResponseModel { Success = false, Message = "Кошелек не найден" })); } var payment = new Payment { Wallet = wallet, Amount = request.Amount, Type = request.Type, Status = PaymentStatus.New }; PaymentService.Create(payment); var paymentRequest = new RegisterDORequest($"user#{user.ID}deposit#{payment.ID}_{DateTime.UtcNow.Millisecond}", (payment.Amount * 100)); if (request.SessionID != 0) { paymentRequest.SetSessionID(request.SessionID); } var paymentResponse = await SberbankAPI.RegisterDO(paymentRequest); if (paymentResponse.OrderId == null) { payment.Status = PaymentStatus.Canceled; PaymentService.Update(payment); return(BadRequest(new ResponseModel { Success = false, Message = $"{paymentResponse.ErrorMessage} Код ошибки: {paymentResponse.ErrorCode}" })); } payment.OrderID = paymentResponse.OrderId; PaymentService.Update(payment); return(Ok(new CreatePaymentResponse { RedirectUrl = paymentResponse.FormUrl })); }