diff --git a/KYC.API.Proxy.sln b/KYC.API.Proxy.sln index 3a14eef..06f5119 100644 --- a/KYC.API.Proxy.sln +++ b/KYC.API.Proxy.sln @@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KYC.API.Proxy", "src\KYC.AP EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KYC.API.Proxy.Tests", "tests\KYC.API.Proxy.Tests\KYC.API.Proxy.Tests.csproj", "{0AB264DD-CE17-4ED3-BA4B-0539625C48F2}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KycWebHook", "src\KYC.DataBase\KycWebHook\KycWebHook.csproj", "{2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KycWebHook.Tests", "tests\KycWebHook.Tests\KycWebHook.Tests.csproj", "{41596964-623E-4796-9C70-DABA79EF7309}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -25,6 +29,14 @@ Global {0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Debug|Any CPU.Build.0 = Debug|Any CPU {0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Release|Any CPU.ActiveCfg = Release|Any CPU {0AB264DD-CE17-4ED3-BA4B-0539625C48F2}.Release|Any CPU.Build.0 = Release|Any CPU + {2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230}.Release|Any CPU.Build.0 = Release|Any CPU + {41596964-623E-4796-9C70-DABA79EF7309}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {41596964-623E-4796-9C70-DABA79EF7309}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41596964-623E-4796-9C70-DABA79EF7309}.Release|Any CPU.ActiveCfg = Release|Any CPU + {41596964-623E-4796-9C70-DABA79EF7309}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -32,6 +44,8 @@ Global GlobalSection(NestedProjects) = preSolution {655F6E73-17FD-437B-B76B-C5C8CDB1BDC2} = {0E291973-D91C-486C-A9D2-8926C656A382} {0AB264DD-CE17-4ED3-BA4B-0539625C48F2} = {753B9E81-B38A-4B54-830B-F80B129D5E6F} + {2AD5E45D-CBD7-433D-9CB5-4C1C98CD0230} = {0E291973-D91C-486C-A9D2-8926C656A382} + {41596964-623E-4796-9C70-DABA79EF7309} = {753B9E81-B38A-4B54-830B-F80B129D5E6F} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0803A1E1-195C-4AC0-A580-70F2086DE96D} diff --git a/nuget.config b/nuget.config new file mode 100644 index 0000000..9f3e922 --- /dev/null +++ b/nuget.config @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<configuration> + <packageSources> + <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" /> + <add key="Github" value="https://nuget.pkg.github.com/The-Poolz/index.json" /> + </packageSources> + <packageSourceCredentials> + <Github> + <add key="Username" value="%GITHUB_PACKAGE_USER_NAME%" /> + <add key="ClearTextPassword" value="%GITHUB_PACKAGE_TOKEN%" /> + </Github> + </packageSourceCredentials> +</configuration> diff --git a/src/KYC.DataBase/KycWebHook/KycWebHook.csproj b/src/KYC.DataBase/KycWebHook/KycWebHook.csproj new file mode 100644 index 0000000..4107834 --- /dev/null +++ b/src/KYC.DataBase/KycWebHook/KycWebHook.csproj @@ -0,0 +1,18 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> + <AWSProjectType>Lambda</AWSProjectType> + <!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. --> + <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> + <!-- Generate ready to run images during publishing to improve cold start time. --> + <PublishReadyToRun>true</PublishReadyToRun> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" /> + <PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.0" /> + <PackageReference Include="KYC.DataBase" Version="1.0.2" /> + </ItemGroup> +</Project> \ No newline at end of file diff --git a/src/KYC.DataBase/KycWebHook/LambdaFunction.cs b/src/KYC.DataBase/KycWebHook/LambdaFunction.cs new file mode 100644 index 0000000..48d30f3 --- /dev/null +++ b/src/KYC.DataBase/KycWebHook/LambdaFunction.cs @@ -0,0 +1,41 @@ +using System.Net; +using KYC.DataBase; +using KycWebHook.Models; +using Amazon.Lambda.Core; +using Newtonsoft.Json.Linq; + +[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] + +namespace KycWebHook; + +public class LambdaFunction +{ + private readonly KycDbContext context; + + public LambdaFunction() + : this(new KycDbContext()) + { } + + public LambdaFunction(KycDbContext context) + { + this.context = context; + } + + public async Task<int> RunAsync(HttpResponse httpResponse) + { + Console.WriteLine(JToken.FromObject(httpResponse)); + + var user = context.Users.FirstOrDefault(x => x.RecordId == httpResponse.RecordId); + if (user == null) + { + context.Users.Add(httpResponse); + } + else + { + context.Users.Update(httpResponse); + } + await context.SaveChangesAsync(); + + return (int)HttpStatusCode.OK; + } +} \ No newline at end of file diff --git a/src/KYC.DataBase/KycWebHook/Models/HttpResponse.cs b/src/KYC.DataBase/KycWebHook/Models/HttpResponse.cs new file mode 100644 index 0000000..bfa9083 --- /dev/null +++ b/src/KYC.DataBase/KycWebHook/Models/HttpResponse.cs @@ -0,0 +1,8 @@ +using KYC.DataBase.Models; + +namespace KycWebHook.Models; + +public class HttpResponse : User +{ + public string Event { get; set; } = null!; +} \ No newline at end of file diff --git a/src/KYC.DataBase/KycWebHook/Properties/launchSettings.json b/src/KYC.DataBase/KycWebHook/Properties/launchSettings.json new file mode 100644 index 0000000..a82d404 --- /dev/null +++ b/src/KYC.DataBase/KycWebHook/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "Mock Lambda Test Tool": { + "commandName": "Executable", + "commandLineArgs": "--port 5050", + "workingDirectory": ".\\bin\\$(Configuration)\\net6.0", + "executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-6.0.exe" + } + } +} \ No newline at end of file diff --git a/src/KYC.DataBase/KycWebHook/Readme.md b/src/KYC.DataBase/KycWebHook/Readme.md new file mode 100644 index 0000000..41da91e --- /dev/null +++ b/src/KYC.DataBase/KycWebHook/Readme.md @@ -0,0 +1,2 @@ +# KycWebHook + diff --git a/tests/KycWebHook.Tests/KycWebHook.Tests.csproj b/tests/KycWebHook.Tests/KycWebHook.Tests.csproj new file mode 100644 index 0000000..9e7cbfd --- /dev/null +++ b/tests/KycWebHook.Tests/KycWebHook.Tests.csproj @@ -0,0 +1,31 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <RootNamespace>KycWebHook.Tests</RootNamespace> + <AssemblyName>KycWebHook.Tests</AssemblyName> + <IsPackable>false</IsPackable> + <IsTestProject>true</IsTestProject> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="FluentAssertions" Version="6.11.0" /> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> + <PackageReference Include="xunit" Version="2.4.2" /> + <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + <PrivateAssets>all</PrivateAssets> + </PackageReference> + <PackageReference Include="coverlet.collector" Version="3.2.0"> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + <PrivateAssets>all</PrivateAssets> + </PackageReference> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\src\KYC.DataBase\KycWebHook\KycWebHook.csproj" /> + </ItemGroup> + +</Project> diff --git a/tests/KycWebHook.Tests/LambdaFunctionTests.cs b/tests/KycWebHook.Tests/LambdaFunctionTests.cs new file mode 100644 index 0000000..9701c59 --- /dev/null +++ b/tests/KycWebHook.Tests/LambdaFunctionTests.cs @@ -0,0 +1,45 @@ +using ConfiguredSqlConnection.Extensions; +using Xunit; +using FluentAssertions; +using KYC.DataBase; +using KycWebHook.Models; +using System.Net; + +namespace KycWebHook.Tests; + +public class LambdaFunctionTests +{ + private readonly HttpResponse httpResponse = new() + { + Guid = "614967cde3227d00125ebc4f", + Status = "deleted", + ClientId = "client_id", + Event = "user.deleted", + RecordId = "5ffffb44baaaaf001236b1d1", + RefId = null + }; + + [Fact] + public async Task RunAsync_WhenUserNotExist() + { + var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString()); + + var result = await new LambdaFunction(context).RunAsync(httpResponse); + + result.Should().Be((int)HttpStatusCode.OK); + context.Users.Should().HaveCount(1); + } + + [Fact] + public async Task RunAsync_WhenUserExist() + { + var context = new DbContextFactory<KycDbContext>().Create(ContextOption.InMemory, Guid.NewGuid().ToString()); + context.Users.Add(httpResponse); + await context.SaveChangesAsync(); + + var result = await new LambdaFunction(context).RunAsync(httpResponse); + + result.Should().Be((int)HttpStatusCode.OK); + context.Users.First().Should().BeEquivalentTo(httpResponse); + } +}