Skip to content

Commit

Permalink
Fixed Admin
Browse files Browse the repository at this point in the history
  • Loading branch information
acmoune committed May 18, 2024
1 parent 5b00992 commit 51887e0
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public async Task Handle(BookingConfirmedEvent notification, CancellationToken c
Nous confirmons votre réservation du {notification.Booking.Campaign.BookingDate}, pour le service {notification.Booking.Campaign.Service.Name}.
Ce service va de {notification.Booking.Campaign.Service.StartTime} à {notification.Booking.Campaign.Service.EndTime}.
Veuillez télécharger votre PASS (QR Code) à ce lien: {notification.Booking.QrCodeUrl}
Veuillez télécharger votre PASS (QR Code) en pièce jointe.
Vous devrez le présenter à l'entrée pour être identifié. Veuillez également le partager à tous ceux qui seront à votre table.
Nous vous disons à bientôt.
Cordialement,
OSAN Cave.
";
await _emailSender.SendMail(to, subject, body);
await _emailSender.SendMailWithPng(to, subject, body, notification.Booking.QrCodeUrl!);
}
}
59 changes: 59 additions & 0 deletions src/OsanWebsite.Core/Infrastructure/EmailService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using MimeKit;
using OsanWebsite.Core.Infrastructure.Tools;

namespace OsanWebsite.Core.Infrastructure;

Expand Down Expand Up @@ -51,4 +52,62 @@ public async Task<bool> SendMail(string to, string subject, string body)

return success;
}

public async Task<bool> SendMailWithPng(string to, string subject, string body, string pngUrl)
{
var smtpServer = Environment.GetEnvironmentVariable("OSAN_SMTP_Server");
var smtpPort = Environment.GetEnvironmentVariable("OSAN_SMTP_Port");
var smtpUser = Environment.GetEnvironmentVariable("OSAN_SMTP_User");
var smtpPwd = Environment.GetEnvironmentVariable("OSAN_SMTP_Password");

bool success;

var message = new MimeMessage();
message.From.Add(new MailboxAddress("O'SAN Cave", smtpUser));
message.ReplyTo.Add(new MailboxAddress("O'SAN Cave", smtpUser));
message.To.Add(new MailboxAddress("", to));
message.Subject = subject;

var textBody = new TextPart("plain")
{
Text = body,
};

var pngBytes = await FileDownloader.DownloadAsBytes(pngUrl);
var pngStream = new MemoryStream(pngBytes);

var attachment = new MimePart("image", "png")
{
Content = new MimeContent(pngStream, ContentEncoding.Default),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Default,
FileName = $"{Guid.NewGuid()}.png",
};

var multipart = new Multipart("mixed");
multipart.Add(textBody);
multipart.Add(attachment);

message.Body = multipart;

try
{
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Connect(smtpServer, int.Parse(smtpPort!), true);
await client.AuthenticateAsync(smtpUser, smtpPwd);
await client.SendAsync(message);
await client.DisconnectAsync(true);
}
_logger.LogInformation($"Sent email to {to}, concerning {subject}");
success = true;
}
catch (Exception ex)
{
_logger.LogError(ex.Message, ex);
success = false;
}

return success;
}
}
1 change: 1 addition & 0 deletions src/OsanWebsite.Core/Infrastructure/IEmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public interface IEmailService
{
Task<bool> SendMail(string to, string subject, string body);
Task<bool> SendMailWithPng(string to, string subject, string body, string pngUrl);
}
2 changes: 1 addition & 1 deletion src/OsanWebsite.Core/Infrastructure/QrCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class QrCodeGenerator : IQrCodeGenerator
{
public async Task<QrCodeInfo> GenerateFrom(string text)
{
var fileName = $"{text}.png";
var fileName = $"{text}_{Guid.NewGuid().ToString()}.png";

var memoryStream = CodeGenerator.CreateQrCodePngStream(text);
await S3Uploader.Upload(memoryStream, fileName);
Expand Down
10 changes: 6 additions & 4 deletions src/OsanWebsite.Core/Infrastructure/Tools/CodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using IronBarCode;
using QRCoder;

namespace OsanWebsite.Core.Infrastructure.Tools;

public static class CodeGenerator
{
public static MemoryStream CreateQrCodePngStream(string code)
{
return QRCodeWriter.CreateQrCode(code, 500, QRCodeWriter.QrErrorCorrectionLevel.Medium)
.SetMargins(30)
.ToPngStream();
var generator = new QRCoder.QRCodeGenerator();
var data = generator.CreateQrCode(code, QRCoder.QRCodeGenerator.ECCLevel.L);
var png = new PngByteQRCode(data);
var dataBytes = png.GetGraphic(20);
return new MemoryStream(dataBytes);
}
}
13 changes: 13 additions & 0 deletions src/OsanWebsite.Core/Infrastructure/Tools/FileDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OsanWebsite.Core.Infrastructure.Tools;

public static class FileDownloader
{
public static async Task<byte[]> DownloadAsBytes(string url)
{
using (HttpClient http = new HttpClient())
{
var myBytes = await http.GetByteArrayAsync(url);
return myBytes;
}
}
}
3 changes: 1 addition & 2 deletions src/OsanWebsite.Core/OsanWebsite.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.307.11" />
<PackageReference Include="BarCode" Version="2024.4.4" />
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="GraphQL.Client" Version="6.0.5" />
<PackageReference Include="GraphQL.Client.Serializer.Newtonsoft" Version="6.0.5" />
<PackageReference Include="MailKit" Version="4.4.0" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Net.Codecrete.QrCodeGenerator" Version="2.0.4" />
<PackageReference Include="QRCoder" Version="1.5.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/OsanWebsite.Web/Pages/AboutPage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@

<div style="display:flex; justify-content: space-evenly; font-size: 16pt;padding: 5px 0px; border-bottom: 1px solid #e1bbc5; border-top: 1px solid #e1bbc5">
<a href="https://www.facebook.com/loungeosan" target="_blank" class="osan-link"><span class="fa-brands fa-facebook"></span></a>
<a href="https://www.instagram.com/loungeosan" target="_blank" class="osan-link"><span class="fa-brands fa-instagram"></span></a>
<a href="https://twitter.com/OSANCave" target="_blank" class="osan-link"><span class="fa-brands fa-x-twitter"></span></a>
<a href="https://www.instagram.com/loungeosan" target="_blank" class="osan-link"><span class="fa-brands fa-instagram"></span></a>
<a href="@("https://www.youtube.com/@osancave")" class="osan-link" target="_blank"><span class="fa-brands fa-youtube"></span></a>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/OsanWebsite.Web/Pages/Shared/_Sidebar.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

<div style="display:flex; justify-content: space-evenly; font-size: 16pt;padding-top: 2px;">
<a href="https://www.facebook.com/loungeosan" target="_blank" class="social-link"><span class="fa-brands fa-facebook"></span></a>
<a href="https://www.instagram.com/loungeosan" target="_blank" class="social-link"><span class="fa-brands fa-instagram"></span></a>
<a href="https://twitter.com/OSANCave" target="_blank" class="social-link"><span class="fa-brands fa-x-twitter"></span></a>
<a href="https://www.instagram.com/loungeosan" target="_blank" class="social-link"><span class="fa-brands fa-instagram"></span></a>
<a href="@("https://www.youtube.com/@osancave")" class="social-link" target="_blank"><span class="fa-brands fa-youtube"></span></a>
</div>

Expand Down

0 comments on commit 51887e0

Please sign in to comment.