Removed the primary employee creation form create organization API and added interval time for signalR
This commit is contained in:
parent
92a234ff7e
commit
bb328ad980
@ -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();
|
||||
|
||||
@ -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<IConfiguration>();
|
||||
var emailSender = scope.ServiceProvider.GetRequiredService<IEmailSender>();
|
||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
|
||||
//var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
|
||||
// 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<object>.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<object>.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<OrganizationVM>(organization);
|
||||
response.CreatedBy = _mapper.Map<BasicEmployeeVM>(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<object>.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<object>.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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user