public async Task Process()
        {
            var logConst = "UserScheduleProcessor-Process";

            try
            {
                CareStreamContextFactory apiContextFactory = new CareStreamContextFactory();

                _cc = apiContextFactory.CreateDbContext(new string[] { });

                if (_cc == null)
                {
                    return;
                }



                var bulkUserFiles = _cc.BulkUserFiles.Where(x => x.Status == CareStreamConst.Bulk_User_Loaded_Status ||
                                                            x.Status == CareStreamConst.Bulk_User_Started_Status).ToList();

                if (bulkUserFiles.Any())
                {
                    var _dbHelper = new DbHelper(_logger, _cc);
                    foreach (var bulkUserFile in bulkUserFiles)
                    {
                        try
                        {
                            _dbHelper.UpdateTable(bulkUserFile, CareStreamConst.Bulk_User_Started_Status);


                            var fileId = bulkUserFile.Id;

                            var bulkUsers = _cc.BulkUsers.Where(x => x.FileId == fileId &&
                                                                (x.Status == CareStreamConst.Bulk_User_Loaded_Status ||
                                                                 x.Status == CareStreamConst.Bulk_User_Started_Status ||
                                                                 x.Status == CareStreamConst.Bulk_User_InProgress_Status)).ToList();
                            if (bulkUsers.Any())
                            {
                                await ProcessUserAction(bulkUsers, bulkUserFile.Action);
                            }

                            _dbHelper.UpdateTable(bulkUserFile, CareStreamConst.Bulk_User_Completed_Status);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"{logConst}:error processing for file id {bulkUserFile.Id}");
                            _logger.LogError(ex);

                            bulkUserFile.Error = $"{ex.ToString()}. Message: {ex.Message}";
                            _dbHelper.UpdateTable(bulkUserFile, CareStreamConst.Bulk_User_Failed_Status);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"{logConst}:Exception occured while processing the bulk user...");
                _logger.LogError(ex);
            }
        }
        public async Task ProcessUser(List <BulkUser> bulkUsers)
        {
            var logConst = "UserInvite-ProcessUser";

            try
            {
                foreach (var bulkUser in bulkUsers)
                {
                    try
                    {
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Started_Status);

                        #region UserModel

                        var inviteUser = new InviteUser
                        {
                            CustomizedMessageBody   = bulkUser.CustomizedMessageBody,
                            InvitedUserEmailAddress = bulkUser.InviteeEmail,
                            InviteRedeemUrl         = bulkUser.InviteRedirectURL,
                            SendInvitationMessage   = bulkUser.SendEmail.ToString()
                        };


                        #endregion

                        if (string.IsNullOrEmpty(inviteUser.InvitedUserEmailAddress) && string.IsNullOrEmpty(inviteUser.InviteRedeemUrl))
                        {
                            var errorMessage = $"[Cannot send user invite] for bulk user id [{bulkUser.Id}] required field is missing, please correct the record.";

                            _logger.LogWarn(errorMessage);
                            bulkUser.Error = errorMessage;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                            continue;
                        }


                        var userService = new UserService(_logger);

                        await userService.SendInvite(inviteUser);

                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Completed_Status);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"{logConst}:error updating user for bulk user id {bulkUser.Id}");
                        _logger.LogError(ex);

                        bulkUser.Error = $"{ex.ToString()}. Message: {ex.Message}";
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"{logConst}:Exception occured while processing the bulk user update...");
                _logger.LogError(ex);
            }
        }
