diff --git a/src/Server/Controllers/CompanyController.cs b/src/Server/Controllers/CompanyController.cs index 695b90a..d27bb70 100644 --- a/src/Server/Controllers/CompanyController.cs +++ b/src/Server/Controllers/CompanyController.cs @@ -2,13 +2,18 @@ using Microsoft.AspNetCore.Mvc; using BoredCareers.Services.DatabaseService; using BoredCareers.Entities; using System.Web.Http; +using BoredCareers.Services; namespace BoredCareers.Controllers { [ApiController] [Route("api/company")] public class CompanyController : MistoxControllerBase { - public CompanyController(DatabaseService db) : base(db) {} + EmailService _emailContext; + + public CompanyController(DatabaseService db, EmailService emailContext) : base(db) { + _emailContext = emailContext; + } [HttpGet] public async Task GetCompany(int CompanyID) { @@ -60,6 +65,57 @@ namespace BoredCareers.Controllers { return NotFound("Not logged in"); } - } + [HttpGet("sendverifyemail")] + public async Task> SendVerify([FromQuery] int CompanyID) { + try { + string key = "v" + CompanyID; + // Stop from sending multiple emails quickly + if (_emailContext._SentEmails.ContainsKey(key)) { + DateTime PreviousSentTime = _emailContext._SentEmails.GetValueOrDefault(key); + if (PreviousSentTime.AddMinutes(5) > DateTime.Now) { + return NotFound("Cannot sent another verify email until 5 minutes has elapsed"); + } else { + _emailContext._SentEmails.Remove(key); + } + } + Company? test = await _databaseService.GetCompany(CompanyID); + if (test != null) { + test.EmailToken = Guid.NewGuid().ToString(); + await _databaseService.SetCompany(test); + string EmailContents = EmailService.CompanyVerifyEmailSubject; + EmailContents = Substitue(EmailContents, "@CompanyName", test.Name); + EmailContents = Substitue(EmailContents, "@ID", CompanyID.ToString()); + EmailContents = Substitue(EmailContents, "@VerifyPassword", test.EmailToken); + + string result = _emailContext.Send(test.Email, EmailService.CompanyVerifyEmailSubject, EmailContents); + _emailContext._SentEmails.Add(key, DateTime.Now); + return Redirect("/"); + } + return NotFound("Account not found"); + } catch (Exception) { + return NotFound("An internal server error has occured"); + } + } + + [HttpGet("verifyemail")] + public async Task> VerifyEmail([FromQuery] int CompanyID, [FromQuery] string EmailToken) { + try { + Company? test = await _databaseService.GetCompany(CompanyID); + if (test != null) { + if (test.EmailToken == EmailToken) { + test.EmailToken = ""; + test.EmailVerified = true; + await _databaseService.SetCompany(test); + return Redirect("/"); + } + return BadRequest("The token isn't valid"); + } + return BadRequest("Account not found"); ; + } catch { + return BadRequest("An internal server error has occured"); + } + } + + } } diff --git a/src/Server/Services/EmailService/CompanyVerifyEmail.cs b/src/Server/Services/EmailService/CompanyVerifyEmail.cs new file mode 100755 index 0000000..ccaecca --- /dev/null +++ b/src/Server/Services/EmailService/CompanyVerifyEmail.cs @@ -0,0 +1,52 @@ +namespace BoredCareers.Services { + public partial class EmailService { + +// @UserName +// @VerifyPassword +// https://mistox.com/api/account/verifyemail?UserName=@UserName&Guid=@VerifyPassword + + public static string CompanyVerifyEmailSubject = "Verify Your Email Address"; + public static string CompanyVerifyEmailBody = @" + + + + + + Verify Your Email + + + + + + +
+ + + + + + + + + + +
+

Verify Email Request

+
+

Hi @CompanyName,

+

Thank you for making an account with us:

+

In order to start using your account we need to verify your email address by clicking the link below:

+

+ Verify Email +

+

If you didn't create an account please ignore this email.

+

Best regards

+
+

If you have any questions, feel free to contact support.

+
+
+ +"; + + } +} \ No newline at end of file diff --git a/src/Server/Services/EmailService/JobAutoCloseEmail.cs b/src/Server/Services/EmailService/JobAutoCloseEmail.cs index a1055f3..00aeb87 100755 --- a/src/Server/Services/EmailService/JobAutoCloseEmail.cs +++ b/src/Server/Services/EmailService/JobAutoCloseEmail.cs @@ -6,7 +6,7 @@ namespace BoredCareers.Services { // https://mistox.com/api/account/verifyemail?UserName=@UserName&Guid=@VerifyPassword public static string JobAutoClosedSubject = "Verify Your Email Address"; - public static string JobAutoClosedEmail = @" + public static string JobAutoClosedBody = @"