Implement an API to store employee information with a captured image and update employee information.
This commit is contained in:
parent
235ca073ce
commit
98409f5b87
@ -30,21 +30,15 @@
|
|||||||
|
|
||||||
// public int TenantId { get; set; }
|
// public int TenantId { get; set; }
|
||||||
}
|
}
|
||||||
public class CreateQuickUserDto
|
public class MobileUserManageDto
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid? Id { get; set; }
|
||||||
public string? FirstName { get; set; }
|
public string FirstName { get; set; } = string.Empty;
|
||||||
public string? LastName { get; set; }
|
public string? LastName { get; set; }
|
||||||
|
public string PhoneNumber { get; set; } = string.Empty;
|
||||||
public string? Gender { get; set; }
|
public string? Gender { get; set; }
|
||||||
|
public Guid JobRoleId { get; set; }
|
||||||
public string? CurrentAddress { get; set; }
|
public string? ProfileImage { get; set; }
|
||||||
public string? PhoneNumber { get; set; }
|
|
||||||
|
|
||||||
public string? EmergencyPhoneNumber { get; set; }
|
|
||||||
public string? EmergencyContactPerson { get; set; }
|
|
||||||
|
|
||||||
public Guid? JobRoleId { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Marco.Pms.Model.Employees;
|
using Marco.Pms.Model.Dtos.Employees;
|
||||||
|
using Marco.Pms.Model.Employees;
|
||||||
using Marco.Pms.Model.ViewModels.Activities;
|
using Marco.Pms.Model.ViewModels.Activities;
|
||||||
using Marco.Pms.Model.ViewModels.Employee;
|
using Marco.Pms.Model.ViewModels.Employee;
|
||||||
|
|
||||||
@ -8,6 +9,11 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
{
|
{
|
||||||
public static EmployeeVM ToEmployeeVMFromEmployee(this Employee model)
|
public static EmployeeVM ToEmployeeVMFromEmployee(this Employee model)
|
||||||
{
|
{
|
||||||
|
string? base64String = null;
|
||||||
|
if ((model.Photo != null))
|
||||||
|
{
|
||||||
|
base64String = Convert.ToBase64String(model.Photo);
|
||||||
|
}
|
||||||
return new EmployeeVM
|
return new EmployeeVM
|
||||||
{
|
{
|
||||||
Id = model.Id,
|
Id = model.Id,
|
||||||
@ -27,7 +33,7 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
PanNumber = model.PanNumber,
|
PanNumber = model.PanNumber,
|
||||||
PermanentAddress = model.PermanentAddress,
|
PermanentAddress = model.PermanentAddress,
|
||||||
PhoneNumber = model.PhoneNumber,
|
PhoneNumber = model.PhoneNumber,
|
||||||
Photo = model.Photo,
|
Photo = base64String,
|
||||||
IsActive = model.IsActive,
|
IsActive = model.IsActive,
|
||||||
IsSystem = model.IsSystem,
|
IsSystem = model.IsSystem,
|
||||||
JoiningDate = model.JoiningDate
|
JoiningDate = model.JoiningDate
|
||||||
@ -45,5 +51,30 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
JobRoleName = employee.JobRole != null ? employee.JobRole.Name : ""
|
JobRoleName = employee.JobRole != null ? employee.JobRole.Name : ""
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
public static Employee ToEmployeeFromMobileUserManageDto(this MobileUserManageDto model, Guid TenantId, byte[]? image)
|
||||||
|
{
|
||||||
|
return new Employee
|
||||||
|
{
|
||||||
|
ApplicationUserId = null,
|
||||||
|
FirstName = model.FirstName,
|
||||||
|
LastName = model.LastName,
|
||||||
|
Email = string.Empty,
|
||||||
|
TenantId = TenantId,
|
||||||
|
CurrentAddress = string.Empty,
|
||||||
|
BirthDate = null,
|
||||||
|
EmergencyPhoneNumber = string.Empty,
|
||||||
|
EmergencyContactPerson = string.Empty,
|
||||||
|
AadharNumber = string.Empty,
|
||||||
|
Gender = model.Gender,
|
||||||
|
MiddleName = string.Empty,
|
||||||
|
PanNumber = string.Empty,
|
||||||
|
PermanentAddress = string.Empty,
|
||||||
|
PhoneNumber = model.PhoneNumber,
|
||||||
|
Photo = image,
|
||||||
|
JobRoleId = model.JobRoleId,
|
||||||
|
JoiningDate = null,
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
public bool IsActive { get; set; } = true;
|
public bool IsActive { get; set; } = true;
|
||||||
public string? PanNumber { get; set; }
|
public string? PanNumber { get; set; }
|
||||||
|
|
||||||
public byte[]? Photo { get; set; } // To store the captured photo
|
public string? Photo { get; set; } // To store the captured photo
|
||||||
|
|
||||||
public string? ApplicationUserId { get; set; }
|
public string? ApplicationUserId { get; set; }
|
||||||
|
|
||||||
|
@ -84,8 +84,6 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("list/{projectid?}")]
|
[Route("list/{projectid?}")]
|
||||||
public async Task<IActionResult> GetEmployeesByProject(Guid? projectid, [FromQuery] bool ShowInactive)
|
public async Task<IActionResult> GetEmployeesByProject(Guid? projectid, [FromQuery] bool ShowInactive)
|
||||||
@ -103,6 +101,7 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
return Ok(ApiResponse<object>.SuccessResponse(result, "Filter applied.", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(result, "Filter applied.", 200));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("search/{name}/{projectid?}")]
|
[Route("search/{name}/{projectid?}")]
|
||||||
public async Task<IActionResult> SearchEmployee(string name, Guid? projectid)
|
public async Task<IActionResult> SearchEmployee(string name, Guid? projectid)
|
||||||
@ -264,145 +263,90 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
Employee newEmployee = GetNewEmployeeModel(model, tenantId, string.Empty);
|
Employee newEmployee = GetNewEmployeeModel(model, tenantId, string.Empty);
|
||||||
_context.Employees.Add(newEmployee);
|
_context.Employees.Add(newEmployee);
|
||||||
}
|
}
|
||||||
try
|
await _context.SaveChangesAsync();
|
||||||
{
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
return BadRequest(ex.InnerException?.Message ?? ex.Message);
|
|
||||||
}
|
|
||||||
responsemessage = "User created successfully.";
|
responsemessage = "User created successfully.";
|
||||||
|
|
||||||
}
|
}
|
||||||
return Ok(ApiResponse<object>.SuccessResponse("Success.", responsemessage, 200));
|
return Ok(ApiResponse<object>.SuccessResponse("Success.", responsemessage, 200));
|
||||||
}
|
}
|
||||||
//[HttpPost("manage-mobile")]
|
|
||||||
//public async Task<IActionResult> CreateUserMoblie([FromBody] CreateUserDto model)
|
|
||||||
//{
|
|
||||||
// Guid tenantId = _userHelper.GetTenantId();
|
|
||||||
// if (model == null)
|
|
||||||
// return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", "Invaild Data", 400));
|
|
||||||
|
|
||||||
// if (model.FirstName == null && model.PhoneNumber == null)
|
[HttpPost("manage-mobile")]
|
||||||
// return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", "Invaild Data", 400));
|
public async Task<IActionResult> CreateUserMoblie([FromBody] MobileUserManageDto model)
|
||||||
|
{
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("User submitted empty or null employee information during employee creation or update in tenant {TenantId}", tenantId);
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", "Invaild Data", 400));
|
||||||
|
}
|
||||||
|
|
||||||
// string responsemessage = "";
|
if (string.IsNullOrWhiteSpace(model.FirstName) || string.IsNullOrWhiteSpace(model.PhoneNumber))
|
||||||
|
{
|
||||||
|
_logger.LogWarning("User submitted empty or null first name or phone number during employee creation or update in tenant {TenantId}", tenantId);
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("First name and phone number are required fields.", "First name and phone number are required fields.", 400));
|
||||||
|
}
|
||||||
|
|
||||||
// if (model.Email != null)
|
if (model.Id == null)
|
||||||
// {
|
{
|
||||||
// // Check if user already exists by email
|
byte[]? imageBytes = null;
|
||||||
// IdentityUser? existingUser = await _userHelper.GetRegisteredUser(model.Email);
|
if (!string.IsNullOrWhiteSpace(model.ProfileImage))
|
||||||
// var existingEmployee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == model.Id && e.IsActive == true);
|
{
|
||||||
// var demo = existingUser != new IdentityUser();
|
try
|
||||||
// if (existingUser != null)
|
{
|
||||||
// {
|
imageBytes = Convert.FromBase64String(model.ProfileImage);
|
||||||
// /* Identity user Exists - Create/update employee Employee */
|
}
|
||||||
|
catch (FormatException)
|
||||||
|
{
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid image format.", "Invalid image format", 400));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Employee employee = model.ToEmployeeFromMobileUserManageDto(tenantId, imageBytes);
|
||||||
|
_context.Employees.Add(employee);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
// // Update Employee record
|
EmployeeVM employeeVM = employee.ToEmployeeVMFromEmployee();
|
||||||
// existingEmployee = await _context.Employees.FirstOrDefaultAsync(e => e.Email == model.Email && e.Id == model.Id && e.IsActive == true);
|
|
||||||
// if (existingEmployee != null)
|
|
||||||
// {
|
|
||||||
// existingEmployee = GetUpdateEmployeeModel(model, existingEmployee, existingUser);
|
|
||||||
|
|
||||||
// _context.Employees.Update(existingEmployee);
|
_logger.LogInfo($"Employee {employee.FirstName} {employee.LastName} created in tenant {tenantId}");
|
||||||
// await _context.SaveChangesAsync();
|
return Ok(ApiResponse<object>.SuccessResponse(employeeVM, "Employee created successfully", 200));
|
||||||
// responsemessage = "User updated successfully.";
|
}
|
||||||
// }
|
else
|
||||||
// else
|
{
|
||||||
// {
|
Employee? existingEmployee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == model.Id.Value);
|
||||||
// // Create Employee record if missing
|
if (existingEmployee == null)
|
||||||
// //Employee newEmployee = GetNewEmployeeModel(model, TenantId, existingUser.Id);
|
{
|
||||||
// //_context.Employees.Add(newEmployee);
|
_logger.LogError("User tries to update employee {EmployeeId} but not found in database", model.Id);
|
||||||
// return Conflict(ApiResponse<object>.ErrorResponse("Email already exist", "Email already exist", 409));
|
return NotFound(ApiResponse<object>.ErrorResponse("Employee not found", "Employee not found", 404));
|
||||||
// }
|
}
|
||||||
|
byte[]? imageBytes = null;
|
||||||
|
if (!string.IsNullOrWhiteSpace(model.ProfileImage))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
imageBytes = Convert.FromBase64String(model.ProfileImage);
|
||||||
|
}
|
||||||
|
catch (FormatException)
|
||||||
|
{
|
||||||
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid image format.", "Invalid image format", 400));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
imageBytes ??= existingEmployee.Photo;
|
||||||
|
|
||||||
// }
|
existingEmployee.FirstName = model.FirstName;
|
||||||
// else
|
existingEmployee.LastName = model.LastName;
|
||||||
// {
|
existingEmployee.Gender = model.Gender;
|
||||||
// var user = new ApplicationUser
|
existingEmployee.PhoneNumber = model.PhoneNumber;
|
||||||
// {
|
existingEmployee.JobRoleId = model.JobRoleId;
|
||||||
// UserName = model.Email,
|
existingEmployee.Photo = imageBytes;
|
||||||
// Email = model.Email,
|
|
||||||
// EmailConfirmed = true,
|
|
||||||
// TenantId = tenantId
|
|
||||||
|
|
||||||
// };
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
// // Create Identity User
|
EmployeeVM employeeVM = existingEmployee.ToEmployeeVMFromEmployee();
|
||||||
// var result = await _userManager.CreateAsync(user, "User@123");
|
|
||||||
// if (!result.Succeeded)
|
|
||||||
// return Ok(ApiResponse<object>.ErrorResponse("Failed to create user", result.Errors, 400));
|
|
||||||
|
|
||||||
// if (existingEmployee == null)
|
_logger.LogInfo($"Employee {existingEmployee.FirstName} {existingEmployee.LastName} updated in tenant {tenantId}");
|
||||||
// {
|
return Ok(ApiResponse<object>.SuccessResponse(employeeVM, "Employee updated successfully", 200));
|
||||||
// Employee newEmployee = GetNewEmployeeModel(model, tenantId, user.Id);
|
}
|
||||||
// _context.Employees.Add(newEmployee);
|
}
|
||||||
|
|
||||||
// await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
|
|
||||||
// /* SEND USER REGISTRATION MAIL*/
|
|
||||||
// var token = await _userManager.GeneratePasswordResetTokenAsync(user);
|
|
||||||
// var resetLink = $"{_configuration["AppSettings:WebFrontendUrl"]}/reset-password?token={WebUtility.UrlEncode(token)}";
|
|
||||||
// if (newEmployee.FirstName != null)
|
|
||||||
// {
|
|
||||||
// await _emailSender.SendResetPasswordEmailOnRegister(user.Email, newEmployee.FirstName, resetLink);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// existingEmployee.Email = model.Email;
|
|
||||||
// existingEmployee = GetUpdateEmployeeModel(model, existingEmployee, existingUser);
|
|
||||||
|
|
||||||
// _context.Employees.Update(existingEmployee);
|
|
||||||
// await _context.SaveChangesAsync();
|
|
||||||
|
|
||||||
|
|
||||||
// /* SEND USER REGISTRATION MAIL*/
|
|
||||||
// var token = await _userManager.GeneratePasswordResetTokenAsync(user);
|
|
||||||
// var resetLink = $"{_configuration["AppSettings:WebFrontendUrl"]}/reset-password?token={WebUtility.UrlEncode(token)}";
|
|
||||||
// if (existingEmployee.FirstName != null)
|
|
||||||
// {
|
|
||||||
// await _emailSender.SendResetPasswordEmailOnRegister(user.Email, existingEmployee.FirstName, resetLink);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
// responsemessage = "User created successfully. Password reset link is sent to registered email";
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// var existingEmployee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == model.Id && e.IsActive == true);
|
|
||||||
// if (existingEmployee != null)
|
|
||||||
// {
|
|
||||||
// existingEmployee = GetUpdateEmployeeModel(model, existingEmployee);
|
|
||||||
// _context.Employees.Update(existingEmployee);
|
|
||||||
// responsemessage = "User updated successfully.";
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// // Create Employee record if missing
|
|
||||||
// Employee newEmployee = GetNewEmployeeModel(model, tenantId, string.Empty);
|
|
||||||
// _context.Employees.Add(newEmployee);
|
|
||||||
// }
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
// await _context.SaveChangesAsync();
|
|
||||||
// }
|
|
||||||
// catch (Exception ex)
|
|
||||||
// {
|
|
||||||
// return BadRequest(ex.InnerException?.Message ?? ex.Message);
|
|
||||||
// }
|
|
||||||
// responsemessage = "User created successfully.";
|
|
||||||
|
|
||||||
// }
|
|
||||||
// return Ok(ApiResponse<object>.SuccessResponse("Success.", responsemessage, 200));
|
|
||||||
//}
|
|
||||||
|
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<IActionResult> SuspendEmployee(Guid id)
|
public async Task<IActionResult> SuspendEmployee(Guid id)
|
||||||
@ -484,7 +428,6 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
}
|
}
|
||||||
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Employee Suspended successfully", 200));
|
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Employee Suspended successfully", 200));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Employee GetNewEmployeeModel(CreateUserDto model, Guid TenantId, string ApplicationUserId)
|
private static Employee GetNewEmployeeModel(CreateUserDto model, Guid TenantId, string ApplicationUserId)
|
||||||
{
|
{
|
||||||
var newEmployee = new Employee
|
var newEmployee = new Employee
|
||||||
@ -511,7 +454,6 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
};
|
};
|
||||||
return newEmployee;
|
return newEmployee;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Employee GetUpdateEmployeeModel(CreateUserDto model, Employee existingEmployee, IdentityUser? existingIdentityUser = null)
|
private static Employee GetUpdateEmployeeModel(CreateUserDto model, Employee existingEmployee, IdentityUser? existingIdentityUser = null)
|
||||||
{
|
{
|
||||||
if (existingEmployee.ApplicationUserId == null && existingIdentityUser != null)
|
if (existingEmployee.ApplicationUserId == null && existingIdentityUser != null)
|
||||||
@ -536,7 +478,6 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
|
|
||||||
return existingEmployee;
|
return existingEmployee;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<FileDetails> GetFileDetails(IFormFile file)
|
private static async Task<FileDetails> GetFileDetails(IFormFile file)
|
||||||
{
|
{
|
||||||
FileDetails info = new FileDetails();
|
FileDetails info = new FileDetails();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user