public static BulkUploadHistoryViewModel GetHistoryFileUrl(this List <BulkUploadHistoryViewModel> model, UserContext.UserContextInfo userContext, ProviderPortalEntities db, int bulkUploadId)
        {
            var url = (from bulk in db.BulkUploads
                       where bulk.BulkUploadId == bulkUploadId
                       select bulk).FirstOrDefault();


            if ((userContext.IsOrganisation() && url.UserOrganisationId != userContext.ItemId.Value) ||
                (userContext.IsProvider() && url.UserProviderId != userContext.ItemId.Value))
            {
                return(null);
            }

            BulkUploadHistoryViewModel filteredModel = null;

            if (url != null)
            {
                filteredModel = new BulkUploadHistoryViewModel
                {
                    FileName    = url.FileName,
                    DownloadUrl = url.FilePath
                };
            }

            return(filteredModel);
        }
        public static void Populate(this List <BulkUploadHistoryViewModel> model, UserContext.UserContextInfo userContext, ProviderPortalEntities db, Constants.BulkUpload_DataType dataType)
        {
            try
            {
                //IQueryable<BulkUploadHistory> historyRecords;
                IQueryable <BulkUpload> historyRecords;

                if (userContext.IsOrganisation())
                {
                    //all providerswhich belongs to the organsiation
                    var providersToSearch = BulkUploadValidateExtension.GetPermittedBulkUploadProviders(db, userContext, dataType);

                    //get history of all the provider which belongs to organisation + organisation's respective upload

                    /*
                     * historyRecords =
                     *  db.BulkUploadHistories.Include("BulkUploadHistoryProviders.Provider")
                     *      .Where(
                     *          x =>
                     *          providersToSearch.Contains(x.UserProviderId.Value)
                     || x.UserOrganisationId == userContext.ItemId);
                     */

                    historyRecords =
                        db.BulkUploads
                        .Include("BulkUploadProviders.Provider")
                        .Where(x =>
                               (providersToSearch.Contains(x.UserProviderId.Value) || x.UserOrganisationId == userContext.ItemId)
                               &&
                               (x.FileContentType == null || x.FileContentType == (int)dataType));
                }
                else
                {
                    /*
                     * historyRecords =
                     *  db.BulkUploadHistories.Include("BulkUploadHistoryProviders.Provider")
                     *      .Where(
                     *          x =>
                     *          x.UserProviderId.HasValue && x.UserProviderId == userContext.ItemId
                     || x.BulkUploadHistoryProviders.Any(p => p.ProviderId == userContext.ItemId));
                     */

                    historyRecords =
                        db.BulkUploads
                        .Include("BulkUploadProviders.Provider")
                        .Where(x =>
                               (x.UserProviderId.HasValue && x.UserProviderId == userContext.ItemId || x.BulkUploadProviders.Any(p => p.ProviderId == userContext.ItemId))
                               &&
                               (x.FileContentType == null || x.FileContentType == (int)dataType));
                }

                foreach (var historyRecord in historyRecords.OrderByDescending(x => x.BulkUploadId))
                {
                    var currentStatus =
                        historyRecord.BulkUploadStatusHistories.OrderByDescending(x => x.CreatedDateTimeUtc)
                        .FirstOrDefault();
                    if (currentStatus == null)
                    {
                        continue;
                    }
                    var item = new BulkUploadHistoryViewModel
                    {
                        BulkUploadId                  = historyRecord.BulkUploadId,
                        FileName                      = historyRecord.FileName,
                        DownloadUrl                   = historyRecord.FilePath,
                        IsOrganisationUpload          = userContext.IsOrganisation(),
                        ProviderName                  = LoadProviderName(historyRecord),
                        IsAuthorisedToViewAndDownload = userContext.IsOrganisation()
                            ? historyRecord.UserOrganisationId == userContext.ItemId
                            : historyRecord.UserProviderId == userContext.ItemId,
                        UserName            = currentStatus.AspNetUser.Name,
                        UploadedDateTime    = currentStatus.CreatedDateTimeUtc.ToLocalTime(),
                        StatusDescription   = currentStatus.BulkUploadStatu.BulkUploadStatusText,
                        IsDownloadAvailable =
                            !string.IsNullOrEmpty(historyRecord.FilePath) && File.Exists(historyRecord.FilePath),
                        IsUploadSuccessful =
                            currentStatus.BulkUploadStatusId.Equals((int)Constants.BulkUploadStatus.Published),
                    };

                    if (historyRecord.FileContentType != null)
                    {
                        item.FileContentType = (Constants.FileContentType)Enum.Parse(typeof(Constants.FileContentType), historyRecord.FileContentType.ToString());
                    }

                    model.Add(item);
                }
            }
            catch (Exception ex)
            {
                AppGlobal.Log.WriteLog(string.Concat(ex.Message, "-", ex.StackTrace));
            }
        }