private Entity MapEventToEntity <T>(SyncEntityEvent <T> @event, string sourceSystemName) where T : EntityBase
        {
            if (@event.Details is LearningProvider learningProvider)
            {
                var sourceSystemId = sourceSystemName.Equals(SourceSystemNames.UkRegisterOfLearningProviders, StringComparison.InvariantCultureIgnoreCase)
                    ? learningProvider.Ukprn?.ToString()
                    : learningProvider.Urn?.ToString();
                return(new Entity
                {
                    EntityType = EntityNameTranslator.LearningProviderSingular,
                    SourceSystemName = sourceSystemName,
                    SourceSystemId = sourceSystemId,
                    Name = learningProvider.Name,
                    Type = learningProvider.Type,
                    SubType = learningProvider.SubType,
                    Status = learningProvider.Status,
                    OpenDate = learningProvider.OpenDate,
                    CloseDate = learningProvider.CloseDate,
                    Urn = learningProvider.Urn,
                    Ukprn = learningProvider.Ukprn,
                    Uprn = learningProvider.Uprn,
                    CompaniesHouseNumber = learningProvider.CompaniesHouseNumber,
                    CharitiesCommissionNumber = learningProvider.CharitiesCommissionNumber,
                    AcademyTrustCode = learningProvider.AcademyTrustCode,
                    DfeNumber = learningProvider.DfeNumber,
                    LocalAuthorityCode = learningProvider.LocalAuthorityCode,
                    ManagementGroupType = learningProvider.ManagementGroup?.Type,
                    ManagementGroupId = learningProvider.ManagementGroup?.Identifier,
                    ManagementGroupCode = learningProvider.ManagementGroup?.Code,
                    ManagementGroupUkprn = learningProvider.ManagementGroup?.Ukprn,
                    ManagementGroupCompaniesHouseNumber = learningProvider.ManagementGroup?.CompaniesHouseNumber,
                });
            }

            if (@event.Details is ManagementGroup managementGroup)
            {
                return(new Entity
                {
                    EntityType = EntityNameTranslator.ManagementGroupSingular,
                    SourceSystemName = sourceSystemName,
                    SourceSystemId = managementGroup.Code,
                    ManagementGroupType = managementGroup.Type,
                    ManagementGroupId = managementGroup.Identifier,
                    ManagementGroupCode = managementGroup.Code,
                    ManagementGroupUkprn = managementGroup.Ukprn,
                    ManagementGroupCompaniesHouseNumber = managementGroup.CompaniesHouseNumber,
                });
            }

            throw new Exception($"Unprocessable event for type {typeof(T)}");
        }
        public async Task ReceiveSyncEntityAsync <T>(SyncEntityEvent <T> @event, string sourceSystemName, CancellationToken cancellationToken)
            where T : EntityBase
        {
            var entity = MapEventToEntity(@event, sourceSystemName);

            var queueItem = new SyncQueueItem
            {
                Entity            = entity,
                PointInTime       = @event.PointInTime,
                InternalRequestId = _executionContextManager.SpiExecutionContext.InternalRequestId,
                ExternalRequestId = _executionContextManager.SpiExecutionContext.ExternalRequestId,
            };

            var messageId = await _syncQueue.EnqueueEntityForSyncAsync(queueItem, cancellationToken);

            _logger.Info($"Queued item with message id {messageId}");
        }