Merge pull request #529 from ONLYOFFICE/feature/refactoring-webhooks.core

Feature/refactoring webhooks.core
This commit is contained in:
Alexey Bannov 2022-02-21 22:35:35 +03:00 committed by GitHub
commit 8ac659957b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 403 additions and 416 deletions

View File

@ -9,6 +9,10 @@
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
<PropertyGroup>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="proto\webhook_request.proto" />
</ItemGroup>

View File

@ -1,27 +1,24 @@
namespace ASC.Webhooks.Core.Dao.Models
namespace ASC.Webhooks.Core.Dao.Models;
public class WebhookEntry
{
public class WebhookEntry
public int Id { get; set; }
public string Payload { get; set; }
public string SecretKey { get; set; }
public string Uri { get; set; }
public override bool Equals(object other)
{
public int Id { get; set; }
public string Payload { get; set; }
public string Uri { get; set; }
public string SecretKey { get; set; }
public override bool Equals(object other)
{
var toCompareWith = other as WebhookEntry;
if (toCompareWith == null)
return false;
return this.Id == toCompareWith.Id &&
this.Payload == toCompareWith.Payload &&
this.Uri == toCompareWith.Uri &&
this.SecretKey == toCompareWith.SecretKey;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
var toCompareWith = other as WebhookEntry;
if (toCompareWith == null)
return false;
return Id == toCompareWith.Id &&
Payload == toCompareWith.Payload &&
Uri == toCompareWith.Uri &&
SecretKey == toCompareWith.SecretKey;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}

View File

@ -1,51 +1,49 @@
#nullable disable

namespace ASC.Webhooks.Core.Dao.Models
namespace ASC.Webhooks.Core.Dao.Models;
public partial class WebhooksConfig
{
public partial class WebhooksConfig
{
public int ConfigId { get; set; }
public int TenantId { get; set; }
public string Uri { get; set; }
public string SecretKey { get; set; }
}
public static class WebhooksConfigExtension
{
public static ModelBuilderWrapper AddWebhooksConfig(this ModelBuilderWrapper modelBuilder)
{
modelBuilder
.Add(MySqlAddWebhooksConfig, Provider.MySql);
//.Add(PgSqlAddLoginEvents, Provider.Postgre);
return modelBuilder;
}
public static void MySqlAddWebhooksConfig(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<WebhooksConfig>(entity =>
{
entity.HasKey(e => new { e.ConfigId })
.HasName("PRIMARY");
entity.ToTable("webhooks_config");
entity.Property(e => e.ConfigId)
.HasColumnType("int")
.HasColumnName("config_id");
entity.Property(e => e.TenantId)
.HasColumnName("tenant_id")
.HasColumnType("int unsigned");
entity.Property(e => e.Uri)
.HasMaxLength(50)
.HasColumnName("uri")
.HasDefaultValueSql("''");
entity.Property(e => e.SecretKey)
.HasMaxLength(50)
.HasColumnName("secret_key")
.HasDefaultValueSql("''");
});
}
}
public int ConfigId { get; set; }
public string SecretKey { get; set; }
public int TenantId { get; set; }
public string Uri { get; set; }
}
public static class WebhooksConfigExtension
{
public static ModelBuilderWrapper AddWebhooksConfig(this ModelBuilderWrapper modelBuilder)
{
modelBuilder
.Add(MySqlAddWebhooksConfig, Provider.MySql);
//.Add(PgSqlAddLoginEvents, Provider.Postgre);
return modelBuilder;
}
public static void MySqlAddWebhooksConfig(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<WebhooksConfig>(entity =>
{
entity.HasKey(e => new { e.ConfigId })
.HasName("PRIMARY");
entity.ToTable("webhooks_config");
entity.Property(e => e.ConfigId)
.HasColumnType("int")
.HasColumnName("config_id");
entity.Property(e => e.TenantId)
.HasColumnName("tenant_id")
.HasColumnType("int unsigned");
entity.Property(e => e.Uri)
.HasMaxLength(50)
.HasColumnName("uri")
.HasDefaultValueSql("''");
entity.Property(e => e.SecretKey)
.HasMaxLength(50)
.HasColumnName("secret_key")
.HasDefaultValueSql("''");
});
}
}

View File

@ -1,91 +1,88 @@
#nullable disable
namespace ASC.Webhooks.Core.Dao.Models;
namespace ASC.Webhooks.Core.Dao.Models
public partial class WebhooksLog
{
public partial class WebhooksLog
{
public int Id { get; set; }
public int ConfigId { get; set; }
public string Uid { get; set; }
public int TenantId { get; set; }
public string RequestPayload { get; set; }
public string RequestHeaders { get; set; }
public string ResponsePayload { get; set; }
public string ResponseHeaders { get; set; }
public DateTime CreationTime { get; set; }
public string Event { get; set; }
public ProcessStatus Status { get; set; }
}
public static class WebhooksPayloadExtension
{
public static ModelBuilderWrapper AddWebhooksLog(this ModelBuilderWrapper modelBuilder)
{
modelBuilder
.Add(MySqlAddWebhooksLog, Provider.MySql);
//.Add(PgSqlAddUser, Provider.Postgre)
return modelBuilder;
}
private static void MySqlAddWebhooksLog(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<WebhooksLog>(entity =>
{
entity.HasKey(e => new { e.Id })
.HasName("PRIMARY");
entity.ToTable("webhooks_logs");
entity.Property(e => e.Id)
.HasColumnType("int")
.HasColumnName("id")
.ValueGeneratedOnAdd();
entity.Property(e => e.ConfigId)
.HasColumnType("int")
.HasColumnName("config_id");
entity.Property(e => e.Uid)
.HasColumnType("varchar")
.HasColumnName("uid")
.HasMaxLength(50);
entity.Property(e => e.TenantId)
.HasColumnName("tenant_id")
.HasColumnType("int unsigned");
entity.Property(e => e.RequestPayload)
.IsRequired()
.HasColumnName("request_payload")
.HasColumnType("json");
entity.Property(e => e.RequestHeaders)
.HasColumnName("request_headers")
.HasColumnType("json");
entity.Property(e => e.ResponsePayload)
.HasColumnName("response_payload")
.HasColumnType("json");
entity.Property(e => e.ResponseHeaders)
.HasColumnName("response_headers")
.HasColumnType("json");
entity.Property(e => e.Event)
.HasColumnType("varchar")
.HasColumnName("event")
.HasMaxLength(100);
entity.Property(e => e.CreationTime)
.HasColumnType("datetime")
.HasColumnName("creation_time");
entity.Property(e => e.Status)
.HasColumnType("varchar")
.HasColumnName("status")
.HasMaxLength(50);
});
}
}
public int ConfigId { get; set; }
public DateTime CreationTime { get; set; }
public string Event { get; set; }
public int Id { get; set; }
public string RequestHeaders { get; set; }
public string RequestPayload { get; set; }
public string ResponseHeaders { get; set; }
public string ResponsePayload { get; set; }
public ProcessStatus Status { get; set; }
public int TenantId { get; set; }
public string Uid { get; set; }
}
public static class WebhooksPayloadExtension
{
public static ModelBuilderWrapper AddWebhooksLog(this ModelBuilderWrapper modelBuilder)
{
modelBuilder
.Add(MySqlAddWebhooksLog, Provider.MySql);
//.Add(PgSqlAddUser, Provider.Postgre)
return modelBuilder;
}
private static void MySqlAddWebhooksLog(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<WebhooksLog>(entity =>
{
entity.HasKey(e => new { e.Id })
.HasName("PRIMARY");
entity.ToTable("webhooks_logs");
entity.Property(e => e.Id)
.HasColumnType("int")
.HasColumnName("id")
.ValueGeneratedOnAdd();
entity.Property(e => e.ConfigId)
.HasColumnType("int")
.HasColumnName("config_id");
entity.Property(e => e.Uid)
.HasColumnType("varchar")
.HasColumnName("uid")
.HasMaxLength(50);
entity.Property(e => e.TenantId)
.HasColumnName("tenant_id")
.HasColumnType("int unsigned");
entity.Property(e => e.RequestPayload)
.IsRequired()
.HasColumnName("request_payload")
.HasColumnType("json");
entity.Property(e => e.RequestHeaders)
.HasColumnName("request_headers")
.HasColumnType("json");
entity.Property(e => e.ResponsePayload)
.HasColumnName("response_payload")
.HasColumnType("json");
entity.Property(e => e.ResponseHeaders)
.HasColumnName("response_headers")
.HasColumnType("json");
entity.Property(e => e.Event)
.HasColumnType("varchar")
.HasColumnName("event")
.HasMaxLength(100);
entity.Property(e => e.CreationTime)
.HasColumnType("datetime")
.HasColumnName("creation_time");
entity.Property(e => e.Status)
.HasColumnType("varchar")
.HasColumnName("status")
.HasMaxLength(50);
});
}
}

View File

@ -1,46 +1,43 @@
#nullable disable
namespace ASC.Webhooks.Core.Dao
namespace ASC.Webhooks.Core.Dao;
public class MySqlWebhooksDbContext : WebhooksDbContext { }
public class PostgreSqlWebhooksDbContext : WebhooksDbContext { }
public partial class WebhooksDbContext : BaseDbContext
{
public class MySqlWebhooksDbContext : WebhooksDbContext { }
public class PostgreSqlWebhooksDbContext : WebhooksDbContext { }
public partial class WebhooksDbContext : BaseDbContext
public WebhooksDbContext() { }
public WebhooksDbContext(DbContextOptions<WebhooksDbContext> options)
: base(options)
{
public virtual DbSet<WebhooksConfig> WebhooksConfigs { get; set; }
public virtual DbSet<WebhooksLog> WebhooksLogs { get; set; }
public WebhooksDbContext() { }
public WebhooksDbContext(DbContextOptions<WebhooksDbContext> options)
: base(options)
{
}
}
protected override Dictionary<Provider, Func<BaseDbContext>> ProviderContext
public virtual DbSet<WebhooksConfig> WebhooksConfigs { get; set; }
public virtual DbSet<WebhooksLog> WebhooksLogs { get; set; }
protected override Dictionary<Provider, Func<BaseDbContext>> ProviderContext
{
get
{
get
return new Dictionary<Provider, Func<BaseDbContext>>()
{
return new Dictionary<Provider, Func<BaseDbContext>>()
{
{ Provider.MySql, () => new MySqlWebhooksDbContext() } ,
{ Provider.PostgreSql, () => new PostgreSqlWebhooksDbContext() } ,
};
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ModelBuilderWrapper
.From(modelBuilder, Provider)
.AddWebhooksConfig()
.AddWebhooksLog();
{ Provider.MySql, () => new MySqlWebhooksDbContext() } ,
{ Provider.PostgreSql, () => new PostgreSqlWebhooksDbContext() } ,
};
}
}
public static class WebhooksDbExtension
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
public static DIHelper AddWebhooksDbContextService(this DIHelper services)
{
return services.AddDbContextManagerService<TenantDbContext>();
}
ModelBuilderWrapper
.From(modelBuilder, Provider)
.AddWebhooksConfig()
.AddWebhooksLog();
}
}
public static class WebhooksDbExtension
{
public static DIHelper AddWebhooksDbContextService(this DIHelper services)
{
return services.AddDbContextManagerService<TenantDbContext>();
}
}

View File

@ -1,111 +1,108 @@
namespace ASC.Webhooks.Core
namespace ASC.Webhooks.Core;
[Scope]
public class DbWorker
{
[Scope]
public class DbWorker
private Lazy<WebhooksDbContext> _lazyWebhooksDbContext;
private readonly TenantManager _tenantManager;
private WebhooksDbContext WebhooksDbContext { get => _lazyWebhooksDbContext.Value; }
public DbWorker(DbContextManager<WebhooksDbContext> webhooksDbContext, TenantManager tenantManager)
{
private Lazy<WebhooksDbContext> LazyWebhooksDbContext { get; }
private WebhooksDbContext webhooksDbContext { get => LazyWebhooksDbContext.Value; }
private TenantManager TenantManager { get; }
public DbWorker(DbContextManager<WebhooksDbContext> webhooksDbContext, TenantManager tenantManager)
{
LazyWebhooksDbContext = new Lazy<WebhooksDbContext>(() => webhooksDbContext.Value);
TenantManager = tenantManager;
}
public int WriteToJournal(WebhooksLog webhook)
{
var entity = webhooksDbContext.WebhooksLogs.Add(webhook);
webhooksDbContext.SaveChanges();
return entity.Entity.Id;
}
public WebhookEntry ReadFromJournal(int id)
{
return webhooksDbContext.WebhooksLogs
.Where(it => it.Id == id)
.Join(webhooksDbContext.WebhooksConfigs, t => t.ConfigId, t => t.ConfigId, (payload, config) => new { payload, config })
.Select(t => new WebhookEntry { Id = t.payload.Id, Payload = t.payload.RequestPayload, SecretKey = t.config.SecretKey, Uri = t.config.Uri })
.OrderBy(t => t.Id).FirstOrDefault();
}
public void AddWebhookConfig(WebhooksConfig webhooksConfig)
{
webhooksConfig.TenantId = TenantManager.GetCurrentTenant().TenantId;
var addObj = webhooksDbContext.WebhooksConfigs.Where(it =>
it.SecretKey == webhooksConfig.SecretKey &&
it.TenantId == webhooksConfig.TenantId &&
it.Uri == webhooksConfig.Uri).FirstOrDefault();
if (addObj != null)
return;
webhooksDbContext.WebhooksConfigs.Add(webhooksConfig);
webhooksDbContext.SaveChanges();
}
public void RemoveWebhookConfig(WebhooksConfig webhooksConfig)
{
webhooksConfig.TenantId = TenantManager.GetCurrentTenant().TenantId;
var removeObj = webhooksDbContext.WebhooksConfigs.Where(it =>
it.SecretKey == webhooksConfig.SecretKey &&
it.TenantId == webhooksConfig.TenantId &&
it.Uri == webhooksConfig.Uri).FirstOrDefault();
webhooksDbContext.WebhooksConfigs.Remove(removeObj);
webhooksDbContext.SaveChanges();
}
public void UpdateWebhookConfig(WebhooksConfig webhooksConfig)
{
webhooksConfig.TenantId = TenantManager.GetCurrentTenant().TenantId;
var updateObj = webhooksDbContext.WebhooksConfigs.Where(it =>
it.SecretKey == webhooksConfig.SecretKey &&
it.TenantId == webhooksConfig.TenantId &&
it.Uri == webhooksConfig.Uri).FirstOrDefault();
webhooksDbContext.WebhooksConfigs.Update(updateObj);
webhooksDbContext.SaveChanges();
}
public List<WebhooksConfig> GetWebhookConfigs(int tenant)
{
return webhooksDbContext.WebhooksConfigs.Where(t => t.TenantId == tenant).ToList();
}
public void UpdateWebhookJournal(int id, ProcessStatus status, string responsePayload, string responseHeaders, string requestHeaders)
{
var webhook = webhooksDbContext.WebhooksLogs.Where(t => t.Id == id).FirstOrDefault();
webhook.Status = status;
webhook.ResponsePayload = responsePayload;
webhook.ResponseHeaders = responseHeaders;
webhook.RequestHeaders = requestHeaders;
webhooksDbContext.WebhooksLogs.Update(webhook);
webhooksDbContext.SaveChanges();
}
public List<WebhooksLog> GetTenantWebhooks()
{
var tenant = TenantManager.GetCurrentTenant().TenantId;
return webhooksDbContext.WebhooksLogs.Where(it => it.TenantId == tenant)
.Select(t => new WebhooksLog
{
Uid = t.Uid,
CreationTime = t.CreationTime,
RequestPayload = t.RequestPayload,
RequestHeaders = t.RequestHeaders,
ResponsePayload = t.ResponsePayload,
ResponseHeaders = t.ResponseHeaders,
Status = t.Status
}).ToList();
}
public int ConfigsNumber()
{
return webhooksDbContext.WebhooksConfigs.Count();
}
_lazyWebhooksDbContext = new Lazy<WebhooksDbContext>(() => webhooksDbContext.Value);
_tenantManager = tenantManager;
}
}
public void AddWebhookConfig(WebhooksConfig webhooksConfig)
{
webhooksConfig.TenantId = _tenantManager.GetCurrentTenant().TenantId;
var addObj = WebhooksDbContext.WebhooksConfigs.Where(it =>
it.SecretKey == webhooksConfig.SecretKey &&
it.TenantId == webhooksConfig.TenantId &&
it.Uri == webhooksConfig.Uri).FirstOrDefault();
if (addObj != null)
return;
WebhooksDbContext.WebhooksConfigs.Add(webhooksConfig);
WebhooksDbContext.SaveChanges();
}
public int ConfigsNumber()
{
return WebhooksDbContext.WebhooksConfigs.Count();
}
public List<WebhooksLog> GetTenantWebhooks()
{
var tenant = _tenantManager.GetCurrentTenant().TenantId;
return WebhooksDbContext.WebhooksLogs.Where(it => it.TenantId == tenant)
.Select(t => new WebhooksLog
{
Uid = t.Uid,
CreationTime = t.CreationTime,
RequestPayload = t.RequestPayload,
RequestHeaders = t.RequestHeaders,
ResponsePayload = t.ResponsePayload,
ResponseHeaders = t.ResponseHeaders,
Status = t.Status
}).ToList();
}
public List<WebhooksConfig> GetWebhookConfigs(int tenant)
{
return WebhooksDbContext.WebhooksConfigs.Where(t => t.TenantId == tenant).ToList();
}
public WebhookEntry ReadFromJournal(int id)
{
return WebhooksDbContext.WebhooksLogs
.Where(it => it.Id == id)
.Join(WebhooksDbContext.WebhooksConfigs, t => t.ConfigId, t => t.ConfigId, (payload, config) => new { payload, config })
.Select(t => new WebhookEntry { Id = t.payload.Id, Payload = t.payload.RequestPayload, SecretKey = t.config.SecretKey, Uri = t.config.Uri })
.OrderBy(t => t.Id).FirstOrDefault();
}
public void RemoveWebhookConfig(WebhooksConfig webhooksConfig)
{
webhooksConfig.TenantId = _tenantManager.GetCurrentTenant().TenantId;
var removeObj = WebhooksDbContext.WebhooksConfigs.Where(it =>
it.SecretKey == webhooksConfig.SecretKey &&
it.TenantId == webhooksConfig.TenantId &&
it.Uri == webhooksConfig.Uri).FirstOrDefault();
WebhooksDbContext.WebhooksConfigs.Remove(removeObj);
WebhooksDbContext.SaveChanges();
}
public void UpdateWebhookConfig(WebhooksConfig webhooksConfig)
{
webhooksConfig.TenantId = _tenantManager.GetCurrentTenant().TenantId;
var updateObj = WebhooksDbContext.WebhooksConfigs.Where(it =>
it.SecretKey == webhooksConfig.SecretKey &&
it.TenantId == webhooksConfig.TenantId &&
it.Uri == webhooksConfig.Uri).FirstOrDefault();
WebhooksDbContext.WebhooksConfigs.Update(updateObj);
WebhooksDbContext.SaveChanges();
}
public void UpdateWebhookJournal(int id, ProcessStatus status, string responsePayload, string responseHeaders, string requestHeaders)
{
var webhook = WebhooksDbContext.WebhooksLogs.Where(t => t.Id == id).FirstOrDefault();
webhook.Status = status;
webhook.ResponsePayload = responsePayload;
webhook.ResponseHeaders = responseHeaders;
webhook.RequestHeaders = requestHeaders;
WebhooksDbContext.WebhooksLogs.Update(webhook);
WebhooksDbContext.SaveChanges();
}
public int WriteToJournal(WebhooksLog webhook)
{
var entity = WebhooksDbContext.WebhooksLogs.Add(webhook);
WebhooksDbContext.SaveChanges();
return entity.Entity.Id;
}
}

