| | 1 | | namespace Syki.Back.Features.Academic.CallWebhooks; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Chamada de Webhook |
| | 5 | | /// </summary> |
| | 6 | | public class WebhookCall : Entity |
| | 7 | | { |
| 0 | 8 | | public Guid Id { get; set; } |
| 0 | 9 | | public Guid InstitutionId { get; set; } |
| 0 | 10 | | public Guid WebhookId { get; set; } |
| 0 | 11 | | public string Payload { get; set; } |
| 0 | 12 | | public WebhookEventType Event { get; set; } |
| 0 | 13 | | public WebhookCallStatus Status { get; set; } |
| 0 | 14 | | public int AttemptsCount { get; set; } |
| 0 | 15 | | public DateTime CreatedAt { get; set; } |
| 0 | 16 | | public List<WebhookCallAttempt> Attempts { get; set; } |
| | 17 | |
|
| 0 | 18 | | private WebhookCall() { } |
| | 19 | |
|
| 0 | 20 | | public WebhookCall( |
| 0 | 21 | | Guid institutionId, |
| 0 | 22 | | Guid webhookId, |
| 0 | 23 | | object data, |
| 0 | 24 | | DomainEventId eventId, |
| 0 | 25 | | WebhookEventType eventType) |
| | 26 | | { |
| 0 | 27 | | Id = Guid.CreateVersion7(); |
| 0 | 28 | | InstitutionId = institutionId; |
| 0 | 29 | | WebhookId = webhookId; |
| 0 | 30 | | Payload = (new { EventId = eventId, EventType = eventType, Data = data }).Serialize(); |
| 0 | 31 | | Event = eventType; |
| 0 | 32 | | CreatedAt = DateTime.UtcNow; |
| | 33 | |
|
| 0 | 34 | | AddDomainEvent(new WebhookCallCreatedDomainEvent(Id)); |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Success(int statusCode, string response) |
| | 38 | | { |
| 0 | 39 | | AttemptsCount++; |
| 0 | 40 | | Status = WebhookCallStatus.Success; |
| 0 | 41 | | Attempts.Add(new WebhookCallAttempt(Id, WebhookCallAttemptStatus.Success, statusCode, response)); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Failed(int statusCode, string response) |
| | 45 | | { |
| 0 | 46 | | Status = WebhookCallStatus.Error; |
| 0 | 47 | | Attempts.Add(new WebhookCallAttempt(Id, WebhookCallAttemptStatus.Error, statusCode, response)); |
| | 48 | |
|
| 0 | 49 | | AttemptsCount++; |
| | 50 | |
|
| 0 | 51 | | if (AttemptsCount < 4) |
| | 52 | | { |
| 0 | 53 | | var delaySeconds = AttemptsCount switch |
| 0 | 54 | | { |
| 0 | 55 | | 1 => 1 * 60, |
| 0 | 56 | | 2 => 5 * 60, |
| 0 | 57 | | 3 => 30 * 60, |
| 0 | 58 | | _ => 0 |
| 0 | 59 | | }; |
| | 60 | |
|
| 0 | 61 | | AddDomainEvent(new WebhookCallFailedDomainEvent(Id, delaySeconds)); |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public GetWebhookCallOut ToGetWebhookCallOut() |
| | 66 | | { |
| 0 | 67 | | return new GetWebhookCallOut |
| 0 | 68 | | { |
| 0 | 69 | | Id = Id, |
| 0 | 70 | | Event = Event, |
| 0 | 71 | | Status = Status, |
| 0 | 72 | | CreatedAt = CreatedAt, |
| 0 | 73 | | AttemptsCount = AttemptsCount, |
| 0 | 74 | | }; |
| | 75 | | } |
| | 76 | |
|
| | 77 | |
|
| | 78 | | public GetWebhookCallFullOut ToGetWebhookCallFullOut(string webhookName) |
| | 79 | | { |
| 0 | 80 | | return new() |
| 0 | 81 | | { |
| 0 | 82 | | Id = Id, |
| 0 | 83 | | Event = Event, |
| 0 | 84 | | Status = Status, |
| 0 | 85 | | Payload = Payload, |
| 0 | 86 | | CreatedAt = CreatedAt, |
| 0 | 87 | | WebhookId = WebhookId, |
| 0 | 88 | | WebhookName = webhookName, |
| 0 | 89 | | AttemptsCount = AttemptsCount, |
| 0 | 90 | | Attempts = Attempts.OrderByDescending(x => x.CreatedAt).Select(x => x.ToOut()).ToList(), |
| 0 | 91 | | }; |
| | 92 | | } |
| | 93 | | } |