|
| 1 | +using HrPayroll.Api.Data; |
| 2 | +using HrPayroll.Api.Data.Entities; |
| 3 | +using Microsoft.EntityFrameworkCore; |
| 4 | + |
| 5 | +namespace HrPayroll.Api.Tests.Helpers; |
| 6 | + |
| 7 | +/// <summary>Creates finance rows directly in the test database.</summary> |
| 8 | +public static class TestFinance |
| 9 | +{ |
| 10 | + /// <summary>Today on UAE time, which is what the API treats as "today".</summary> |
| 11 | + public static DateOnly Today => DateOnly.FromDateTime(DateTime.UtcNow.AddHours(4)); |
| 12 | + |
| 13 | + public static string Unique(string prefix = "Acct") => $"{prefix} {Guid.NewGuid():N}"[..30]; |
| 14 | + |
| 15 | + public static async Task<FinAccount> AddAccountAsync( |
| 16 | + string connectionString, string? name = null, decimal opening = 1000m, DateOnly? openingDate = null, |
| 17 | + string kind = FinAccountKinds.Bank, bool isActive = true, string? accountNumber = null, string? iban = null) |
| 18 | + { |
| 19 | + var now = DateTime.UtcNow; |
| 20 | + var account = new FinAccount |
| 21 | + { |
| 22 | + Name = name ?? Unique(), Kind = kind, OpeningBalance = opening, OpeningDate = openingDate ?? Today.AddDays(-60), |
| 23 | + IsActive = isActive, AccountNumber = accountNumber, Iban = iban, CreatedUtc = now, UpdatedUtc = now, |
| 24 | + }; |
| 25 | + await using var db = TestUsers.NewContext(connectionString); |
| 26 | + db.FinAccounts.Add(account); |
| 27 | + await db.SaveChangesAsync(); |
| 28 | + return account; |
| 29 | + } |
| 30 | + |
| 31 | + public static async Task<FinEntry> AddEntryAsync( |
| 32 | + string connectionString, int accountId, string kind, decimal amount, DateOnly? date = null, |
| 33 | + string status = FinStatuses.Active, int? categoryId = null, string party = "Test party", string createdBy = "tester") |
| 34 | + { |
| 35 | + var now = DateTime.UtcNow; |
| 36 | + var entry = new FinEntry |
| 37 | + { |
| 38 | + Kind = kind, EntryNo = (kind == FinEntryKinds.Income ? "INC-" : "EXP-") + Guid.NewGuid().ToString("N")[..12], |
| 39 | + EntryDate = date ?? Today.AddDays(-5), CategoryId = categoryId ?? (kind == FinEntryKinds.Income ? 10 : 1), |
| 40 | + Party = party, AccountId = accountId, Amount = amount, Status = status, |
| 41 | + VoidedUtc = status == FinStatuses.Voided ? now : null, VoidedBy = status == FinStatuses.Voided ? "tester" : null, |
| 42 | + CreatedBy = createdBy, CreatedUtc = now, |
| 43 | + }; |
| 44 | + entry.Lines.Add(new FinEntryLine { CategoryId = entry.CategoryId, Amount = amount }); // an ordinary entry is one line |
| 45 | + await using var db = TestUsers.NewContext(connectionString); |
| 46 | + db.FinEntries.Add(entry); |
| 47 | + await db.SaveChangesAsync(); |
| 48 | + return entry; |
| 49 | + } |
| 50 | + |
| 51 | + public static async Task<FinTransfer> AddTransferAsync( |
| 52 | + string connectionString, int fromId, int toId, decimal amount, DateOnly? date = null, string status = FinStatuses.Active) |
| 53 | + { |
| 54 | + var now = DateTime.UtcNow; |
| 55 | + var transfer = new FinTransfer |
| 56 | + { |
| 57 | + TransferNo = "TRF-" + Guid.NewGuid().ToString("N")[..12], TransferDate = date ?? Today.AddDays(-3), |
| 58 | + FromAccountId = fromId, ToAccountId = toId, Amount = amount, Status = status, |
| 59 | + VoidedUtc = status == FinStatuses.Voided ? now : null, CreatedBy = "tester", CreatedUtc = now, |
| 60 | + }; |
| 61 | + await using var db = TestUsers.NewContext(connectionString); |
| 62 | + db.FinTransfers.Add(transfer); |
| 63 | + await db.SaveChangesAsync(); |
| 64 | + return transfer; |
| 65 | + } |
| 66 | + |
| 67 | + public static async Task<FinPartner> AddPartnerAsync(string connectionString, string? name = null, decimal opening = 0m, bool isActive = true) |
| 68 | + { |
| 69 | + var now = DateTime.UtcNow; |
| 70 | + var partner = new FinPartner { Name = name ?? Unique("Partner"), OpeningCapital = opening, IsActive = isActive, CreatedUtc = now, UpdatedUtc = now }; |
| 71 | + await using var db = TestUsers.NewContext(connectionString); |
| 72 | + db.FinPartners.Add(partner); |
| 73 | + await db.SaveChangesAsync(); |
| 74 | + return partner; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary>Capital put in (<see cref="FinEntryKinds.Capital"/>) or paid back (<see cref="FinEntryKinds.CapitalOut"/>) by a partner.</summary> |
| 78 | + public static async Task<FinEntry> AddCapitalAsync( |
| 79 | + string connectionString, int accountId, int partnerId, decimal amount, DateOnly? date = null, string kind = FinEntryKinds.Capital, string status = FinStatuses.Active) |
| 80 | + { |
| 81 | + var now = DateTime.UtcNow; |
| 82 | + await using var db = TestUsers.NewContext(connectionString); |
| 83 | + var partner = await db.FinPartners.AsNoTracking().SingleAsync(p => p.Id == partnerId); |
| 84 | + var entry = new FinEntry |
| 85 | + { |
| 86 | + Kind = kind, EntryNo = (kind == FinEntryKinds.Capital ? "CAP-" : "RTN-") + Guid.NewGuid().ToString("N")[..12], |
| 87 | + EntryDate = date ?? Today.AddDays(-5), CategoryId = kind == FinEntryKinds.Capital ? 11 : 12, Party = partner.Name, PartnerId = partnerId, |
| 88 | + AccountId = accountId, Amount = amount, Status = status, |
| 89 | + VoidedUtc = status == FinStatuses.Voided ? now : null, VoidedBy = status == FinStatuses.Voided ? "tester" : null, CreatedBy = "tester", CreatedUtc = now, |
| 90 | + }; |
| 91 | + db.FinEntries.Add(entry); |
| 92 | + await db.SaveChangesAsync(); |
| 93 | + return entry; |
| 94 | + } |
| 95 | + |
| 96 | + public static async Task<List<AuditLog>> AuditAsync(string connectionString, string action, string? entityId = null) |
| 97 | + { |
| 98 | + await using var db = TestUsers.NewContext(connectionString); |
| 99 | + return await db.AuditLogs.AsNoTracking().Where(a => a.Action == action && (entityId == null || a.EntityId == entityId)).ToListAsync(); |
| 100 | + } |
| 101 | +} |
0 commit comments