Example #3
0
        public async Task ProcessUser(List <BulkUser> bulkUsers)
        {
            var logConst = "UserCreate-ProcessUser";

            try
            {
                foreach (var bulkUser in bulkUsers)
                {
                    try
                    {
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Started_Status);

                        #region UserModel

                        var userModel = new UserModel
                        {
                            AccountEnabled = bulkUser.BlockSignIn
                        };

                        if (!string.IsNullOrEmpty(bulkUser.UserPrincipalName))
                        {
                            userModel.SignInName        = bulkUser.UserPrincipalName;
                            userModel.UserPrincipalName = bulkUser.UserPrincipalName;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.DisplayName))
                        {
                            userModel.DisplayName = bulkUser.DisplayName;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.InitialPassword))
                        {
                            userModel.Password = bulkUser.InitialPassword;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.FirstName))
                        {
                            userModel.GivenName = bulkUser.FirstName;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.LastName))
                        {
                            userModel.Surname = bulkUser.LastName;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.JobTitle))
                        {
                            userModel.JobTitle = bulkUser.JobTitle;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.Department))
                        {
                            userModel.Department = bulkUser.Department;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.Usagelocation))
                        {
                            userModel.UsageLocation = bulkUser.Usagelocation;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.StreetAddress))
                        {
                            userModel.StreetAddress = bulkUser.StreetAddress;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.State))
                        {
                            userModel.State = bulkUser.State;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.Country))
                        {
                            userModel.Country = bulkUser.Country;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.Office))
                        {
                            userModel.OfficeLocation = bulkUser.Office;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.City))
                        {
                            userModel.City = bulkUser.City;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.ZIP))
                        {
                            userModel.PostalCode = bulkUser.ZIP;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.MobilePhone))
                        {
                            userModel.MobilePhone = bulkUser.MobilePhone;
                        }

                        if (!string.IsNullOrEmpty(bulkUser.OfficePhone))
                        {
                            userModel.BusinessPhone = bulkUser.OfficePhone;
                        }
                        #endregion

                        var client = GraphClientUtility.GetGraphServiceClient();
                        if (client == null)
                        {
                            var errorMessage = $"[User Create] Unable to get the proxy for Graph client for user [{bulkUser.UserPrincipalName}]";
                            _logger.LogWarn(errorMessage);
                            continue;
                        }

                        var userService = new UserService(_logger);
                        if (!userService.IsUserModelValid(userModel))
                        {
                            var errorMessage = $"[Cannot created the user] For user [{bulkUser.UserPrincipalName}] required field for creation is missing, please correct the record.";

                            _logger.LogWarn(errorMessage);
                            bulkUser.Error = errorMessage;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                            continue;
                        }

                        var tenantId = GraphClientUtility.TenantId;
                        var b2cExtensionAppClientId = GraphClientUtility.b2cExtensionAppClientId;

                        var newUser = userService.BuildUserForCreation(userModel, tenantId, b2cExtensionAppClientId);

                        var result = await client.Users.Request().AddAsync(newUser);

                        if (result != null)
                        {
                            bulkUser.ObjectID = result.Id;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Completed_Status);
                        }
                        else
                        {
                            bulkUser.Error = $"Failed to create user for [{bulkUser.UserPrincipalName}]";
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"{logConst}:error creating user for bulk user id {bulkUser.Id}");
                        _logger.LogError(ex);

                        bulkUser.Error = $"{ex.ToString()}. Message: {ex.Message}";
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"{logConst}:Exception occured while processing the bulk user...");
                _logger.LogError(ex);
            }
        }
        public async Task ProcessUser(List <BulkUser> bulkUsers)
        {
            var logConst = "UserUpdate-ProcessUser";

            try
            {
                foreach (var bulkUser in bulkUsers)
                {
                    try
                    {
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Started_Status);

                        #region UserModel

                        var userModel = new UserModel
                        {
                            AccountEnabled    = bulkUser.BlockSignIn,
                            UserPrincipalName = bulkUser.UserPrincipalName,
                            DisplayName       = bulkUser.DisplayName,
                            Password          = bulkUser.InitialPassword,
                            GivenName         = bulkUser.FirstName,
                            Surname           = bulkUser.LastName,
                            JobTitle          = bulkUser.JobTitle,
                            Department        = bulkUser.Department,
                            UsageLocation     = bulkUser.Usagelocation,
                            StreetAddress     = bulkUser.StreetAddress,
                            State             = bulkUser.State,
                            Country           = bulkUser.Country,
                            OfficeLocation    = bulkUser.Office,
                            City          = bulkUser.City,
                            PostalCode    = bulkUser.ZIP,
                            MobilePhone   = bulkUser.MobilePhone,
                            BusinessPhone = bulkUser.OfficePhone
                        };


                        #endregion

                        if (string.IsNullOrEmpty(bulkUser.UserPrincipalName) && string.IsNullOrEmpty(bulkUser.ObjectID))
                        {
                            var errorMessage = $"[Cannot update user] For bulk user id [{bulkUser.Id}] required field is missing, please correct the record.";

                            _logger.LogWarn(errorMessage);
                            bulkUser.Error = errorMessage;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                            continue;
                        }

                        var client = GraphClientUtility.GetGraphServiceClient();
                        if (client == null)
                        {
                            var errorMessage = $"[User Create] Unable to get the proxy for Graph client for user [{bulkUser.UserPrincipalName}]";
                            _logger.LogWarn(errorMessage);
                            continue;
                        }

                        var userId = bulkUser.ObjectID;

                        if (string.IsNullOrEmpty(userId))
                        {
                            var userAsync = await client.Users.Request().Filter($"userPrincipalName eq '{bulkUser.UserPrincipalName}'").GetAsync();

                            if (userAsync.Any())
                            {
                                userId = userAsync[0].Id;
                            }
                        }

                        if (string.IsNullOrEmpty(userId))
                        {
                            bulkUser.Error = $"Failed to get user for [{bulkUser.UserPrincipalName}]";
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                            continue;
                        }


                        var b2cExtensionAppClientId = GraphClientUtility.b2cExtensionAppClientId;

                        var userService  = new UserService(_logger);
                        var updatingUser = userService.BuildUserForUpdate(userModel, b2cExtensionAppClientId);

                        var result = await client.Users[userId].Request().UpdateAsync(updatingUser);

                        if (result != null)
                        {
                            bulkUser.ObjectID = result.Id;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Completed_Status);
                        }
                        else
                        {
                            bulkUser.Error = $"Failed to update user for [{bulkUser.UserPrincipalName}]";
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"{logConst}:error updating user for bulk user id {bulkUser.Id}");
                        _logger.LogError(ex);

                        bulkUser.Error = $"{ex.ToString()}. Message: {ex.Message}";
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"{logConst}:Exception occured while processing the bulk user update...");
                _logger.LogError(ex);
            }
        }
