Exemple #1
0
        public async Task <JsonResult> GetUserById()//(string Id)
        {
            try
            {
                //to get userid from access token
                string Id = User.Claims.First(c => c.Type == "UserID").Value;
                UserDetailsViewModel _userDetails = new UserDetailsViewModel();
                var user = await _userManager.FindByIdAsync(Id);

                var userstatus = user.UserStatus;
                if (user != null)
                {
                    var roles = await _userManager.GetRolesAsync(user);

                    var bussiness = new BussinessDetailViewModel();
                    if (roles[0] == Constants.isAdmin)
                    {
                        //get bussiness details of admin by id
                        bussiness = await _bussinessService.GetBussinessDetailsById(user.Id);
                    }
                    else
                    {
                        //get bussiness details of parent admin if role is subadmin
                        bussiness = await _bussinessService.GetBussinessDetailsById(user.ParentUserId);
                    }
                    _userDetails.Id               = user.Id;
                    _userDetails.Name             = user.Name;
                    _userDetails.Email            = user.Email;
                    _userDetails.PhoneNumber      = user.PhoneNumber;
                    _userDetails.ProfilePic       = user.ProfilePic;
                    _userDetails.Language         = user.Language;
                    _userDetails.UserType         = roles.FirstOrDefault();
                    _userDetails.DeviceToken      = user.DeviceToken;
                    _userDetails.DeviceType       = user.DeviceType;
                    _userDetails.AccessToken      = user.AccessToken;
                    _userDetails.ParentUserId     = user.ParentUserId;
                    _userDetails.UserStatus       = user.UserStatus;
                    _userDetails.IsActive         = user.IsActive;
                    _userDetails.Dob              = user.Dob;
                    _userDetails.Gender           = user.Gender;
                    _userDetails.CreatedDate      = user.CreatedDate;
                    _userDetails.BussinessDetails = bussiness;
                    return(new JsonResult(new { status = StatusCodes.Status200OK, success = true, message = "user profile" + ResponseMessages.msgShownSuccess, userstatus, user_info = _userDetails }));
                }
                return(new JsonResult(new { status = StatusCodes.Status404NotFound, success = false, message = ResponseMessages.msgCouldNotFoundAssociatedUser, userstatus = false }));
            }
            catch (Exception ex)
            {
                return(new JsonResult(new { status = StatusCodes.Status500InternalServerError, success = false, message = ResponseMessages.msgSomethingWentWrong + ex.Message, userstatus = false }));
            }
        }
