Removed the organization ID from manage employee DTO

This commit is contained in:
ashutosh.nehete 2025-09-30 19:44:00 +05:30
parent 71cc442054
commit 061512d501
2 changed files with 18 additions and 13 deletions

View File

@ -19,7 +19,7 @@
public string? EmergencyPhoneNumber { get; set; }
public string? EmergencyContactPerson { get; set; }
public Guid JobRoleId { get; set; }
public required Guid OrganizationId { get; set; }
public Guid? OrganizationId { get; set; }
public required bool HasApplicationAccess { get; set; }
}
public class MobileUserManageDto
@ -33,7 +33,7 @@
public required string Gender { get; set; }
public Guid JobRoleId { get; set; }
public string? ProfileImage { get; set; }
public required Guid OrganizationId { get; set; }
public Guid? OrganizationId { get; set; }
public required bool HasApplicationAccess { get; set; }
}

View File

@ -470,6 +470,9 @@ namespace MarcoBMS.Services.Controllers
Guid tenantId = _userHelper.GetTenantId();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
Guid employeeId = Guid.Empty;
if (model == null)
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", "Invaild Data", 400));
@ -602,6 +605,7 @@ namespace MarcoBMS.Services.Controllers
{
// Correlation and context capture for logs
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
Guid organizationId = model.OrganizationId ?? loggedInEmployee.OrganizationId;
{
if (model == null)
@ -625,10 +629,10 @@ namespace MarcoBMS.Services.Controllers
if (model.Id.HasValue && model.Id.Value != Guid.Empty)
{
existingEmployee = await _context.Employees
.FirstOrDefaultAsync(e => e.Id == model.Id && e.OrganizationId == model.OrganizationId);
.FirstOrDefaultAsync(e => e.Id == model.Id && e.OrganizationId == organizationId);
if (existingEmployee == null)
{
_logger.LogInfo("Employee not found for update. Id={EmployeeId}, Org={OrgId}", model.Id, model.OrganizationId);
_logger.LogInfo("Employee not found for update. Id={EmployeeId}, Org={OrgId}", model.Id, organizationId);
return NotFound(ApiResponse<object>.ErrorResponse("Employee not found", "Employee not found in database", 404));
}
}
@ -680,10 +684,10 @@ namespace MarcoBMS.Services.Controllers
if (!string.IsNullOrWhiteSpace(model.Email))
{
var emailExists = await _context.Employees
.AnyAsync(e => e.Email == model.Email && e.OrganizationId == model.OrganizationId);
.AnyAsync(e => e.Email == model.Email && e.OrganizationId == organizationId);
if (emailExists)
{
_logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email, model.OrganizationId);
_logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email, organizationId);
return StatusCode(403, ApiResponse<object>.ErrorResponse(
"Employee with email already exists",
"Employee with this email already exists", 403));
@ -723,7 +727,7 @@ namespace MarcoBMS.Services.Controllers
existingEmployee.ApplicationUserId = createdIdentityUser.Id;
await SendResetIfApplicableAsync(createdIdentityUser, existingEmployee.FirstName ?? "User");
}
existingEmployee.OrganizationId = organizationId;
await _context.SaveChangesAsync();
employeeId = existingEmployee.Id;
@ -745,7 +749,7 @@ namespace MarcoBMS.Services.Controllers
newEmployee.ApplicationUserId = createdIdentityUser.Id;
await SendResetIfApplicableAsync(createdIdentityUser, newEmployee.FirstName ?? "User");
}
newEmployee.OrganizationId = organizationId;
await _context.Employees.AddAsync(newEmployee);
await _context.SaveChangesAsync();
@ -877,6 +881,7 @@ namespace MarcoBMS.Services.Controllers
public async Task<IActionResult> CreateUserMobileAsync([FromBody] MobileUserManageDto model)
{
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
Guid organizationId = model.OrganizationId ?? loggedInEmployee.OrganizationId;
if (tenantId == Guid.Empty)
{
_logger.LogWarning("Tenant resolution failed in CreateUserMobile"); // structured warning
@ -912,11 +917,11 @@ namespace MarcoBMS.Services.Controllers
if (model.Id == null || model.Id == Guid.Empty)
{
var emailExists = await _context.Employees
.AnyAsync(e => e.Email == model.Email && e.OrganizationId == model.OrganizationId);
.AnyAsync(e => e.Email == model.Email && e.OrganizationId == organizationId);
if (emailExists)
{
_logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email ?? string.Empty, model.OrganizationId);
_logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email ?? string.Empty, organizationId);
return StatusCode(409, ApiResponse<object>.ErrorResponse("Employee with email already exists", "Employee with this email already exists", 409));
}
@ -933,7 +938,7 @@ namespace MarcoBMS.Services.Controllers
JoiningDate = model.JoiningDate,
JobRoleId = model.JobRoleId,
Photo = imageBytes,
OrganizationId = model.OrganizationId,
OrganizationId = organizationId,
HasApplicationAccess = model.HasApplicationAccess,
};
@ -1001,7 +1006,7 @@ namespace MarcoBMS.Services.Controllers
existingEmployee.PhoneNumber = model.PhoneNumber;
existingEmployee.JoiningDate = model.JoiningDate;
existingEmployee.JobRoleId = model.JobRoleId;
existingEmployee.OrganizationId = model.OrganizationId;
existingEmployee.OrganizationId = organizationId;
existingEmployee.HasApplicationAccess = model.HasApplicationAccess;
if (string.IsNullOrWhiteSpace(existingEmployee.Email) && !string.IsNullOrWhiteSpace(model.Email))
@ -1011,7 +1016,7 @@ namespace MarcoBMS.Services.Controllers
if (emailExists)
{
_logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email, model.OrganizationId);
_logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email, organizationId);
return StatusCode(409, ApiResponse<object>.ErrorResponse("Employee with email already exists", "Employee with this email already exists", 409));
}
existingEmployee.Email = model.Email;