public ResponseViewModel <BusinessEmployeeViewModel> Register(BusinessEmployeeViewModel model)
        {
            var data     = new ResponseViewModel <BusinessEmployeeViewModel>();
            var hasEmail = _db.tblBusinessEmployees.Any(d => d.Email.ToLower() == model.Email.ToLower() && d.ServiceLocationId == model.ServiceLocationId);

            if (hasEmail)
            {
                data.Status  = false;
                data.Message = "This business email has been taken. Please try another email id.";
            }
            else
            {
                var businessEmployee = new tblBusinessEmployee()
                {
                    FirstName         = model.FirstName,
                    LastName          = model.LastName,
                    Password          = Security.Encrypt(model.Password, true),
                    Email             = model.Email,
                    STD               = model.STD,
                    PhoneNumber       = model.PhoneNumber,
                    ServiceLocationId = model.ServiceLocationId,
                    IsAdmin           = model.IsAdmin,
                    Created           = DateTime.Now.ToUniversalTime(),
                    IsActive          = model.IsActive
                };
                _db.tblBusinessEmployees.Add(businessEmployee);
                var response = _db.SaveChanges();
                data.Message = response > 0 ? "success" : "failed";
                data.Status  = response > 0 ? true : false;
                data.Data    = model;
            }
            return(data);
        }
