| | | 1 | | using StronglyTypedIds; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | |
| | | 4 | | namespace Syki.Back.Commands; |
| | | 5 | | |
| | | 6 | | public class Command |
| | | 7 | | { |
| | | 8 | | public CommandId Id { get; set; } |
| | | 9 | | public Guid InstitutionId { get; set; } |
| | | 10 | | public string Type { get; set; } |
| | | 11 | | public string Data { get; set; } |
| | | 12 | | public CommandStatus Status { get; set; } |
| | | 13 | | public DateTime CreatedAt { get; set; } |
| | | 14 | | public int Duration { get; set; } |
| | | 15 | | public DateTime? ProcessedAt { get; set; } |
| | | 16 | | public Guid? ProcessorId { get; set; } |
| | | 17 | | public string? Error { get; set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Id do evento que gerou o comando |
| | | 21 | | /// </summary> |
| | | 22 | | public DomainEventId? EventId { get; set; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Id do comando que gerou o comando |
| | | 26 | | /// Utilizado quando um comando gera outro em seu handler |
| | | 27 | | /// </summary> |
| | | 28 | | public CommandId? ParentId { get; set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Id do comando com erro que gerou o comando atual |
| | | 32 | | /// Utilizado quando o comando original está com erro e é reprocessado |
| | | 33 | | /// O comando atual é uma cópia do original (imutabilidade) |
| | | 34 | | /// </summary> |
| | | 35 | | public CommandId? OriginalId { get; set; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Id do lote que contém o comando |
| | | 39 | | /// </summary> |
| | | 40 | | public CommandBatchId? BatchId { get; set; } |
| | | 41 | | |
| | | 42 | | public DateTime? NotBefore { get; set; } |
| | | 43 | | |
| | | 44 | | public string? ActivityId { get; set; } |
| | | 45 | | |
| | | 46 | | public Command() { } |
| | | 47 | | |
| | | 48 | | public Command( |
| | | 49 | | Guid institutionId, |
| | | 50 | | object data, |
| | | 51 | | DomainEventId? eventId = null, |
| | | 52 | | CommandId? parentId = null, |
| | | 53 | | CommandId? originalId = null, |
| | | 54 | | CommandBatchId? batchId = null, |
| | | 55 | | int? delaySeconds = null, |
| | | 56 | | string? activityId = null |
| | | 57 | | ) { |
| | | 58 | | Id = CommandId.CreateNew(); |
| | | 59 | | InstitutionId = institutionId; |
| | | 60 | | Type = data.GetType().ToString(); |
| | | 61 | | Data = data.Serialize(); |
| | | 62 | | CreatedAt = DateTime.UtcNow; |
| | | 63 | | EventId = eventId; |
| | | 64 | | ParentId = parentId; |
| | | 65 | | OriginalId = originalId; |
| | | 66 | | BatchId = batchId; |
| | | 67 | | ActivityId = activityId; |
| | | 68 | | NotBefore = delaySeconds != null ? DateTime.UtcNow.AddSeconds(delaySeconds.Value) : null; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public void SetAwaiting() |
| | | 72 | | { |
| | | 73 | | Status = CommandStatus.Awaiting; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | public void Processed(double duration) |
| | | 77 | | { |
| | | 78 | | ProcessedAt = DateTime.UtcNow; |
| | | 79 | | Duration = Convert.ToInt32(duration); |
| | | 80 | | Status = Error.HasValue() ? CommandStatus.Error : CommandStatus.Success; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public ActivityContext GetParentContext() |
| | | 84 | | { |
| | | 85 | | ActivityContext.TryParse(ActivityId, null, out var parsedContext); |
| | | 86 | | return parsedContext; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | [StronglyTypedId] |
| | | 91 | | public partial struct CommandId |
| | | 92 | | { |
| | | 93 | | public static CommandId CreateNew() |
| | | 94 | | { |
| | 2830 | 95 | | return new CommandId(Guid.CreateVersion7()); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public class CommandIdEfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<Co |
| | | 99 | | { |
| | 20 | 100 | | public CommandIdEfCoreValueConverter() : this(null) { } |
| | | 101 | | |
| | | 102 | | public CommandIdEfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints |
| | 10 | 103 | | : base( |
| | 10 | 104 | | id => id.Value, |
| | 10 | 105 | | value => new CommandId(value), |
| | 10 | 106 | | mappingHints |
| | 10 | 107 | | ) |
| | 10 | 108 | | { } |
| | | 109 | | } |
| | | 110 | | } |