using System.Collections.Generic; using System.Threading.Tasks; using System; using Newtonsoft.Json; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels; using LCU.Library.Internal.CoreApiWrapperLib.Utilities; using LCU.Library.Internal.CoreApiWrapperLib.Configuration; using LCU.Library.Internal.CoreApiWrapperLib.CoreApiUtilities; using LCU.Library.Internal.CoreApiWrapperModel.Constants; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Persons; using LCU.Library.Internal.CoreApiWrapperLib.CoreDbUtilities; namespace LCU.Library.Internal.CoreApiWrapperLib.DataAccess { public class PersonUserFieldDao : BaseCoreDao, IPersonUserFieldDao { private readonly ICoreDbExecution _coreDbExecution; private readonly CoreConfiguration _coreConfiguration; private readonly CoreApiConfiguration _coreApiConfiguration; private const string GetPersonsForCheckHoldsUpdate = "Person/GetPersonsForCheckHoldsUpdate"; private const string DnaTimeFormat = "MM-dd-yyyy HH:mm:ss"; public PersonUserFieldDao(ICoreRequest coreRequest, CoreConfiguration coreConfiguration, ICoreDbExecution coreDbExecution) : base(coreRequest) { _coreConfiguration = coreConfiguration; _coreApiConfiguration = _coreConfiguration.CoreApiConfiguration; _coreDbExecution = coreDbExecution; } public async Task UpdatePersonUserField(CoreApiMessage personUserFieldUpdateRequest, int personId, string userFieldId) { var strPersonId = JsonConvert.ToString(personId); var endpoint = HttpUtilities.ReplaceRootParameter(_coreApiConfiguration.CoreEndPointConfiguration.PutPersonUserField, RequestRootParameters.PersonId, strPersonId); endpoint = HttpUtilities.ReplaceRootParameter(endpoint, RequestRootParameters.UserFieldId, userFieldId); var jsonBody = JsonConvert.SerializeObject(personUserFieldUpdateRequest); var result = await _coreRequest.PutRequestObject>(endpoint, null, jsonBody); return result?.MessageBody; } public async Task InsertPersonUserField(CoreApiMessage personUserFieldRequest, int personId) { var strPersonId = JsonConvert.ToString(personId); var endpoint = HttpUtilities.ReplaceRootParameter(_coreApiConfiguration.CoreEndPointConfiguration.PersonUserFields, RequestRootParameters.PersonId, strPersonId); var jsonBody = JsonConvert.SerializeObject(personUserFieldRequest); var result = await _coreRequest.PostRequestObject>(endpoint, null, jsonBody); return result?.MessageBody; } public async Task>> FindPersonUserFieldCoreApiResponseByPersonId(CoreRequestParameters coreRequestParameters, int personId) { var endpointParameters = HttpUtilities.BuildDictionaryOfNonNullValues(coreRequestParameters); var endpoint = HttpUtilities.ReplaceRootParameter(_coreApiConfiguration.CoreEndPointConfiguration.PersonUserFields, RequestRootParameters.PersonId, JsonConvert.ToString(personId)); return await _coreRequest.GetRequestObject>>(endpoint, endpointParameters); } public async Task> FindPersonsForAtmCheckHolds(DateTime? resetStartDate, DateTime? resetEndDate) { var query = QueryUtilities.GetQuery(GetPersonsForCheckHoldsUpdate); var personUserFieldDateParams = new { ResetStartDate = resetStartDate?.ToString(DnaTimeFormat), //to indicate the start and end date to reset user field values ResetEndDate = resetEndDate?.ToString(DnaTimeFormat) }; return await _coreDbExecution.ExecuteQuery(query, personUserFieldDateParams); } } }