-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from QuantozTechnology/email-notifications
Email notifications
- Loading branch information
Showing
18 changed files
with
517 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
backend/core/src/Core.Domain/Abstractions/ISendGridMailService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Core.Domain.Entities.CustomerAggregate; | ||
using Core.Domain.Entities.MailAggregate; | ||
using Core.Domain.Entities.TransactionAggregate; | ||
|
||
namespace Core.Domain.Abstractions | ||
{ | ||
public interface ISendGridMailService | ||
{ | ||
public Task SendMailAsync(Mail mail, Customer customer, Transaction transaction); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
backend/core/src/Core.Domain/Entities/MailAggregate/Mail.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
namespace Core.Domain.Entities.MailAggregate | ||
{ | ||
public class Mail | ||
{ | ||
public required string Code { get; set; } | ||
|
||
public string? Created { get; set; } | ||
|
||
public string? Sent { get; set; } | ||
|
||
public string? Status { get; set; } | ||
|
||
public string? Type { get; set; } | ||
|
||
public int? Count { get; set; } | ||
|
||
public MailEntityCodes? References { get; set; } | ||
|
||
public MailContent? Content { get; set; } | ||
|
||
public MailRecipient? Recipient { get; set; } | ||
} | ||
|
||
public class MailEntityCodes | ||
{ | ||
public string? AccountCode { get; set; } | ||
|
||
public string? CustomerCode { get; set; } | ||
|
||
public string? TokenPaymentCode { get; set; } | ||
} | ||
|
||
public class MailContent | ||
{ | ||
public string? Subject { get; set; } | ||
|
||
public string? Html { get; set; } | ||
|
||
public string? Text { get; set; } | ||
} | ||
|
||
public class MailRecipient | ||
{ | ||
public string? Email { get; set; } | ||
|
||
public string? CC { get; set; } | ||
|
||
public string? BCC { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
backend/core/src/Core.Domain/Repositories/IMailsRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Core.Domain.Entities.MailAggregate; | ||
|
||
namespace Core.Domain.Repositories | ||
{ | ||
public interface IMailsRepository | ||
{ | ||
Task<IEnumerable<Mail>> GetMailsAsync(string status, CancellationToken cancellationToken = default); | ||
|
||
Task<Mail> UpdateMailSent(string code, CancellationToken cancellationToken = default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
backend/core/src/Core.Infrastructure/Compliance/SendGridMailService/MailTemplate.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace Core.Infrastructure.Compliance.SendGridMailService | ||
{ | ||
public class MailTemplate | ||
{ | ||
[JsonProperty("customerFullName")] | ||
public string? CustomerFullName { get; set; } | ||
|
||
[JsonProperty("amount")] | ||
public string? Amount { get; set; } | ||
|
||
[JsonProperty("accountCode")] | ||
public string? AccountCode { get; set; } | ||
|
||
[JsonProperty("customerBankAccount")] | ||
public string? BankAccount { get; set; } | ||
|
||
[JsonProperty("transactionCode")] | ||
public string? TransactionCode { get; set; } | ||
|
||
[JsonProperty("payoutAmount")] | ||
public string? PayoutAmount { get; set; } | ||
|
||
[JsonProperty("createdDate")] | ||
public string? CreatedDate { get; set; } | ||
|
||
[JsonProperty("finishedDate")] | ||
public string? FinishedDate { get; set; } | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
backend/core/src/Core.Infrastructure/Compliance/SendGridMailService/SendGridMailService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using Core.Domain; | ||
using Core.Domain.Abstractions; | ||
using Core.Domain.Entities.CustomerAggregate; | ||
using Core.Domain.Entities.MailAggregate; | ||
using Core.Domain.Entities.TransactionAggregate; | ||
using Core.Domain.Exceptions; | ||
using Core.Infrastructure.Nexus; | ||
using SendGrid; | ||
using SendGrid.Helpers.Mail; | ||
using System.Net; | ||
|
||
namespace Core.Infrastructure.Compliance.SendGridMailService | ||
{ | ||
public class SendGridMailService : ISendGridMailService | ||
{ | ||
private readonly SendGridClient _sendGridClient; | ||
private readonly SendGridMailServiceOptions _mailOptions; | ||
|
||
public SendGridMailService( | ||
SendGridMailServiceOptions mailOptions) | ||
{ | ||
_mailOptions = mailOptions; | ||
_sendGridClient = new SendGridClient(_mailOptions.ApiKey); | ||
} | ||
|
||
public async Task SendMailAsync(Mail mail, Customer customer, Transaction transaction) | ||
{ | ||
if (mail == null) | ||
{ | ||
throw new CustomErrorsException("MailService", "mail", "An error occured while sending mail."); | ||
} | ||
|
||
var from = new EmailAddress(_mailOptions.Sender); | ||
var to = new EmailAddress(mail.Recipient?.Email) ?? throw new CustomErrorsException("MailService", "toAddress", "An error occured while sending mail."); | ||
|
||
var msg = new SendGridMessage(); | ||
|
||
msg.SetFrom(new EmailAddress(from.Email, from.Name)); | ||
msg.AddTo(new EmailAddress(to.Email, to.Name)); | ||
|
||
// Payout | ||
if (mail.Type == MailType.TransactionSellFinish.ToString()) | ||
{ | ||
msg.SetTemplateId(_mailOptions.Templates.WithdrawalTemplateID); | ||
} | ||
else if (mail.Type == MailType.TransactionBuyFinish.ToString()) | ||
{ | ||
msg.SetTemplateId(_mailOptions.Templates.FundingtemplateID); | ||
} | ||
|
||
// Fill in the dynamic template fields | ||
var templateData = new MailTemplate() | ||
{ | ||
CustomerFullName = customer?.GetName(), | ||
AccountCode = mail.References?.AccountCode, | ||
TransactionCode = mail.References?.TokenPaymentCode, | ||
BankAccount = customer?.BankAccount, | ||
Amount = transaction.Amount.ToString(), | ||
CreatedDate = DateTimeProvider.FormatDateTimeWithoutMilliseconds(transaction.Created), | ||
FinishedDate = DateTimeProvider.FormatDateTimeWithoutMilliseconds(transaction.Finished) | ||
}; | ||
|
||
if (mail.Type == MailType.TransactionSellFinish.ToString()) | ||
{ | ||
//TODO: set payout amount when transaction details in nexus api would also return the net fiat amount | ||
//templateData.PayoutAmount = transaction.NetFiatAmount.ToString() | ||
} | ||
|
||
msg.SetTemplateData(templateData); | ||
|
||
var response = await _sendGridClient.SendEmailAsync(msg); | ||
|
||
if (response.StatusCode != HttpStatusCode.Accepted) | ||
{ | ||
throw new CustomErrorsException("MailService", "mail", "An error occured while sending mail."); | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...core/src/Core.Infrastructure/Compliance/SendGridMailService/SendGridMailServiceOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2023 Quantoz Technology B.V. and contributors. Licensed | ||
// under the Apache License, Version 2.0. See the NOTICE file at the root | ||
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Core.Infrastructure.Compliance.SendGridMailService | ||
{ | ||
public class SendGridMailServiceOptions | ||
{ | ||
[Required] | ||
public required string ApiKey { get; set; } | ||
|
||
[Required] | ||
public required Templates Templates { get; set; } | ||
|
||
[Required] | ||
public required string Sender { get; set; } | ||
} | ||
|
||
public class Templates | ||
{ | ||
[Required] | ||
public required string WithdrawalTemplateID { get; set; } | ||
|
||
[Required] | ||
public required string FundingtemplateID { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.