Skip to content

Commit

Permalink
Endpoints for "old" bankinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
larsolavk committed Dec 19, 2024
1 parent 6155974 commit 829843e
Show file tree
Hide file tree
Showing 6 changed files with 611 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ public class BankFileStore(ILogger<BankFileStore> logger) : IBankStore
private const string CustomerRelationsPath = "./Testdata/Json/Bank/CustomerRelations";
private const string BankDetailsPath = "./Testdata/Json/Bank/BankDetails";
private const string TransactionsPath = "./Testdata/Json/Bank/Transactions";
private const string AllInOnePath = "./Testdata/Json/Bank/AllInOne";

public async Task<BankResponse> GetAllInOne(int partyId)
{
EnsureDirectory(AllInOnePath);

var response = await GetSpecificAllInOne(partyId);
if (response is not null)
{
logger.LogInformation("Returning SPECIFIC all-in-one testdata for partyId [{partyId}]", partyId);
return response;
}

logger.LogInformation("Returning DEFAULT all-in-one testdata for partyId [{partyId}]", partyId);
return await GetDefault<BankResponse>(AllInOnePath);

}

public async Task<IEnumerable<BankCustomerRelation>> GetCustomerRelations(int partyId)
{
Expand All @@ -20,7 +37,7 @@ public async Task<IEnumerable<BankCustomerRelation>> GetCustomerRelations(int pa
}

logger.LogInformation("Returning DEFAULT customer relations testdata for partyId [{partyId}]", partyId);
return await GetDefaultCustomerRelations();
return await GetDefault<List<BankCustomerRelation>>(CustomerRelationsPath);
}

public async Task<BankResponse> GetBankDetails(int partyId, string bankOrgNo)
Expand All @@ -35,10 +52,10 @@ public async Task<BankResponse> GetBankDetails(int partyId, string bankOrgNo)
}

logger.LogInformation("Returning DEFAULT bank details testdata for partyId [{partyId}], bankOrgNo [{bankOrgNo}]", partyId, bankOrgNo);
return await GetDefaultBankDetails();
return await GetDefault<BankResponse>(BankDetailsPath);
}

