Refinery wage claims: status change allowed when the commission claim is new (was: refused whenever one existed)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6027edb98b
commit
90260edd8e
|
|
@ -28,6 +28,8 @@ public interface IWageClaimRepository
|
|||
Task<long> InsertClaimAsync(string createdBy, CancellationToken ct = default);
|
||||
Task<int> AttachTripsAsync(long claimId, IReadOnlyList<string> statuses, DateOnly from, DateOnly to, CancellationToken ct = default);
|
||||
|
||||
/// <summary>STATUS_CODE of a commission claim (null when it does not exist).</summary>
|
||||
Task<string?> CommissionClaimStatusAsync(long commissionClaimId, CancellationToken ct = default);
|
||||
Task SetClaimStatusAsync(long claimId, string status, string userLogin, CancellationToken ct = default);
|
||||
|
||||
Task<IReadOnlyList<TransferTripRow>> TripsToTransferAsync(long claimId, CancellationToken ct = default);
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ public static class ClaimMessages
|
|||
public const string CommissionVariableIssue = "يوجد خطأ في تعريف قيمة عمولة الاجور"; // ERROR.REFINERY_VARIABLES_COMMISSION_ISSUE
|
||||
/// <summary>New text (the legacy code had no such case): the Jopetrol integration is switched off by configuration.</summary>
|
||||
public const string JopetrolDisabled = "خدمة الترحيل إلى المصفاة غير مفعلة في هذه البيئة، لم يتم تغيير أي شيء";
|
||||
/// <summary>New text: a closed claim that already belongs to a commission claim cannot be reopened.</summary>
|
||||
public const string ReopenHasCommission = "لا يمكن اعادة فتح المطالبة لانها مرتبطة بمطالبة عمولة";
|
||||
/// <summary>New text: the status of a wage claim can change only while its commission claim (if any) is still new.</summary>
|
||||
public const string CommissionNotNew = "لا يمكن تغيير حالة المطالبة لان مطالبة العمولة المرتبطة بها ليست بحالة جديد";
|
||||
public const string NotFound ="المطالبة غير موجودة";
|
||||
|
||||
// log captions (method names of the *_LOG tables)
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ public sealed class WageClaimService(IWageClaimRepository repo, IUnitOfWork unit
|
|||
{
|
||||
var claim = await GetAsync(id, ct);
|
||||
if (claim.StatusCode != WageClaimRules.New) throw new BusinessException("NO_AUTHORITY", ClaimMessages.NoAuthority);
|
||||
await EnsureCommissionNewAsync(claim, ct);
|
||||
|
||||
// only the claim record changes: the trips (permits) keep their status
|
||||
await unit.ExecuteInTransactionAsync(async c =>
|
||||
|
|
@ -98,12 +99,20 @@ public sealed class WageClaimService(IWageClaimRepository repo, IUnitOfWork unit
|
|||
return new ClaimResult(ClaimMessages.Success, id);
|
||||
}
|
||||
|
||||
/// <summary>Reopen: CLOSE claim -> NEW (+ log), trips untouched. Refused once a commission claim took the wage claim.</summary>
|
||||
/// <summary>The status of a wage claim changes only when it has no commission claim or that commission claim is NEW.</summary>
|
||||
private async Task EnsureCommissionNewAsync(WageClaimRowDto claim, CancellationToken ct)
|
||||
{
|
||||
if (claim.CommissionClaimId is null) return;
|
||||
if (await repo.CommissionClaimStatusAsync(claim.CommissionClaimId.Value, ct) != WageClaimRules.New)
|
||||
throw new BusinessException("COMMISSION_NOT_NEW", ClaimMessages.CommissionNotNew);
|
||||
}
|
||||
|
||||
/// <summary>Reopen: CLOSE claim -> NEW (+ log), trips untouched. Refused when its commission claim is not NEW.</summary>
|
||||
public async Task<ClaimResult> ReopenAsync(long id, string? ip, CancellationToken ct)
|
||||
{
|
||||
var claim = await GetAsync(id, ct);
|
||||
if (claim.StatusCode != WageClaimRules.Close) throw new BusinessException("NO_AUTHORITY", ClaimMessages.NoAuthority);
|
||||
if (claim.CommissionClaimId is not null) throw new BusinessException("REOPEN_HAS_COMMISSION", ClaimMessages.ReopenHasCommission);
|
||||
await EnsureCommissionNewAsync(claim, ct);
|
||||
|
||||
await unit.ExecuteInTransactionAsync(async c =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -122,6 +122,9 @@ internal sealed class WageClaimRepository(IDbSession session, ILogger<WageClaimR
|
|||
return ExecuteAsync("UPDATE REFINERY_TRIPS t SET t.WAGE_CLAIM_ID = :ClaimId" + CandidateWhere(p, statuses, from, to), p, ct);
|
||||
}
|
||||
|
||||
public async Task<string?> CommissionClaimStatusAsync(long commissionClaimId, CancellationToken ct = default) =>
|
||||
(await QueryAsync<string>("SELECT STATUS_CODE FROM REFINERY_COMMISSION_CLAIM WHERE ID = :Id", new { Id = commissionClaimId }, ct)).FirstOrDefault();
|
||||
|
||||
public Task SetClaimStatusAsync(long claimId, string status, string userLogin, CancellationToken ct = default) =>
|
||||
ExecuteAsync("""
|
||||
UPDATE REFINERY_WAGE_CLAIM SET STATUS_CODE = :Status, LAST_UPDATE_DATE = SYSDATE, UPDATED_BY = :UserLogin WHERE ID = :Id
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ public class WageClaimServiceTests
|
|||
public List<TransferTripRow> ToTransfer = [];
|
||||
public int Candidates = 5;
|
||||
public long? CommissionId;
|
||||
public string? CommissionStatus = "NEW";
|
||||
public readonly List<string> Writes = [];
|
||||
|
||||
public Task<PagedResult<WageClaimRowDto>> SearchAsync(WageClaimQuery query, PageRequest page, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
|
|
@ -64,6 +65,7 @@ public class WageClaimServiceTests
|
|||
public Task<int> CountCandidateTripsAsync(IReadOnlyList<string> statuses, DateOnly from, DateOnly to, CancellationToken ct = default) => Task.FromResult(Candidates);
|
||||
public Task<long> InsertClaimAsync(string createdBy, CancellationToken ct = default) { Writes.Add("insert:" + createdBy); return Task.FromResult(9L); }
|
||||
public Task<int> AttachTripsAsync(long claimId, IReadOnlyList<string> statuses, DateOnly from, DateOnly to, CancellationToken ct = default) { Writes.Add("attach:" + claimId + ":" + string.Join('+', statuses)); return Task.FromResult(Candidates); }
|
||||
public Task<string?> CommissionClaimStatusAsync(long commissionClaimId, CancellationToken ct = default) => Task.FromResult(CommissionStatus);
|
||||
public Task SetClaimStatusAsync(long claimId, string status, string userLogin, CancellationToken ct = default) { Writes.Add("status:" + status); return Task.CompletedTask; }
|
||||
public Task<IReadOnlyList<TransferTripRow>> TripsToTransferAsync(long claimId, CancellationToken ct = default) => Task.FromResult<IReadOnlyList<TransferTripRow>>(ToTransfer);
|
||||
public Task MarkTripTransferredAsync(TransferTripRow trip, long userId, string? ip, CancellationToken ct = default) { Writes.Add("transferred:" + trip.Id); return Task.CompletedTask; }
|
||||
|
|
@ -148,15 +150,33 @@ public class WageClaimServiceTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Reopen_needs_a_closed_claim_without_a_commission_claim()
|
||||
public async Task Reopen_needs_a_closed_claim()
|
||||
{
|
||||
var notClosed = new FakeRepo { ClaimStatus = "NEW" };
|
||||
await Assert.ThrowsAsync<BusinessException>(() => Service(notClosed).ReopenAsync(5, null, default));
|
||||
var taken = new FakeRepo { ClaimStatus = "CLOSE", CommissionId = 3 };
|
||||
var ex = await Assert.ThrowsAsync<BusinessException>(() => Service(taken).ReopenAsync(5, null, default));
|
||||
Assert.Equal("REOPEN_HAS_COMMISSION", ex.Code);
|
||||
Assert.Empty(notClosed.Writes);
|
||||
Assert.Empty(taken.Writes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Status_changes_are_allowed_while_the_commission_claim_is_new()
|
||||
{
|
||||
var repo = new FakeRepo { ClaimStatus = "CLOSE", CommissionId = 3, CommissionStatus = "NEW" };
|
||||
await Service(repo).ReopenAsync(5, null, default);
|
||||
Assert.Contains("status:NEW", repo.Writes);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("CLOSE")]
|
||||
[InlineData("COMPLETE")]
|
||||
public async Task Status_changes_are_refused_once_the_commission_claim_is_not_new(string commissionStatus)
|
||||
{
|
||||
var reopen = new FakeRepo { ClaimStatus = "CLOSE", CommissionId = 3, CommissionStatus = commissionStatus };
|
||||
var ex = await Assert.ThrowsAsync<BusinessException>(() => Service(reopen).ReopenAsync(5, null, default));
|
||||
Assert.Equal("COMMISSION_NOT_NEW", ex.Code);
|
||||
var close = new FakeRepo { ClaimStatus = "NEW", CommissionId = 3, CommissionStatus = commissionStatus };
|
||||
await Assert.ThrowsAsync<BusinessException>(() => Service(close).CloseAsync(5, null, default));
|
||||
Assert.Empty(reopen.Writes);
|
||||
Assert.Empty(close.Writes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in New Issue