using System.Data.Common; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Validation; using CRMSalesDAL.DTO.LeadScoringConfig; using CRMSalesDAL.Query.LeadScoringConfig; using Newtonsoft.Json; namespace CRMSalesDAL.CustomCode.LeadScoringConfig; public class LeadScoringConfigDAL : ILeadScoringConfigDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public LeadScoringConfigDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetLeadScoringConfig(int leadScoringConfigId, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QuerySingleAsync(loginDTO, LeadScoringConfigQB.GET_LEADSCORINGCONFIG, new { LeadScoringConfigId = leadScoringConfigId }, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task GetLatestLeadScoringConfig(LoginDTO loginDTO, CancellationToken ct) { try { // QuerySingleAsync wraps Dapper's QuerySingleOrDefaultAsync — returns null (not a // throw) when no active LeadScoringConfig row exists yet (same precedent as // BusinessAspirationGoalDAL.GetLatestBusinessAspirationGoal). return await _QueryExecutor .QuerySingleAsync(loginDTO, LeadScoringConfigQB.GET_LATEST_LEADSCORINGCONFIG, null!, cancellationToken: ct) .ConfigureAwait(false); } catch { throw; } } public async Task GetSelectListLeadScoringConfig(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { var result = await _QueryExecutor .QueryAsync(loginDTO, LeadScoringConfigQB.GET_SELECTLIST_LEADSCORINGCONFIG, null!, cancellationToken: ct) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch { throw; } } public async Task SaveLeadScoringConfig(LeadScoringConfigDTO leadScoringConfigDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, LeadScoringConfigQB.SAVE_LEADSCORINGCONFIG, leadScoringConfigDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.SaveErrorMessage); throw new Exception(error); } } public async Task UpdateLeadScoringConfig(LeadScoringConfigDTO leadScoringConfigDTO, LoginDTO loginDTO, DbTransaction tx, CancellationToken ct) { try { return await _QueryExecutor .ExecuteAsync(loginDTO, LeadScoringConfigQB.UPDATE_LEADSCORINGCONFIG, leadScoringConfigDTO, tx, ct) .ConfigureAwait(false); } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.UpdateErrorMessage); throw new Exception(error); } } public async Task DeleteLeadScoringConfig(int leadScoringConfigId, LoginDTO loginDTO, CancellationToken ct) { try { int result = await _QueryExecutor .ExecuteAsync(loginDTO, LeadScoringConfigQB.DELETE_LEADSCORINGCONFIG, new { LeadScoringConfigId = leadScoringConfigId }, cancellationToken: ct) .ConfigureAwait(false); return result > 0 ? SuccessResponse.DeleteSuccessMessage : ErrorResponse.DeleteNotFoundMessage; } catch (Exception ex) { string error = await _Validation.HandleException(ex, ErrorResponse.DeleteErrorMessage); throw new Exception(error); } } }