public async Task <CarePackageHistoryViewResponse> ExecuteAsync(Guid packageId)
        {
            var package = await _carePackageGateway.GetPackageAsync(packageId, PackageFields.Approver | PackageFields.Broker | PackageFields.Resources, false).EnsureExistsAsync($"Package with id {packageId} not found");

            var packageHistory = await _carePackageHistoryGateway.ListAsync(packageId);

            var historyResponse = new CarePackageHistoryViewResponse
            {
                CarePackageId = package.Id,
                PackageType   = package.PackageType.GetDisplayName(),
                BrokeredBy    = package.Broker?.Name,
                AssignedOn    = package.DateAssigned,
                ApprovedBy    = package.Approver?.Name,
                ApprovedOn    = package.DateApproved,
                SocialWorkerCarePlanFileId   = package.Resources?.Where(r => r.Type == PackageResourceType.CarePlanFile).OrderByDescending(x => x.DateCreated).FirstOrDefault()?.FileId,
                SocialWorkerCarePlanFileName = package.Resources?.Where(r => r.Type == PackageResourceType.CarePlanFile).OrderByDescending(x => x.DateCreated).FirstOrDefault()?.Name,
                History = packageHistory.OrderByDescending(h => h.Id).ToResponse()
            };

            return(historyResponse);
        }
        public async Task <ServiceUserPackagesViewResponse> ExecuteAsync(Guid serviceUserId)
        {
            // Check service user exists
            var serviceUser = await _serviceUserGateway.GetByIdAsync(serviceUserId).EnsureExistsAsync($"Service user with id {serviceUserId} not found");

            const PackageFields fields = PackageFields.Details | PackageFields.Reclaims | PackageFields.Settings | PackageFields.Resources;
            var userPackages           = await _carePackageGateway.GetServiceUserPackagesAsync(serviceUserId, fields);

            var response = new ServiceUserPackagesViewResponse
            {
                ServiceUser = serviceUser.ToBasicDomain().ToResponse(),
                Packages    = new List <ServiceUserPackageViewItemResponse>()
            };

            var packagesResponse = new List <ServiceUserPackageViewItemResponse>();

            foreach (var carePackage in userPackages)
            {
                var coreCost = carePackage.Details
                               .SingleOrDefault(d => d.Type is PackageDetailType.CoreCost);

                var packageResponse = new ServiceUserPackageViewItemResponse
                {
                    PackageId             = carePackage.Id,
                    PackageStatus         = CalculatePackageStatus(carePackage, coreCost),
                    PackageType           = carePackage.PackageType.GetDisplayName(),
                    IsS117Client          = carePackage.Settings?.IsS117Client,
                    IsS117ClientConfirmed = carePackage.Settings?.IsS117ClientConfirmed,
                    DateAssigned          = carePackage.DateAssigned,
                    GrossTotal            = 0,
                    NetTotal = 0,
                    SocialWorkerCarePlanFileId   = carePackage.Resources?.Where(r => r.Type == PackageResourceType.CarePlanFile).OrderByDescending(x => x.DateCreated).FirstOrDefault()?.FileId,
                    SocialWorkerCarePlanFileName = carePackage.Resources?.Where(r => r.Type == PackageResourceType.CarePlanFile).OrderByDescending(x => x.DateCreated).FirstOrDefault()?.Name,
                    Notes        = new List <CarePackageHistoryResponse>(),
                    PackageItems = new List <CarePackageCostItemResponse>()
                };

                var additionalNeeds = carePackage.Details
                                      .Where(d => d.Type == PackageDetailType.AdditionalNeed).ToList();

                packageResponse.PackageItems = CollectPackageItems(carePackage, coreCost, additionalNeeds, carePackage.Reclaims.ToList());

                var preferences = FilterPreferences.PackageItemStatus();

                packageResponse.PackageItems = packageResponse.PackageItems.OrderBy(
                    item => preferences.IndexOf(item.Status)).ThenBy(x => x.StartDate);

                // Get care package history if package request i.e new, in-progress, not-approved
                if (carePackage.Status.In(PackageStatus.New, PackageStatus.InProgress, PackageStatus.NotApproved))
                {
                    var packageHistory = await _carePackageHistoryGateway.ListAsync(carePackage.Id);

                    packageResponse.Notes = packageHistory.OrderByDescending(h => h.Id).ToResponse();
                }

                var(grossTotal, netTotal)  = CalculateTotals(coreCost, additionalNeeds, carePackage.Reclaims.ToList());
                packageResponse.GrossTotal = grossTotal;
                packageResponse.NetTotal   = netTotal;

                packagesResponse.Add(packageResponse);
            }

            response.Packages = packagesResponse;

            return(response);
        }