diff --git a/EnvelopeGenerator.Domain/Interfaces/IEnvelope.cs b/EnvelopeGenerator.Domain/Interfaces/IEnvelope.cs index 22254dea..24a8d282 100644 --- a/EnvelopeGenerator.Domain/Interfaces/IEnvelope.cs +++ b/EnvelopeGenerator.Domain/Interfaces/IEnvelope.cs @@ -1,4 +1,11 @@ -namespace EnvelopeGenerator.Domain.Interfaces +using EnvelopeGenerator.Domain.Constants; +using EnvelopeGenerator.Domain.Exceptions; +#if NETFRAMEWORK +using System; +using System.Linq; +#endif + +namespace EnvelopeGenerator.Domain.Interfaces { public interface IEnvelope { @@ -27,5 +34,37 @@ { return envelope.DocResult != null; } + + public static EnvelopeType Type(this IEnvelope envelope) + { + if (!Enum.IsDefined(typeof(EnvelopeType), envelope.EnvelopeTypeId)) + { +#if NETFRAMEWORK + var values = Enum.GetValues(typeof(EnvelopeType)).Cast(); +#else + var values = Enum.GetValues(); +#endif + var validValues = string.Join(", ", values.Select(value => $"{(int)value} = {value}")); + + throw new DataIntegrityException( + $"Invalid EnvelopeTypeId '{envelope.EnvelopeTypeId}' for envelope. " + + $"Valid {nameof(EnvelopeType)} values are: [{validValues}]. " + + "This indicates a data integrity issue: the envelope may have been " + + "created or updated with an invalid EnvelopeTypeId in the database."); + } + + return (EnvelopeType)Enum.ToObject(typeof(EnvelopeType), envelope.EnvelopeTypeId); + } + + public static void SetType(this IEnvelope envelope, EnvelopeType type) + { + envelope.EnvelopeTypeId = (int)type; + } + + public static TEnvelope WithType(this TEnvelope envelope, EnvelopeType type) where TEnvelope : IEnvelope + { + envelope.SetType(type); + return envelope; + } } } \ No newline at end of file