diff --git a/Marco.Pms.Model/Mapper/TenantMapper.cs b/Marco.Pms.Model/Mapper/TenantMapper.cs index 0d8be9b..c4a88f0 100644 --- a/Marco.Pms.Model/Mapper/TenantMapper.cs +++ b/Marco.Pms.Model/Mapper/TenantMapper.cs @@ -24,13 +24,13 @@ namespace Marco.Pms.Model.Mapper { return new TenantVM { - OrganizationName = tenant.Name, - About = tenant.Description, - Website = tenant.DomainName, - Name = tenant.ContactName, + OrganizationName = tenant.Name ?? string.Empty, + About = tenant.Description ?? string.Empty, + Website = tenant.DomainName ?? string.Empty, + Name = tenant.ContactName ?? string.Empty, OragnizationSize = tenant.OragnizationSize, IndustryId = tenant.IndustryId, - ContactNumber = tenant.ContactNumber, + ContactNumber = tenant.ContactNumber ?? string.Empty, OnBoardingDate = tenant.OnBoardingDate, }; } diff --git a/Marco.Pms.Services/Controllers/TenantController.cs b/Marco.Pms.Services/Controllers/TenantController.cs index 5693845..ef98176 100644 --- a/Marco.Pms.Services/Controllers/TenantController.cs +++ b/Marco.Pms.Services/Controllers/TenantController.cs @@ -1,4 +1,5 @@ -using System.Text.Json; +using System.Net; +using System.Text.Json; using Marco.Pms.DataAccess.Data; using Marco.Pms.Model.Dtos.Tenant; using Marco.Pms.Model.Employees; @@ -21,11 +22,15 @@ namespace Marco.Pms.Services.Controllers private readonly ApplicationDbContext _context; private readonly UserManager _userManager; private readonly ILoggingService _logger; - public TenantController(ApplicationDbContext context, UserManager userManager, ILoggingService logger) + private readonly IEmailSender _emailSender; + private readonly IConfiguration _configuration; + public TenantController(ApplicationDbContext context, UserManager userManager, ILoggingService logger, IEmailSender emailSender, IConfiguration configuration) { _context = context; _userManager = userManager; _logger = logger; + _emailSender = emailSender; + _configuration = configuration; } [HttpPost] public async Task CreateTenant([FromForm] CreateTenantDto createTenantDto) @@ -34,7 +39,7 @@ namespace Marco.Pms.Services.Controllers { return BadRequest(ModelState); } - var existedUser = await _userManager.FindByEmailAsync(createTenantDto.Email); + var existedUser = await _context.ApplicationUsers.FirstOrDefaultAsync(u => u.Email == createTenantDto.Email && u.IsActive == true); var existedTenanat = await _context.Tenants.FirstOrDefaultAsync(t => t.Name == createTenantDto.OrganizatioinName); if (existedUser == null && existedTenanat == null) { @@ -47,7 +52,7 @@ namespace Marco.Pms.Services.Controllers if (newTenant != null) { - RoleConfiguration settings = JsonSerializer.Deserialize(jsonString); + RoleConfiguration settings = JsonSerializer.Deserialize(jsonString) ?? new RoleConfiguration(); var TenantId = newTenant.Id; JobRole jobRole = new JobRole { @@ -88,7 +93,7 @@ namespace Marco.Pms.Services.Controllers Email = createTenantDto.Email, TenantId = TenantId, IsRootUser = true, - EmailConfirmed = true + EmailConfirmed = true // comment this line of code after implimenting proper email verification }; var result = await _userManager.CreateAsync(user, createTenantDto.Password); @@ -96,6 +101,13 @@ namespace Marco.Pms.Services.Controllers { try { + var token = await _userManager.GeneratePasswordResetTokenAsync(user); + var resetLink = $"{_configuration["AppSettings:WebFrontendUrl"]}/reset-password?token={WebUtility.UrlEncode(token)}"; + + if (user.Email == null) return NotFound(ApiResponse.ErrorResponse("Email Not found", "Email Not found", 404)); + + await _emailSender.SendTenanatRegistrationSuccessEmail(user.Email, newTenant.ContactName ?? "", resetLink); + Employee newEmployee = CreateTenantDtoToEmployee(createTenantDto, TenantId, user.Id, jobRole.Id); _context.Employees.Add(newEmployee); await _context.SaveChangesAsync(); @@ -134,7 +146,7 @@ namespace Marco.Pms.Services.Controllers } - return BadRequest("Tenant Already Exists"); + return BadRequest("Email Already Exists"); } @@ -191,7 +203,7 @@ namespace Marco.Pms.Services.Controllers var user = await _context.ApplicationUsers.FirstOrDefaultAsync(u => u.TenantId == tenantId && u.IsRootUser == true); if (tenant != null && user != null) { - var employee = await _context.Employees.FirstOrDefaultAsync(e => e.TenantId == tenantId && e.ApplicationUserId == user.Id); + var employee = await _context.Employees.FirstOrDefaultAsync(e => e.TenantId == tenantId && e.ApplicationUserId == user.Id) ?? new Employee(); tenant.IsActive = false; user.IsActive = false; user.IsRootUser = false; @@ -224,7 +236,7 @@ namespace Marco.Pms.Services.Controllers Gender = "", MiddleName = "", PanNumber = "", - PeramnentAddress = "", + PermanentAddress = "", PhoneNumber = model.ContactNumber, Photo = null, // GetFileDetails(model.Photo).Result.FileData, JobRoleId = jobRoleId, diff --git a/Marco.Pms.Services/EmailTemplates/tenant-email-verification.html b/Marco.Pms.Services/EmailTemplates/tenant-email-verification.html new file mode 100644 index 0000000..5dc4b18 --- /dev/null +++ b/Marco.Pms.Services/EmailTemplates/tenant-email-verification.html @@ -0,0 +1,568 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+
+
+ + +
+
+
+ + + + + + + + +
+ + + + + + + +
+ Sita +
+ +
+ + +
+
+
+ + +
+
+
+ + + + + +
+
+
+ + +
+
+
+ + + + + + + + +
+ + + + + +
+ + Image + +
+ +
+ + +
+
+
+ + +
+
+
+ + + + + +
+
+
+ + +
+
+
+ + + + + + + + +
+ + + + + +
+ + Image + +
+ +
+ + + + + + + +
+ +
+

{{MAIL_TITLE}}

+
+ +
+ + +
+
+
+ + +
+
+
+ + + + + +
+
+
+ + +
+
+
+ + + + + + + + +
+ +
+

Hello,{{CONTACT_PERSON}}

+

 

+

🎉 Your registration is successful! We're excited to have you on board.

+

 

+

Please set your password and log in using your registered email address.

+

 

+

To set your password, please follow the link below:

+
+ +
+ + + + + + + +
+ + + + +
+ + + + + + + +
+ +
+

Please ignore this email if you did not request a password change.
 

+
+ +
+ + +
+
+
+ + +
+
+
+ + + + + +
+
+
+ + +
+
+
+ + + + + + + + +
+ +
+ + +

Contact Us: info@marcoaiot.com

+ + +
+ +
+ + +
+
+
+ + +
+
+
+ + + + + + + + +
+ +
+
+ + + + + + + + +
+ + Facebook + +
+ + + + + + + + +
+ + Twitter + +
+ + + + + + + + +
+ + Instagram + +
+ + + + + + + + +
+ + LinkedIn + +
+ + +
+
+ +
+ + + +
+
+
+ + +
+
+
+ + + + + +
+
+
+ + +
+
+
+ + + + + + + + +
+

Marco AIoT Technologies Pvt. Ltd. ©  All Rights Reserved

+ + + +
+ + +
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+ + + + + + + + +
+ +
+ You're receiving this email because you have a MarcoPMS account. This email is not a marketing or promotional email. That is why this email does not contain an unsubscribe link. You will receive this email even if you have unsubscribed from MarcoPMS's marketing emails + +
+ +
+ + +
+
+
+ + +
+
+
+ +
+ + + + + diff --git a/Marco.Pms.Services/Service/EmailSender.cs b/Marco.Pms.Services/Service/EmailSender.cs index 1baac55..f63f4be 100644 --- a/Marco.Pms.Services/Service/EmailSender.cs +++ b/Marco.Pms.Services/Service/EmailSender.cs @@ -1,4 +1,5 @@ -using MailKit.Net.Smtp; +using System.Xml.Linq; +using MailKit.Net.Smtp; using Marco.Pms.Model.Utilities; using Microsoft.Extensions.Options; using MimeKit; @@ -107,6 +108,24 @@ namespace MarcoBMS.Services.Service } + public async Task SendTenanatRegistrationSuccessEmail(string toEmail, string contactPerson, string resetLink) + { + var replacements = new Dictionary + { + { "MAIL_TITLE", "Registration Successful" }, + { "CONTACT_PERSON", contactPerson }, + {"RESET_PWD_URL",resetLink} + }; + + string emailBody = await GetEmailTemplate("tenant-email-verification", replacements); + + List toEmails = new List + { + toEmail + }; + await SendEmailAsync(toEmails, "Registration Successful", emailBody); + } + public async Task SendEmailAsync(List toEmails, string subject, string body) { var email = new MimeMessage(); @@ -126,6 +145,6 @@ namespace MarcoBMS.Services.Service await smtp.SendAsync(email); await smtp.DisconnectAsync(true); } - } + } } diff --git a/Marco.Pms.Services/Service/IEmailSender.cs b/Marco.Pms.Services/Service/IEmailSender.cs index e2e41be..a096ded 100644 --- a/Marco.Pms.Services/Service/IEmailSender.cs +++ b/Marco.Pms.Services/Service/IEmailSender.cs @@ -8,6 +8,7 @@ namespace MarcoBMS.Services.Service Task SendResetPasswordEmailOnRegister(string toEmail, string toName, string resetLink); Task SendResetPasswordSuccessEmail(string toEmail, string userName); Task SendRequestDemoEmail(List toEmails, InquiryEmailObject demoEmailObject); + Task SendTenanatRegistrationSuccessEmail(string toEmail, string contactPerson, string resetLink); Task SendEmailAsync(List toEmails, string subject, string body); } }