Example #5
0
        public async Task ProcessUser(List <BulkUser> bulkUsers)
        {
            var logConst = "UserDelete-ProcessUser";

            try
            {
                foreach (var bulkUser in bulkUsers)
                {
                    try
                    {
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Started_Status);

                        if (string.IsNullOrEmpty(bulkUser.UserPrincipalName))
                        {
                            var errorMessage = $"[User delete] user id is missing, please correct the record.";

                            _logger.LogWarn(errorMessage);
                            bulkUser.Error = errorMessage;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                            continue;
                        }

                        var client = GraphClientUtility.GetGraphServiceClient();
                        if (client == null)
                        {
                            var errorMessage = $"[User Delete] Unable to get the proxy for Graph client for user [{bulkUser.UserPrincipalName}]";
                            _logger.LogWarn(errorMessage);
                            continue;
                        }

                        var userAsync = await client.Users.Request().Filter($"userPrincipalName eq '{bulkUser.UserPrincipalName}'").GetAsync();

                        if (userAsync.Any())
                        {
                            foreach (var user in userAsync)
                            {
                                try
                                {
                                    await client.Users[user.Id].Request().DeleteAsync();
                                }
                                catch (Exception ex)
                                {
                                    _logger.LogError($"{logConst}:error deleting user for bulk user id {bulkUser.Id}");
                                    _logger.LogError(ex);

                                    bulkUser.Error = $"{ex.ToString()}. Message: {ex.Message}";
                                    _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                                    continue;
                                }
                                _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Completed_Status);
                            }
                        }
                        else
                        {
                            var errorMessage = $"[User delete] unable to get the detail for user id {bulkUser.UserPrincipalName}";

                            _logger.LogWarn(errorMessage);
                            bulkUser.Error = errorMessage;
                            _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"{logConst}:error creating user for bulk user id {bulkUser.Id}");
                        _logger.LogError(ex);

                        bulkUser.Error = $"{ex.ToString()}. Message: {ex.Message}";
                        _dbHelper.UpdateTable(bulkUser, CareStreamConst.Bulk_User_Failed_Status);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"{logConst}:Exception occured while processing the bulk user...");
                _logger.LogError(ex);
            }
        }