using MediatR; using System.Threading; using System.Threading.Tasks; using Reports.Core.SeedWork.Domain; using System.IO; using Telerik.Reporting; using Telerik.Reporting.Processing; using Reports.Core.Services; using Microsoft.Extensions.Configuration; using System.IdentityModel.Tokens.Jwt; using System.Linq; namespace Reports.Queries.Reports.Handlers { public class GetKSHBIHandler : IRequestHandler> { private readonly ICustomerService _customerService; private readonly IConfiguration _config; private readonly UserToken _user; private readonly string _requestId; private readonly IProcessService _processService; public GetKSHBIHandler(ICustomerService customerService, IConfiguration config, IHttpContextService contextService, IProcessService processService) { _customerService = customerService; _config = config; _user = contextService.GetUserToken(); _requestId = contextService.GetRequestId(); _processService = processService; } public async Task> Handle(GetKSHBI request, CancellationToken cancellationToken) { var res = "0"; var channel = _user.Claims.Where(x => x.Type == "channel").FirstOrDefault()?.Value; try { var response = await _customerService.GetKSHBI(request.CustomerNumber, request.Token.Replace("Bearer ", "")); if (response.ResponseType == "Error") { throw new DomainException(response.ResponseMessage); } var url = $"{_config["ReportsPath"]}KSHBI.trdp"; var tokenHandler = new JwtSecurityTokenHandler(); var jwtToken = tokenHandler.ReadToken(request.Token.Replace("Bearer ", "")) as JwtSecurityToken; var dega = jwtToken.Claims.FirstOrDefault(x => x.Type == "userbranch")?.Value; var user = jwtToken.Claims.FirstOrDefault(x => x.Type == "name")?.Value; var branchName = await _customerService.GetBranchName(dega, request.Token.Replace("Bearer ", "")); FileStream stream = new FileStream(url, FileMode.Open, FileAccess.Read); var reportPackager = new ReportPackager(); var report = reportPackager.Unpackage(stream); report.DataSource = response; report.ReportParameters["customer"].Value = request.CustomerNumber ?? ""; report.ReportParameters["MotherMaiden"].Value = request.MotherMaiden ?? ""; report.ReportParameters["AccCurrency"].Value = request.AccCurrency ?? ""; report.ReportParameters["DebitCard"].Value = request.DebitCard ?? ""; report.ReportParameters["DebitCurrency"].Value = request.DebitCurrency ?? ""; report.ReportParameters["UserNameSurname"].Value = user ?? ""; report.ReportParameters["Dega"].Value = branchName ?? ""; ReportProcessor reportProcessor = new ReportProcessor(); InstanceReportSource instanceReportSource = new InstanceReportSource(); instanceReportSource.ReportDocument = report; RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null, cancellationToken); string fileName = result.DocumentName + "." + result.Extension; stream.Close(); res = "1"; return new Response { ResponseCode = 200, ResponseMessage = "Success", ResponseType = "OK", Result = new ReportResult { Content = result.DocumentBytes, Name = fileName } }; } finally { if (channel == "MBL") _processService.AddActionLog("KSHBI_Report", "Customer", res, request.CustomerNumber, request.Token, _requestId); else _processService.AddActionLog("KSHBI_Report", "IPRINT", res, request.CustomerNumber, request.Token, _requestId); } } } }