2023-05-31 09:04:20 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
|
// any third-party rights.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
|
2023-02-21 11:22:34 +00:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
namespace ASC.Webhooks.Core;
|
|
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
|
public class DbWorker
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2023-05-06 16:55:12 +00:00
|
|
|
|
public static readonly IReadOnlyList<string> MethodList = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"POST",
|
|
|
|
|
"PUT",
|
|
|
|
|
"DELETE"
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-28 18:00:49 +00:00
|
|
|
|
private readonly IDbContextFactory<WebhooksDbContext> _dbContextFactory;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
private readonly TenantManager _tenantManager;
|
|
|
|
|
private readonly AuthContext _authContext;
|
2023-02-21 11:22:34 +00:00
|
|
|
|
private readonly IMapper _mapper;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
|
|
|
|
|
private int Tenant
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _tenantManager.GetCurrentTenant().Id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-21 11:22:34 +00:00
|
|
|
|
public DbWorker(
|
|
|
|
|
IDbContextFactory<WebhooksDbContext> dbContextFactory,
|
|
|
|
|
TenantManager tenantManager,
|
|
|
|
|
AuthContext authContext,
|
2023-05-31 09:04:20 +00:00
|
|
|
|
IMapper mapper)
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
|
|
|
|
_dbContextFactory = dbContextFactory;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
_tenantManager = tenantManager;
|
|
|
|
|
_authContext = authContext;
|
2023-02-21 11:22:34 +00:00
|
|
|
|
_mapper = mapper;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
2023-04-14 16:31:36 +00:00
|
|
|
|
public async Task<WebhooksConfig> AddWebhookConfig(string uri, string name, string secretKey, bool? enabled, bool? ssl)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-01-30 16:31:31 +00:00
|
|
|
|
|
2023-06-29 08:32:05 +00:00
|
|
|
|
var objForCreate = await Queries.WebhooksConfigByUriAsync(webhooksDbContext, Tenant, uri, name);
|
2023-01-30 16:31:31 +00:00
|
|
|
|
|
2023-02-21 11:22:34 +00:00
|
|
|
|
if (objForCreate != null)
|
2023-01-30 16:31:31 +00:00
|
|
|
|
{
|
|
|
|
|
return objForCreate;
|
2023-05-31 09:04:20 +00:00
|
|
|
|
}
|
2022-08-18 09:03:13 +00:00
|
|
|
|
|
2023-02-27 13:39:52 +00:00
|
|
|
|
var toAdd = new WebhooksConfig
|
|
|
|
|
{
|
|
|
|
|
TenantId = Tenant,
|
|
|
|
|
Uri = uri,
|
2023-03-14 16:27:12 +00:00
|
|
|
|
SecretKey = secretKey,
|
2023-04-14 16:31:36 +00:00
|
|
|
|
Name = name,
|
|
|
|
|
Enabled = enabled ?? true,
|
|
|
|
|
SSL = ssl ?? true
|
2023-02-27 13:39:52 +00:00
|
|
|
|
};
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
|
|
|
|
toAdd = await webhooksDbContext.AddOrUpdateAsync(r => r.WebhooksConfigs, toAdd);
|
2022-08-18 09:03:13 +00:00
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
return toAdd;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 07:53:53 +00:00
|
|
|
|
public async IAsyncEnumerable<WebhooksConfigWithStatus> GetTenantWebhooksWithStatus()
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
|
|
|
|
|
2023-07-10 07:53:53 +00:00
|
|
|
|
var q = Queries.WebhooksConfigWithStatusAsync(webhooksDbContext, Tenant);
|
|
|
|
|
|
|
|
|
|
await foreach (var webhook in q)
|
|
|
|
|
{
|
|
|
|
|
yield return webhook;
|
|
|
|
|
}
|
2023-06-27 15:29:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-10 07:53:53 +00:00
|
|
|
|
public async IAsyncEnumerable<WebhooksConfig> GetWebhookConfigs()
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2022-08-17 09:18:50 +00:00
|
|
|
|
var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-06-27 15:29:23 +00:00
|
|
|
|
|
2023-07-10 07:53:53 +00:00
|
|
|
|
var q = Queries.WebhooksConfigsAsync(webhooksDbContext, Tenant);
|
|
|
|
|
|
|
|
|
|
await foreach (var webhook in q)
|
|
|
|
|
{
|
|
|
|
|
yield return webhook;
|
|
|
|
|
}
|
2023-05-31 09:04:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 18:46:23 +00:00
|
|
|
|
public async Task<WebhooksConfig> UpdateWebhookConfig(int id, string name, string uri, string key, bool? enabled, bool? ssl)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2022-08-17 09:18:50 +00:00
|
|
|
|
|
2023-06-01 10:54:19 +00:00
|
|
|
|
var updateObj = await Queries.WebhooksConfigAsync(webhooksDbContext, Tenant, id);
|
2022-08-17 09:18:50 +00:00
|
|
|
|
|
|
|
|
|
if (updateObj != null)
|
|
|
|
|
{
|
2023-03-14 16:27:12 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(name))
|
|
|
|
|
{
|
|
|
|
|
updateObj.Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(uri))
|
|
|
|
|
{
|
|
|
|
|
updateObj.Uri = uri;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
|
|
|
|
{
|
|
|
|
|
updateObj.SecretKey = key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
if (enabled.HasValue)
|
|
|
|
|
{
|
|
|
|
|
updateObj.Enabled = enabled.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 18:46:23 +00:00
|
|
|
|
if (ssl.HasValue)
|
|
|
|
|
{
|
2023-04-11 09:59:02 +00:00
|
|
|
|
updateObj.SSL = ssl.Value;
|
2023-04-06 18:46:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 09:18:50 +00:00
|
|
|
|
webhooksDbContext.WebhooksConfigs.Update(updateObj);
|
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
2022-08-18 09:03:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
return updateObj;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
|
|
|
|
public async Task<WebhooksConfig> RemoveWebhookConfigAsync(int id)
|
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2022-08-17 09:18:50 +00:00
|
|
|
|
|
2023-06-01 10:54:19 +00:00
|
|
|
|
var removeObj = await Queries.WebhooksConfigAsync(webhooksDbContext, Tenant, id);
|
2023-01-30 16:31:31 +00:00
|
|
|
|
|
2023-02-21 11:22:34 +00:00
|
|
|
|
if (removeObj != null)
|
2023-01-30 16:31:31 +00:00
|
|
|
|
{
|
|
|
|
|
webhooksDbContext.WebhooksConfigs.Remove(removeObj);
|
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
}
|
2022-08-18 09:03:13 +00:00
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
return removeObj;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 08:51:02 +00:00
|
|
|
|
public IAsyncEnumerable<DbWebhooks> ReadJournal(int startIndex, int limit, DateTime? deliveryFrom, DateTime? deliveryTo, string hookUri, int? hookId, int? configId, int? eventId, WebhookGroupStatus? webhookGroupStatus)
|
2022-08-18 09:34:48 +00:00
|
|
|
|
{
|
2023-06-01 12:59:17 +00:00
|
|
|
|
using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-05-16 09:22:53 +00:00
|
|
|
|
var q = GetQueryForJournal(deliveryFrom, deliveryTo, hookUri, hookId, configId, eventId, webhookGroupStatus);
|
2022-08-22 08:00:55 +00:00
|
|
|
|
|
|
|
|
|
if (startIndex != 0)
|
|
|
|
|
{
|
|
|
|
|
q = q.Skip(startIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (limit != 0)
|
|
|
|
|
{
|
|
|
|
|
q = q.Take(limit);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 08:51:02 +00:00
|
|
|
|
return q.AsAsyncEnumerable();
|
2022-08-18 09:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 09:22:53 +00:00
|
|
|
|
public async Task<int> GetTotalByQuery(DateTime? deliveryFrom, DateTime? deliveryTo, string hookUri, int? hookId, int? configId, int? eventId, WebhookGroupStatus? webhookGroupStatus)
|
|
|
|
|
{
|
|
|
|
|
return await GetQueryForJournal(deliveryFrom, deliveryTo, hookUri, hookId, configId, eventId, webhookGroupStatus).CountAsync();
|
2022-08-18 09:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
public async Task<WebhooksLog> ReadJournal(int id)
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-05-29 12:01:13 +00:00
|
|
|
|
|
2023-06-27 15:29:23 +00:00
|
|
|
|
var fromDb = await Queries.WebhooksLogAsync(webhooksDbContext, id);
|
2023-05-16 08:51:02 +00:00
|
|
|
|
|
|
|
|
|
if (fromDb != null)
|
|
|
|
|
{
|
|
|
|
|
fromDb.Log.Config = fromDb.Config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fromDb.Log;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
2022-07-28 18:00:49 +00:00
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
public async Task<WebhooksLog> WriteToJournal(WebhooksLog webhook)
|
2022-08-17 09:18:50 +00:00
|
|
|
|
{
|
2023-05-19 11:42:02 +00:00
|
|
|
|
webhook.TenantId = await _tenantManager.GetCurrentTenantIdAsync();
|
2022-08-17 09:18:50 +00:00
|
|
|
|
webhook.Uid = _authContext.CurrentAccount.ID;
|
2022-07-28 18:00:49 +00:00
|
|
|
|
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
|
|
|
|
var entity = await webhooksDbContext.WebhooksLogs.AddAsync(webhook);
|
2022-08-17 09:18:50 +00:00
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
|
|
|
|
return entity.Entity;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
}
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
|
|
|
|
public async Task<WebhooksLog> UpdateWebhookJournal(int id, int status, DateTime delivery, string requestHeaders, string responsePayload, string responseHeaders)
|
2022-07-28 18:00:49 +00:00
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2022-08-16 10:36:48 +00:00
|
|
|
|
|
2023-06-27 15:29:23 +00:00
|
|
|
|
var webhook = (await Queries.WebhooksLogAsync(webhooksDbContext, id))?.Log;
|
2023-05-16 08:51:02 +00:00
|
|
|
|
if (webhook != null)
|
|
|
|
|
{
|
|
|
|
|
webhook.Status = status;
|
|
|
|
|
webhook.RequestHeaders = requestHeaders;
|
|
|
|
|
webhook.ResponsePayload = responsePayload;
|
|
|
|
|
webhook.ResponseHeaders = responseHeaders;
|
|
|
|
|
webhook.Delivery = delivery;
|
2023-05-31 09:04:20 +00:00
|
|
|
|
|
2023-05-16 08:51:02 +00:00
|
|
|
|
webhooksDbContext.WebhooksLogs.Update(webhook);
|
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
}
|
2022-08-18 09:03:13 +00:00
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
return webhook;
|
2023-02-21 11:22:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Register(List<Webhook> webhooks)
|
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-02-21 11:22:34 +00:00
|
|
|
|
|
2023-06-29 08:32:05 +00:00
|
|
|
|
var dbWebhooks = await Queries.DbWebhooksAsync(webhooksDbContext).ToListAsync();
|
2023-02-21 11:22:34 +00:00
|
|
|
|
|
|
|
|
|
foreach (var webhook in webhooks)
|
|
|
|
|
{
|
2023-06-29 08:32:05 +00:00
|
|
|
|
if (!dbWebhooks.Any(r => r.Route == webhook.Route && r.Method == webhook.Method))
|
2023-02-21 11:22:34 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await webhooksDbContext.Webhooks.AddAsync(_mapper.Map<DbWebhook>(webhook));
|
|
|
|
|
await webhooksDbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<Webhook>> GetWebhooksAsync()
|
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-06-01 10:54:19 +00:00
|
|
|
|
var webHooks = await Queries.DbWebhooksAsync(webhooksDbContext).ToListAsync();
|
2023-02-21 11:22:34 +00:00
|
|
|
|
return _mapper.Map<List<DbWebhook>, List<Webhook>>(webHooks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Webhook> GetWebhookAsync(int id)
|
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-06-01 10:54:19 +00:00
|
|
|
|
var webHook = await Queries.DbWebhookAsync(webhooksDbContext, id);
|
2023-02-21 11:22:34 +00:00
|
|
|
|
return _mapper.Map<DbWebhook, Webhook>(webHook);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Webhook> GetWebhookAsync(string method, string routePattern)
|
|
|
|
|
{
|
2023-06-01 11:27:27 +00:00
|
|
|
|
await using var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-02-21 11:22:34 +00:00
|
|
|
|
|
2023-06-01 10:54:19 +00:00
|
|
|
|
var webHook = await Queries.DbWebhookByMethodAsync(webhooksDbContext, method, routePattern);
|
2023-02-21 11:22:34 +00:00
|
|
|
|
|
|
|
|
|
return _mapper.Map<DbWebhook, Webhook>(webHook);
|
2023-05-31 09:04:20 +00:00
|
|
|
|
}
|
2023-05-16 09:22:53 +00:00
|
|
|
|
|
|
|
|
|
private IQueryable<DbWebhooks> GetQueryForJournal(DateTime? deliveryFrom, DateTime? deliveryTo, string hookUri, int? hookId, int? configId, int? eventId, WebhookGroupStatus? webhookGroupStatus)
|
|
|
|
|
{
|
|
|
|
|
var webhooksDbContext = _dbContextFactory.CreateDbContext();
|
2023-06-27 15:29:23 +00:00
|
|
|
|
|
2023-05-16 09:22:53 +00:00
|
|
|
|
var q = webhooksDbContext.WebhooksLogs
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.OrderByDescending(t => t.Id)
|
|
|
|
|
.Where(r => r.TenantId == Tenant)
|
|
|
|
|
.Join(webhooksDbContext.WebhooksConfigs.AsNoTracking(), r => r.ConfigId, r => r.Id, (log, config) => new DbWebhooks { Log = log, Config = config });
|
|
|
|
|
|
|
|
|
|
if (deliveryFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var from = deliveryFrom.Value;
|
|
|
|
|
q = q.Where(r => r.Log.Delivery >= from);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (deliveryTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var to = deliveryTo.Value;
|
|
|
|
|
q = q.Where(r => r.Log.Delivery <= to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(hookUri))
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Config.Uri == hookUri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hookId != null)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.WebhookId == hookId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configId != null)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.ConfigId == configId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eventId != null)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.Id == eventId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (webhookGroupStatus != null && webhookGroupStatus != WebhookGroupStatus.None)
|
|
|
|
|
{
|
|
|
|
|
if ((webhookGroupStatus & WebhookGroupStatus.NotSent) != WebhookGroupStatus.NotSent)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.Status != 0);
|
|
|
|
|
}
|
|
|
|
|
if ((webhookGroupStatus & WebhookGroupStatus.Status2xx) != WebhookGroupStatus.Status2xx)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.Status < 200 || r.Log.Status >= 300);
|
|
|
|
|
}
|
|
|
|
|
if ((webhookGroupStatus & WebhookGroupStatus.Status3xx) != WebhookGroupStatus.Status3xx)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.Status < 300 || r.Log.Status >= 400);
|
|
|
|
|
}
|
|
|
|
|
if ((webhookGroupStatus & WebhookGroupStatus.Status4xx) != WebhookGroupStatus.Status4xx)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.Status < 400 || r.Log.Status >= 500);
|
|
|
|
|
}
|
|
|
|
|
if ((webhookGroupStatus & WebhookGroupStatus.Status5xx) != WebhookGroupStatus.Status5xx)
|
|
|
|
|
{
|
|
|
|
|
q = q.Where(r => r.Log.Status < 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return q;
|
2023-06-27 15:29:23 +00:00
|
|
|
|
}
|
2023-04-06 18:46:23 +00:00
|
|
|
|
}
|
2023-06-27 15:29:23 +00:00
|
|
|
|
public class DbWebhooks
|
2023-05-16 08:51:02 +00:00
|
|
|
|
{
|
|
|
|
|
public WebhooksLog Log { get; set; }
|
2023-06-27 15:29:23 +00:00
|
|
|
|
public WebhooksConfig Config { get; set; }
|
2023-05-16 08:51:02 +00:00
|
|
|
|
}
|
2023-04-06 18:46:23 +00:00
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum WebhookGroupStatus
|
|
|
|
|
{
|
2023-04-18 09:58:18 +00:00
|
|
|
|
None = 0,
|
|
|
|
|
NotSent = 1,
|
|
|
|
|
Status2xx = 2,
|
|
|
|
|
Status3xx = 4,
|
|
|
|
|
Status4xx = 8,
|
|
|
|
|
Status5xx = 16
|
2023-05-29 12:01:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 09:04:20 +00:00
|
|
|
|
static file class Queries
|
2023-05-29 12:01:13 +00:00
|
|
|
|
{
|
2023-06-29 08:32:05 +00:00
|
|
|
|
public static readonly Func<WebhooksDbContext, int, string, string, Task<WebhooksConfig>> WebhooksConfigByUriAsync =
|
2023-06-01 10:54:19 +00:00
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
2023-06-29 08:32:05 +00:00
|
|
|
|
(WebhooksDbContext ctx, int tenantId, string uri, string name) =>
|
2023-06-01 10:54:19 +00:00
|
|
|
|
ctx.WebhooksConfigs
|
2023-06-29 08:32:05 +00:00
|
|
|
|
.FirstOrDefault(r => r.TenantId == tenantId && r.Uri == uri && r.Name == name));
|
2023-06-01 10:54:19 +00:00
|
|
|
|
|
2023-06-27 15:29:23 +00:00
|
|
|
|
public static readonly Func<WebhooksDbContext, int, IAsyncEnumerable<WebhooksConfigWithStatus>> WebhooksConfigWithStatusAsync =
|
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx, int tenantId) =>
|
|
|
|
|
ctx.WebhooksConfigs
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(it => it.TenantId == tenantId)
|
|
|
|
|
.GroupJoin(ctx.WebhooksLogs, c => c.Id, l => l.ConfigId, (configs, logs) => new { configs, logs })
|
|
|
|
|
.Select(it =>
|
|
|
|
|
new WebhooksConfigWithStatus
|
|
|
|
|
{
|
|
|
|
|
WebhooksConfig = it.configs,
|
|
|
|
|
Status = it.logs.OrderBy(it => it.Delivery).LastOrDefault().Status
|
|
|
|
|
}));
|
|
|
|
|
|
2023-06-01 10:54:19 +00:00
|
|
|
|
public static readonly Func<WebhooksDbContext, int, IAsyncEnumerable<WebhooksConfig>> WebhooksConfigsAsync =
|
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx, int tenantId) =>
|
|
|
|
|
ctx.WebhooksConfigs
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(it => it.TenantId == tenantId));
|
|
|
|
|
|
|
|
|
|
public static readonly Func<WebhooksDbContext, int, int, Task<WebhooksConfig>> WebhooksConfigAsync =
|
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx, int tenantId, int id) =>
|
|
|
|
|
ctx.WebhooksConfigs
|
|
|
|
|
.FirstOrDefault(it => it.TenantId == tenantId && it.Id == id));
|
|
|
|
|
|
2023-06-27 15:29:23 +00:00
|
|
|
|
public static readonly Func<WebhooksDbContext, int, Task<DbWebhooks>> WebhooksLogAsync =
|
2023-06-01 10:54:19 +00:00
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx, int id) =>
|
|
|
|
|
ctx.WebhooksLogs
|
|
|
|
|
.AsNoTracking()
|
2023-06-27 15:29:23 +00:00
|
|
|
|
.Where(it => it.Id == id)
|
|
|
|
|
.Join(ctx.WebhooksConfigs, r => r.ConfigId, r => r.Id, (log, config) => new DbWebhooks { Log = log, Config = config })
|
|
|
|
|
.FirstOrDefault());
|
2023-06-01 10:54:19 +00:00
|
|
|
|
|
|
|
|
|
public static readonly Func<WebhooksDbContext, IAsyncEnumerable<DbWebhook>> DbWebhooksAsync =
|
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx) =>
|
|
|
|
|
ctx.Webhooks.AsNoTracking());
|
|
|
|
|
|
|
|
|
|
public static readonly Func<WebhooksDbContext, int, Task<DbWebhook>> DbWebhookAsync =
|
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx, int id) =>
|
|
|
|
|
ctx.Webhooks
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.FirstOrDefault(r => r.Id == id));
|
|
|
|
|
|
|
|
|
|
public static readonly Func<WebhooksDbContext, string, string, Task<DbWebhook>> DbWebhookByMethodAsync =
|
|
|
|
|
Microsoft.EntityFrameworkCore.EF.CompileAsyncQuery(
|
|
|
|
|
(WebhooksDbContext ctx, string method, string routePattern) =>
|
|
|
|
|
ctx.Webhooks
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.FirstOrDefault(r => r.Method == method && r.Route == routePattern));
|
2022-02-03 13:43:37 +00:00
|
|
|
|
}
|