using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Domain.Exceptions; #if NETFRAMEWORK using System; using System.Linq; #endif namespace EnvelopeGenerator.Domain.Interfaces { public interface IEnvelope { int? EnvelopeTypeId { get; set; } byte[] #if nullable ? #endif DocResult { get; } } public static class EnvelopeExtensions { public static bool IsReadAndConfirm(this IEnvelope envelope) { return envelope.EnvelopeTypeId == 2; } public static bool IsReadAndSign(this IEnvelope envelope) { return envelope.EnvelopeTypeId != 2; } public static bool IsBurned(this IEnvelope envelope) { 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; } } }