public async Task <IEnumerable <FamilyClientMessage> > Load(Guid?htsClientId = null, params LoadAction[] actions)
        {
            var messages = new List <FamilyClientMessage>();

            if (!actions.Any())
            {
                actions = new[] { LoadAction.All }
            }
            ;

            //  Set Facility
            var facility = _practiceRepository.GetDefault();

            if (null == facility)
            {
                throw new Exception($"Default Faciltity Not found");
            }

            //      MESSAGE_HEADER

            var facilityCode = facility.Code;
            var header       = MESSAGE_HEADER.Create(facilityCode);

            //      CLIENT

            var familyMembers = _clientStageRelationshipRepository.GetAll(x => !x.IsPartner);

            if (!htsClientId.IsNullOrEmpty())
            {
                familyMembers = familyMembers.Where(x => x.SecondaryClientId == htsClientId);
            }

            foreach (var familyMember in familyMembers)
            {
                var stagedClient = _clientStageRepository.GetQueued(familyMember.SecondaryClientId);

                if (null != stagedClient && !stagedClient.IsIndex)
                {
                    header.UpdateMfl(stagedClient.SiteCode);

                    #region PATIENT_IDENTIFICATION

                    var pid = PARTNER_FAMILY_PATIENT_IDENTIFICATION.Create(stagedClient, familyMember.IndexClientId,
                                                                           familyMember.Relation);

                    #endregion

                    FAMILY_ENCOUNTER encounter = null;

                    if (!actions.Contains(LoadAction.RegistrationOnly))
                    {
                        PLACER_DETAIL         placerDetail    = null;
                        FAMILY_SCREENING      familyScreening = null;
                        List <FAMILY_TRACING> familyTracings  = new List <FAMILY_TRACING>();

                        #region ENCOUNTERS

                        var screening = await _clientFamilyScreeningStageExtractor.Extract(stagedClient.ClientId);

                        var screeningStage = screening.OrderBy(x => x.ScreeningDate).LastOrDefault();

                        //  PLACER_DETAIL

                        if (null != screeningStage)
                        {
                            placerDetail = PLACER_DETAIL.Create(screeningStage.UserId, screeningStage.Id);

                            //  FAMILY_SCREENING

                            if (actions.Contains(LoadAction.All) || actions.Contains(LoadAction.ContactScreenig))
                            {
                                familyScreening = FAMILY_SCREENING.Create(screeningStage);
                            }
                        }

                        //  FAMILY_TRACING

                        if (actions.Contains(LoadAction.All) || actions.Contains(LoadAction.ContactTracing))
                        {
                            var allTracing = await _clientFamilyTracingStageExtractor.Extract(stagedClient.ClientId);

                            if (allTracing.Any())
                            {
                                familyTracings = FAMILY_TRACING.Create(allTracing.ToList());
                            }
                        }

                        #endregion

                        encounter = new FAMILY_ENCOUNTER(placerDetail, familyScreening, familyTracings);
                    }

                    messages.Add(new FamilyClientMessage(header,
                                                         new List <FAMILY> {
                        new FAMILY(pid, encounter)
                    }, stagedClient.ClientId));
                }
            }

            return(messages);
        }
        public async Task <IEnumerable <IndexClientMessage> > Load(Guid?htsClientId = null, params LoadAction[] actions)
        {
            var messages = new List <IndexClientMessage>();

            if (!actions.Any())
            {
                actions = new[] { LoadAction.All }
            }
            ;

            var facs = _practiceRepository.GetAllDefault().ToList();

            //  Set Facility
            var facility = facs.FirstOrDefault(x => x.IsDefault);

            if (null == facility)
            {
                throw new Exception($"Default Faciltity Not found");
            }

            //      MESSAGE_HEADER

            var facilityCode = facility.Code;
            var header       = MESSAGE_HEADER.Create(facilityCode);

            //      NEWCLIENT

            var stagedIndexClients = _clientStageRepository.GetIndexClients();

            if (!htsClientId.IsNullOrEmpty())
            {
                stagedIndexClients = stagedIndexClients.Where(x => x.ClientId == htsClientId);
            }

            foreach (var stagedClient in stagedIndexClients)
            {
                header.UpdateMfl(stagedClient.SiteCode);

                #region PATIENT_IDENTIFICATION

                var pid = PATIENT_IDENTIFICATION.Create(stagedClient);

                #endregion

                ENCOUNTERS encounter = null;
                if (!actions.Contains(LoadAction.RegistrationOnly))
                {
                    var pretests = _clientPretestStageRepository.GetByClientId(stagedClient.ClientId).ToList();

                    //    PRETEST AND TESTING

                    if (pretests.Any())
                    {
                        var lastPretest = pretests.OrderByDescending(x => x.EncounterDate).FirstOrDefault();

                        foreach (var pretest in pretests)
                        {
                            var pretestEncounter =
                                await CreatePretestEncounters(header, pid, stagedClient, pretest, actions);

                            messages.Add(pretestEncounter);
                        }

                        if (null != lastPretest)
                        {
                            var nonPretest =
                                await CreateNonPretestEncounters(header, pid, stagedClient, lastPretest, actions);

                            if (null != nonPretest)
                            {
                                messages.Add(nonPretest);
                            }
                        }
                    }
                    else
                    {
                        var registration = CreateRegistration(header, pid, stagedClient, encounter);
                        messages.Add(registration);
                    }
                }
                else
                {
                    messages.Add(new IndexClientMessage(header,
                                                        new List <NEWCLIENT> {
                        NEWCLIENT.Create(pid, encounter)
                    }, stagedClient.ClientId));
                }
            }

            return(messages);
        }