Beispiel #2
0
        public async Task <IActionResult> UpdateEmployee([FromRoute] string id, [FromBody] BusinessEmployeeViewModel employeeModel)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the employee from the database
            BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

            if (employee == null)
            {
                errorMessage.Message = "Could not find employee";
                return(Json(error));
            }

            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

            if (user == null)
            {
                errorMessage.Message = "Could not find user profile";
                return(Json(error));
            }

            // Update information
            employee.FirstName      = employeeModel.FirstName;
            employee.LastName       = employeeModel.LastName;
            employee.Position       = employeeModel.Position;
            employee.CanEditLibrary = employeeModel.CanEditLibrary;

            user.Email = employeeModel.Email;

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                await _userManager.UpdateNormalizedEmailAsync(user);

                db.SaveChanges();
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Could not update employee information";
                return(Json(error));
            }

            // Model data to return to view
            var result = new BusinessEmployeeViewModel
            {
                Id             = employee.UserId,
                FirstName      = employee.FirstName,
                LastName       = employee.LastName,
                Position       = employee.Position,
                ProfilePicture = employee.ProfilePicture,
                CanEditLibrary = employee.CanEditLibrary,
                Email          = user.Email
            };

            return(Ok(result));
        }
        // PUT: api/businessemployee/5
        public IHttpActionResult Put(long?id, [FromBody] BusinessEmployeeViewModel model)
        {
            try
            {
                if (!id.HasValue)
                {
                    return(Ok(new { status = false, data = "Please provide a valid ID." }));
                }
                else
                {
                    var businessEmployee = _db.tblBusinessEmployees.Find(id);
                    if (businessEmployee != null)
                    {
                        if (businessEmployee.LoginId.ToLower() == model.LoginId.ToLower())
                        {
                            businessEmployee.FirstName         = model.FirstName;
                            businessEmployee.LastName          = model.LastName;
                            businessEmployee.LoginId           = model.LoginId;
                            businessEmployee.Password          = Security.Encrypt(model.Password, true);
                            businessEmployee.Email             = model.Email;
                            businessEmployee.STD               = model.STD;
                            businessEmployee.PhoneNumber       = model.PhoneNumber;
                            businessEmployee.ServiceLocationId = model.ServiceLocationId;
                            businessEmployee.IsAdmin           = model.IsAdmin;
                            businessEmployee.Created           = DateTime.Now.ToUniversalTime();
                            businessEmployee.IsActive          = model.IsActive;

                            _db.Entry(businessEmployee).State = EntityState.Modified;
                            var response = _db.SaveChanges();
                            if (response > 0)
                            {
                                return(Ok(new { status = true, data = businessEmployee }));
                            }
                            else
                            {
                                return(Ok(new { status = false, data = "There was a problem to update the data." }));
                            }
                        }
                        else
                        {
                            return(Ok(new { status = false, data = "Please provide a valid login id to update." }));
                        }
                    }
                    else
                    {
                        return(Ok(new { status = false, data = "Not a valid data to update. Please provide a valid id." }));
                    }
                }
            }
            catch (Exception ex)
            {
                return(Ok(new { status = false, data = "", message = ex.Message.ToString() }));
            }
        }
 // POST: api/businessemployee
 public IHttpActionResult Post([FromBody] BusinessEmployeeViewModel model)
 {
     try
     {
         if (model != null)
         {
             var check = _db.tblBusinessEmployees.Any(d => d.Email.ToLower() == model.Email.ToLower() && model.ServiceLocationId == model.ServiceLocationId);
             if (!check)
             {
                 var businessEmployee = new tblBusinessEmployee()
                 {
                     FirstName         = model.FirstName,
                     LastName          = model.LastName,
                     Password          = Security.Encrypt(model.Password, true),
                     Email             = model.Email,
                     STD               = model.STD,
                     PhoneNumber       = model.PhoneNumber,
                     ServiceLocationId = model.ServiceLocationId,
                     IsAdmin           = model.IsAdmin,
                     Created           = DateTime.Now.ToUniversalTime(),
                     IsActive          = model.IsActive
                 };
                 _db.tblBusinessEmployees.Add(businessEmployee);
                 var response = _db.SaveChanges();
                 if (response > 0)
                 {
                     return(Ok(new { status = true, data = businessEmployee, message = "success" }));
                 }
                 else
                 {
                     return(Ok(new { status = false, data = "", message = "There was a problem." }));
                 }
             }
             else
             {
                 return(Ok(new { status = false, data = "", message = "Email id has already been taken, please try another email id with same service location." }));
             }
         }
         else
         {
             return(Ok(new { status = false, data = "", message = "Model is not valid." }));
         }
     }
     catch (Exception ex)
     {
         return(Ok(new { status = false, data = "", message = ex.Message.ToString() }));
     }
 }
 // PUT: api/businessemployee/5
 public IHttpActionResult Put(long?id, [FromBody] BusinessEmployeeViewModel model)
 {
     try
     {
         if (!id.HasValue)
         {
             return(Ok(new { status = false, data = "", message = "Please provide a valid id." }));
         }
         else
         {
             var businessEmployee = _db.tblBusinessEmployees.Find(id);
             if (businessEmployee != null)
             {
                 if (businessEmployee.Email.ToLower() == model.Email.ToLower())
                 {
                     businessEmployee.FirstName         = model.FirstName;
                     businessEmployee.LastName          = model.LastName;
                     businessEmployee.STD               = model.STD;
                     businessEmployee.PhoneNumber       = model.PhoneNumber;
                     businessEmployee.ServiceLocationId = model.ServiceLocationId;
                     _db.Entry(businessEmployee).State  = EntityState.Modified;
                     var response = _db.SaveChanges();
                     if (response > 0)
                     {
                         return(Ok(new { status = true, data = businessEmployee, message = "success" }));
                     }
                     else
                     {
                         return(Ok(new { status = false, data = "", message = "There was a problem to update the data." }));
                     }
                 }
                 else
                 {
                     return(Ok(new { status = false, data = "", message = "Please provide a valid id to update." }));
                 }
             }
             else
             {
                 return(Ok(new { status = false, data = "", message = "Not a valid data to update. Please provide a valid id." }));
             }
         }
     }
     catch (Exception ex)
     {
         return(Ok(new { status = false, data = "", message = ex.Message.ToString() }));
     }
 }
 // POST: api/businessemployee
 public IHttpActionResult Post([FromBody] BusinessEmployeeViewModel model)
 {
     try
     {
         if (model != null)
         {
             var businessEmployee = new tblBusinessEmployee()
             {
                 FirstName         = model.FirstName,
                 LastName          = model.LastName,
                 LoginId           = model.LoginId,
                 Password          = Security.Encrypt(model.Password, true),
                 Email             = model.Email,
                 STD               = model.STD,
                 PhoneNumber       = model.PhoneNumber,
                 ServiceLocationId = model.ServiceLocationId,
                 IsAdmin           = model.IsAdmin,
                 Created           = DateTime.Now.ToUniversalTime(),
                 IsActive          = model.IsActive
             };
             _db.tblBusinessEmployees.Add(businessEmployee);
             var response = _db.SaveChanges();
             if (response > 0)
             {
                 return(Ok(new { status = true, data = businessEmployee }));
             }
             else
             {
                 return(Ok(new { status = false, data = "There was a problem." }));
             }
         }
         else
         {
             return(Ok(new { status = false, data = "Model is not valid." }));
         }
     }
     catch (Exception ex)
     {
         return(Ok(new { status = false, data = "", message = ex.Message.ToString() }));
     }
 }
        public ResponseViewModel <BusinessEmployeeViewModel> UpdateEmployee(BusinessEmployeeViewModel model)
        {
            var data = new ResponseViewModel <BusinessEmployeeViewModel>();

            try
            {
                var businessEmployee = _db.tblBusinessEmployees.Find(model.Id);
                if (businessEmployee != null)
                {
                    var password = Security.Decrypt(businessEmployee.Password, true);
                    if (password == model.OldPassword)
                    {
                        if (businessEmployee.Email.ToLower() == model.Email.ToLower())
                        {
                            businessEmployee.Password         = Security.Encrypt(model.Password, true);
                            _db.Entry(businessEmployee).State = EntityState.Modified;
                            var response = _db.SaveChanges();
                            data.Message = response > 0 ? "success" : "failed";
                            data.Status  = response > 0 ? true : false;
                        }
                        else
                        {
                            data.Message = "Please enter a valid email id.";
                        }
                    }
                    else
                    {
                        data.Message = "Please enter your valid old password.";
                    }
                }
                else
                {
                    data.Message = "It is not a valid information.";
                }
            }
            catch (Exception ex)
            {
                data.Message = "ex: " + ex.Message.ToString();
            }
            return(data);
        }
