public override async Task <MessageResult> IssueLicenceCredential(IssueLicenceCredentialMessage message, ServerCallContext context)
        {
            var(schema, schemaVersion) = OrgBookUtils.GetSchemaFromConfig(message.LicenceType);
            int?orgbookTopicId = await _orgbookClient.GetTopicId(message.RegistrationId);

            if (orgbookTopicId == null)
            {
                _dynamics.Licenceses.Update(message.LicenceId, new MicrosoftDynamicsCRMadoxioLicences()
                {
                    AdoxioOrgbookcredentialresult = (int)OrgBookCredentialStatus.Fail
                });
                _logger.LogError($"Failed to issue credential - Registration ID: {message.RegistrationId} does not exist.");
                return(new MessageResult()
                {
                    Success = false
                });
            }
            else if (schema == null || schemaVersion == null)
            {
                _dynamics.Licenceses.Update(message.LicenceId, new MicrosoftDynamicsCRMadoxioLicences()
                {
                    AdoxioOrgbookcredentialresult = (int)OrgBookCredentialStatus.Fail
                });
                _logger.LogError($"Schema {message.LicenceType} not found.");
                return(new MessageResult()
                {
                    Success = false
                });
            }
            else
            {
                string         licenceGuid     = Utils.ParseGuid(message.LicenceId);
                var            licence         = _dynamics.GetLicenceByIdWithChildren(licenceGuid);
                VonAgentClient _vonAgentClient = new VonAgentClient(new HttpClient(), _logger, schema, schemaVersion, Configuration["AGENT_URL"]);
                bool           issueSuccess    = await _vonAgentClient.CreateLicenceCredential(licence, message.RegistrationId);

                if (issueSuccess)
                {
                    _dynamics.Licenceses.Update(message.LicenceId, new MicrosoftDynamicsCRMadoxioLicences()
                    {
                        AdoxioOrgbookcredentialresult = (int)OrgBookCredentialStatus.Pass
                    });
                    _logger.LogInformation($"Successfully issued credential to {message.RegistrationId}.");
                    return(new MessageResult()
                    {
                        Success = true
                    });
                }
                else
                {
                    _dynamics.Licenceses.Update(message.LicenceId, new MicrosoftDynamicsCRMadoxioLicences()
                    {
                        AdoxioOrgbookcredentialresult = (int)OrgBookCredentialStatus.Fail
                    });
                    _logger.LogInformation($"Failed to issue licence credential to {message.RegistrationId}.");
                    return(new MessageResult()
                    {
                        Success = false
                    });
                }
            }
        }
        public override async Task <MessageResult> SyncOrgbookToLicences(GenericRequest request, ServerCallContext context)
        {
            _logger.LogInformation("Starting SyncOrgbookToLicences");
            IList <MicrosoftDynamicsCRMadoxioLicences> result;

            try
            {
                var expand = new List <string> {
                    "adoxio_Licencee", "adoxio_LicenceType"
                };
                string filter = $"adoxio_orgbookcredentialresult eq {(int)OrgBookCredentialStatus.Pass} and adoxio_orgbookcredentialid eq null and statuscode eq 1";
                result = _dynamics.Licenceses.Get(filter: filter, expand: expand).Value;
            }
            catch (HttpOperationException odee)
            {
                _logger.LogError("Error getting Licences");
                _logger.LogError("Request:");
                _logger.LogError(odee.Request.Content);
                _logger.LogError("Response:");
                _logger.LogError(odee.Response.Content);

                // fail if we can't get results.
                return(new MessageResult()
                {
                    Success = false
                });
            }

            // now for each one process it.
            foreach (var item in result)
            {
                string registrationId = item.AdoxioLicencee?.AdoxioBcincorporationnumber;
                string licenceId      = item.AdoxioLicencesid;
                string licenceNumber  = item.AdoxioLicencenumber;
                int?   orgbookTopicId = await _orgbookClient.GetTopicId(registrationId);

                if (orgbookTopicId != null)
                {
                    var(schemaName, schemaVersion) = OrgBookUtils.GetSchemaFromConfig(item.AdoxioLicenceType.AdoxioName);

                    var schemaId = await _orgbookClient.GetSchemaId(schemaName, schemaVersion);

                    var credentialId = await _orgbookClient.GetLicenceCredentialId((int)orgbookTopicId, (int)schemaId, licenceNumber);

                    if (credentialId == null)
                    {
                        _logger.LogInformation($"Credential ID for {licenceNumber} not found in the orgbook.");
                        continue;
                    }
                    string credentialLink = _orgbookClient.ORGBOOK_BASE_URL + "/en/organization/" + registrationId + "/cred/" + credentialId.ToString();

                    _dynamics.Licenceses.Update(licenceId, new MicrosoftDynamicsCRMadoxioLicences()
                    {
                        AdoxioOrgbookcredentialid   = credentialId.ToString(),
                        AdoxioOrgbookcredentiallink = credentialLink
                    });
                    _logger.LogInformation($"Successfully updated licence - credential ID: {credentialId} to {registrationId}.");
                }
                else
                {
                    _logger.LogError($"Failed to update licence with new credential ID for Registration ID: {registrationId}.");
                }
            }

            _logger.LogInformation("End of SyncOrgbookToLicences");
            return(new MessageResult()
            {
                Success = true
            });
        }