Removed the primary employee creation form create organization API and added interval time for signalR

This commit is contained in:
ashutosh.nehete 2025-11-17 17:59:30 +05:30
parent 92a234ff7e
commit bb328ad980
2 changed files with 57 additions and 64 deletions

View File

@ -73,7 +73,11 @@ builder.Services.AddCors(options =>
#region Core Web & Framework Services #region Core Web & Framework Services
builder.Services.AddControllers().AddNewtonsoftJson(); builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddSignalR(); builder.Services.AddSignalR(options =>
{
options.KeepAliveInterval = TimeSpan.FromSeconds(15); // server sends ping every 15s (default)
options.ClientTimeoutInterval = TimeSpan.FromSeconds(30); // max time client waits after last message before timing out
});
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();
builder.Services.AddMemoryCache(); builder.Services.AddMemoryCache();

View File

@ -12,9 +12,7 @@ using Marco.Pms.Model.ViewModels.Projects;
using Marco.Pms.Services.Helpers; using Marco.Pms.Services.Helpers;
using Marco.Pms.Services.Service.ServiceInterfaces; using Marco.Pms.Services.Service.ServiceInterfaces;
using MarcoBMS.Services.Service; using MarcoBMS.Services.Service;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Net;
namespace Marco.Pms.Services.Service namespace Marco.Pms.Services.Service
{ {
@ -382,51 +380,50 @@ namespace Marco.Pms.Services.Service
}; };
_context.TenantOrgMappings.Add(newOrganizationTenantMapping); _context.TenantOrgMappings.Add(newOrganizationTenantMapping);
// Prepare user creation for identity //// Prepare user creation for identity
var user = new ApplicationUser //var user = new ApplicationUser
{ //{
UserName = model.Email, // UserName = model.Email,
Email = model.Email, // Email = model.Email,
EmailConfirmed = true // EmailConfirmed = true
}; //};
var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>(); //var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var emailSender = scope.ServiceProvider.GetRequiredService<IEmailSender>(); //var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
// Create Identity user with a default password (recommend to improve password handling) ////Create Identity user with a default password (recommend to improve password handling)
var result = await userManager.CreateAsync(user, "User@123"); //var result = await userManager.CreateAsync(user, "User@123");
if (!result.Succeeded) //if (!result.Succeeded)
{ //{
_logger.LogWarning("Failed to create identity user for email {Email}: {Errors}", model.Email, result.Errors); // _logger.LogWarning("Failed to create identity user for email {Email}: {Errors}", model.Email, result.Errors);
return ApiResponse<object>.ErrorResponse("Failed to create user", result.Errors, 400); // return ApiResponse<object>.ErrorResponse("Failed to create user", result.Errors, 400);
} //}
// Get admin job role or fallback role of the tenant //// Get admin job role or fallback role of the tenant
var jobRole = await _context.JobRoles.FirstOrDefaultAsync(jr => jr.Name == "Admin" && jr.TenantId == tenantId) //var jobRole = await _context.JobRoles.FirstOrDefaultAsync(jr => jr.Name == "Admin" && jr.TenantId == tenantId)
?? await _context.JobRoles.FirstOrDefaultAsync(jr => jr.TenantId == tenantId); // ?? await _context.JobRoles.FirstOrDefaultAsync(jr => jr.TenantId == tenantId);
// Parse full name safely (consider improving split logic for multi-part names) //// Parse full name safely (consider improving split logic for multi-part names)
var fullName = model.ContactPerson.Split(' ', StringSplitOptions.RemoveEmptyEntries); //var fullName = model.ContactPerson.Split(' ', StringSplitOptions.RemoveEmptyEntries);
Employee newEmployee = new Employee //Employee newEmployee = new Employee
{ //{
FirstName = fullName.Length > 0 ? fullName[0] : string.Empty, // FirstName = fullName.Length > 0 ? fullName[0] : string.Empty,
LastName = fullName.Length > 1 ? fullName[^1] : string.Empty, // LastName = fullName.Length > 1 ? fullName[^1] : string.Empty,
Email = model.Email, // Email = model.Email,
PermanentAddress = model.Address, // PermanentAddress = model.Address,
CurrentAddress = model.Address, // CurrentAddress = model.Address,
PhoneNumber = model.ContactNumber, // PhoneNumber = model.ContactNumber,
ApplicationUserId = user.Id, // ApplicationUserId = user.Id,
JobRoleId = jobRole?.Id ?? Guid.Empty, // JobRoleId = jobRole?.Id ?? Guid.Empty,
IsActive = true, // IsActive = true,
IsSystem = false, // IsSystem = false,
IsPrimary = true, // IsPrimary = true,
OrganizationId = organization.Id, // OrganizationId = organization.Id,
HasApplicationAccess = true // HasApplicationAccess = true
}; //};
_context.Employees.Add(newEmployee); //_context.Employees.Add(newEmployee);
// Map organization services // Map organization services
if (model.ServiceIds?.Any() ?? false) if (model.ServiceIds?.Any() ?? false)
@ -445,14 +442,6 @@ namespace Marco.Pms.Services.Service
await transaction.CommitAsync(); await transaction.CommitAsync();
// Send user registration email with password reset link
var token = await userManager.GeneratePasswordResetTokenAsync(user);
var resetLink = $"{configuration["AppSettings:WebFrontendUrl"]}/reset-password?token={WebUtility.UrlEncode(token)}";
if (!string.IsNullOrEmpty(newEmployee.FirstName))
{
await emailSender.SendResetPasswordEmailOnRegister(user.Email, newEmployee.FirstName, resetLink);
}
// Prepare response DTO // Prepare response DTO
var response = _mapper.Map<OrganizationVM>(organization); var response = _mapper.Map<OrganizationVM>(organization);
response.CreatedBy = _mapper.Map<BasicEmployeeVM>(loggedInEmployee); response.CreatedBy = _mapper.Map<BasicEmployeeVM>(loggedInEmployee);
@ -981,21 +970,21 @@ namespace Marco.Pms.Services.Service
organization.UpdatedById = loggedInEmployee.Id; organization.UpdatedById = loggedInEmployee.Id;
organization.UpdatedAt = DateTime.UtcNow; organization.UpdatedAt = DateTime.UtcNow;
// Fetch the primary active employee of the organization //// Fetch the primary active employee of the organization
var employee = await _context.Employees.FirstOrDefaultAsync(e => e.OrganizationId == id && e.IsPrimary && e.IsActive); //var employee = await _context.Employees.FirstOrDefaultAsync(e => e.OrganizationId == id && e.IsPrimary && e.IsActive);
if (employee == null) //if (employee == null)
{ //{
_logger.LogWarning("Primary employee not found for OrganizationId: {OrganizationId}", id); // _logger.LogWarning("Primary employee not found for OrganizationId: {OrganizationId}", id);
return ApiResponse<object>.ErrorResponse("Primary employee not found", "Primary employee not found", 404); // return ApiResponse<object>.ErrorResponse("Primary employee not found", "Primary employee not found", 404);
} //}
// Split contact person's name into first and last names //// Split contact person's name into first and last names
var fullName = (model.ContactPerson ?? string.Empty).Split(' ', StringSplitOptions.RemoveEmptyEntries); //var fullName = (model.ContactPerson ?? string.Empty).Split(' ', StringSplitOptions.RemoveEmptyEntries);
employee.FirstName = fullName.Length > 0 ? fullName[0] : string.Empty; //employee.FirstName = fullName.Length > 0 ? fullName[0] : string.Empty;
employee.LastName = fullName.Length > 1 ? fullName[^1] : string.Empty; //employee.LastName = fullName.Length > 1 ? fullName[^1] : string.Empty;
employee.CurrentAddress = model.Address; //employee.CurrentAddress = model.Address;
employee.PermanentAddress = model.Address; //employee.PermanentAddress = model.Address;
employee.PhoneNumber = model.ContactNumber; //employee.PhoneNumber = model.ContactNumber;
// Update organization's service mappings if service IDs are provided // Update organization's service mappings if service IDs are provided
if (model.ServiceIds?.Any() ?? false) if (model.ServiceIds?.Any() ?? false)