Beispiel #8
0
        public async Task <IActionResult> GetEmployeeById(string id)
        {
            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            // Get the employee from the database
            BusinessEmployees employee = db.BusinessEmployees.Where(e => e.UserId == id).SingleOrDefault();

            if (employee == null)
            {
                errorMessage.Message = "Could not find employee";
                return(Json(error));
            }

            ApplicationUser user = db.ApplicationUser.Where(e => e.Id == id).SingleOrDefault();

            if (user == null)
            {
                errorMessage.Message = "Could not find user profile";
                return(Json(error));
            }

            // Get the data for the employee
            var employeeModel = new BusinessEmployeeViewModel
            {
                Id             = employee.UserId,
                FirstName      = employee.FirstName,
                LastName       = employee.LastName,
                Email          = user.Email,
                Position       = employee.Position,
                PhoneNumber    = employee.PhoneNumber,
                CanEditLibrary = employee.CanEditLibrary
            };

            return(Ok(employeeModel));
        }
Beispiel #9
0
        public ResponseViewModel <RegisterViewModel> Register(RegisterViewModel model)
        {
            var data = new ResponseViewModel <RegisterViewModel>();

            var hasName  = _db.tblBusinesses.Any(d => d.Name.ToLower() == model.Business.Name.ToLower());
            var hasEmail = _db.tblBusinessEmployees.Any(d => d.Email.ToLower() == model.Business.Email.ToLower());

            if (hasName)
            {
                data.Status  = false;
                data.Message = "This business name has been taken. Please try another name.";
            }
            else if (hasEmail)
            {
                data.Status  = false;
                data.Message = "This business email has been taken. Please try another email id.";
            }
            else
            {
                using (var dbTran = _db.Database.BeginTransaction())
                {
                    try
                    {
                        var business = new tblBusiness()
                        {
                            Name               = model.Business.Name,
                            ShortName          = model.Business.ShortName,
                            IsInternational    = model.Business.IsInternational,
                            FaxNumbers         = model.Business.FaxNumbers,
                            PhoneNumbers       = model.Business.PhoneNumbers,
                            Logo               = model.Business.Logo,
                            Add1               = model.Business.Add1,
                            Add2               = model.Business.Add2,
                            City               = model.Business.City,
                            State              = model.Business.State,
                            CountryId          = model.Business.CountryId,
                            Email              = model.Business.Email,
                            Website            = model.Business.Website,
                            Created            = DateTime.Now.ToUniversalTime(),
                            IsActive           = model.Business.IsActive,
                            Zip                = model.Business.Zip,
                            MembershipId       = model.Business.MembershipId,
                            BusinessCategoryId = model.Business.BusinessCategoryId,
                            TimezoneId         = model.Business.TimezoneId
                        };

                        _db.tblBusinesses.Add(business);
                        var responseBusiness = _db.SaveChanges();

                        var serviceLocation = new tblServiceLocation()
                        {
                            Name        = "Main Address",
                            Add1        = model.Business.Add1,
                            Add2        = model.Business.Add2,
                            City        = model.Business.City,
                            State       = model.Business.State,
                            CountryId   = model.Business.CountryId.Value,
                            Created     = DateTime.Now.ToUniversalTime(),
                            IsActive    = model.Business.IsActive,
                            Zip         = model.Business.Zip,
                            BusinessId  = business.Id,
                            TimezoneId  = business.TimezoneId,
                            Description = ""
                        };

                        _db.tblServiceLocations.Add(serviceLocation);
                        var responseServiceLocation = _db.SaveChanges();

                        //Setup default business hours
                        //var businessHourController = new BusinessHourController();
                        //var responseBusinessHour = businessHourController.SetupBusinessHours(serviceLocation.Id);
                        var today = DateTime.Now;
                        var date  = new DateTime(today.Year, today.Month, today.Day, 8, 00, 00, DateTimeKind.Utc);
                        for (int i = 0; i < 7; i++)
                        {
                            var businessHour = new tblBusinessHour()
                            {
                                WeekDayId         = i,
                                IsStartDay        = i == 0 ? true : false,
                                IsHoliday         = false,
                                From              = date,
                                To                = date.AddHours(10),
                                IsSplit1          = false,
                                FromSplit1        = null,
                                ToSplit1          = null,
                                IsSplit2          = false,
                                FromSplit2        = null,
                                ToSplit2          = null,
                                ServiceLocationId = serviceLocation.Id
                            };
                            _db.tblBusinessHours.Add(businessHour);
                        }
                        var responseBusinessHour = _db.SaveChanges();
                        //End

                        var businessEmployee = new tblBusinessEmployee()
                        {
                            FirstName         = model.Employee.FirstName,
                            LastName          = model.Employee.LastName,
                            Password          = Security.Encrypt(model.Employee.Password, true),
                            Email             = model.Employee.Email,
                            STD               = model.Employee.STD,
                            PhoneNumber       = model.Employee.PhoneNumber,
                            ServiceLocationId = serviceLocation.Id,
                            IsAdmin           = true,
                            Created           = DateTime.Now.ToUniversalTime(),
                            IsActive          = true
                        };
                        _db.tblBusinessEmployees.Add(businessEmployee);
                        var responseBusinessEmployee = _db.SaveChanges();

                        var businessViewModel = new BusinessViewModel()
                        {
                            Add1 = business.Add1,
                            Add2 = business.Add2,
                            BusinessCategoryId = business.BusinessCategoryId,
                            City            = business.City,
                            CountryId       = business.CountryId,
                            Created         = business.Created,
                            Email           = business.Email,
                            FaxNumbers      = business.FaxNumbers,
                            Id              = business.Id,
                            IsActive        = business.IsActive,
                            IsInternational = business.IsInternational,
                            Logo            = business.Logo,
                            MembershipId    = business.MembershipId,
                            Name            = business.Name,
                            PhoneNumbers    = business.PhoneNumbers,
                            ShortName       = business.ShortName,
                            State           = business.State,
                            TimezoneId      = business.TimezoneId,
                            Website         = business.Website,
                            Zip             = business.Zip
                        };

                        var businessEmployeeViewModel = new BusinessEmployeeViewModel()
                        {
                            Created           = businessEmployee.Created,
                            Email             = businessEmployee.Email,
                            FirstName         = businessEmployee.FirstName,
                            Id                = businessEmployee.Id,
                            IsActive          = businessEmployee.IsActive,
                            IsAdmin           = businessEmployee.IsAdmin,
                            LastName          = businessEmployee.LastName,
                            PhoneNumber       = businessEmployee.PhoneNumber,
                            ServiceLocationId = businessEmployee.ServiceLocationId,
                            STD               = businessEmployee.STD
                        };

                        if (responseBusiness > 0 && responseServiceLocation > 0 && responseBusinessEmployee > 0 && responseBusinessHour > 0)
                        {
                            data.Status        = true;
                            data.Message       = "Transaction successed.";
                            data.Data          = new RegisterViewModel();
                            data.Data.Business = new BusinessViewModel();
                            data.Data.Employee = new BusinessEmployeeViewModel();
                            data.Data.Business = businessViewModel;
                            data.Data.Employee = businessEmployeeViewModel;
                            dbTran.Commit();
                        }
                        else
                        {
                            data.Status = false;
                            var reason = "";
                            if (responseBusiness <= 0)
                            {
                                reason += "Business setup issue. ";
                            }
                            if (responseServiceLocation <= 0)
                            {
                                reason += " Service location issue.";
                            }
                            if (responseBusinessEmployee <= 0)
                            {
                                reason += " Business employee issue.";
                            }
                            if (responseBusinessHour <= 0)
                            {
                                reason += " Business hour issue.";
                            }

                            data.Message       = "Registration failed. reason: " + reason;
                            data.Data.Business = new BusinessViewModel();
                            data.Data.Employee = new BusinessEmployeeViewModel();
                            dbTran.Rollback();
                        }
                    }
                    catch (Exception ex)
                    {
                        data.Status        = false;
                        data.Message       = "Registration failed. ex: " + ex.Message.ToString();
                        data.Data.Business = new BusinessViewModel();
                        data.Data.Employee = new BusinessEmployeeViewModel();
                        dbTran.Rollback();
                    }
                }
            }
            return(data);
        }
