public async Task <ACMGenericResult <DispatchChannel> > GetDispatchChannel(string dispatchId)
        {
            var result = new ACMGenericResult <DispatchChannel>();

            try
            {
                AccountConfiguration accountConfiguration = await ViaMongoDB.GetAccountConfiguration();

                if (accountConfiguration.DispatchChannels == null)
                {
                    result.StatusCode = 204;
                    result.Value      = null;
                }
                else
                {
                    DispatchChannel dispatchChannel = accountConfiguration.DispatchChannels.Find(x => x.DispatchId == dispatchId);
                    if (dispatchChannel == default)
                    {
                        result.StatusCode = 204;
                        result.Value      = null;
                    }
                    else
                    {
                        result.StatusCode = 200;
                        result.Value      = dispatchChannel;
                    }
                }
            }
            catch (Exception)
            {
                result.StatusCode = 500;
                result.Value      = null;
            }
            return(result);
        }
        public bool ValidateBearerToken(string authToken, AccountConfiguration accConfiguration)
        {
            try
            {
                if (accConfiguration == null)
                {
                    return(false);
                }

                if (string.IsNullOrWhiteSpace(authToken))
                {
                    return(false);
                }


                if (!(authToken.StartsWith("Bearer ") || authToken.StartsWith("Basic ")))
                {
                    return(false);
                }

                if (InvitationsMemoryCache.GetInstance().GetFromMemoryCache(authToken) != null)
                {
                    return(true);
                }
                else
                {
                    lock (authTokenLock) {
                        if (InvitationsMemoryCache.GetInstance().GetFromMemoryCache(authToken) != null)
                        {
                            return(true);
                        }

                        var settings = new HTTPWrapper().GetSettings(authToken).GetAwaiter().GetResult();
                        if (string.IsNullOrWhiteSpace(settings))
                        {
                            return(false);
                        }

                        Settings settingsRes = JsonConvert.DeserializeObject <Settings>(settings);

                        if (!settingsRes.user.Equals(accConfiguration?.WXMAdminUser, StringComparison.OrdinalIgnoreCase))
                        {
                            return(false);
                        }

                        InvitationsMemoryCache.GetInstance().SetToMemoryCache("settings", settings);
                        InvitationsMemoryCache.GetInstance().SetAuthTokenToMemoryCache(authToken);
                    }
                    return(true);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
 public ProcessInvitations(string AuthToken, ViaMongoDB viaMongo, string batchid, EventLogList eventLogList,
                           AccountConfiguration accConfig)
 {
     FinalToken           = AuthToken;
     mongoDBConn          = viaMongo;
     hTTPWrapper          = new HTTPWrapper(batchid, eventLogList);
     BatchId              = batchid;
     EventLogList         = eventLogList;
     accountConfiguration = accConfig;
 }
        public async Task <bool> IsUserAdminValid(string wxmAdmin)
        {
            AccountConfiguration accountConfiguration = await ViaMongoDB.GetAccountConfiguration();

            string dbAdmin = accountConfiguration?.WXMAdminUser;

            if (string.IsNullOrWhiteSpace(dbAdmin))
            {
                return(true);
            }
            else
            {
                return(wxmAdmin == dbAdmin);
            }
        }
        public async Task <ACMGenericResult <Vendor> > AddOrUpdateVendor(Vendor newVendor)
        {
            var result = new ACMGenericResult <Vendor>();

            try
            {
                AccountConfiguration accountConfiguration = await ViaMongoDB.GetAccountConfiguration();

                if (accountConfiguration.Vendors == null)
                {
                    accountConfiguration.Vendors = new List <Vendor> {
                        newVendor
                    }
                }
                ;
                else
                {
                    int index = accountConfiguration.Vendors.FindIndex(x => string.Equals(x.VendorName, newVendor.VendorName));
                    if (index == -1)
                    {
                        accountConfiguration.Vendors.Add(newVendor);
                    }
                    else
                    {
                        accountConfiguration.Vendors[index] = ToClone(newVendor);
                    }
                }
                result.StatusCode = 200;
                result.Value      = (await ViaMongoDB.UpdateAccountConfiguration_Vendors(accountConfiguration.Vendors))
                                    .Vendors.Find(x => string.Equals(x.VendorName, newVendor.VendorName));
            }
            catch (Exception)
            {
                result.StatusCode = 500;
                result.Value      = null;
            }
            return(result);
        }
        public async Task <ACMGenericResult <DispatchChannel> > AddOrUpdateDispatchChannel(DispatchChannel dispatchChannel)
        {
            var result = new ACMGenericResult <DispatchChannel>();

            try
            {
                AccountConfiguration accountConfiguration = await ViaMongoDB.GetAccountConfiguration();

                if (accountConfiguration.DispatchChannels == null)
                {
                    accountConfiguration.DispatchChannels = new List <DispatchChannel> {
                        dispatchChannel
                    }
                }
                ;
                else
                {
                    int index = accountConfiguration.DispatchChannels.FindIndex(x => x.DispatchId == dispatchChannel.DispatchId);
                    if (index == -1)
                    {
                        accountConfiguration.DispatchChannels.Add(dispatchChannel);
                    }
                    else
                    {
                        accountConfiguration.DispatchChannels[index] = ToClone(dispatchChannel);
                    }
                }
                result.StatusCode = 200;
                result.Value      = (await ViaMongoDB.UpdateAccountConfiguration_DispatchChannels(accountConfiguration.DispatchChannels))
                                    .DispatchChannels?.Find(x => x.DispatchId == dispatchChannel.DispatchId);
            }
            catch (Exception)
            {
                result.StatusCode = 500;
                result.Value      = null;
            }
            return(result);
        }
        public async Task <ACMGenericResult <Vendor> > GetVendor(string vendorName)
        {
            var result = new ACMGenericResult <Vendor>();

            try
            {
                AccountConfiguration accountConfiguration = await ViaMongoDB.GetAccountConfiguration();

                if (accountConfiguration.Vendors == null)
                {
                    result.StatusCode = 204;
                    result.Value      = null;
                }
                else
                {
                    Vendor vendor = accountConfiguration.Vendors
                                    .Find(x => string.Equals(x.VendorName, vendorName, StringComparison.InvariantCultureIgnoreCase));
                    if (vendor == default)
                    {
                        result.StatusCode = 204;
                        result.Value      = null;
                    }
                    else
                    {
                        result.StatusCode = 200;
                        result.Value      = vendor;
                    }
                }
            }
            catch (Exception)
            {
                result.StatusCode = 500;
                result.Value      = null;
            }
            return(result);
        }
        public async Task <List <DispatchChannel> > ConfigureDispatchChannels(List <Dispatch> dispatches, List <DeliveryPlan> deliveryPlans, List <Question> preFillQuestions)
        {
            List <DispatchChannel> dispatchChannels = new List <DispatchChannel>();

            foreach (Dispatch dispatch in dispatches)
            {
                DeliveryPlan         deliveryPlan   = deliveryPlans.Find(x => x.id == dispatch.DeliveryPlanId);
                List <StaticPrefill> staticPrefills = preFillQuestions
                                                      .Where(x => x.DisplayLocation?.Contains(dispatch.QuestionnaireName, StringComparer.InvariantCultureIgnoreCase) ?? false)?
                                                      .Select(x => new StaticPrefill {
                    Note = x.Note, PrefillValue = null, QuestionId = x.Id
                })?
                                                      .ToList() ?? new List <StaticPrefill>();
                DispatchChannel dispatchChannel = new DispatchChannel
                {
                    ChannelDetails = new ChannelDetails
                    {
                        Email = new Channel
                        {
                            IsValid    = deliveryPlan?.schedule?.Any(x => x.onChannel?.StartsWith("email") ?? false) ?? false ? true : false,
                            Vendorname = null
                        },
                        Sms = new Channel
                        {
                            IsValid    = deliveryPlan?.schedule?.Any(x => x.onChannel?.StartsWith("sms") ?? false) ?? false ? true : false,
                            Vendorname = null
                        }
                    },
                    DispatchId     = dispatch.Id,
                    DispatchName   = dispatch.Name + (dispatch.IsLive == true ? string.Empty : " [PAUSED]"),
                    StaticPrefills = staticPrefills,
                    Notify         = new Notify
                    {
                        D = null,
                        E = null,
                        F = null,
                        I = null,
                        W = null
                    }
                };
                dispatchChannels.Add(dispatchChannel);
            }

            AccountConfiguration accountConfiguration = await ViaMongoDB.GetAccountConfiguration();

            if (accountConfiguration.DispatchChannels == null)
            {
                accountConfiguration.DispatchChannels = dispatchChannels;
            }
            else
            {
                foreach (DispatchChannel dc in dispatchChannels)
                {
                    int index1 = accountConfiguration.DispatchChannels.FindIndex(x => x.DispatchId == dc.DispatchId);
                    if (index1 == -1)                                                                                   //Add new dispatch channel
                    {
                        accountConfiguration.DispatchChannels.Add(dc);
                    }
                    else                                                                                                //Update existing dispatch channel
                    {
                        accountConfiguration.DispatchChannels[index1].ChannelDetails.Email.IsValid = dc.ChannelDetails.Email.IsValid;
                        accountConfiguration.DispatchChannels[index1].ChannelDetails.Sms.IsValid   = dc.ChannelDetails.Sms.IsValid;

                        accountConfiguration.DispatchChannels[index1].DispatchName = dc.DispatchName;

                        foreach (StaticPrefill sp in dc.StaticPrefills)
                        {
                            int index2 = accountConfiguration.DispatchChannels[index1].StaticPrefills.FindIndex(x => x.QuestionId == sp.QuestionId);
                            if (index2 == -1)
                            {
                                accountConfiguration.DispatchChannels[index1].StaticPrefills.Add(sp);                   //Add new static prefill
                            }
                            else
                            {
                                accountConfiguration.DispatchChannels[index1].StaticPrefills[index2].Note = sp.Note;    //Update existing static prefill
                            }
                        }
                        accountConfiguration.DispatchChannels[index1].
                        StaticPrefills.RemoveAll(x => dc.StaticPrefills.All(y => y.QuestionId != x.QuestionId));        //Remove old static prefills

                        if (accountConfiguration.DispatchChannels[index1].Notify == default)
                        {
                            accountConfiguration.DispatchChannels[index1].Notify = dc.Notify;
                        }
                    }
                }
                accountConfiguration.DispatchChannels                                                                   //Remove old dispatch channels
                .RemoveAll(x => dispatchChannels.All(y => y.DispatchId != x.DispatchId));
            }
            return((await ViaMongoDB.UpdateAccountConfiguration_DispatchChannels(accountConfiguration.DispatchChannels)).DispatchChannels);
        }