private void AddConfiguredGatewaysToContext()
        {
            var gatewayEntityCollection = new Dictionary <Guid, Entity>();

            var configurationService = new TestAllocationConfigurationService();
            var gatewayIdsString     = configurationService.DestinationGatewayIds;
            var gatewayIds           = gatewayIdsString.Split(',');

            for (int i = 0; i < gatewayIds.Length; i++)
            {
                var gatewayId = new Guid(gatewayIds[i]);
                var gateway   = new Entity(EntityName.Gateway, gatewayId);
                gateway[Attributes.Gateway.GatewayId] = gatewayId;
                gatewayEntityCollection.Add(gatewayId, gateway);
                ValidGatewayIds.Add(gatewayId);
            }
            Context.Data.Add(EntityName.Gateway, gatewayEntityCollection);
        }
        private EntityCollection GetBookings(
            List <Entity> bookings,
            List <Entity> gateways,
            List <Entity> customerBookingRoles,
            List <Entity> contacts,
            List <Entity> accounts,
            List <Entity> cases,
            List <Entity> countries,
            List <Entity> businessUnits,
            List <Entity> teams)
        {
            var result = new List <Entity>();

            // fetch xml filter
            var filteredData = (from b in bookings
                                join g in gateways on((EntityReference)b[Attributes.Booking.DestinationGatewayId]).Id equals g.Id
                                join team in teams on
                                new { Id = b[Attributes.Booking.OwningTeam] != null ? ((EntityReference)b[Attributes.Booking.OwningTeam]).Id : Guid.Empty }
                                equals new { Id = team.Id }
                                join country in countries on((EntityReference)b[Attributes.Booking.SourceMarketId]).Id equals country.Id
                                join businessUnit in businessUnits on((EntityReference)country[Attributes.Country.BusinessUnitId]).Id equals businessUnit.Id
                                join defaultTeam in teams on businessUnit.Id equals((EntityReference)defaultTeam[Attributes.Team.BusinessUnitId]).Id
                                join role in customerBookingRoles on b.Id equals((EntityReference)role[Attributes.CustomerBookingRole.BookingId]).Id into cbr
                                from role in cbr.DefaultIfEmpty()
                                join contact in contacts on
                                new { Id = role != null ? ((EntityReference)role[Attributes.CustomerBookingRole.Customer]).Id : Guid.Empty }
                                equals new { Id = contact.Id } into c
                                from contact in c.DefaultIfEmpty()
                                join contactCase in cases on
                                new { Id = contact != null ? contact.Id : Guid.Empty }
                                equals new { Id = ((EntityReference)contactCase[Attributes.Case.CustomerId]).Id } into cc
                                from contactCase in cc.DefaultIfEmpty()
                                join account in accounts on
                                new { Id = role != null ? ((EntityReference)role[Attributes.CustomerBookingRole.Customer]).Id : Guid.Empty }
                                equals new { Id = account.Id } into a
                                from account in a.DefaultIfEmpty()
                                join accountCase in cases on
                                new { Id = account != null ? account.Id : Guid.Empty }
                                equals
                                new { Id = ((EntityReference)accountCase[Attributes.Case.CustomerId]).Id }  into ac
                                from accountCase in ac.DefaultIfEmpty()
                                where ((DateTime)b[Attributes.Booking.ReturnDate]).Date == DateTime.Now.Date.AddDays(-2) &&
                                ValidGatewayIds.Contains(((EntityReference)b[Attributes.Booking.DestinationGatewayId]).Id) &&
                                (bool)defaultTeam[Attributes.Team.IsDefaultTeam] == true &&
                                (bool)team[Attributes.Team.IsHotelTeam] == true
                                select new
            {
                BookingId = b.Id,
                ContactId = contact != null ? contact.Id : Guid.Empty,
                AccountId = account != null ? account.Id : Guid.Empty,
                ContactCaseId = contactCase != null ? contactCase.Id : Guid.Empty,
                AccountCaseId = accountCase != null ? accountCase.Id: Guid.Empty,
                DefaultTeamId = defaultTeam.Id,
                BusinessUnitName = businessUnit[Attributes.BusinessUnit.Name]
            }).GroupBy(r => new { r.BookingId, r.ContactId, r.AccountId, r.ContactCaseId, r.AccountCaseId, r.DefaultTeamId }).
                               Select(r => r.First()).OrderBy(r => r.BookingId).ThenBy(r => r.ContactId).ThenBy(r => r.AccountId);

            foreach (var item in filteredData)
            {
                var b = new Entity(EntityName.Booking, item.BookingId);
                b[Attributes.Booking.BookingId] = item.BookingId;
                b["team.teamid"]       = new AliasedValue(EntityName.Team, Attributes.Team.TeamId, item.DefaultTeamId);
                b["businessunit.name"] = new AliasedValue(EntityName.BusinessUnit, Attributes.BusinessUnit.Name, item.BusinessUnitName);
                if (item.ContactId != Guid.Empty)
                {
                    b["contact.contactid"] = new AliasedValue(EntityName.Contact, Attributes.Contact.ContactId, item.ContactId);
                }
                if (item.AccountId != Guid.Empty)
                {
                    b["account.accountid"] = new AliasedValue(EntityName.Account, Attributes.Account.AccountId, item.AccountId);
                }
                if (item.ContactCaseId != Guid.Empty)
                {
                    b["contactincident.incidentid"] = new AliasedValue(EntityName.Case, Attributes.Case.CaseId, item.ContactCaseId);
                    b["contactincident.ownerid"]    = new AliasedValue(EntityName.User, Attributes.User.UserId, new EntityReference(EntityName.User, SystemUser.Id));
                }
                if (item.AccountCaseId != Guid.Empty)
                {
                    b["accountincident.incidentid"] = new AliasedValue(EntityName.Case, Attributes.Case.CaseId, item.AccountCaseId);
                    b["accountincident.ownerid"]    = new AliasedValue(EntityName.User, Attributes.User.UserId, new EntityReference(EntityName.User, SystemUser.Id));
                }
                result.Add(b);
            }

            return(new EntityCollection(result));
        }