View File

@ -1,8 +1,7 @@
namespace ASC.Webhooks.Core
namespace ASC.Webhooks.Core;
[Scope]
public interface IWebhookPublisher
{
[Scope]
public interface IWebhookPublisher
{
public void Publish(string eventName, string requestPayload);
}
}
public void Publish(string eventName, string requestPayload);
}

View File

@ -1,68 +1,67 @@
namespace ASC.Webhooks.Core.Migrations
namespace ASC.Webhooks.Core.Migrations;
public partial class WebhooksDbContextMySql : Migration
{
public partial class WebhooksDbContextMySql : Migration
protected override void Down(MigrationBuilder migrationBuilder)
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.DropTable(
name: "webhooks_config");
migrationBuilder.CreateTable(
name: "webhooks_config",
columns: table => new
{
config_id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
tenant_id = table.Column<uint>(type: "int unsigned", nullable: false),
uri = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, defaultValueSql: "''")
.Annotation("MySql:CharSet", "utf8mb4"),
secret_key = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, defaultValueSql: "''")
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PRIMARY", x => x.config_id);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.DropTable(
name: "webhooks_logs");
}
migrationBuilder.CreateTable(
name: "webhooks_logs",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
config_id = table.Column<int>(type: "int", nullable: false),
uid = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
tenant_id = table.Column<uint>(type: "int unsigned", nullable: false),
request_payload = table.Column<string>(type: "json", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
request_headers = table.Column<string>(type: "json", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
response_payload = table.Column<string>(type: "json", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
response_headers = table.Column<string>(type: "json", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
creation_time = table.Column<DateTime>(type: "datetime", nullable: false),
@event = table.Column<string>(name: "event", type: "varchar(100)", maxLength: 100, nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
status = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PRIMARY", x => x.id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:CharSet", "utf8mb4");
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "webhooks_config");
migrationBuilder.CreateTable(
name: "webhooks_config",
columns: table => new
{
config_id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
tenant_id = table.Column<uint>(type: "int unsigned", nullable: false),
uri = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, defaultValueSql: "''")
.Annotation("MySql:CharSet", "utf8mb4"),
secret_key = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, defaultValueSql: "''")
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PRIMARY", x => x.config_id);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.DropTable(
name: "webhooks_logs");
}
migrationBuilder.CreateTable(
name: "webhooks_logs",
columns: table => new
{
id = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
config_id = table.Column<int>(type: "int", nullable: false),
uid = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
tenant_id = table.Column<uint>(type: "int unsigned", nullable: false),
request_payload = table.Column<string>(type: "json", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
request_headers = table.Column<string>(type: "json", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
response_payload = table.Column<string>(type: "json", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
response_headers = table.Column<string>(type: "json", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
creation_time = table.Column<DateTime>(type: "datetime", nullable: false),
@event = table.Column<string>(name: "event", type: "varchar(100)", maxLength: 100, nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
status = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PRIMARY", x => x.id);
})
.Annotation("MySql:CharSet", "utf8mb4");
}
}

View File

@ -1,56 +1,55 @@
namespace ASC.Webhooks.Core
namespace ASC.Webhooks.Core;
[Scope]
public class WebhookPublisher : IWebhookPublisher
{
[Scope]
public class WebhookPublisher : IWebhookPublisher
private readonly DbWorker _dbWorker;
private readonly TenantManager _tenantManager;
private readonly ICacheNotify<WebhookRequest> _webhookNotify;
public WebhookPublisher(
DbWorker dbWorker,
TenantManager tenantManager,
IOptionsMonitor<ILog> options,
ICacheNotify<WebhookRequest> webhookNotify)
{
private DbWorker DbWorker { get; }
private TenantManager TenantManager { get; }
private ICacheNotify<WebhookRequest> WebhookNotify { get; }
public WebhookPublisher(
DbWorker dbWorker,
TenantManager tenantManager,
IOptionsMonitor<ILog> options,
ICacheNotify<WebhookRequest> webhookNotify)
{
DbWorker = dbWorker;
TenantManager = tenantManager;
WebhookNotify = webhookNotify;
}
public void Publish(string eventName, string requestPayload)
{
var tenantId = TenantManager.GetCurrentTenant().TenantId;
var webhookConfigs = DbWorker.GetWebhookConfigs(tenantId);
foreach (var config in webhookConfigs)
{
var webhooksLog = new WebhooksLog
{
Uid = Guid.NewGuid().ToString(),
TenantId = tenantId,
Event = eventName,
CreationTime = DateTime.UtcNow,
RequestPayload = requestPayload,
Status = ProcessStatus.InProcess,
ConfigId = config.ConfigId
};
var DbId = DbWorker.WriteToJournal(webhooksLog);
var request = new WebhookRequest()
{
Id = DbId
};
WebhookNotify.Publish(request, Common.Caching.CacheNotifyAction.Update);
}
}
_dbWorker = dbWorker;
_tenantManager = tenantManager;
_webhookNotify = webhookNotify;
}
public enum ProcessStatus
public void Publish(string eventName, string requestPayload)
{
InProcess,
Success,
Failed
var tenantId = _tenantManager.GetCurrentTenant().TenantId;
var webhookConfigs = _dbWorker.GetWebhookConfigs(tenantId);
foreach (var config in webhookConfigs)
{
var webhooksLog = new WebhooksLog
{
Uid = Guid.NewGuid().ToString(),
TenantId = tenantId,
Event = eventName,
CreationTime = DateTime.UtcNow,
RequestPayload = requestPayload,
Status = ProcessStatus.InProcess,
ConfigId = config.ConfigId
};
var DbId = _dbWorker.WriteToJournal(webhooksLog);
var request = new WebhookRequest()
{
Id = DbId
};
_webhookNotify.Publish(request, CacheNotifyAction.Update);
}
}
}
public enum ProcessStatus
{
InProcess,
Success,
Failed
}