public bool DeleteDeptReminder(Guid userId, Guid deptReminderId)
        {
            var res = false;

            using (var sessionTask = _MongoDBClient.StartSessionAsync())
            {
                var session = sessionTask.Result;
                session.StartTransaction();

                try
                {
                    var userDetail = _UserCollection.GetById(userId);

                    if (userDetail != null)
                    {
                        var removeSeft  = userDetail.SelfDeptReminderIds.Remove(deptReminderId);
                        var removeOther = userDetail.OtherDeptReminderIds.Remove(deptReminderId);

                        if (!removeSeft && !removeOther)
                        {
                        }
                        else
                        {
                            var delete = _DeptReminderCollection.Delete(deptReminderId) >= 0;

                            if (delete)
                            {
                                var updateUser = _UserCollection.Replace(userDetail) >= 0;

                                if (updateUser)
                                {
                                    /*TODO*/
                                    // send mail
                                    res = true;
                                    session.CommitTransactionAsync().Wait();
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    session.AbortTransactionAsync().Wait();
                    throw;
                }

                if (!res)
                {
                    session.AbortTransactionAsync().Wait();
                }
            }
            return(res);
        }
예제 #2
0
        public async void CreateTransaction(User user)
        {
            using (var session = await _MongoDBClient.StartSessionAsync())
            {
                session.StartTransaction();
                try
                {
                    _Collection.InsertOne(user);

                    await session.CommitTransactionAsync();
                }
                catch (Exception)
                {
                    await session.AbortTransactionAsync();

                    throw;
                }
            }
        }
        public bool ConfirmForgetting(Guid id, string email, string otp)
        {
            var res = false;

            var details = _UserCollection.Get(new UserFilter()
            {
                Email = email
            });

            if (details != null && details.Any())
            {
                var detail = details.FirstOrDefault();
                // Get chi tiết giao dịch
                var transactions = _TransactionCollection.GetMany(new TransactionFilter()
                {
                    Id = id, ReferenceId = detail.Id, Type = 2
                });
                if (transactions.Any())
                {
                    var transaction = transactions.FirstOrDefault();
                    // Check Otp
                    if (transaction.Otp == otp)
                    {
                        // Check hết hạn
                        var now = DateTime.Now;
                        if (now <= transaction.ExpireTime && now >= transaction.CreateTime)
                        {
                            // Update mật khẩu
                            using (var sessionTask = _MongoDBClient.StartSessionAsync())
                            {
                                var session = sessionTask.Result;
                                session.StartTransaction();
                                try
                                {
                                    // Create mật khẩu mới
                                    string pass = _Context.MakeOTP(8);

                                    if (_UserCollection.ChangePassword(new UserFilter()
                                    {
                                        Id = detail.Id
                                    }, Encrypting.Bcrypt(pass)) > 0)
                                    {
                                        // Update giao dịch
                                        transaction.ConfirmTime = DateTime.Now;

                                        if (_TransactionCollection.Replace(transaction) > 0)
                                        {
                                            // Send mail
                                            var sb = new StringBuilder();
                                            sb.AppendFormat($"Dear {detail.Name},");
                                            sb.AppendFormat("<br /><br /><b>Yêu cầu quên mật khẩu của bạn đã thực hiện thành công, mật khẩu mới của bạn là:</b>");
                                            sb.AppendFormat($"<br /><br /><b>{pass}</b>");
                                            sb.AppendFormat($"<br /><br /><b>Vui lòng đăng nhập vào hệ thống để kiểm tra.</b>");
                                            sb.AppendFormat($"<br /><br /><b>Nếu yêu cầu không phải của bạn, vui lòng bỏ qua mail này.</b>");

                                            if (_Context.SendMail("Yêu cầu quên mật khẩu", sb.ToString(), detail.Email, detail.Name))
                                            {
                                                res = true;
                                            }
                                            else
                                            {
                                                _Setting.Message.SetMessage("Gửi mail thất bại!");
                                            }
                                        }
                                        else
                                        {
                                            _Setting.Message.SetMessage("Không thể cập nhật thông tin giao dịch!");
                                        }
                                    }
                                    else
                                    {
                                        _Setting.Message.SetMessage("Không thể cập nhật thông tin mật khẩu!");
                                    }

                                    if (res)
                                    {
                                        session.CommitTransactionAsync();
                                    }
                                    else
                                    {
                                        session.AbortTransactionAsync();
                                    }
                                }
                                catch (Exception ex)
                                {
                                    session.AbortTransactionAsync();
                                    throw ex;
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            _Setting.Message.SetMessage("Phiên giao dịch đã hết hạn!");
                        }
                    }
                    else
                    {
                        _Setting.Message.SetMessage("Mã OTP không đúng!");
                    }
                }
                else
                {
                    _Setting.Message.SetMessage("Không tìm thấy thông tin giao dịch!");
                }
            }
            else
            {
                _Setting.Message.SetMessage("Không tìm thấy thông tin người dùng!");
            }

            return(res);
        }
예제 #4
0
        public bool ConfirmTransfer(Guid userId, Guid transactionId, string otp)
        {
            var res = false;

            using (var sessionTask = _MongoDBClient.StartSessionAsync())
            {
                var session = sessionTask.Result;
                session.StartTransaction();
                try
                {
                    // Get detail user
                    var userDetail = _UserCollection.GetById(userId);

                    if (userDetail != null)
                    {
                        // Get Get chi tiết giao dịch
                        var transaction = _TransactionCollection.GetById(transactionId);
                        if (transaction != null)
                        {
                            // Check OTP
                            if (transaction.Otp == otp)
                            {
                                // Check hết hạn
                                if (transaction.ExpireTime >= DateTime.Now)
                                {
                                    // Get chi tiết chuyển tiền
                                    var transfer = _TransferCollection.GetById(transaction.ReferenceId);
                                    // Check user hiện tại có tạo yêu cầu chuyển tiền
                                    if (transfer != null &&
                                        transfer.SourceLinkingBankId == Guid.Empty &&
                                        transfer.SourceAccountNumber == userDetail.AccountNumber)
                                    {
                                        // Tru so du
                                        userDetail.CheckingAccount.AccountBalance -= transfer.Money;
                                        transfer.Fee = _Context.TransactionCost(transfer.Money);

                                        // Tru phi
                                        if (transfer.IsSenderPay)
                                        {
                                            userDetail.CheckingAccount.AccountBalance -= transfer.Fee;
                                        }

                                        if (userDetail.CheckingAccount.AccountBalance >= 0)
                                        {
                                            // Luu thong tin nguoi gui
                                            var payOut = _UserCollection.UpdateCheckingAccount(new UserFilter()
                                            {
                                                Id = userId
                                            }, userDetail.CheckingAccount);

                                            if (payOut > 0)
                                            {
                                                // Cong tien nguoi nhan
                                                var success = false;
                                                if (transfer.DestinationLinkingBankId == Guid.Empty)
                                                {
                                                    // noi bo
                                                    var detailRecepients = _UserCollection.Get(new UserFilter()
                                                    {
                                                        AccountNumber = transfer.DestinationAccountNumber
                                                    });
                                                    if (detailRecepients.Any())
                                                    {
                                                        var detailRecepient = detailRecepients.FirstOrDefault();
                                                        detailRecepient.CheckingAccount.AccountBalance += transfer.Money;
                                                        // Tru phi
                                                        if (!transfer.IsSenderPay)
                                                        {
                                                            detailRecepient.CheckingAccount.AccountBalance -= transfer.Fee;
                                                        }

                                                        payOut = _UserCollection.UpdateCheckingAccount(new UserFilter()
                                                        {
                                                            Id = detailRecepient.Id
                                                        }, detailRecepient.CheckingAccount);

                                                        if (payOut > 0)
                                                        {
                                                            success = true;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        _Setting.Message.SetMessage("Không tìm thấy thông tin người nhận!");
                                                    }
                                                }
                                                else
                                                {
                                                    // lien ngan hang
                                                    // TODO
                                                    // Khuê
                                                    IExternalBanking externalBanking = null;
                                                    if (transfer.DestinationLinkingBankId == Guid.Parse("8df09f0a-fd6d-42b9-804c-575183dadaf3"))
                                                    {
                                                        externalBanking = new ExternalBanking_BKTBank(_Encrypt, _Setting);
                                                        externalBanking.SetPartnerCode();
                                                    }

                                                    var result = externalBanking.PayIn(transfer.SourceAccountNumber, transfer.DestinationAccountNumber, transfer.Money, transfer.Description);

                                                    success = result;
                                                }

                                                if (success)
                                                {
                                                    // Update trạng thái chuyển tiền
                                                    transfer.IsConfirmed = true;
                                                    var updateTransfer = _TransferCollection.Replace(transfer);
                                                    if (updateTransfer > 0)
                                                    {
                                                        // Update trạng thái giao dịch
                                                        transaction.ConfirmTime = DateTime.Now;
                                                        var updateTransaction = _TransactionCollection.Replace(transaction);
                                                        if (updateTransaction > 0)
                                                        {
                                                            // Send mail
                                                            // TODO
                                                            res = true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            _Setting.Message.SetMessage("Số dư không đủ!");
                                        }
                                    }
                                    else
                                    {
                                        _Setting.Message.SetMessage("Không tìm thấy thông tin chuyển tiền!");
                                    }
                                }
                                else
                                {
                                    _Setting.Message.SetMessage("Phiên giao dịch hết hạn!");
                                }
                            }
                            else
                            {
                                _Setting.Message.SetMessage("Sai mã OTP!");
                            }
                        }
                        else
                        {
                            _Setting.Message.SetMessage("Không tìm thấy thông tin giao dịch!");
                        }
                    }
                    else
                    {
                        _Setting.Message.SetMessage("Không tìm thấy thông tin người gửi yêu cầu!");
                    }

                    if (res)
                    {
                        session.CommitTransactionAsync();
                    }
                    else
                    {
                        session.AbortTransactionAsync();
                    }
                }
                catch (Exception)
                {
                    session.AbortTransactionAsync().Wait();
                    throw;
                }
            }

            return(res);
        }
        public User AddUser(Account account)
        {
            User res = null;

            var duplicates = _UserCollection.Get(new UserFilter()
            {
                Email = account.Email
            });

            if (duplicates != null && duplicates.Any())
            {
                return(res);
            }

            using (var sessionTask = _MongoDBClient.StartSessionAsync())
            {
                var session = sessionTask.Result;
                session.StartTransaction();
                try
                {
                    //var passDecrypt = Encrypting.AesDecrypt(account.Password, Encoding.UTF8.GetBytes(_Setting.AesKey), Encoding.UTF8.GetBytes(_Setting.AesIv), Encoding.UTF8);
                    //var lstUser = _UserCollection.Get(new UserFilter() { Username = account.Username });
                    //if (!lstUser.Any())
                    //{
                    // phát sinh password ngẫu nhiên
                    string randomPass = _Context.MakeOTP(10);
                    // test only
                    account.Password = "******";

                    res        = new User();
                    res.Id     = Guid.Empty;
                    res.Name   = account.Name;
                    res.Gender = account.Gender;
                    res.Email  = account.Email;
                    res.Phone  = account.Phone;
                    //res.Password = Encrypting.Bcrypt(passDecrypt);
                    res.Password = Encrypting.Bcrypt(randomPass);
                    res.Address  = account.Address;
                    res.Role     = 1;

                    // Tao so tai khoan
                    while (true)
                    {
                        res.AccountNumber = _Context.MakeOTP(10, isAllDigits: true);
                        if (!_UserCollection.Get(new UserFilter()
                        {
                            AccountNumber = res.AccountNumber
                        }).Any())
                        {
                            break;
                        }
                    }
                    res.Username = string.Concat(account.Name.Split(' ').Last(), res.AccountNumber);

                    // Tạo thông tin tài khoản thanh toán
                    res.CheckingAccount = new BankAccount()
                    {
                        AccountBalance = 0,
                        Description    = "Tài khoản thanh toán",
                        Name           = res.AccountNumber
                    };

                    _UserCollection.Create(res);

                    // tạo thành công trả về password
                    res.Password = randomPass;
                    if (res.Id.Equals(Guid.Empty))
                    {
                        res = null;
                    }
                    //}
                    //else
                    //{
                    //    _Setting.Message.SetMessage("Trùng tên đăng nhập");
                    //}
                    session.CommitTransactionAsync().Wait();
                }
                catch
                {
                    session.AbortTransactionAsync().Wait();
                }
            }
            return(res);
        }