using System.Security.Cryptography; using System.Text; using EventFlow.Core; namespace Books.Api.Domain.Accounts; public class AccountId(string value) : Identity(value) { /// /// Creates a deterministic AccountId based on company and account number. /// This prevents duplicate accounts even with race conditions. /// Uses SHA256 to generate a deterministic GUID from the input. /// public static AccountId FromCompanyAndNumber(string companyId, string accountNumber) { // Create a deterministic GUID from the company ID and account number var input = $"{companyId}:{accountNumber}"; var hash = SHA256.HashData(Encoding.UTF8.GetBytes(input)); // Use the first 16 bytes of the hash to create a GUID var guidBytes = new byte[16]; Array.Copy(hash, guidBytes, 16); // Set version (4) and variant bits to make it a valid UUID guidBytes[6] = (byte)((guidBytes[6] & 0x0F) | 0x40); // Version 4 guidBytes[8] = (byte)((guidBytes[8] & 0x3F) | 0x80); // Variant 1 var deterministicGuid = new Guid(guidBytes); return new AccountId($"account-{deterministicGuid:D}"); } }