Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion OpenSky.API/Controllers/FinancialController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public async Task<ActionResult<ApiResponse<string>>> BobsYourUncle()
try
{
this.logger.LogInformation($"{this.User.Identity?.Name} | POST Financial/bobsYourUncle");

// ReSharper disable AssignNullToNotNullAttribute
var user = await this.userManager.FindByNameAsync(this.User.Identity?.Name);
if (user == null)
Expand Down Expand Up @@ -133,6 +133,57 @@ public async Task<ActionResult<ApiResponse<string>>> BobsYourUncle()
}
}

/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Resets user balance
/// </summary>
/// <remarks>
/// Damir, 29/11/2023
/// </remarks>
/// <returns>
/// An asynchronous result that yields the account balances.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it yields a status string

/// </returns>
/// -------------------------------------------------------------------------------------------------
[HttpPost("resetFunds", Name = "ResetFunds")]
public async Task<ActionResult<ApiResponse<string>>> ResetFunds()
{
try
{
this.logger.LogInformation($"{this.User.Identity?.Name} | POST Financial/resetFunds");

// ReSharper disable AssignNullToNotNullAttribute
var user = await this.userManager.FindByNameAsync(this.User.Identity?.Name);
if (user == null)
{
return new ApiResponse<string> { Message = "Unable to find user record!", IsError = true };
}

var resetRecord = new FinancialRecord
{
ID = Guid.NewGuid(),
Timestamp = DateTime.UtcNow,
UserID = user.Id,
Category = FinancialCategory.None,
Expense = user.PersonalAccountBalance - user.PersonalAccountBalance,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will just set the Expense to 0, just set user.PersonalAccountBalance here

Description = "Funds have been reset"
};
await this.db.FinancialRecords.AddAsync(resetRecord);

var saveEx = await this.db.SaveDatabaseChangesAsync(this.logger, "Failed to reset funds");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before saving the changes you also need to actually adjust the user's balance. The financial record above is just the trace record of what happened.

user.PersonalAccountBalance = 0;

if (saveEx != null)
{
throw saveEx;
}

return new ApiResponse<string>("Funds have been successfully reset!");
}
catch (Exception ex)
{
this.logger.LogError(ex, $"{this.User.Identity?.Name} | POST Financial/resetFunds");
return new ApiResponse<string>(ex);
}
}

/// -------------------------------------------------------------------------------------------------
/// <summary>
/// Get the current account balances.
Expand Down