Beispiel #10
0
        public async Task <IActionResult> GetAllEmployees()
        {
            var id = "";

            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            try
            {
                id = User.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").SingleOrDefault().Value;
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Id was not found";
                return(BadRequest(error));
            }

            // Get the user
            BusinessUsers user = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

            if (user == null)
            {
                errorMessage.Message = "Could not find the business user profile";
                return(Json(error));
            }

            List <BusinessEmployees> employees = new List <BusinessEmployees>();

            employees = db.BusinessEmployees.Where(e => e.BusinessUserId == user.BusinessUserId).ToList();

            List <BusinessEmployeeViewModel> employeeList = new List <BusinessEmployeeViewModel>();

            // Format list with required data
            foreach (BusinessEmployees employee in employees)
            {
                // Get the application user profile
                var profile = db.ApplicationUser.Where(e => e.Id == employee.UserId).SingleOrDefault();

                if (profile == null)
                {
                    errorMessage.Message = "Could not find the employee profile";
                    return(Json(error));
                }

                // Create view model to return
                var businessEmployee = new BusinessEmployeeViewModel
                {
                    Id             = profile.Id,
                    FirstName      = employee.FirstName,
                    LastName       = employee.LastName,
                    Email          = profile.Email,
                    Position       = employee.Position,
                    ProfilePicture = employee.ProfilePicture,
                    PhoneNumber    = employee.PhoneNumber
                };

                employeeList.Add(businessEmployee);
            }

            return(Ok(employeeList.ToArray()));
        }
