diff --git a/src/AdminKycProxy/LambdaFunction.cs b/src/AdminKycProxy/LambdaFunction.cs index bbcc63c..59984ed 100644 --- a/src/AdminKycProxy/LambdaFunction.cs +++ b/src/AdminKycProxy/LambdaFunction.cs @@ -3,7 +3,6 @@ using Flurl.Http; using KYC.DataBase; using SecretsManager; -using EnvironmentManager; using Amazon.Lambda.Core; using AdminKycProxy.Models; @@ -13,7 +12,7 @@ namespace AdminKycProxy; public class LambdaFunction { - private const int MaxRetries = 20; + private const int limit = 20; private readonly LambdaSettings lambdaSettings; private readonly KycDbContext context; @@ -29,37 +28,25 @@ public LambdaFunction(SecretManager secretManager, KycDbContext context) public async Task RunAsync() { - var skip = new EnvManager().GetEnvironmentValue("DOWNLOADED_FROM", true); - var url = new Url(lambdaSettings.Url); - url = url.SetQueryParam("skip", skip); - url = url.SetQueryParam("limit", MaxRetries); - - var hasMore = true; - while (hasMore) - { - var response = url + HttpResponse? response = null; + var skip = context.Users.Count(); + var url = lambdaSettings.Url + .SetQueryParam("limit", limit) .WithHeader("Authorization", lambdaSettings.SecretApiKey) - .WithHeader("cache-control", "no-cache") - .GetAsync() - .ReceiveJson() - .GetAwaiter() - .GetResult(); + .WithHeader("cache-control", "no-cache"); - if (response.Data.Records.Length == 0) - { - hasMore = false; - skip = response.Data.Total; - continue; - } + while (response == null || response.Data.Records.Length > 0) + { + response = await url + .SetQueryParam("skip", skip) + .GetJsonAsync(); - context.Users.AddRange(response.Data.Records); + context.Users.AddRange(response.Data.Records.Where(x => + !context.Users.Contains(x))); - skip += MaxRetries; - url.SetQueryParam("skip", skip); + skip += limit; } - await context.SaveChangesAsync(); - return HttpStatusCode.OK; } } \ No newline at end of file diff --git a/tests/AdminKycProxy.Tests/LambdaFunctionTests.cs b/tests/AdminKycProxy.Tests/LambdaFunctionTests.cs index 5b8bd4e..288c44c 100644 --- a/tests/AdminKycProxy.Tests/LambdaFunctionTests.cs +++ b/tests/AdminKycProxy.Tests/LambdaFunctionTests.cs @@ -56,10 +56,10 @@ internal async Task RunAsync() }; using var httpTest = new HttpTest(); httpTest - .ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?skip=0&limit=20") + .ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?limit=20&skip=0") .RespondWithJson(response); httpTest - .ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?skip=20&limit=20") + .ForCallsTo("https://kyc.blockpass.org/kyc/1.0/connect/ClientId/applicants?limit=20&skip=20") .RespondWithJson(new HttpResponse()); var context = new DbContextFactory().Create(ContextOption.InMemory, Guid.NewGuid().ToString());