private async Task <SalesforceUser> MapActiveDirectoryUserToSalesforceUser(AzureActiveDirectoryUser user) { var operatingSystemTask = CheckUserFranchiseAgainstFranchiseSource(user, ProgramDataSources.ClearCare, "ClearCare", "N/A"); var timeZoneTask = _timeZoneService.RetrieveTimeZoneAndPopulateUsersCountryCode(user); await Task.WhenAll(operatingSystemTask, timeZoneTask); var salesforceUser = new SalesforceUser { FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, FranchiseNumber = user.FranchiseNumber, ExternalId = user.ExternalId, FederationId = user.FederationId, MobilePhone = user.MobilePhone, Address = user.Address, City = user.City, State = user.State, PostalCode = user.PostalCode, CountryCode = user.CountryCode, IsOwner = user.IsOwner, Title = user.Title, OperatingSystem = await operatingSystemTask, TimeZone = await timeZoneTask }; return(salesforceUser); }
public async Task ProcessMessage_ExceptionRetrievingSalesforceFranchiseData() { // ARRANGE var appCache = (IAppCache)_context.Properties["appCache"]; var loggerMock = new Mock <ILogger <SalesforceMessageProcessor> >(); var tokenServiceMock = new Mock <IHISCTokenService>(); var programDataServiceMock = new Mock <IProgramDataService>(); var timeZoneServiceMock = new Mock <ITimeZoneService>(); var salesforceUserPublishServiceMock = new Mock <ISalesforceUserService>(); var unitUnderTest = new SalesforceMessageProcessor(appCache, loggerMock.Object, tokenServiceMock.Object, programDataServiceMock.Object, timeZoneServiceMock.Object, salesforceUserPublishServiceMock.Object); tokenServiceMock.Setup(mock => mock.RetrieveToken()) .Throws <System.IO.IOException>() .Verifiable(); var salesforceUser = new AzureActiveDirectoryUser { Address = "11218 John Galt Blvd.", FranchiseNumber = "100" }; // ACT await unitUnderTest.ProcessUser(salesforceUser); }
public async Task ProcessMessage_SuccessSyncUserWithClearCareOperatingSystem() { // ARRANGE var appCache = (IAppCache)_context.Properties["appCache"]; var loggerMock = new Mock <ILogger <SalesforceMessageProcessor> >(); var tokenServiceMock = new Mock <IHISCTokenService>(); var programDataServiceMock = new Mock <IProgramDataService>(); var timeZoneServiceMock = new Mock <ITimeZoneService>(); var salesforceUserPublishServiceMock = new Mock <ISalesforceUserService>(); var unitUnderTest = new SalesforceMessageProcessor(appCache, loggerMock.Object, tokenServiceMock.Object, programDataServiceMock.Object, timeZoneServiceMock.Object, salesforceUserPublishServiceMock.Object); var salesforceFranchises = new[] { 100 }; var clearCareFranchises = new[] { 100 }; var token = "TOKEN"; tokenServiceMock.Setup(mock => mock.RetrieveToken()) .ReturnsAsync(token) .Verifiable(); programDataServiceMock.Setup(mock => mock.RetrieveFranchises(ProgramDataSources.Salesforce, token, true)) .ReturnsAsync(salesforceFranchises) .Verifiable(); programDataServiceMock.Setup(mock => mock.RetrieveFranchises(ProgramDataSources.ClearCare, token, true)) .ReturnsAsync(clearCareFranchises) .Verifiable(); var timeZone = "Americas/NewYork"; timeZoneServiceMock.Setup(mock => mock.RetrieveTimeZoneAndPopulateUsersCountryCode(It.IsAny <AzureActiveDirectoryUser>())) .ReturnsAsync(timeZone) .Verifiable(); var salesforceUser = new AzureActiveDirectoryUser { Address = "11218 John Galt Blvd.", FranchiseNumber = "100" }; // ACT await unitUnderTest.ProcessUser(salesforceUser); // ASSERT tokenServiceMock.Verify(); programDataServiceMock.Verify(); timeZoneServiceMock.Verify(); salesforceUserPublishServiceMock.Verify(mock => mock.Publish(It.IsAny <SalesforceUser>())); }
public async Task <string> RetrieveTimeZoneAndPopulateUsersCountryCode(AzureActiveDirectoryUser user) { var cacheKey = $"{_cacheKeyPrefix}{user.FranchiseNumber}"; var timeZoneAndCountry = await _cache.GetOrAddAsync(cacheKey, () => GetUserTimeZoneCountry(user), DateTimeOffset.UtcNow.AddHours(_franchiseTimeZoneCacheDurationInHours)); user.CountryCode = timeZoneAndCountry.Country; return(timeZoneAndCountry.TimeZone); }
private async Task DeactivateUserIfActiveInSalesforce(AzureActiveDirectoryUser user) { var userJson = System.Text.Json.JsonSerializer.Serialize(user); var userExistsAndIsActiveInSalesforce = await UserExistsAndIsActiveInSalesforce(user); if (userExistsAndIsActiveInSalesforce) { _logger.LogInformation($"User will be Deactivated: {userJson}"); await _salesforceUserService.Deactivate(user.ExternalId); } else { _logger.LogInformation($"User will NOT be Deactived as it is NOT an Active User in Salesforce: {userJson}"); } }
public async Task ProcessUser(AzureActiveDirectoryUser user) { if (user == null) { _logger.LogInformation("User will NOT be Processed as it is null."); return; } var json = System.Text.Json.JsonSerializer.Serialize(user); var usersFranchiseExistsInProgramDataSalesforceFranchises = await CheckUserFranchiseAgainstFranchiseSource(user, ProgramDataSources.Salesforce, true, false); if (usersFranchiseExistsInProgramDataSalesforceFranchises) { if (user.DeactivationDateTimeOffset.HasValue) { if (user.DeactivationDateTimeOffset.Value <= DateTimeOffset.Now) { _logger.LogInformation($"User is a candidate for Deactivation: {json}"); await DeactivateUserIfActiveInSalesforce(user); } else { _logger.LogInformation($"User will NOT be Deactived OR Published as it has a Deactivation Date AND it's Deactivation Date has not passed: {json}"); } } else { _logger.LogInformation($"User will be Published: {json}"); var salesforceUser = await MapActiveDirectoryUserToSalesforceUser(user); await _salesforceUserService.Publish(salesforceUser); } } else { var usersFranchiseExistsInSalesforce = await UsersFranchiseExistsInSalesforceFranchises(user.FranchiseNumber); if (usersFranchiseExistsInSalesforce) { _logger.LogInformation($"Users franchise does NOT exist in Program Data, but does exist in Salesforce and is a candidate for Deactivation: {json}"); await DeactivateUserIfActiveInSalesforce(user); } else { _logger.LogInformation($"User will NOT be Deactived OR Published as it's Franchise is NOT a Salesforce Franchise: {json}"); } } }
private async Task <T> CheckUserFranchiseAgainstFranchiseSource <T>(AzureActiveDirectoryUser user, ProgramDataSources source, T success, T fail) { var parsed = int.TryParse(user.FranchiseNumber, out var userFranchiseNumber); if (parsed) { var franchises = await RetrieveFranchiseData(source); if (franchises.Any(franchiseNumber => franchiseNumber == userFranchiseNumber)) { return(success); } } return(fail); }
public async Task <IActionResult> TimeZoneAndCountry([FromQuery] string franchiseNumber, [FromQuery] string address, [FromQuery] string city, [FromQuery] string state, [FromQuery] string postalCode) { var user = new AzureActiveDirectoryUser { FranchiseNumber = franchiseNumber, Address = address, City = city, State = state, PostalCode = postalCode }; var timeZone = await _timeZoneService.RetrieveTimeZoneAndPopulateUsersCountryCode(user); user.TimeZone = timeZone; return(Ok(user)); }
private async Task <bool> UserExistsAndIsActiveInSalesforce(AzureActiveDirectoryUser user) { // note: using the default caching duration of 20 minutes var allUsers = await _cache.GetOrAddAsync($"{_cacheKeyPrefix}AllUsers", _salesforceUserService.RetrieveAllUsers); var isActive = allUsers.records.Any(sfUser => sfUser.IsActive && sfUser.HI_GUID__c != null && sfUser.HI_GUID__c.Equals(user.ExternalId)); if (!isActive) { var json = System.Text.Json.JsonSerializer.Serialize(user); _logger.LogDebug($"User is NOT active OR does not exist in Salesforce: {json}"); } return(isActive); }
private async Task <string> GetSalesforceSupportedTimeZoneAndPopulateUsersCountryCodeIfAvailable(AzureActiveDirectoryUser user) { var geoCodeResult = await _googleApiService.GeoCode(user.Address, user.City, user.State, user.PostalCode); if (string.IsNullOrWhiteSpace(user.CountryCode)) { user.CountryCode = geoCodeResult.ToCountryCode(); } var timeZoneResult = await _googleApiService.TimeZone(geoCodeResult.geometry?.location); var salesforceSupportedTimeZones = await _salesforceTimeZoneManagerService.GetSupportedTimeZones(); var timeZone = timeZoneResult.ToSalesforceTimeZone(salesforceSupportedTimeZones); if (string.IsNullOrWhiteSpace(timeZone)) { var userJson = System.Text.Json.JsonSerializer.Serialize(user); var salesforceTimeZonesJson = System.Text.Json.JsonSerializer.Serialize(salesforceSupportedTimeZones); _logger.LogWarning($"No Salesforce Supported Time Zone was found for dstOffset: {timeZoneResult.dstOffset}, rawOffset: {timeZoneResult.rawOffset}, timeZoneName: {timeZoneResult.timeZoneName}, for user: {userJson}, with salesforceTimeZones: {salesforceTimeZonesJson}"); } return(timeZone); }
private async Task <(string TimeZone, string Country)> GetUserTimeZoneCountry(AzureActiveDirectoryUser user) { var timeZone = await GetSalesforceSupportedTimeZoneAndPopulateUsersCountryCodeIfAvailable(user); return(timeZone, user.CountryCode); }