예제 #1
0
 public static DocumentWorkerEventResponse ToDocumentEventResponse(this ProcessDocumentResponse processDocumentDoneResponse)
 {
     return(new DocumentWorkerEventResponse
     {
         ErrorMsgTxt = processDocumentDoneResponse.ErrorMsgTxt,
         EventReason = processDocumentDoneResponse.EventReason,
         EventStatus = processDocumentDoneResponse.EventStatus,
         SuccessBit = processDocumentDoneResponse.SuccessBit
     });
 }
예제 #2
0
        public async Task <ProcessDocumentResponse> ProcessDocumentAsync(ProcessDocumentRequest request)
        {
            var response = new ProcessDocumentResponse();

            try
            {
                var sdkConfig     = _cache.Get <EncompassSdkConfig>(ConstantString.EncompassSdkCacheKey);
                var tokenResponse = await RequestAccessToken(sdkConfig).ConfigureAwait(false);

                // get access token
                if (!tokenResponse.StatusCode.IsSuccessStatusCode())
                {
                    response.SuccessBit  = false;
                    response.EventStatus = EventStatusEnum.Error;
                    response.ErrorMsgTxt = JsonConvert.SerializeObject(new
                    {
                        errorCode   = tokenResponse.StatusCode,
                        errorMsgTxt = JsonConvert.SerializeObject(tokenResponse.Body.FailureResponse)
                    });
                }
                var accessToken = tokenResponse.Body.SuccessResponse.AccessToken;

                // get list of latest documents via api
                var getAttachmentApis = await _encompassClient.QueryDocumentAsync(new GetAttachmentRequest
                {
                    AccessToken = accessToken,
                    LoanGuid    = request.LoanGuid
                }).ConfigureAwait(false);

                if (getAttachmentApis.Body?.FailureResponse != null)
                {
                    response.SuccessBit  = false;
                    response.EventStatus = EventStatusEnum.Error;
                    response.ErrorMsgTxt = JsonConvert.SerializeObject(new
                    {
                        errorCode   = getAttachmentApis.StatusCode,
                        errorMsgTxt = string.Format(ConstantString.ErrorWhileGettingAttachments,
                                                    JsonConvert.SerializeObject(getAttachmentApis.Body?.FailureResponse))
                    });
                }

                var cachedDocuments = await GetDocumentCacheAsync(request.LoanGuid).ConfigureAwait(false);

                var apiDocuments           = ParseDoucmentApiResponse(getAttachmentApis.Body);
                var apiDocumentIds         = apiDocuments.Select(x => x.Key).ToHashSet();
                var apiNotCacheDocumentIds = GetDocumentIdToDownload(cachedDocuments, apiDocumentIds);

                // latest documents from api the same as cache
                if (apiNotCacheDocumentIds.Count == 0)
                {
                    response.SuccessBit  = true;
                    response.EventStatus = EventStatusEnum.Done;
                    response.EventReason = string.Format(ConstantString.AllDocumentDowloaded);
                    return(response);
                }

                // here means there are mismatch between documents in cache and in api
                var latestPersistedDocuments = await GetPersistedDocumentAsync(request.LoanGuid).ConfigureAwait(false);

                var apiNotDbDocumentIds = GetDocumentIdToDownload(latestPersistedDocuments, apiNotCacheDocumentIds);

                // there is no new documents
                if (apiNotDbDocumentIds.Count == 0)
                {
                    response = await TryUpdateDocumentCache(request.LoanGuid, latestPersistedDocuments, response).ConfigureAwait(false);

                    return(response);
                }

                // in db, we do not even have documents so we need to perform download.
                var addingDbResults = await AddNewDocument(apiNotDbDocumentIds, apiDocuments, request.LoanGuid).ConfigureAwait(false);

                var successAddingDbResults = ExtractSuccessDbResult(addingDbResults, response.DocErrors, false);

                var downloadDocumentResponses = await PerformDownloadDocument(successAddingDbResults, request.LoanGuid, accessToken).ConfigureAwait(false);

                var downloadSuccessResponses = ExtractSuccessApiResult(downloadDocumentResponses, response.DocErrors, false);

                var updatingDbResults = await UpdateNewDocument(downloadSuccessResponses, request.LoanGuid);

                var updatedSuccessDbResults = ExtractSuccessDbResult(updatingDbResults, response.DocErrors, true);

                var updatededDocumentGuids = GetUpdatedDocumentGuid(updatedSuccessDbResults);

                if (cachedDocuments != null)
                {
                    updatededDocumentGuids.UnionWith(cachedDocuments);
                }
                response = await TryUpdateDocumentCache(request.LoanGuid, updatededDocumentGuids, response).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                response.SuccessBit  = false;
                response.EventStatus = EventStatusEnum.Error;
                response.EventReason = ex.Message;
                response.ErrorMsgTxt = JsonConvert.SerializeObject(new { message = JsonConvert.SerializeObject(ex.Message), stackTrace = ex.StackTrace });
            }

            return(response);
        }
예제 #3
0
        private async Task <ProcessDocumentResponse> TryUpdateDocumentCache(Guid loanGuid, HashSet <Guid> documentGuids, ProcessDocumentResponse response)
        {
            response.SuccessBit = await SetDocumentCacheAsync(loanGuid, documentGuids).ConfigureAwait(false);

            if (response.SuccessBit)
            {
                response.EventStatus = EventStatusEnum.Done;
                response.EventReason = string.Format(ConstantString.AllDocumentDowloaded);
            }
            else
            {
                response.EventStatus = EventStatusEnum.CacheOutOfSync;
                response.EventReason = string.Format(ConstantString.CacheIssueAllDocumentDowloaded);
            }

            return(response);
        }