using FLSDAL.DTO.Session; using FLSDAL.Query.Session; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; namespace FLSDAL.CustomCode.Session; public class FlsSessionDAL : IFlsSessionDAL { private readonly IQueryExecutor _qe; public FlsSessionDAL(IQueryExecutor queryExecutor) => _qe = queryExecutor; public async Task GetOpenContextAsync(int flsRespondentId, LoginDTO login, CancellationToken ct) { var rows = await _qe.QueryAsync( login, FlsSessionQB.GET_OPEN_CONTEXT, new { FlsRespondentId = flsRespondentId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); return rows?.FirstOrDefault(); } public async Task GetActiveSessionIdAsync(int flsRespondentId, int stepNo, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync( login, FlsSessionQB.GET_ACTIVE_SESSION_ID, new { FlsRespondentId = flsRespondentId, StepNo = stepNo, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task GetMaxAttemptNoAsync(int flsRespondentId, int stepNo, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync( login, FlsSessionQB.GET_MAX_ATTEMPT_NO, new { FlsRespondentId = flsRespondentId, StepNo = stepNo, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task InsertSessionAsync( int flsSessionId, int flsRespondentId, int flsInstanceId, int groupId, int stepNo, int attemptNo, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, FlsSessionQB.INSERT_SESSION, new { FlsSessionId = flsSessionId, FlsRespondentId = flsRespondentId, FlsInstanceId = flsInstanceId, GroupId = groupId, StepNo = stepNo, AttemptNo = attemptNo, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task MarkRespondentOpenedAsync(int flsRespondentId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, FlsSessionQB.MARK_RESPONDENT_OPENED, new { FlsRespondentId = flsRespondentId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task SaveDraftAsync(int flsSessionId, string draftDataJson, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, FlsSessionQB.SAVE_DRAFT_SESSION, new { FlsSessionId = flsSessionId, DraftDataJson = draftDataJson, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task SubmitAsync(int flsSessionId, int flsRespondentId, int groupId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, FlsSessionQB.SUBMIT_SESSION, new { FlsSessionId = flsSessionId, FlsRespondentId = flsRespondentId, GroupId = groupId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task MarkExternalAsync(int flsRespondentId, string sourceReference, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, FlsSessionQB.MARK_EXTERNAL, new { FlsRespondentId = flsRespondentId, SourceReference = sourceReference, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task GetByIdAsync(int flsSessionId, LoginDTO login, CancellationToken ct) => await _qe.QuerySingleAsync( login, FlsSessionQB.GET_SESSION_BY_ID, new { FlsSessionId = flsSessionId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task LogResponseAuditAsync( int flsSessionId, int flsRespondentId, int flsInstanceId, int auditAction, string? ipAddress, string? userAgent, int performedBy, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, FlsResponseAuditQB.INSERT_RESPONSE_AUDIT, new { FlsSessionId = flsSessionId, FlsRespondentId = flsRespondentId, FlsInstanceId = flsInstanceId, AuditAction = auditAction, IpAddress = ipAddress, UserAgent = userAgent, PerformedBy = performedBy, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetAuditTrailBySessionAsync(int flsSessionId, LoginDTO login, CancellationToken ct) => await _qe.QueryAsync( login, FlsResponseAuditQB.GET_AUDIT_TRAIL_BY_SESSION, new { FlsSessionId = flsSessionId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task> GetAuditTrailByRespondentAsync(int flsRespondentId, LoginDTO login, CancellationToken ct) => await _qe.QueryAsync( login, FlsResponseAuditQB.GET_AUDIT_TRAIL_BY_RESPONDENT, new { FlsRespondentId = flsRespondentId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); }