Skip to content

Commit

Permalink
fix(api): Correctly restore deleted accounts in currency
Browse files Browse the repository at this point in the history
  • Loading branch information
VMelnalksnis committed May 20, 2023
1 parent 4244457 commit 541f615
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions source/Gnomeshade.Data/Gnomeshade.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<EmbeddedResource Include="Migrations\00000026_linked_product.sql" />
<EmbeddedResource Include="Repositories\Queries\AccountInCurrency\Delete.sql" />
<EmbeddedResource Include="Repositories\Queries\AccountInCurrency\Insert.sql" />
<EmbeddedResource Include="Repositories\Queries\AccountInCurrency\RestoreDeleted.sql" />
<EmbeddedResource Include="Repositories\Queries\AccountInCurrency\Select.sql" />
<EmbeddedResource Include="Repositories\Queries\Account\Balance.sql" />
<EmbeddedResource Include="Repositories\Queries\Account\Delete.sql" />
Expand Down
16 changes: 16 additions & 0 deletions source/Gnomeshade.Data/Repositories/AccountInCurrencyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using System;
using System.Data.Common;
using System.Threading.Tasks;

using Dapper;

using Gnomeshade.Data.Entities;

Expand Down Expand Up @@ -39,4 +42,17 @@ public AccountInCurrencyRepository(ILogger<AccountInCurrencyRepository> logger,

/// <inheritdoc />
protected override string NotDeleted => "a.deleted_at IS NULL";

public Task RestoreDeletedAsync(Guid id, Guid ownerId)
{
return DbConnection.ExecuteAsync(Queries.AccountInCurrency.RestoreDeleted, new { id, ownerId });
}

public Task RestoreDeletedAsync(Guid id, Guid ownerId, DbTransaction dbTransaction)
{
return DbConnection.ExecuteAsync(
Queries.AccountInCurrency.RestoreDeleted,
new { id, ownerId },
transaction: dbTransaction);
}
}
2 changes: 2 additions & 0 deletions source/Gnomeshade.Data/Repositories/Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ internal static class AccountInCurrency
{
internal static string Delete { get; } = Read($"Queries.{nameof(AccountInCurrency)}.Delete.sql");

internal static string RestoreDeleted { get; } = Read($"Queries.{nameof(AccountInCurrency)}.RestoreDeleted.sql");

internal static string Insert { get; } = Read($"Queries.{nameof(AccountInCurrency)}.Insert.sql");

internal static string Select { get; } = Read($"Queries.{nameof(AccountInCurrency)}.Select.sql");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
WITH a AS (SELECT accounts_in_currency.id
FROM accounts_in_currency
INNER JOIN owners ON owners.id = accounts_in_currency.owner_id
INNER JOIN ownerships ON owners.id = ownerships.owner_id
INNER JOIN access ON access.id = ownerships.access_id
WHERE accounts_in_currency.id = @id
AND ownerships.user_id = @ownerId
AND (access.normalized_name = 'DELETE' OR access.normalized_name = 'OWNER'))
UPDATE accounts_in_currency
SET modified_at = CURRENT_TIMESTAMP,
modified_by_user_id = @ownerId,
deleted_at = NULL,
deleted_by_user_id = NULL
FROM a
WHERE accounts_in_currency.id IN (SELECT id FROM a);
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,7 @@ protected TransactionImportService(
if (deletedCurrency is not null)
{
RestoringDeletedCurrency(otherCurrency.AlphabeticCode, otherAccount.Id);
deletedCurrency.DeletedAt = null;
deletedCurrency.DeletedByUserId = null;
deletedCurrency.ModifiedByUserId = user.Id;

await _inCurrencyRepository.UpdateAsync(deletedCurrency, dbTransaction);
await _inCurrencyRepository.RestoreDeletedAsync(deletedCurrency.Id, user.Id, dbTransaction);
otherAccountCurrency = await _inCurrencyRepository.GetByIdAsync(deletedCurrency.Id, user.Id, dbTransaction);
RestoredDeletedCurrency(otherCurrency.AlphabeticCode, otherAccount.Id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public async Task AddAsync_WithTransaction()
getAccountInCurrency.Should().BeEquivalentTo(expectedAccountInCurrency);
findAccountInCurrency.Should().BeEquivalentTo(expectedAccountInCurrency);

await _inCurrencyRepository.DeleteAsync(firstAccountInCurrency.Id, TestUser.Id);
var deleted = await _inCurrencyRepository.FindByIdAsync(firstAccountInCurrency.Id, TestUser.Id);
deleted.Should().BeNull();

await _inCurrencyRepository.RestoreDeletedAsync(getAccountInCurrency.Id, TestUser.Id);
var restored = await _inCurrencyRepository.GetByIdAsync(firstAccountInCurrency.Id, TestUser.Id);
restored.Should().BeEquivalentTo(getAccountInCurrency, options => options.Excluding(a => a.ModifiedAt));

(await FluentActions.Awaiting(() => _inCurrencyRepository.AddAsync(firstAccountInCurrency))
.Should()
.ThrowAsync<NpgsqlException>())
Expand Down

0 comments on commit 541f615

Please sign in to comment.