2021-07-08 17:32:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
using ASC.Common;
|
2021-08-24 08:08:58 +00:00
|
|
|
|
using ASC.Core;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
using ASC.Core.Common.EF;
|
2021-08-26 18:43:41 +00:00
|
|
|
|
using ASC.Webhooks.Core.Dao;
|
|
|
|
|
using ASC.Webhooks.Core.Dao.Models;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
|
2021-08-26 18:43:41 +00:00
|
|
|
|
namespace ASC.Webhooks.Core
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
|
|
|
|
[Scope]
|
|
|
|
|
public class DbWorker
|
|
|
|
|
{
|
2021-07-26 17:03:55 +00:00
|
|
|
|
private Lazy<WebhooksDbContext> LazyWebhooksDbContext { get; }
|
|
|
|
|
private WebhooksDbContext webhooksDbContext { get => LazyWebhooksDbContext.Value; }
|
2021-08-24 08:08:58 +00:00
|
|
|
|
private TenantManager TenantManager { get; }
|
2021-08-19 12:56:14 +00:00
|
|
|
|
|
2021-08-24 08:08:58 +00:00
|
|
|
|
public DbWorker(DbContextManager<WebhooksDbContext> webhooksDbContext, TenantManager tenantManager)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2021-07-26 17:03:55 +00:00
|
|
|
|
LazyWebhooksDbContext = new Lazy<WebhooksDbContext>(() => webhooksDbContext.Value);
|
2021-08-24 08:08:58 +00:00
|
|
|
|
TenantManager = tenantManager;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-15 15:45:17 +00:00
|
|
|
|
public int WriteToJournal(WebhooksPayload webhook)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2021-07-26 17:03:55 +00:00
|
|
|
|
var entity = webhooksDbContext.WebhooksPayloads.Add(webhook);
|
|
|
|
|
webhooksDbContext.SaveChanges();
|
2021-07-15 15:45:17 +00:00
|
|
|
|
return entity.Entity.Id;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:26:28 +00:00
|
|
|
|
public WebhookEntry ReadFromJournal(int id)
|
|
|
|
|
{
|
|
|
|
|
return webhooksDbContext.WebhooksPayloads
|
|
|
|
|
.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, Data = t.payload.Data, SecretKey = t.config.SecretKey, Uri = t.config.Uri })
|
|
|
|
|
.OrderBy(t => t.Id).FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 08:40:20 +00:00
|
|
|
|
public void AddWebhookConfig(WebhooksConfig webhooksConfig)
|
|
|
|
|
{
|
2021-08-24 08:08:58 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2021-07-26 17:03:55 +00:00
|
|
|
|
webhooksDbContext.WebhooksConfigs.Add(webhooksConfig);
|
|
|
|
|
webhooksDbContext.SaveChanges();
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-24 08:08:58 +00:00
|
|
|
|
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)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2021-08-24 08:08:58 +00:00
|
|
|
|
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();
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
2021-07-08 17:32:59 +00:00
|
|
|
|
|
|
|
|
|
public List<WebhooksConfig> GetWebhookConfigs(int tenant)
|
|
|
|
|
{
|
2021-07-26 17:03:55 +00:00
|
|
|
|
return webhooksDbContext.WebhooksConfigs.Where(t => t.TenantId == tenant).ToList();
|
2021-07-08 17:32:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateStatus(int id, ProcessStatus status)
|
|
|
|
|
{
|
2021-07-26 17:03:55 +00:00
|
|
|
|
var webhook = webhooksDbContext.WebhooksPayloads.Where(t => t.Id == id).FirstOrDefault();
|
2021-07-08 17:32:59 +00:00
|
|
|
|
webhook.Status = status;
|
2021-07-26 17:03:55 +00:00
|
|
|
|
webhooksDbContext.WebhooksPayloads.Update(webhook);
|
|
|
|
|
webhooksDbContext.SaveChanges();
|
2021-07-15 15:45:17 +00:00
|
|
|
|
}
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
|
|
|
|
public int ConfigsNumber()
|
|
|
|
|
{
|
|
|
|
|
return webhooksDbContext.WebhooksConfigs.Count();
|
|
|
|
|
}
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|