From cd81b058843d90f151c404afd01f00cb5f299752 Mon Sep 17 00:00:00 2001 From: asantossherweb Date: Fri, 15 Nov 2024 17:42:31 -0500 Subject: [PATCH] Fail Transfer event added to the webhook --- Xtkl.NceTransferWebhooks/Program.cs | 124 ++++------------------------ 1 file changed, 14 insertions(+), 110 deletions(-) diff --git a/Xtkl.NceTransferWebhooks/Program.cs b/Xtkl.NceTransferWebhooks/Program.cs index df9ec0c..8d57f83 100644 --- a/Xtkl.NceTransferWebhooks/Program.cs +++ b/Xtkl.NceTransferWebhooks/Program.cs @@ -11,6 +11,9 @@ using Xtkl.NceTransferWebhooks.DTOs; using Xtkl.NceTransferWebhooks.Model; +const string EVENT_COMPLETE_TRANSFER = "complete-transfer"; +const string EVENT_FAIL_TRANSFER = "fail-transfer"; + var builder = WebApplication.CreateBuilder(args); builder.Logging.ClearProviders(); @@ -83,13 +86,13 @@ .WithMetadata(new SwaggerResponseAttribute(500, "Internal server error - unexpected error occurred")) .WithOpenApi(); -app.MapPost("/transfer-completed-us", async (TransferWebhookDto request, IConfiguration config) => +app.MapPost("/transfer-webhook-us", async (TransferWebhookDto request, IConfiguration config) => { try { - var transfer = await GetTransfer(request.AuditUri, TransferStatus.Complete, TenantRegion.US, config); + var transfer = await GetTransfer(request.AuditUri, TenantRegion.US, config); - await SendEmail(transfer, TenantRegion.US, config); + await SendEmail(transfer, request.EventName, TenantRegion.US, config); return await SendToCumulus(transfer, config); } @@ -98,16 +101,16 @@ return Results.Problem($"Error: {ex.Message}"); } }) - .WithName("TransferCompletedUsa") + .WithName("TransferWebhookUsa") .WithOpenApi(); -app.MapPost("/transfer-completed-ca", async (TransferWebhookDto request, IConfiguration config) => +app.MapPost("/transfer-webhook-ca", async (TransferWebhookDto request, IConfiguration config) => { try { - var transfer = await GetTransfer(request.AuditUri, TransferStatus.Complete, TenantRegion.CA, config); + var transfer = await GetTransfer(request.AuditUri, TenantRegion.CA, config); - await SendEmail(transfer, TenantRegion.CA, config); + await SendEmail(transfer, request.EventName, TenantRegion.CA, config); return await SendToCumulus(transfer, config); } @@ -116,113 +119,14 @@ return Results.Problem($"Error: {ex.Message}"); } }) - .WithName("TransferCompletedCanada") - .WithOpenApi(); - -app.MapPost("/transfer-failed-ca", async (TransferWebhookDto request, IConfiguration config) => - { - var apiKey = config["SendGrid:ApiKey"]; - var fromEmail = config["SendGrid:FromEmail"]; - var fromName = config["SendGrid:FromName"]; - var toEmail = config["SendGrid:ToEmail"]; - var toName = config["SendGrid:ToName"]; - - try - { - var client = new SendGridClient(apiKey); - var from = new EmailAddress(fromEmail, fromName); - var subject = "NCE Transfer Completed - CA"; - var to = new EmailAddress(toEmail, toName); - - var htmlContent = $@" - - -

Team,

-

We would like to inform you that the NCE transfer process has been successfully completed. Please find the details below:

- -

{JsonSerializer.Serialize(request)}

- - - -

If you have any questions or need further assistance, please don�t hesitate to reach out.

- -

Best regards,
- Sherweb Support Team - - "; - - var msg = MailHelper.CreateSingleEmail(from, to, subject, null, htmlContent); - - await client.SendEmailAsync(msg); - - return Results.Ok("Notification sent successfully."); - - } - catch (Exception ex) - { - return Results.Problem($"Error: {ex.Message}"); - } - }) - .WithName("TransferFailedCanada") + .WithName("TransferWebhookCanada") .WithOpenApi(); -app.MapPost("/transfer-failed-us", async (TransferWebhookDto request, IConfiguration config) => - { - var apiKey = config["SendGrid:ApiKey"]; - var fromEmail = config["SendGrid:FromEmail"]; - var fromName = config["SendGrid:FromName"]; - var toEmail = config["SendGrid:ToEmail"]; - var toName = config["SendGrid:ToName"]; - - try - { - var client = new SendGridClient(apiKey); - var from = new EmailAddress(fromEmail, fromName); - var subject = "NCE Transfer Completed - US"; - var to = new EmailAddress(toEmail, toName); - - var htmlContent = $@" - - -

Team,

-

We would like to inform you that the NCE transfer process has been successfully completed. Please find the details below:

- -

{JsonSerializer.Serialize(request)}

- - - -

If you have any questions or need further assistance, please don�t hesitate to reach out.

- -

Best regards,
- Sherweb Support Team - - "; - - var msg = MailHelper.CreateSingleEmail(from, to, subject, null, htmlContent); - - await client.SendEmailAsync(msg); - - return Results.Ok("Notification sent successfully."); - - } - catch (Exception ex) - { - return Results.Problem($"Error: {ex.Message}"); - } - }) - .WithName("TransferFailedUsa") - .WithOpenApi(); app.Run(); #region Private -async Task GetTransfer(string url, TransferStatus status, TenantRegion region, IConfiguration config) +async Task GetTransfer(string url, TenantRegion region, IConfiguration config) { var partnerCredentials = GetPartnerCredentials(region, config); @@ -242,7 +146,7 @@ async Task GetTransfer(string url, TransferStatus status, TenantRegion return JsonSerializer.Deserialize(result); } -async Task SendEmail(Transfer transfer, TenantRegion region, IConfiguration config) +async Task SendEmail(Transfer transfer, string status, TenantRegion region, IConfiguration config) { var apiKey = config["SendGrid:ApiKey"]; var fromEmail = config["SendGrid:FromEmail"]; @@ -252,7 +156,7 @@ async Task SendEmail(Transfer transfer, TenantRegion region, IConfiguration conf var client = new SendGridClient(apiKey); var from = new EmailAddress(fromEmail, fromName); - var subject = $"NCE Transfer Completed - {region.ToString()}"; + var subject = $"NCE Transfer {status} - {region.ToString()}"; var to = new EmailAddress(toEmail, toName); var htmlContent = $@"