Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions SqlServerAgentJobDeploymentTool/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private async Task LoadManifestFromPathAsync(string manifestPath)
private async Task RunDeploymentAsync(bool dryRun)
{
SetBusyState(true);
string currentOperation = "preparing deployment";
string currentOperation = string.Empty;
string? currentContext = null;

try
Expand Down Expand Up @@ -465,10 +465,11 @@ private async Task RunDeploymentAsync(bool dryRun)
}
catch (Exception ex)
{
DeploymentErrorReporter.ReportUi(ex, _logger, AppendOutput, currentOperation, currentContext);
string operation = string.IsNullOrWhiteSpace(currentOperation) ? "preparing deployment" : currentOperation;
DeploymentErrorReporter.ReportUi(ex, _logger, AppendOutput, operation, currentContext);
UpdateValidationSummary();
SetStatus("Deployment failed");
MessageBox.Show(this, DeploymentErrorReporter.GetUserMessage(ex, currentOperation, currentContext), "Deployment failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show(this, DeploymentErrorReporter.GetUserMessage(ex, operation, currentContext), "Deployment failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Expand Down
9 changes: 5 additions & 4 deletions SqlServerAgentJobDeploymentTool/SqlAgentDeploymentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace SqlServerAgentJobDeploymentTool;

internal sealed class SqlAgentDeploymentService
{
private const string JobIdParameterName = "@job_id";
private readonly SqlConnection _connection;
private readonly TextWriter _writer;
private readonly ILogger<SqlAgentDeploymentService> _logger;
Expand Down Expand Up @@ -55,7 +56,7 @@ private async Task DeployJobAsync(JobDefinition job, string? databaseNameOverrid
job.Schedules.Count);

int deleteLevel = job.DeleteLevel ?? 0;
SqlParameter jobIdParameter = new("@job_id", System.Data.SqlDbType.UniqueIdentifier)
SqlParameter jobIdParameter = new(JobIdParameterName, System.Data.SqlDbType.UniqueIdentifier)
{
Direction = System.Data.ParameterDirection.Output
};
Expand Down Expand Up @@ -110,7 +111,7 @@ private async Task AddJobStepAsync(Guid jobId, JobStepDefinition step, int stepI

List<SqlParameter> parameters =
[
new SqlParameter("@job_id", jobId),
new SqlParameter(JobIdParameterName, jobId),
new SqlParameter("@step_id", stepId),
new SqlParameter("@step_name", step.Name),
new SqlParameter("@subsystem", ToSubsystemName(step.Subsystem)),
Expand Down Expand Up @@ -143,7 +144,7 @@ private async Task AddJobScheduleAsync(Guid jobId, JobScheduleDefinition schedul

List<SqlParameter> parameters =
[
new SqlParameter("@job_id", jobId),
new SqlParameter(JobIdParameterName, jobId),
new SqlParameter("@name", schedule.Name),
new SqlParameter("@enabled", schedule.Enabled),
new SqlParameter("@freq_type", (int)schedule.FrequencyType),
Expand Down Expand Up @@ -233,7 +234,7 @@ private async Task AddJobServerAsync(Guid jobId, string? targetServerName, Cance
{
List<SqlParameter> parameters =
[
new SqlParameter("@job_id", jobId)
new SqlParameter(JobIdParameterName, jobId)
];

if (!string.IsNullOrWhiteSpace(targetServerName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ private static string BuildMobileNumber() =>

public static class TransactionFileFieldResolver
{
private static readonly Dictionary<string, Func<TransactionFileContext, string?, object>> FieldSourceHandlers = new(StringComparer.OrdinalIgnoreCase)
{
[TransactionFileFieldSources.MerchantId] = static (context, _) => context.Merchant.MerchantId,
[TransactionFileFieldSources.ContractId] = static (context, _) => context.Contract.ContractId,
[TransactionFileFieldSources.ContractIssuer] = static (context, _) => context.Contract.Issuer,
[TransactionFileFieldSources.ProductCode] = static (context, _) => GetTransaction(context.Transaction).ProductCode,
[TransactionFileFieldSources.Description] = static (context, _) => GetTransaction(context.Transaction).Description,
[TransactionFileFieldSources.RecipientMobileNumber] = static (context, _) => GetTransaction(context.Transaction).RecipientMobileNumber,
[TransactionFileFieldSources.Quantity] = static (context, format) => ApplyFormat(GetTransaction(context.Transaction).Quantity, format),
[TransactionFileFieldSources.UnitAmount] = static (context, format) => ApplyFormat(GetTransaction(context.Transaction).UnitAmount, format),
[TransactionFileFieldSources.TotalAmount] = static (context, format) => ApplyFormat(GetTransaction(context.Transaction).TotalAmount, format),
[TransactionFileFieldSources.Currency] = static (context, _) => GetTransaction(context.Transaction).Currency,
[TransactionFileFieldSources.TransactionDateUtc] = static (context, format) => ApplyDateFormat(GetTransaction(context.Transaction).TransactionDateUtc, format),
[TransactionFileFieldSources.ProcessingDateUtc] = static (context, format) => ApplyDateFormat(context.ProcessingTimestampUtc, format),
[TransactionFileFieldSources.RecordCount] = static (context, format) => ApplyFormat(context.RecordCount, format),
[TransactionFileFieldSources.FileTotalAmount] = static (context, format) => ApplyFormat(context.FileTotalAmount, format ?? "0.00")
};

public static string GetTextValue(FileFieldOptions field, TransactionFileContext context)
{
return GetValue(field, context)?.ToString() ?? string.Empty;
Expand All @@ -108,86 +126,22 @@ public static object GetValue(FileFieldOptions field, TransactionFileContext con
throw new InvalidOperationException($"Field '{field.Name}' must define either a source or a literal value.");
}

var transaction = context.Transaction;

var source = field.Source.Trim();

if (source.Equals(TransactionFileFieldSources.MerchantId, StringComparison.OrdinalIgnoreCase))
{
return context.Merchant.MerchantId;
}

if (source.Equals(TransactionFileFieldSources.ContractId, StringComparison.OrdinalIgnoreCase))
{
return context.Contract.ContractId;
}

if (source.Equals(TransactionFileFieldSources.ContractIssuer, StringComparison.OrdinalIgnoreCase))
{
return context.Contract.Issuer;
}

if (source.Equals(TransactionFileFieldSources.ProductCode, StringComparison.OrdinalIgnoreCase))
{
return GetTransaction(transaction).ProductCode;
}

if (source.Equals(TransactionFileFieldSources.Description, StringComparison.OrdinalIgnoreCase))
{
return GetTransaction(transaction).Description;
}

if (source.Equals(TransactionFileFieldSources.RecipientMobileNumber, StringComparison.OrdinalIgnoreCase))
{
return GetTransaction(transaction).RecipientMobileNumber;
}

if (source.Equals(TransactionFileFieldSources.Quantity, StringComparison.OrdinalIgnoreCase))
{
return ApplyFormat(GetTransaction(transaction).Quantity, field.Format);
}

if (source.Equals(TransactionFileFieldSources.UnitAmount, StringComparison.OrdinalIgnoreCase))
{
return ApplyFormat(GetTransaction(transaction).UnitAmount, field.Format);
}

if (source.Equals(TransactionFileFieldSources.TotalAmount, StringComparison.OrdinalIgnoreCase))
{
return ApplyFormat(GetTransaction(transaction).TotalAmount, field.Format);
}

if (source.Equals(TransactionFileFieldSources.Currency, StringComparison.OrdinalIgnoreCase))
{
return GetTransaction(transaction).Currency;
}

if (source.Equals(TransactionFileFieldSources.TransactionDateUtc, StringComparison.OrdinalIgnoreCase))
{
return ApplyDateFormat(GetTransaction(transaction).TransactionDateUtc, field.Format);
}

if (source.Equals(TransactionFileFieldSources.ProcessingDateUtc, StringComparison.OrdinalIgnoreCase))
{
return ApplyDateFormat(context.ProcessingTimestampUtc, field.Format);
}
return ResolveFieldSourceValue(field.Source.Trim(), field.Format, context);
}

if (source.Equals(TransactionFileFieldSources.RecordCount, StringComparison.OrdinalIgnoreCase))
{
return ApplyFormat(context.RecordCount, field.Format);
}
private static GeneratedTransaction GetTransaction(GeneratedTransaction? transaction) =>
transaction ?? throw new InvalidOperationException("The requested field requires a transaction record, but no transaction context was supplied.");

if (source.Equals(TransactionFileFieldSources.FileTotalAmount, StringComparison.OrdinalIgnoreCase))
private static object ResolveFieldSourceValue(string source, string? format, TransactionFileContext context)
{
if (!FieldSourceHandlers.TryGetValue(source, out var handler))
{
return ApplyFormat(context.FileTotalAmount, field.Format ?? "0.00");
throw new InvalidOperationException($"Unsupported field source '{source}'.");
}

throw new InvalidOperationException($"Unsupported field source '{field.Source}'.");
return handler(context, format);
}

private static GeneratedTransaction GetTransaction(GeneratedTransaction? transaction) =>
transaction ?? throw new InvalidOperationException("The requested field requires a transaction record, but no transaction context was supplied.");

private static object ApplyFormat<T>(T value, string? format)
where T : IFormattable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using TransactionProcessing.MerchantFileProcessor.Configuration;
using Microsoft.Extensions.Primitives;
Expand Down Expand Up @@ -186,7 +185,6 @@ private static async Task<IResult> HandleFileProfileFieldConfigurationAsync(
string section,
HttpRequest request,
IMerchantProcessingConfigurationStore configurationStore,
JsonSerializerOptions jsonSerializerOptions,
CancellationToken cancellationToken)
{
var form = await request.ReadFormAsync(cancellationToken);
Expand Down
Loading
Loading