using System;
using System.Threading.Tasks;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.QueryExecutor;
using GoodBooks.PAY.PAYDAL.DTO.Webhook;
using GoodBooks.PAY.PAYDAL.Query.Webhook;
using Newtonsoft.Json;
namespace GoodBooks.PAY.PAYDAL.CustomCode.Webhook
{
///
/// DAL implementation for TPAYWEBHOOKEVENT.
/// Injects only IQueryExecutor per architecture rules.
///
public class PayWebhookEventDAL : IPayWebhookEventDAL
{
private readonly IQueryExecutor _queryExecutor;
public PayWebhookEventDAL(IQueryExecutor queryExecutor)
{
_queryExecutor = queryExecutor;
}
///
public async Task InsertWebhookEvent(PayWebhookEventDTO dto, LoginDTO login)
{
try
{
// TPAYWEBHOOKEVENT uses IDENTITY PK — ExecuteIdentityAsync returns the new ID.
// CREATEDON is set server-side in the SQL (GETUTCDATE()); we still pass dto
// so Dapper maps all other columns by name.
int newId = await _queryExecutor.ExecuteIdentityAsync(
login,
PayWebhookEventQB.INSERT_WEBHOOK_EVENT,
new
{
dto.PayGatewayId,
dto.IdempotencyKey,
dto.EventType,
dto.GatewayPayOrderId,
dto.GatewayTxnId,
dto.RawPayload,
dto.SignatureValid,
dto.IsProcessed,
dto.ProcessedOn,
dto.ProcessingError,
dto.CreatedById,
dto.TenantId
}).ConfigureAwait(false);
return newId;
}
catch (Exception)
{
throw;
}
}
///
public async Task UpdateSignatureValid(int webhookEventId, byte signatureValid, LoginDTO login)
{
try
{
int rows = await _queryExecutor.ExecuteAsync(
login,
PayWebhookEventQB.UPDATE_WEBHOOK_SIGNATURE,
new
{
PayWebhookEventId = webhookEventId,
SignatureValid = signatureValid
}).ConfigureAwait(false);
return rows;
}
catch (Exception)
{
throw;
}
}
///
public async Task UpdateProcessed(int webhookEventId, byte isProcessed, string? error, LoginDTO login)
{
try
{
int rows = await _queryExecutor.ExecuteAsync(
login,
PayWebhookEventQB.UPDATE_WEBHOOK_PROCESSED,
new
{
PayWebhookEventId = webhookEventId,
IsProcessed = isProcessed,
ProcessingError = error
}).ConfigureAwait(false);
return rows;
}
catch (Exception)
{
throw;
}
}
///
public async Task CheckIdempotency(string idempotencyKey, int payGatewayId, LoginDTO login)
{
try
{
int count = await _queryExecutor.ExecuteScalarAsync(
login,
PayWebhookEventQB.CHECK_IDEMPOTENCY,
new
{
IdempotencyKey = idempotencyKey,
PayGatewayId = payGatewayId
}).ConfigureAwait(false);
return count > 0;
}
catch (Exception)
{
throw;
}
}
///
public async Task GetWebhookEventList(int pageOffset, int pageSize, LoginDTO login)
{
try
{
var result = await _queryExecutor.QueryPagedAsync(
login,
PayWebhookEventQB.GET_WEBHOOK_LIST,
new
{
TenantId = login.ClientId,
IsProcessed = (byte?)null,
PayGatewayId = (int?)null,
Offset = pageOffset,
PageSize = pageSize
}).ConfigureAwait(false);
return JsonConvert.SerializeObject(result);
}
catch (Exception)
{
throw;
}
}
}
}