Skip to content

Commit 22b6715

Browse files
committed
Fix report GET_REFERRAL_FEE_PREVIEW_REPORT when return null
1 parent 3d48707 commit 22b6715

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

Source/FikaAmazonAPI/ReportGeneration/ReportManager.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using FikaAmazonAPI.Utils;
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using static FikaAmazonAPI.Utils.Constants;
@@ -379,7 +380,23 @@ public async Task<List<ReferralFeeReportRow>> GetReferralFeeReportDataAsync(Date
379380
}
380381
private async Task<string> GetReferralFeeReportDataAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
381382
{
382-
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_REFERRAL_FEE_PREVIEW_REPORT, fromDate, toDate);
383+
var reportPath = await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_REFERRAL_FEE_PREVIEW_REPORT, fromDate, toDate);
384+
if (reportPath == null)
385+
{
386+
var getOldReports = amazonConnection.Reports.GetReports(new Parameter.Report.ParameterReportList()
387+
{
388+
reportTypes = new ReportTypes[] { ReportTypes.GET_REFERRAL_FEE_PREVIEW_REPORT },
389+
processingStatuses = new List<ProcessingStatuses> { ProcessingStatuses.DONE }
390+
});
391+
392+
if (getOldReports != null && getOldReports.Count > 0)
393+
{
394+
var reportId = getOldReports.FirstOrDefault().ReportId;
395+
return await amazonConnection.Reports.GetReportFileByReportIdAsync(reportId, false);
396+
397+
}
398+
}
399+
return reportPath;
383400
}
384401
#endregion
385402
}

Source/FikaAmazonAPI/Services/ReportService.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public ReportService(AmazonCredential amazonCredential) : base(amazonCredential)
2020
{
2121
}
2222
#region GetReport
23-
public List<Report> GetReports(ParameterReportList parameterReportList) =>
23+
public IList<Report> GetReports(ParameterReportList parameterReportList) =>
2424
Task.Run(() => GetReportsAsync(parameterReportList)).ConfigureAwait(false).GetAwaiter().GetResult();
25-
public async Task<List<Report>> GetReportsAsync(ParameterReportList parameterReportList, CancellationToken cancellationToken = default)
25+
public async Task<IList<Report>> GetReportsAsync(ParameterReportList parameterReportList, CancellationToken cancellationToken = default)
2626
{
2727
if (parameterReportList.marketplaceIds == null || parameterReportList.marketplaceIds.Count == 0)
2828
{
@@ -94,10 +94,10 @@ public GetReportsResponseV00 GetReportsByNextToken(ParameterReportList parameter
9494
Task.Run(() => GetReportsByNextTokenAsync(parameterReportList)).ConfigureAwait(false).GetAwaiter().GetResult();
9595
public async Task<GetReportsResponseV00> GetReportsByNextTokenAsync(ParameterReportList parameterReportList, CancellationToken cancellationToken = default)
9696
{
97-
List<KeyValuePair<string, string>> parameters = null;
98-
if(string.IsNullOrEmpty(parameterReportList.nextToken))
97+
List<KeyValuePair<string, string>> parameters = null;
98+
if (string.IsNullOrEmpty(parameterReportList.nextToken))
9999
parameters = parameterReportList.getParameters();
100-
else
100+
else
101101
{
102102
var parameterReportListNew = new ParameterReportList();
103103
parameterReportListNew.nextToken = parameterReportList.nextToken;
@@ -215,38 +215,44 @@ private async Task<string> GetFileAsync(ReportDocument reportDocument, Cancellat
215215

216216
string tempFilePath = Path.Combine(Path.GetTempPath() + fileName);
217217

218-
try {
219-
if (isEncryptedFile) {
218+
try
219+
{
220+
if (isEncryptedFile)
221+
{
220222
//Later will check
221223
byte[] rawData = client.DownloadData(reportDocument.Url);
222224
byte[] key = Convert.FromBase64String(reportDocument.EncryptionDetails.Key);
223225
byte[] iv = Convert.FromBase64String(reportDocument.EncryptionDetails.InitializationVector);
224226
var reportData = FileTransform.DecryptString(key, iv, rawData);
225227
File.WriteAllText(tempFilePath, reportData);
226228
}
227-
else {
229+
else
230+
{
228231
var stream = await client.OpenReadTaskAsync(new Uri(reportDocument.Url));
229-
using (Stream s = File.Create(tempFilePath)) {
232+
using (Stream s = File.Create(tempFilePath))
233+
{
230234
stream?.CopyTo(s);
231235
}
232236
}
233237

234238
cancellationToken.ThrowIfCancellationRequested();
235239

236-
if (isCompressionFile) {
240+
if (isCompressionFile)
241+
{
237242
var compressionFile = tempFilePath;
238243
tempFilePath = FileTransform.Decompress(tempFilePath);
239244
File.Delete(compressionFile);
240245
}
241246

242-
cancellationToken.ThrowIfCancellationRequested();
247+
cancellationToken.ThrowIfCancellationRequested();
243248

244-
return tempFilePath;
249+
return tempFilePath;
245250
}
246-
catch(OperationCanceledException){
247-
File.Delete(tempFilePath);
251+
catch (OperationCanceledException)
252+
{
253+
File.Delete(tempFilePath);
248254
throw;
249-
}
255+
}
250256
}
251257

252258
public async Task SaveStreamToFileAsync(string fileFullPath, Stream stream, CancellationToken cancellationToken = default)
@@ -309,6 +315,12 @@ public async Task<string> CreateReportAndDownloadFileAsync(ReportTypes reportTyp
309315
parameters.dataEndTime = dataEndTime;
310316

311317
var reportId = await CreateReportAsync(parameters, cancellationToken);
318+
return await GetReportFileByReportIdAsync(reportId, isRestrictedReport, millisecondsDelay, cancellationToken);
319+
320+
}
321+
322+
public async Task<string> GetReportFileByReportIdAsync(string reportId, bool isRestrictedReport, int millisecondsDelay = 500, CancellationToken cancellationToken = default)
323+
{
312324
var filePath = string.Empty;
313325
string ReportDocumentId = string.Empty;
314326

0 commit comments

Comments
 (0)