Beispiel #11
0
        public async Task <IActionResult> AddEmployee([FromBody] BusinessEmployeeViewModel employee)
        {
            var id = "";

            var errorMessage = new ErrorMessageViewModel();
            var error        = new { Error = errorMessage };

            try
            {
                id = User.Claims.Where(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier").SingleOrDefault().Value;
            }
            catch (Exception exception)
            {
                errorMessage.Message = "Id was not found";
                return(BadRequest(error));
            }

            // Get the employer info
            BusinessUsers business = db.BusinessUsers.Where(e => e.UserId == id).SingleOrDefault();

            if (business == null)
            {
                errorMessage.Message = "Could not get the employer info";
                return(Json(error));
            }

            // Check if the user exists
            ApplicationUser user = _userManager.Users.SingleOrDefault(e => e.Email.Equals(employee.Email, StringComparison.InvariantCultureIgnoreCase));

            // If not, create the user identity profile
            if (user == null)
            {
                if (employee.Email != null)
                {
                    ApplicationUser applicationUser = new ApplicationUser
                    {
                        UserName = employee.Email,
                        Email    = employee.Email
                    };

                    // Set temporary password
                    string password = String.Format("{0}{1}#{2}",
                                                    employee.Email.First().ToString().ToUpper(), employee.Email.Substring(1), DateTime.Now.Year);


                    IdentityResult result = await _userManager.CreateAsync(applicationUser, password);

                    if (result.Succeeded)
                    {
                        await _userManager.AddToRoleAsync(applicationUser, "Employee");

                        await _signInManager.SignInAsync(applicationUser, false);

                        // Add the new user to the database
                        BusinessEmployees businessEmployee = new BusinessEmployees
                        {
                            FirstName      = employee.FirstName,
                            LastName       = employee.LastName,
                            Position       = employee.Position,
                            CanEditLibrary = employee.CanEditLibrary,
                            PhoneNumber    = employee.PhoneNumber,
                            UserId         = applicationUser.Id,
                            BusinessUserId = business.BusinessUserId
                        };

                        if (!String.IsNullOrWhiteSpace(employee.ProfilePicture))
                        {
                            var fileName = await fileController.UploadImage(employee.ProfilePicture, Request);

                            if (String.IsNullOrWhiteSpace(fileName))
                            {
                                errorMessage.Message = "Image upload encountered an error";
                                return(Json(error));
                            }

                            businessEmployee.ProfilePicture = fileName;
                        }

                        db.BusinessEmployees.Add(businessEmployee);

                        try
                        {
                            db.SaveChanges();
                        }
                        catch (Exception exception)
                        {
                            errorMessage.Message = "Could not create the business employee";
                            return(Json(error));
                        }

                        // model data
                        var modelData = new BusinessEmployeeViewModel
                        {
                            Id             = businessEmployee.UserId,
                            FirstName      = businessEmployee.FirstName,
                            LastName       = businessEmployee.LastName,
                            Position       = businessEmployee.Position,
                            ProfilePicture = businessEmployee.ProfilePicture,
                            PhoneNumber    = businessEmployee.PhoneNumber,
                            CanEditLibrary = businessEmployee.CanEditLibrary,
                            Email          = applicationUser.Email
                        };

                        return(CreatedAtAction("AddEmployee", new { id = modelData.Id }, modelData));
                    }

                    // Send an email with this link ( SET UP SENDGRID TOWARDS END OF PROJECT )

                    //string code = await _userManager.GeneratePasswordResetTokenAsync(applicationUser);
                    //var callbackUrl = Url.Action("ResetPassword", "Accounts", new { userId = applicationUser.Id, code }, protocol: Request.Scheme);

                    //// Send the email
                    //var emailNotification = new EmailNotificationViewModel
                    //{
                    //    Email = applicationUser.Email,
                    //    Subject = "SPINE - Password Reset",
                    //    Content = "",
                    //    Html = "Please reset your password by clicking <a href =\"" + callbackUrl + "\">here</a>"
                    //};

                    //await notificationsController.SendEmailNotification(emailNotification);
                }
                else
                {
                    errorMessage.Message = "Data is missing the Email field";
                    return(Json(error));
                }
            }

            errorMessage.Message = "An error has occurred";
            return(Json(error));
        }