//----- *** ---// // Create PIN public async Task <bool> createPin(int id, string phone) { bool re = false; if (id != 0) { var account = await db.tblAccounts.FirstOrDefaultAsync(a => a.deleted == null && a.id == id); if (account == null) { throw new HttpException((int)HttpStatusCode.NotFound, "NotFound"); } if (account.phoneNumber != phone) { throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.INVALID_PHONE); } } var loan = db.tblLoanRequests.FirstOrDefault(x => x.id == id && x.status != "rejected" && x.status != "approve" && x.loan_Balance > 0); if (loan != null) { throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.ALREADY_REQUEST_LOAN); } //var ePin = db.tblPins.Where(x => x.deleted == null).Take(2).OrderByDescending(o => o.date); var ePin = db.tblPins.Where(x => x.deleted == null && id != 0 ? x.accountID == id : x.phoneNumber == phone ).OrderByDescending(o => o.date).Take(2); if (ePin != null) { DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now; int i = 0; foreach (var _pin in ePin) { if (i == 0) { dt1 = _pin.date.Value; var tt = DateTime.Now.Subtract(dt1).Minutes + (DateTime.Now.Subtract(dt1).Hours * 60); if (tt > 30) { break; } } if (i == 1) { dt2 = _pin.date.Value; } i++; } if (i > 1) { if (dt1.Subtract(dt2).Minutes + (dt1.Subtract(dt1).Hours * 60) < 30) { throw new HttpException((int)HttpStatusCode.BadRequest, ConstantHelper.PENDING_SMS); } } } tblPin pin = new tblPin(); pin.date = DateTime.Now; if (id != 0) { pin.accountID = id; } else { pin.phoneNumber = phone; } pin.name = GeneratePIN(); db.tblPins.Add(pin); db.SaveChanges(); re = true; return(re); }
// Create PIN public async Task <bool> createPin(int id, string phone) { string _phone = ""; bool re = false; if (id != 0) { var account = await db.tblAccounts.FirstOrDefaultAsync(a => a.acct_Deleted == null && a.acct_AccountID == id); if (account == null) { throw new HttpException((int)HttpStatusCode.NotFound, "NotFound"); } if (account.acct_PhoneNumber != phone) { throw new HttpException(_ErrorCode, ConstantHelper.INVALID_PHONE); } _phone = account.acct_PhoneNumber; } else { tblAccount eAcc = db.tblAccounts.FirstOrDefault(x => x.acct_PhoneNumber.Trim() == phone.Trim()); if (eAcc != null) { throw new HttpException(_ErrorCode, ConstantHelper.PHONE_EXIST); } _phone = phone; } var loan = db.tblLoanRequests.FirstOrDefault(x => x.loan_AccountID == id && x.loan_Status.ToLower() != "rejected" && x.loan_Status.ToLower() != "approve" && x.loan_Balance > 0); if (loan != null) { throw new HttpException(_ErrorCode, ConstantHelper.ALREADY_REQUEST_LOAN); } var dt = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")); var ePin = db.tblPins.Where(x => x.pins_Deleted == null && (id != 0 ? x.pins_AccountID == id : x.pins_Phone == phone) && x.pins_Date >= dt ).OrderByDescending(o => o.pins_Date).Take(2); if (ePin != null) { DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now; int i = 0; foreach (var _pin in ePin) { if (i == 0) { dt1 = _pin.pins_Date.Value; var tt = DateTime.Now.Subtract(dt1).Minutes + (DateTime.Now.Subtract(dt1).Hours * 60); if (tt > 30) { break; } } if (i == 1) { dt2 = _pin.pins_Date.Value; } i++; } if (i > 1) { var a = dt1.Subtract(dt2).Minutes; var b = dt1.Subtract(dt1).Hours; if (dt1.Subtract(dt2).Minutes + (dt1.Subtract(dt2).Hours * 60) < 30) { throw new HttpException(_ErrorCode, ConstantHelper.PENDING_SMS); //var response = new HttpResponseMessage(HttpStatusCode.BadRequest); //response.Content = new StringContent(ConstantHelper.PENDING_SMS);"); } } } tblPin pin = new tblPin(); pin.pins_Date = DateTime.Now; if (id != 0) { pin.pins_AccountID = id; } else { pin.pins_Phone = phone; } pin.pins_Name = GeneratePIN(); db.tblPins.Add(pin); db.SaveChanges(); // if (!string.IsNullOrEmpty(_phone)) { using (HttpClient client = new HttpClient()) { var res = await client.GetAsync("http://api.mekongsms.com/api/sendsms.aspx?username=xware@mekongnet&pass=ea6fac133e078db85a25de53ad18a0e3&sender=MarielBank&smstext=" + "Dear valued customer,\nHere is your pin : " + pin.pins_Name + "&isflash=0&gsm=" + _phone + "\n\nMarielBank"); var status = await res.Content.ReadAsStringAsync(); if (status.Length > 0) { if (status.Substring(0, 1) == "0") { return(true); } } }; } re = false; return(re); }