private async Task <bool> AddToConsoleDetailsAsync(Norms.Domain.Norm norm) { var adapter = new Adapters.Oal.dbo_console_detailsAdapter(); var idPr = await Policy.Handle <SqlException>(e => e.IsTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => adapter.ExecuteScalarAsync <string>($"SELECT [id] FROM dbo.console_details WHERE [console_no] = '{norm.ConsoleTag}'")); var id = idPr.Result; var consoleTagNotInConsoleDetails = string.IsNullOrWhiteSpace(id); if (consoleTagNotInConsoleDetails) { var map = new Integrations.Transforms.RtsNormToOalConsoleDetails(); var row = await map.TransformAsync(norm); var dpr = await Policy.Handle <SqlException>(e => e.IsTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => adapter.ExecuteScalarAsync <int>($"SELECT COUNT(*) FROM dbo.console_details WHERE console_no ='{row.console_no}'")); if (dpr.Result == 0 && !string.IsNullOrWhiteSpace(row.console_no)) { var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => adapter.InsertAsync(row)); var result = await pr; if (result.FinalException != null) { throw result.FinalException; // Send to dead letter queue } System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row"); } return(true); } var detailsPolly = await Policy.Handle <SqlException>(e => e.IsTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(async() => await adapter.LoadOneAsync(id)); if (null != detailsPolly.FinalException) { throw new Exception("Console Details Polly Error", detailsPolly.FinalException); } var details = detailsPolly.Result; var normDateTime = norm.Date.AddHours(norm.Time.Hour).AddMinutes(norm.Time.Minute).AddSeconds(norm.Time.Second); if (details.courier_id == norm.CourierId && details.office_no == norm.LocationId && (details.date_field ?? DateTime.MinValue).AddHours(28) >= normDateTime) { var notes = details.item_consignments.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList(); notes.AddRange(norm.AllConsignmentNotes.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries)); details.item_consignments = string.Join("\t", notes.OrderBy(x => x).Where(x => x.Length > 3 /* remove anything less*/).Distinct()); await adapter.UpdateAsync(details); return(true); } await InsertConsoleDuplicationErrorAndEventExceptionAsync(norm); return(false); }
private async Task InsertConsoleDuplicationErrorAndEventExceptionAsync(Norms.Domain.Norm norm) { Console.WriteLine("insert into console_duplicate_error & event_exception"); // var map = new Integrations.Transforms.RtsNormToOalConsoleDetails(); var normRow = await map.TransformAsync(norm); var error = new Adapters.Oal.dbo_console_duplicate_error { item_consignments = normRow.item_consignments, date_field = normRow.date_field, courier_id = normRow.courier_id, batch_name = normRow.batch_name, beat_no = normRow.beat_no, console_no = normRow.console_no, console_type = null, console_type_desc = null, courier_name = normRow.courier_name, dt_created_oal_date_field = normRow.dt_created_oal_date_field, event_comment = normRow.event_comment, id = GenerateId(20), event_type = normRow.event_type, office_dest = null, version = 0, office_dest_name = null, office_name = normRow.office_name, office_next_code = normRow.office_next_code, office_no = normRow.office_no, other_console_type = null, routing_code = null }; var errorAdapter = new Adapters.Oal.dbo_console_duplicate_errorAdapter(); await Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => errorAdapter.InsertAsync(error)); var exc = new Adapters.Oal.dbo_event_exception { consignment_no = error.console_no, date_field = error.date_field, batch_name = error.batch_name, office_no = error.office_no, version = error.version, id = GenerateId(34), courier_id = error.courier_id, event_class = "pos.oal.NormalConsoleEventNew", event_id = error.id }; var excAdapter = new Adapters.Oal.dbo_event_exceptionAdapter(); await Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout()) .WaitAndRetryAsync(RetryCount, WaitInterval) .ExecuteAndCaptureAsync(() => excAdapter.InsertAsync(exc)); }