public async Task <IActionResult> GetCurrentAccount()
        {
            _logger.LogInformation(LoggingEvents.HttpGet, "Begin method " + this.GetType().Name + "." + MethodBase.GetCurrentMethod().ReflectedType.Name);
            ViewModels.Account result = null;

            // get the current user.
            string       sessionSettings = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings    = JsonConvert.DeserializeObject <UserSettings>(sessionSettings);

            _logger.LogDebug(LoggingEvents.HttpGet, "UserSettings: " + JsonConvert.SerializeObject(userSettings));

            // query the Dynamics system to get the account record.
            if (userSettings.AccountId != null && userSettings.AccountId.Length > 0)
            {
                var accountId = GuidUtility.SanitizeGuidString(userSettings.AccountId);
                MicrosoftDynamicsCRMaccount account = _dynamicsClient.GetAccountByIdWithChildren(new Guid(accountId));
                _logger.LogDebug(LoggingEvents.HttpGet, "Dynamics Account: " + JsonConvert.SerializeObject(account));

                if (account == null)
                {
                    // Sometimes we receive the siteminderbusinessguid instead of the account id.
                    account = await _dynamicsClient.GetAccountBySiteminderBusinessGuid(accountId);

                    if (account == null)
                    {
                        _logger.LogWarning(LoggingEvents.NotFound, "No Account Found.");
                        return(new NotFoundResult());
                    }
                }
                result = account.ToViewModel();
            }
            else
            {
                _logger.LogWarning(LoggingEvents.NotFound, "No Account Found.");
                return(new NotFoundResult());
            }

            _logger.LogDebug(LoggingEvents.HttpGet, "Current Account Result: " +
                             JsonConvert.SerializeObject(result, Formatting.Indented, new JsonSerializerSettings {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
            return(Json(result));
        }
Exemple #2
0
        private bool isBusinessProfileSubmitted(UserSettings userSettings)
        {
            var isSubmitted = false;

            // query the Dynamics system to get the account record.
            if (userSettings.AccountId != null && userSettings.AccountId.Length > 0)
            {
                var accountId = GuidUtility.SanitizeGuidString(userSettings.AccountId);
                MicrosoftDynamicsCRMaccount account = _dynamicsClient.GetAccountByIdWithChildren(new Guid(accountId));
                _logger.LogDebug(LoggingEvents.HttpGet, "Dynamics Account: " + JsonConvert.SerializeObject(account));

                if (account == null)
                {
                    isSubmitted = account.BcgovSubmitteddate != null;
                }
            }

            return(isSubmitted);
        }
        public async Task <IActionResult> UpdateEquipment([FromBody] ViewModels.Equipment item, string id)
        {
            if (!string.IsNullOrEmpty(id) && Guid.TryParse(id, out Guid equipmentId))
            {
                // get the Equipment
                MicrosoftDynamicsCRMbcgovEquipment equipment = _dynamicsClient.GetEquipmentByIdWithChildren(equipmentId);
                if (equipment == null)
                {
                    return(new NotFoundResult());
                }

                // get UserSettings from the session
                string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
                UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

                // Get the current account
                var account = _dynamicsClient.GetAccountByIdWithChildren(Guid.Parse(userSettings.AccountId));

                MicrosoftDynamicsCRMbcgovEquipment patchEquipment = new MicrosoftDynamicsCRMbcgovEquipment();
                patchEquipment.CopyValues(item);

                try
                {
                    await _dynamicsClient.Equipments.UpdateAsync(equipmentId.ToString(), patchEquipment);
                }
                catch (OdataerrorException odee)
                {
                    _logger.LogError("Error updating Equipment");
                    _logger.LogError("Request:");
                    _logger.LogError(odee.Request.Content);
                    _logger.LogError("Response:");
                    _logger.LogError(odee.Response.Content);
                }

                equipment = _dynamicsClient.GetEquipmentByIdWithChildren(equipmentId);
                return(Json(equipment.ToViewModel()));
            }
            else
            {
                return(BadRequest());
            }
        }