Exemple #2
0
        public async Task <IActionResult> Login(LoginModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Ok(new { status = StatusCodes.Status406NotAcceptable, success = false, message = ResponseMessages.msgParametersNotCorrect, userstatus = false }));
                }

                var user = await _userManager.FindByEmailAsync(model.Email);

                if (user == null)
                {
                    return(Ok(new { status = StatusCodes.Status404NotFound, success = false, message = ResponseMessages.msgCouldNotFoundAssociatedUser, userstatus = false }));
                }
                var userstatus = user.UserStatus;
                if (!userstatus)
                {
                    return(Ok(new { status = StatusCodes.Status400BadRequest, success = false, message = ResponseMessages.msgUserBlockedOrDeleted, userstatus = false }));
                }
                if (user != null && userstatus && await _userManager.CheckPasswordAsync(user, model.Password))
                {
                    //check email is confirmed
                    if (!_userManager.IsEmailConfirmedAsync(user).Result)
                    {
                        return(Ok(new { status = StatusCodes.Status200OK, success = false, message = ResponseMessages.msgEmailNotConfirmed, userstatus }));
                    }
                    // update user with device type and device token
                    user.DeviceToken = model.DeviceToken;
                    user.DeviceType  = model.DeviceType;
                    // change the security stamp only on correct username/password
                    await _userManager.UpdateSecurityStampAsync(user);

                    // Get role assigned to the user
                    var roles = await _userManager.GetRolesAsync(user);

                    IdentityOptions _options = new IdentityOptions();

                    var tokenDescriptor = new SecurityTokenDescriptor
                    {
                        Subject = new ClaimsIdentity(new Claim[]
                        {
                            new Claim("UserID", user.Id.ToString()),
                            new Claim(_options.ClaimsIdentity.RoleClaimType, roles.FirstOrDefault())
                        }),
                        Expires            = DateTime.UtcNow.AddDays(1),
                        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appSettings.JWT_Secret)), SecurityAlgorithms.HmacSha256Signature)
                    };

                    var bussiness = new BussinessDetailViewModel();
                    if (roles[0] == Constants.isAdmin)
                    {
                        //get bussiness details of admin by id
                        bussiness = await _bussinessService.GetBussinessDetailsById(user.Id);
                    }
                    else
                    {
                        //get bussiness details of parent admin if role is subadmin
                        bussiness = await _bussinessService.GetBussinessDetailsById(user.ParentUserId);
                    }
                    var tokenHandler  = new JwtSecurityTokenHandler();
                    var securityToken = tokenHandler.CreateToken(tokenDescriptor);
                    var accessToken   = tokenHandler.WriteToken(securityToken);
                    List <Permissions> permissionList = new List <Permissions>();
                    UserInfo           _userinfo      = new UserInfo();
                    _userinfo.Id               = user.Id;
                    _userinfo.Name             = user.Name;
                    _userinfo.ProfilePic       = (user.ProfilePic != null && user.ProfilePic != "") ? GetImageUrl(Constants.userImagesContainer, user.ProfilePic) :"";
                    _userinfo.Email            = user.Email;
                    _userinfo.Status           = user.IsActive;
                    _userinfo.CurrencyId       = (bussiness != null) ? bussiness.CurrencyId : 0;
                    _userinfo.CurrencySymbol   = (bussiness != null && bussiness.CurrencySymbol != null && bussiness.CurrencySymbol != "")? bussiness.CurrencySymbol: "";
                    _userinfo.Currency         = (bussiness != null && bussiness.CurrencyCode != null && bussiness.CurrencyCode != "") ? bussiness.CurrencyCode : "";
                    _userinfo.UserType         = roles.FirstOrDefault();
                    _userinfo.Permissions_List = permissionList;//send later
                    _userinfo.AccessToken      = accessToken;

                    var user_info = new Object();
                    {
                        user_info = _userinfo;
                    };
                    return(Ok(new { status = StatusCodes.Status200OK, success = true, message = ResponseMessages.msgUserLoginSuccess, userstatus, user_info }));
                }
                else
                {
                    return(Ok(new { status = StatusCodes.Status400BadRequest, success = false, message = ResponseMessages.msgInvalidCredentials, userstatus = false }));
                }
            }
            catch (Exception ex)
            {
                return(Ok(new { status = StatusCodes.Status500InternalServerError, success = false, message = ResponseMessages.msgSomethingWentWrong + ex.Message, userstatus = false }));
            }
        }
        public async Task SendInvoiceMail(long InvoiceId)
        {
            try
            {
                InvoiceListViewModel invoiceDetails = new InvoiceListViewModel();
                invoiceDetails = await GetInvoiceByInvoiceId(InvoiceId);

                BussinessDetailViewModel adminbussinessDetials = new BussinessDetailViewModel();
                if (invoiceDetails != null)
                {
                    adminbussinessDetials = await _bussinessService.GetBussinessDetailsById(invoiceDetails.UserId);
                }

                var pathToFile = _hostingEnvironment.WebRootPath
                                 + Path.DirectorySeparatorChar.ToString()
                                 + Constants.mainTemplatesContainer
                                 + Path.DirectorySeparatorChar.ToString()
                                 + Constants.invoicesTemplatesContainer
                                 + Path.DirectorySeparatorChar.ToString()
                                 + Constants.invoice_template_Sample_Invoice_Template;

                var subject = string.Empty;
                if (invoiceDetails.Type == Constants.typeInvoice)
                {
                    subject = Constants.subject_SendInvoice_to_customer + " Invoice No: # " + invoiceDetails.InvoiceNumber;
                }
                else
                {
                    subject = Constants.subject_SendQuotation_to_customer + " Quotation No: # " + invoiceDetails.InvoiceNumber;
                }

                string        customerName = invoiceDetails.FirstName + " " + invoiceDetails.LastName;
                StringBuilder sb           = new StringBuilder();
                foreach (var item in invoiceDetails.ItemList)
                {
                    sb.Append("<tr class='item'>");
                    sb.AppendFormat("<td>{0}</td>", item.Name);
                    sb.AppendFormat("<td>{0}</td>", item.Quantity);
                    sb.AppendFormat("<td>{0}</td>", item.Tax);
                    sb.AppendFormat("<td>{0}</td>", item.Price);
                    sb.Append("</tr>");
                }
                string itemList = sb.ToString();
                var    body     = new BodyBuilder();
                using (StreamReader reader = System.IO.File.OpenText(pathToFile))
                {
                    body.HtmlBody = reader.ReadToEnd();
                }
                string messageBody = body.HtmlBody;
                messageBody = messageBody.Replace("{companylogoUrl}", adminbussinessDetials.BussinessLogo);
                messageBody = messageBody.Replace("{invoiceNumber}", invoiceDetails.InvoiceNumber);
                messageBody = messageBody.Replace("{invoiceDate}", invoiceDetails.Date);
                messageBody = messageBody.Replace("{dueDate}", invoiceDetails.DueDate);
                messageBody = messageBody.Replace("{bussinessName}", adminbussinessDetials.BussinessName);
                messageBody = messageBody.Replace("{bussinessAddress}", CommonMethods.SplitLine(adminbussinessDetials.Address1));
                messageBody = messageBody.Replace("{customerBussiness}", invoiceDetails.BussinessName);
                messageBody = messageBody.Replace("{customerEmail}", invoiceDetails.PersonalEmail);
                messageBody = messageBody.Replace("{itemList}", itemList);
                messageBody = messageBody.Replace("{subTotal}", invoiceDetails.Subtotal.ToString());
                messageBody = messageBody.Replace("{tax}", invoiceDetails.Tax.ToString());
                messageBody = messageBody.Replace("{total}", invoiceDetails.Total.ToString());
                messageBody = messageBody.Replace("{customerName}", customerName);
                messageBody = messageBody.Replace("{currencySymbol}", invoiceDetails.CurrencySymbol);
                await _emailSender.SendEmailAsync(email : invoiceDetails.PersonalEmail, subject : subject, htmlMessage : messageBody);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }