From bb328ad980dc3ff8c5c6d542a7446f51139a0d08 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Mon, 17 Nov 2025 17:59:30 +0530 Subject: [PATCH] Removed the primary employee creation form create organization API and added interval time for signalR --- Marco.Pms.Services/Program.cs | 6 +- .../Service/OrganizationService.cs | 115 ++++++++---------- 2 files changed, 57 insertions(+), 64 deletions(-) diff --git a/Marco.Pms.Services/Program.cs b/Marco.Pms.Services/Program.cs index ac3cd71..7fff6ac 100644 --- a/Marco.Pms.Services/Program.cs +++ b/Marco.Pms.Services/Program.cs @@ -73,7 +73,11 @@ builder.Services.AddCors(options => #region Core Web & Framework Services 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.AddHttpContextAccessor(); builder.Services.AddMemoryCache(); diff --git a/Marco.Pms.Services/Service/OrganizationService.cs b/Marco.Pms.Services/Service/OrganizationService.cs index 977bc14..575f684 100644 --- a/Marco.Pms.Services/Service/OrganizationService.cs +++ b/Marco.Pms.Services/Service/OrganizationService.cs @@ -12,9 +12,7 @@ using Marco.Pms.Model.ViewModels.Projects; using Marco.Pms.Services.Helpers; using Marco.Pms.Services.Service.ServiceInterfaces; using MarcoBMS.Services.Service; -using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; -using System.Net; namespace Marco.Pms.Services.Service { @@ -382,51 +380,50 @@ namespace Marco.Pms.Services.Service }; _context.TenantOrgMappings.Add(newOrganizationTenantMapping); - // Prepare user creation for identity - var user = new ApplicationUser - { - UserName = model.Email, - Email = model.Email, - EmailConfirmed = true - }; + //// Prepare user creation for identity + //var user = new ApplicationUser + //{ + // UserName = model.Email, + // Email = model.Email, + // EmailConfirmed = true + //}; - var configuration = scope.ServiceProvider.GetRequiredService(); - var emailSender = scope.ServiceProvider.GetRequiredService(); - var userManager = scope.ServiceProvider.GetRequiredService>(); + //var configuration = scope.ServiceProvider.GetRequiredService(); + //var userManager = scope.ServiceProvider.GetRequiredService>(); - // Create Identity user with a default password (recommend to improve password handling) - var result = await userManager.CreateAsync(user, "User@123"); - if (!result.Succeeded) - { - _logger.LogWarning("Failed to create identity user for email {Email}: {Errors}", model.Email, result.Errors); - return ApiResponse.ErrorResponse("Failed to create user", result.Errors, 400); - } + ////Create Identity user with a default password (recommend to improve password handling) + //var result = await userManager.CreateAsync(user, "User@123"); + //if (!result.Succeeded) + //{ + // _logger.LogWarning("Failed to create identity user for email {Email}: {Errors}", model.Email, result.Errors); + // return ApiResponse.ErrorResponse("Failed to create user", result.Errors, 400); + //} - // Get admin job role or fallback role of the tenant - var jobRole = await _context.JobRoles.FirstOrDefaultAsync(jr => jr.Name == "Admin" && jr.TenantId == tenantId) - ?? await _context.JobRoles.FirstOrDefaultAsync(jr => jr.TenantId == tenantId); + //// Get admin job role or fallback role of the tenant + //var jobRole = await _context.JobRoles.FirstOrDefaultAsync(jr => jr.Name == "Admin" && jr.TenantId == tenantId) + // ?? await _context.JobRoles.FirstOrDefaultAsync(jr => jr.TenantId == tenantId); - // Parse full name safely (consider improving split logic for multi-part names) - var fullName = model.ContactPerson.Split(' ', StringSplitOptions.RemoveEmptyEntries); + //// Parse full name safely (consider improving split logic for multi-part names) + //var fullName = model.ContactPerson.Split(' ', StringSplitOptions.RemoveEmptyEntries); - Employee newEmployee = new Employee - { - FirstName = fullName.Length > 0 ? fullName[0] : string.Empty, - LastName = fullName.Length > 1 ? fullName[^1] : string.Empty, - Email = model.Email, - PermanentAddress = model.Address, - CurrentAddress = model.Address, - PhoneNumber = model.ContactNumber, - ApplicationUserId = user.Id, - JobRoleId = jobRole?.Id ?? Guid.Empty, - IsActive = true, - IsSystem = false, - IsPrimary = true, - OrganizationId = organization.Id, - HasApplicationAccess = true - }; + //Employee newEmployee = new Employee + //{ + // FirstName = fullName.Length > 0 ? fullName[0] : string.Empty, + // LastName = fullName.Length > 1 ? fullName[^1] : string.Empty, + // Email = model.Email, + // PermanentAddress = model.Address, + // CurrentAddress = model.Address, + // PhoneNumber = model.ContactNumber, + // ApplicationUserId = user.Id, + // JobRoleId = jobRole?.Id ?? Guid.Empty, + // IsActive = true, + // IsSystem = false, + // IsPrimary = true, + // OrganizationId = organization.Id, + // HasApplicationAccess = true + //}; - _context.Employees.Add(newEmployee); + //_context.Employees.Add(newEmployee); // Map organization services if (model.ServiceIds?.Any() ?? false) @@ -445,14 +442,6 @@ namespace Marco.Pms.Services.Service 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 var response = _mapper.Map(organization); response.CreatedBy = _mapper.Map(loggedInEmployee); @@ -981,21 +970,21 @@ namespace Marco.Pms.Services.Service organization.UpdatedById = loggedInEmployee.Id; organization.UpdatedAt = DateTime.UtcNow; - // Fetch the primary active employee of the organization - var employee = await _context.Employees.FirstOrDefaultAsync(e => e.OrganizationId == id && e.IsPrimary && e.IsActive); - if (employee == null) - { - _logger.LogWarning("Primary employee not found for OrganizationId: {OrganizationId}", id); - return ApiResponse.ErrorResponse("Primary employee not found", "Primary employee not found", 404); - } + //// Fetch the primary active employee of the organization + //var employee = await _context.Employees.FirstOrDefaultAsync(e => e.OrganizationId == id && e.IsPrimary && e.IsActive); + //if (employee == null) + //{ + // _logger.LogWarning("Primary employee not found for OrganizationId: {OrganizationId}", id); + // return ApiResponse.ErrorResponse("Primary employee not found", "Primary employee not found", 404); + //} - // Split contact person's name into first and last names - var fullName = (model.ContactPerson ?? string.Empty).Split(' ', StringSplitOptions.RemoveEmptyEntries); - employee.FirstName = fullName.Length > 0 ? fullName[0] : string.Empty; - employee.LastName = fullName.Length > 1 ? fullName[^1] : string.Empty; - employee.CurrentAddress = model.Address; - employee.PermanentAddress = model.Address; - employee.PhoneNumber = model.ContactNumber; + //// Split contact person's name into first and last names + //var fullName = (model.ContactPerson ?? string.Empty).Split(' ', StringSplitOptions.RemoveEmptyEntries); + //employee.FirstName = fullName.Length > 0 ? fullName[0] : string.Empty; + //employee.LastName = fullName.Length > 1 ? fullName[^1] : string.Empty; + //employee.CurrentAddress = model.Address; + //employee.PermanentAddress = model.Address; + //employee.PhoneNumber = model.ContactNumber; // Update organization's service mappings if service IDs are provided if (model.ServiceIds?.Any() ?? false)