Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail Transfer event added to the webhook #9

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
124 changes: 14 additions & 110 deletions Xtkl.NceTransferWebhooks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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 = $@"
<html>
<body>
<p>Team,</p>
<p>We would like to inform you that the NCE transfer process has been successfully completed. Please find the details below:</p>

<p>{JsonSerializer.Serialize(request)}</p>

<ul style='list-style-type:none; padding: 0;'>
<li><strong>Resource URI:</strong> {request.ResourceUri}</li>
<li><strong>Date of Change (UTC):</strong> {request.ResourceChangeUtcDate.ToString("u")}</li>
</ul>

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

<p>Best regards,<br/>
<strong>Sherweb Support Team</strong>
</body>
</html>";

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 = $@"
<html>
<body>
<p>Team,</p>
<p>We would like to inform you that the NCE transfer process has been successfully completed. Please find the details below:</p>

<p>{JsonSerializer.Serialize(request)}</p>

<ul style='list-style-type:none; padding: 0;'>
<li><strong>Resource URI:</strong> {request.ResourceUri}</li>
<li><strong>Date of Change (UTC):</strong> {request.ResourceChangeUtcDate.ToString("u")}</li>
</ul>

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

<p>Best regards,<br/>
<strong>Sherweb Support Team</strong>
</body>
</html>";

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<Transfer> GetTransfer(string url, TransferStatus status, TenantRegion region, IConfiguration config)
async Task<Transfer> GetTransfer(string url, TenantRegion region, IConfiguration config)
{
var partnerCredentials = GetPartnerCredentials(region, config);

Expand All @@ -242,7 +146,7 @@ async Task<Transfer> GetTransfer(string url, TransferStatus status, TenantRegion
return JsonSerializer.Deserialize<Transfer>(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"];
Expand All @@ -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 = $@"
Expand Down