Introduced new extension methods for the `IEnvelope` interface: - Added `Type` method to retrieve and validate `EnvelopeTypeId`, throwing a `DataIntegrityException` for invalid values. - Added `SetType` method to set the `EnvelopeTypeId` based on a given `EnvelopeType`. - Added `WithType` method to enable setting `EnvelopeTypeId` with method chaining. Included conditional compilation for .NET Framework compatibility and added necessary `using` directives for constants and exceptions.
70 lines
2.1 KiB
C#
70 lines
2.1 KiB
C#
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<EnvelopeType>();
|
|
#else
|
|
var values = Enum.GetValues<EnvelopeType>();
|
|
#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<TEnvelope>(this TEnvelope envelope, EnvelopeType type) where TEnvelope : IEnvelope
|
|
{
|
|
envelope.SetType(type);
|
|
return envelope;
|
|
}
|
|
}
|
|
} |