File size: 2,069 Bytes
de8b914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using SCM.Shared.Exceptions;

namespace SCM.Domain.Qualidade;

/// <summary>
/// Agente de qualidade (inspetor/fiscal CQ ou produção). Migrado de MCQ/AgenteController (cl_fiscalCQ).
/// Invariantes: código e nome obrigatórios; CPF, quando informado, deve ser válido (armazenado só com dígitos).
/// </summary>
public sealed class Agente
{
    public const int MaxCodigo = 50;
    public const int MaxNome = 200;

    public Guid Id { get; set; }
    public string Codigo { get; set; } = string.Empty;
    public string Nome { get; set; } = string.Empty;
    public AgenteTipo Tipo { get; set; }
    public string? Cpf { get; set; }
    public string? Email { get; set; }

    public static Agente Create(string codigo, string nome, AgenteTipo tipo, string? cpf, string? email)
        => new()
        {
            Id = Guid.NewGuid(),
            Codigo = NormalizeCodigo(codigo),
            Nome = NormalizeNome(nome),
            Tipo = tipo,
            Cpf = NormalizeCpf(cpf),
            Email = TrimOrNull(email)
        };

    public void Atualizar(string codigo, string nome, AgenteTipo tipo, string? cpf, string? email)
    {
        Codigo = NormalizeCodigo(codigo);
        Nome = NormalizeNome(nome);
        Tipo = tipo;
        Cpf = NormalizeCpf(cpf);
        Email = TrimOrNull(email);
    }

    private static string NormalizeCodigo(string v)
    {
        if (string.IsNullOrWhiteSpace(v)) throw new DomainException("O código do agente é obrigatório.");
        return v.Trim();
    }

    private static string NormalizeNome(string v)
    {
        if (string.IsNullOrWhiteSpace(v)) throw new DomainException("O nome do agente é obrigatório.");
        return v.Trim();
    }

    private static string? NormalizeCpf(string? cpf)
    {
        if (string.IsNullOrWhiteSpace(cpf)) return null;
        if (!CpfValidator.IsValid(cpf)) throw new DomainException("CPF inválido.");
        return CpfValidator.OnlyDigits(cpf);
    }

    private static string? TrimOrNull(string? v) => string.IsNullOrWhiteSpace(v) ? null : v.Trim();
}