private async Task CreateRelationships(int innerResourceId, int outerResourceId, string innerResourceName, string outerResourceName, List <SiteDto> sites, int eFormId)
        {
            var outerInnerResource = _dbContext.OuterInnerResources.SingleOrDefault(x =>
                                                                                    x.InnerResourceId == innerResourceId && x.OuterResourceId == outerResourceId);

            if (sites.Any())
            {
                foreach (var siteDto in sites)
                {
                    var siteMatch = await _dbContext.OuterInnerResourceSites
                                    .Where(x => x.MicrotingSdkSiteId == siteDto.SiteId && x.OuterInnerResourceId == outerInnerResource.Id)
                                    .ToListAsync();

                    if (!siteMatch.Any())
                    {
                        var outerInnerResourceSite = new OuterInnerResourceSite
                        {
                            OuterInnerResourceId = outerInnerResource.Id,
                            MicrotingSdkSiteId   = siteDto.SiteId,
                            MicrotingSdkeFormId  = eFormId
                        };
                        await outerInnerResourceSite.Create(_dbContext);

                        await _bus.SendLocal(new OuterInnerResourcePosteForm(outerInnerResourceSite.Id, eFormId));
                    }
                }
            }
        }
        private async Task UpdateSitesDeployed(
            OuterInnerResource outerInnerResource, List <Site> sites, int eFormId)
        {
            WriteLogEntry("OuterInnerResourceUpdateHandler: UpdateSitesDeployed called");
            var siteIds = new List <int>();

            if (outerInnerResource.WorkflowState == Constants.WorkflowStates.Created)
            {
                if (sites.Any())
                {
                    foreach (var site in sites)
                    {
                        siteIds.Add(site.Id);
                        var outerInnerResourceSites = await _dbContext.OuterInnerResourceSites.Where(
                            x =>
                            x.MicrotingSdkSiteId == site.Id &&
                            x.OuterInnerResourceId == outerInnerResource.Id &&
                            x.WorkflowState == Constants.WorkflowStates.Created).ToListAsync();

                        if (!outerInnerResourceSites.Any())
                        {
                            var outerInnerResourceSite = new OuterInnerResourceSite
                            {
                                OuterInnerResourceId = outerInnerResource.Id,
                                MicrotingSdkSiteId   = site.Id,
                                MicrotingSdkeFormId  = eFormId
                            };
                            await outerInnerResourceSite.Create(_dbContext);

                            await _bus.SendLocal(new OuterInnerResourcePosteForm(outerInnerResourceSite.Id,
                                                                                 eFormId));
                        }
                        else
                        {
                            if (outerInnerResourceSites.First().MicrotingSdkCaseId == null)
                            {
                                await _bus.SendLocal(new OuterInnerResourcePosteForm(
                                                         outerInnerResourceSites.First().Id,
                                                         eFormId));
                            }
                        }
                    }
                }
            }
            var sitesConfigured = _dbContext.OuterInnerResourceSites.Where(x =>
                                                                           x.OuterInnerResourceId == outerInnerResource.Id &&
                                                                           x.WorkflowState != Constants.WorkflowStates.Removed).ToList();

            WriteLogEntry("OuterInnerResourceUpdateHandler: sitesConfigured looked up");

            if (sitesConfigured.Any())
            {
                foreach (var outerInnerResourceSite in sitesConfigured)
                {
                    if (!siteIds.Contains(outerInnerResourceSite.MicrotingSdkSiteId) ||
                        outerInnerResource.WorkflowState == Constants.WorkflowStates.Removed)
                    {
                        if (outerInnerResourceSite.MicrotingSdkCaseId != null)
                        {
                            await outerInnerResourceSite.Delete(_dbContext);

                            await _bus.SendLocal(new OuterInnerResourceDeleteFromServer(outerInnerResourceSite.Id));
                        }
                    }
                }
            }
        }