Ejemplo n.º 1
0
        public async Task <PrintRequestQueryResult> Handle(PrintRequestQuery query)
        {
            if (string.IsNullOrEmpty(query.PrintRequestId))
            {
                throw new ArgumentNullException(nameof(query.PrintRequestId));
            }
            if (string.IsNullOrEmpty(query.RequestingUserId))
            {
                throw new ArgumentNullException(nameof(query.RequestingUserId));
            }

            //get the print request
            var printRequest = (await printingRepository.Query(new QueryPrintRequests {
                ById = query.PrintRequestId
            })).Cast <ReferralPrintRequest>().SingleOrDefault();

            if (printRequest == null)
            {
                throw new NotFoundException("print request not found", query.PrintRequestId);
            }

            //get requesting user
            if (printRequest.RequestingUserId != query.RequestingUserId)
            {
                throw new BusinessLogicException($"User {query.RequestingUserId} cannot query print for another user ({printRequest.RequestingUserId})");
            }
            var requestingUser = (await teamRepository.GetMembers(userId: printRequest.RequestingUserId, includeStatuses: activeOnlyStatus)).Cast <Resources.Teams.TeamMember>().SingleOrDefault();

            if (requestingUser == null)
            {
                throw new NotFoundException($"User {printRequest.RequestingUserId} not found", printRequest.RequestingUserId);
            }

            //load the file
            var file = mapper.Map <EvacuationFile>((await caseRepository.QueryCase(new Resources.Cases.EvacuationFilesQuery {
                FileId = printRequest.FileId
            })).Items.Cast <Resources.Cases.Evacuations.EvacuationFile>().SingleOrDefault());

            if (file == null)
            {
                throw new NotFoundException($"Evacuation file {printRequest.FileId} not found", printRequest.Id);
            }
            await evacuationFileLoader.Load(file);

            //Find referrals to print
            var referrals = mapper.Map <IEnumerable <PrintReferral> >(file.Supports.Where(s => printRequest.SupportIds.Contains(s.Id)), opts => opts.Items.Add("evacuationFile", file)).ToArray();

            if (referrals.Length != printRequest.SupportIds.Count())
            {
                throw new BusinessLogicException($"Print request {printRequest.Id} has {printRequest.SupportIds.Count()} linked supports, but evacuation file {printRequest.FileId} doesn't have all of them");
            }

            //replace community codes with readable name
            var communities = await metadataRepository.GetCommunities();

            foreach (var referral in referrals)
            {
                referral.HostCommunity = communities.Where(c => c.Code == referral.HostCommunity).SingleOrDefault()?.Name;
            }

            var isProduction = env.IsProduction();

            //convert referrals to html
            var printedReferrals = await supportsService.GetReferralHtmlPagesAsync(new SupportsToPrint()
            {
                Referrals      = referrals,
                AddSummary     = printRequest.IncludeSummary,
                AddWatermark   = !isProduction,
                RequestingUser = new PrintRequestingUser {
                    Id = requestingUser.Id, FirstName = requestingUser.FirstName, LastName = requestingUser.LastName
                }
            });

            //convert to pdf
            var content = await pdfGenerator.Generate(printedReferrals);

            var contentType = "application/pdf";

            await printingRepository.Manage(new MarkPrintRequestAsComplete { PrintRequestId = printRequest.Id });

            return(new PrintRequestQueryResult
            {
                Content = content,
                ContentType = contentType,
                PrintedOn = DateTime.UtcNow
            });
        }
Ejemplo n.º 2
0
        public async Task <PrintRequestQueryResult> Handle(PrintRequestQuery query)
        {
            if (string.IsNullOrEmpty(query.PrintRequestId))
            {
                throw new ArgumentNullException(nameof(query.PrintRequestId));
            }
            if (string.IsNullOrEmpty(query.RequestingUserId))
            {
                throw new ArgumentNullException(nameof(query.RequestingUserId));
            }

            //get the print request
            var printRequest = (await printingRepository.Query(new QueryPrintRequests {
                ById = query.PrintRequestId
            })).Cast <ReferralPrintRequest>().SingleOrDefault();

            if (printRequest == null)
            {
                throw new NotFoundException("print request not found", query.PrintRequestId);
            }

            //get requesting user
            if (printRequest.RequestingUserId != query.RequestingUserId)
            {
                throw new Exception($"User {query.RequestingUserId} cannot query print for another user ({printRequest.RequestingUserId})");
            }
            var requestingUser = (await teamRepository.GetMembers(userId: printRequest.RequestingUserId)).Cast <Resources.Team.TeamMember>().SingleOrDefault();

            if (requestingUser == null)
            {
                throw new Exception($"User {printRequest.RequestingUserId} not found");
            }

            //load the file
            var file = mapper.Map <Shared.Contracts.Submissions.EvacuationFile>((await caseRepository.QueryCase(new Resources.Cases.EvacuationFilesQuery {
                FileId = printRequest.FileId
            })).Items.Cast <Resources.Cases.EvacuationFile>().SingleOrDefault());

            if (file == null)
            {
                throw new NotFoundException($"Evacuation file {printRequest.FileId} not found", printRequest.Id);
            }
            await evacuationFileLoader.Load(file);

            //Find referrals to print
            var referrals = mapper.Map <IEnumerable <PrintReferral> >(file.Supports.Where(s => printRequest.SupportIds.Contains(s.Id)), opts => opts.Items.Add("evacuationFile", file)).ToArray();

            if (referrals.Count() != printRequest.SupportIds.Count())
            {
                throw new Exception($"Print request {printRequest.Id} has {printRequest.SupportIds.Count()} linked supports, but evacuation file {printRequest.FileId} doesn't have all of them");
            }

            //convert referrals to html
            var printedReferrals = await supportsService.GetReferralHtmlPagesAsync(new SupportsToPrint()
            {
                Referrals      = referrals,
                AddSummary     = printRequest.IncludeSummary,
                RequestingUser = new PrintRequestingUser {
                    Id = requestingUser.Id, DisplayName = $"{requestingUser.FirstName} {requestingUser.LastName[0]}."
                }
            });

            //convert to pdf
            var content = await pdfGenerator.Generate(printedReferrals);

            var contentType = "application/pdf";

            await printingRepository.Manage(new MarkPrintRequestAsComplete { PrintRequestId = printRequest.Id });

            return(new PrintRequestQueryResult
            {
                Content = content,
                ContentType = contentType,
                PrintedOn = DateTime.Now
            });
        }