Disco.Services.JobTableExtensions.DetermineItems C# (CSharp) Method

DetermineItems() public static method

public static DetermineItems ( this model, DiscoDataContext Database, IQueryable Jobs, bool FilterAuthorization ) : IEnumerable
model this
Database Disco.Data.Repository.DiscoDataContext
Jobs IQueryable
FilterAuthorization bool
return IEnumerable
        public static IEnumerable<JobTableItemModel> DetermineItems(this JobTableModel model, DiscoDataContext Database, IQueryable<Job> Jobs, bool FilterAuthorization)
        {
            List<JobTableItemModel> items;

            // Permissions
            if (FilterAuthorization)
                Jobs = model.FilterPermissions(Jobs, UserService.CurrentAuthorization);

            if (model.ShowStatus || model.ShowLastActivityDate)
            {

                var jobData = Jobs.Select(j => new
                {
                    JobId = j.Id,
                    OpenedDate = j.OpenedDate,
                    ClosedDate = j.ClosedDate,
                    JobTypeId = j.JobTypeId,
                    JobTypeDescription = j.JobType.Description,
                    Device = j.Device,
                    DeviceModelDescription = j.Device.DeviceModel.Description,
                    DeviceAddressId = j.Device.DeviceProfile.DefaultOrganisationAddress,
                    UserId = j.UserId,
                    UserDisplayName = j.User.DisplayName,
                    OpenedTechUserId = j.OpenedTechUserId,
                    OpenedTechUserDisplayName = j.OpenedTechUser.DisplayName,
                    DeviceHeldLocation = j.DeviceHeldLocation,
                    Flags = j.Flags,

                    JobMetaWarranty = j.JobMetaWarranty,
                    JobMetaNonWarranty = j.JobMetaNonWarranty,
                    JobMetaInsurance_ClaimFormSentDate = j.JobMetaInsurance.ClaimFormSentDate,

                    RecentAttachmentDate = (DateTime?)j.JobAttachments.Max(ja => ja.Timestamp),
                    RecentLogDate = (DateTime?)j.JobLogs.Max(jl => jl.Timestamp),

                    WaitingForUserAction = j.WaitingForUserAction,
                    DeviceReadyForReturn = j.DeviceReadyForReturn,
                    DeviceHeld = j.DeviceHeld,
                    DeviceReturnedDate = j.DeviceReturnedDate,

                    JobQueues = j.JobQueues.Where(jq => !jq.RemovedDate.HasValue)
                }).ToList();

                var jobItems = jobData.Select(j => new JobTableStatusItemModel()
                {
                    JobId = j.JobId,
                    OpenedDate = j.OpenedDate,
                    ClosedDate = j.ClosedDate,
                    JobTypeId = j.JobTypeId,
                    JobTypeDescription = j.JobTypeDescription,
                    DeviceSerialNumber = j.Device?.SerialNumber,
                    DeviceProfileId = j.Device?.DeviceProfileId,
                    DeviceModelId = j.Device?.DeviceModelId,
                    DeviceModelDescription = j.DeviceModelDescription,
                    DeviceAddressId = j.DeviceAddressId,
                    UserId = j.UserId,
                    UserDisplayName = j.UserDisplayName,
                    OpenedTechUserId = j.OpenedTechUserId,
                    OpenedTechUserDisplayName = j.OpenedTechUserDisplayName,
                    DeviceHeldLocation = j.DeviceHeldLocation,
                    Flags = j.Flags,

                    JobMetaWarranty_ExternalReference = j.JobMetaWarranty?.ExternalReference,
                    JobMetaWarranty_ExternalLoggedDate = j.JobMetaWarranty?.ExternalLoggedDate,
                    JobMetaWarranty_ExternalCompletedDate = j.JobMetaWarranty?.ExternalCompletedDate,
                    JobMetaNonWarranty_RepairerLoggedDate = j.JobMetaNonWarranty?.RepairerLoggedDate,
                    JobMetaNonWarranty_RepairerCompletedDate = j.JobMetaNonWarranty?.RepairerCompletedDate,
                    JobMetaNonWarranty_AccountingChargeAddedDate = j.JobMetaNonWarranty?.AccountingChargeAddedDate,
                    JobMetaNonWarranty_AccountingChargePaidDate = j.JobMetaNonWarranty?.AccountingChargePaidDate,
                    JobMetaNonWarranty_AccountingChargeRequiredDate = j.JobMetaNonWarranty?.AccountingChargeRequiredDate,
                    JobMetaNonWarranty_IsInsuranceClaim = j.JobMetaNonWarranty?.IsInsuranceClaim,
                    JobMetaInsurance_ClaimFormSentDate = j.JobMetaInsurance_ClaimFormSentDate,
                    JobMetaNonWarranty_InvoiceReceivedDate = j.JobMetaNonWarranty?.InvoiceReceivedDate,
                    JobMetaNonWarranty_PurchaseOrderRaisedDate = j.JobMetaNonWarranty?.PurchaseOrderRaisedDate,
                    JobMetaNonWarranty_PurchaseOrderSentDate = j.JobMetaNonWarranty?.PurchaseOrderSentDate,

                    RecentAttachmentDate = j.RecentAttachmentDate,
                    RecentLogDate = j.RecentLogDate,

                    WaitingForUserAction = j.WaitingForUserAction,
                    DeviceReadyForReturn = j.DeviceReadyForReturn,
                    DeviceHeld = j.DeviceHeld,
                    DeviceReturnedDate = j.DeviceReturnedDate,
                    JobMetaWarranty_ExternalName = j.JobMetaWarranty?.ExternalName,
                    JobMetaNonWarranty_RepairerName = j.JobMetaNonWarranty?.RepairerName,
                    ActiveJobQueues = j.JobQueues?.Where(jq => !jq.RemovedDate.HasValue).Select(jq => new JobTableStatusQueueItemModel()
                    {
                        Id = jq.Id,
                        QueueId = jq.JobQueueId,
                        AddedDate = jq.AddedDate,
                        SLAExpiresDate = jq.SLAExpiresDate,
                        Priority = jq.Priority
                    }).ToList() ?? Enumerable.Empty<JobTableStatusQueueItemModel>()
                });

                items = new List<JobTableItemModel>();
                foreach (var j in jobItems)
                {
                    j.StatusId = j.CalculateStatusId();
                    j.StatusDescription = JobExtensions.JobStatusDescription(j.StatusId, j);

                    var activityDates = new DateTime?[] {
                        j.ActiveJobQueues.Max<JobTableStatusQueueItemModel, DateTime?>(jq => jq.AddedDate),
                        j.ClosedDate,
                        j.DeviceHeld,
                        j.DeviceReadyForReturn,
                        j.DeviceReturnedDate,
                        j.JobMetaInsurance_ClaimFormSentDate,
                        j.JobMetaNonWarranty_AccountingChargeAddedDate,
                        j.JobMetaNonWarranty_AccountingChargePaidDate,
                        j.JobMetaNonWarranty_AccountingChargeRequiredDate,
                        j.JobMetaNonWarranty_InvoiceReceivedDate,
                        j.JobMetaNonWarranty_PurchaseOrderRaisedDate,
                        j.JobMetaNonWarranty_PurchaseOrderSentDate,
                        j.JobMetaNonWarranty_RepairerCompletedDate,
                        j.JobMetaNonWarranty_RepairerLoggedDate,
                        j.JobMetaWarranty_ExternalCompletedDate,
                        j.JobMetaWarranty_ExternalLoggedDate,
                        j.OpenedDate,
                        j.RecentAttachmentDate,
                        j.RecentLogDate,
                        j.WaitingForUserAction
                    };

                    j.LastActivityDate = activityDates.Max().Value;

                    items.Add(j);
                }
            }
            else
            {
                items = Jobs.Select(j => new JobTableItemModel()
                {
                    JobId = j.Id,
                    OpenedDate = j.OpenedDate,
                    ClosedDate = j.ClosedDate,
                    JobTypeId = j.JobTypeId,
                    JobTypeDescription = j.JobType.Description,
                    DeviceSerialNumber = j.Device.SerialNumber,
                    DeviceProfileId = j.Device.DeviceProfileId,
                    DeviceModelId = j.Device.DeviceModelId,
                    DeviceModelDescription = j.Device.DeviceModel.Description,
                    DeviceAddressId = j.Device.DeviceProfile.DefaultOrganisationAddress,
                    UserId = j.UserId,
                    UserDisplayName = j.User.DisplayName,
                    OpenedTechUserId = j.OpenedTechUserId,
                    OpenedTechUserDisplayName = j.OpenedTechUser.DisplayName,
                    DeviceHeldLocation = j.DeviceHeldLocation,
                    Flags = j.Flags
                }).ToList();
            }

            if (!model.ShowDeviceAddress.HasValue)
                model.ShowDeviceAddress = Database.DiscoConfiguration.MultiSiteMode;

            foreach (var j in items)
            {
                j.UserFriendlyId =j.UserId == null ? null : ActiveDirectory.FriendlyAccountId(j.UserId);
                j.OpenedTechUserFriendlyId = ActiveDirectory.FriendlyAccountId(j.OpenedTechUserId);

                if (j.DeviceAddressId.HasValue)
                    j.DeviceAddress = Database.DiscoConfiguration.OrganisationAddresses.GetAddress(j.DeviceAddressId.Value)?.Name;
            }

            return items;
        }