< Summary - Syki

Information
Class: Syki.Back.Features.Academic.CallWebhooks.WebhookCall
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Features/Academic/CallWebhooks/WebhookCall.cs
Tag: 4_16869239191
Line coverage
0%
Covered lines: 0
Uncovered lines: 61
Coverable lines: 61
Total lines: 93
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Id()100%210%
get_InstitutionId()100%210%
get_WebhookId()100%210%
get_Payload()100%210%
get_Event()100%210%
get_Status()100%210%
get_AttemptsCount()100%210%
get_CreatedAt()100%210%
get_Attempts()100%210%
.ctor()100%210%
.ctor(...)100%210%
Success(...)100%210%
Failed(...)0%4260%
ToGetWebhookCallOut()100%210%
ToGetWebhookCallFullOut(...)100%210%

File(s)

/home/runner/work/syki/syki/Back/Features/Academic/CallWebhooks/WebhookCall.cs

#LineLine coverage
 1namespace Syki.Back.Features.Academic.CallWebhooks;
 2
 3/// <summary>
 4/// Chamada de Webhook
 5/// </summary>
 6public class WebhookCall : Entity
 7{
 08    public Guid Id { get; set; }
 09    public Guid InstitutionId { get; set; }
 010    public Guid WebhookId { get; set; }
 011    public string Payload { get; set; }
 012    public WebhookEventType Event { get; set; }
 013    public WebhookCallStatus Status { get; set; }
 014    public int AttemptsCount { get; set; }
 015    public DateTime CreatedAt { get; set; }
 016    public List<WebhookCallAttempt> Attempts { get; set; }
 17
 018    private WebhookCall() { }
 19
 020    public WebhookCall(
 021        Guid institutionId,
 022        Guid webhookId,
 023        object data,
 024        DomainEventId eventId,
 025        WebhookEventType eventType)
 26    {
 027        Id = Guid.CreateVersion7();
 028        InstitutionId = institutionId;
 029        WebhookId = webhookId;
 030        Payload = (new { EventId = eventId, EventType = eventType, Data = data }).Serialize();
 031        Event = eventType;
 032        CreatedAt = DateTime.UtcNow;
 33
 034        AddDomainEvent(new WebhookCallCreatedDomainEvent(Id));
 035    }
 36
 37    public void Success(int statusCode, string response)
 38    {
 039        AttemptsCount++;
 040        Status = WebhookCallStatus.Success;
 041        Attempts.Add(new WebhookCallAttempt(Id, WebhookCallAttemptStatus.Success, statusCode, response));
 042    }
 43
 44    public void Failed(int statusCode, string response)
 45    {
 046        Status = WebhookCallStatus.Error;
 047        Attempts.Add(new WebhookCallAttempt(Id, WebhookCallAttemptStatus.Error, statusCode, response));
 48
 049        AttemptsCount++;
 50
 051        if (AttemptsCount < 4)
 52        {
 053            var delaySeconds = AttemptsCount switch
 054            {
 055                1 => 1 * 60,
 056                2 => 5 * 60,
 057                3 => 30 * 60,
 058                _ => 0
 059            };
 60
 061            AddDomainEvent(new WebhookCallFailedDomainEvent(Id, delaySeconds));
 62        }
 063    }
 64
 65    public GetWebhookCallOut ToGetWebhookCallOut()
 66    {
 067        return new GetWebhookCallOut
 068        {
 069            Id = Id,
 070            Event = Event,
 071            Status = Status,
 072            CreatedAt = CreatedAt,
 073            AttemptsCount = AttemptsCount,
 074        };
 75    }
 76
 77
 78    public GetWebhookCallFullOut ToGetWebhookCallFullOut(string webhookName)
 79    {
 080        return new()
 081        {
 082            Id = Id,
 083            Event = Event,
 084            Status = Status,
 085            Payload = Payload,
 086            CreatedAt = CreatedAt,
 087            WebhookId = WebhookId,
 088            WebhookName = webhookName,
 089            AttemptsCount = AttemptsCount,
 090            Attempts = Attempts.OrderByDescending(x => x.CreatedAt).Select(x => x.ToOut()).ToList(),
 091        };
 92    }
 93}