public async Task<byte[]> GetAccountTransactionsFile(int partyId, string bankOrgNo, string accountRef)
public async Task<byte[]> GetAccountTransactionsFile()
{
EnsureDirectory(TransactionsPath);

Expand All @@ -50,60 +67,64 @@ public async Task<byte[]> GetAccountTransactionsFile(int partyId, string bankOrg
return fileBytes ?? [];
}

private static async Task<IEnumerable<BankCustomerRelation>?> GetSpecificCustomerRelations(int partyId)
private static async Task<BankResponse?> GetSpecificAllInOne(int partyId)
{
EnsureDirectory(CustomerRelationsPath);
EnsureDirectory(AllInOnePath);

var files = Directory.EnumerateFiles(CustomerRelationsPath);
var files = Directory.EnumerateFiles(AllInOnePath);
var file = files.SingleOrDefault(f => f.Contains(partyId.ToString()));

if (file is null) return null;

await using var fileStream = File.OpenRead(file);
var data = await JsonSerializer.DeserializeAsync<List<BankCustomerRelation>>(fileStream);
var data = await JsonSerializer.DeserializeAsync<BankResponse>(fileStream);

return data;
}

private static async Task<IEnumerable<BankCustomerRelation>> GetDefaultCustomerRelations()
private static async Task<TResponse> GetDefault<TResponse>(string path) where TResponse : class, new()
{
var file = Path.Combine(CustomerRelationsPath, "default.json");
EnsureDirectory(path);

var file = Path.Combine(path, "default.json");
if (!File.Exists(file))
return [];
return new TResponse();

await using var fileStream = File.OpenRead(file);
var data = await JsonSerializer.DeserializeAsync<IEnumerable<BankCustomerRelation>>(fileStream);
var data = await JsonSerializer.DeserializeAsync<TResponse>(fileStream);

return data ?? [];
return data ?? new TResponse();
}

private static async Task<BankResponse?> GetSpecificBankDetails(int partyId, string bankOrgNo)
private static async Task<IEnumerable<BankCustomerRelation>?> GetSpecificCustomerRelations(int partyId)
{
var prefix = $"{partyId}-{bankOrgNo}";
var files = Directory.EnumerateFiles(BankDetailsPath);
var file = files.SingleOrDefault(f => f.Contains(prefix));
EnsureDirectory(CustomerRelationsPath);

var files = Directory.EnumerateFiles(CustomerRelationsPath);
var file = files.SingleOrDefault(f => f.Contains(partyId.ToString()));

if (file is null) return null;

await using var fileStream = File.OpenRead(file);
var data = await JsonSerializer.DeserializeAsync<BankResponse>(fileStream);
var data = await JsonSerializer.DeserializeAsync<List<BankCustomerRelation>>(fileStream);

return data;
}

private static async Task<BankResponse> GetDefaultBankDetails()
private static async Task<BankResponse?> GetSpecificBankDetails(int partyId, string bankOrgNo)
{
var file = Path.Combine(BankDetailsPath, "default.json");
if (!File.Exists(file))
return new BankResponse();
var prefix = $"{partyId}-{bankOrgNo}";
var files = Directory.EnumerateFiles(BankDetailsPath);
var file = files.SingleOrDefault(f => f.Contains(prefix));

if (file is null) return null;

await using var fileStream = File.OpenRead(file);
var data = await JsonSerializer.DeserializeAsync<BankResponse>(fileStream);

return data ?? new BankResponse();
return data;
}


private static void EnsureDirectory(string path)
{
if (!Directory.Exists(path))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class Account
public string accountNumber { get; set; }
public Accountdetail accountDetail { get; set; }
public object transactions { get; set; }
public int accountAvailableBalance { get; set; }
public int accountBookedBalance { get; set; }
public decimal accountAvailableBalance { get; set; }
public decimal accountBookedBalance { get; set; }
}

public class Accountdetail
Expand Down Expand Up @@ -82,11 +82,11 @@ public class Identifier1
public class Balance
{
public bool creditLineIncluded { get; set; }
public int amount { get; set; }
public decimal amount { get; set; }
public string creditDebitIndicator { get; set; }
public DateTime registered { get; set; }
public string type { get; set; }
public int creditLineAmount { get; set; }
public decimal creditLineAmount { get; set; }
public string creditLineCurrency { get; set; }
public string currency { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

public interface IBankStore
{
public Task<BankResponse> GetAllInOne(int partyId);
public Task<IEnumerable<BankCustomerRelation>> GetCustomerRelations(int partyId);
public Task<BankResponse> GetBankDetails(int partyId, string bankOrgNo);

public Task<byte[]> GetAccountTransactionsFile(int partyId, string bankOrgNo, string accountRef);
public Task<byte[]> GetAccountTransactionsFile();
}
43 changes: 41 additions & 2 deletions src/oed-testdata.Server/Testdata/Bank/BankEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ public static void MapBankEndpoints(this WebApplication app)
.MapGroup("/api/testdata/banks/{instanceOwnerPartyId:int}/{instanceGuid:guid}")
.MapEndpoints()
.AllowAnonymous();

app
.MapGroup("/api/testdata/externalapi/bank/{instanceOwnerPartyId:int}/{instanceGuid:guid}")
.MapGet("/", GetAllInOne)
.AllowAnonymous();

app
.MapGroup("/api/testdata/externalapi/banktransactions/{instanceOwnerPartyId:int}/{instanceGuid:guid}")
.MapGet("/", GetAllInOneTransactions)
.AllowAnonymous();

}

private static RouteGroupBuilder MapEndpoints(this RouteGroupBuilder group)
{
group.MapGet("/", GetBankCustomerRelations);
Expand All @@ -20,6 +31,34 @@ private static RouteGroupBuilder MapEndpoints(this RouteGroupBuilder group)
return group;
}


private static async Task<IResult> GetAllInOne(
int instanceOwnerPartyId,
HttpContext httpContext,
IBankStore bankStore,
ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger(typeof(BankEndpoints));
logger.LogInformation("Handling call for {path}", httpContext.Request.Path.Value);

var resp = await bankStore.GetAllInOne(instanceOwnerPartyId);
return Results.Ok(resp);
}

private static async Task<IResult> GetAllInOneTransactions(
int instanceOwnerPartyId,
HttpContext httpContext,
IBankStore bankStore,
ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger(typeof(BankEndpoints));
logger.LogInformation("Handling call for {path}", httpContext.Request.Path.Value);

var resp = await bankStore.GetAccountTransactionsFile();
return Results.File(resp, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Transaksjonshistorikk.xlsx");

}

private static async Task<IResult> GetBankCustomerRelations(
int instanceOwnerPartyId,
HttpContext httpContext,
Expand Down Expand Up @@ -59,7 +98,7 @@ private static async Task<IResult> GetAccountTransactions(
var logger = loggerFactory.CreateLogger(typeof(BankEndpoints));
logger.LogInformation("Handling call for {path}", httpContext.Request.Path.Value);

var resp = await bankStore.GetAccountTransactionsFile(instanceOwnerPartyId, bankOrgNo, accountRefNo);
var resp = await bankStore.GetAccountTransactionsFile();
return Results.File(resp, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Transaksjonshistorikk.xlsx");
}

Expand Down
Loading

0 comments on commit 829843e

Please sign in to comment.